-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OpenPifPaf] ONNX Evaluation Pipeline (#915)
* something odd about how images are being passed * getting the same mAP! * second round of refactoring * ready to break it down into smaller PRs * initial commit * ready for testing * remove torch model from the ported code * solving some issues with logging * ready for review * Update src/deepsparse/open_pif_paf/utils/validation/cli.py * prohibit openpifpaf fields in the server --------- Co-authored-by: Konstantin <konstantin@neuralmagic.com>
- Loading branch information
1 parent
89abc5d
commit ffbd351
Showing
11 changed files
with
615 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ | |
# limitations under the License. | ||
# flake8: noqa | ||
from .annotate import * | ||
from .validation import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# flake8: noqa | ||
from .cli import * | ||
from .deepsparse_evaluator import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import argparse | ||
import logging | ||
|
||
import torch | ||
from deepsparse.open_pif_paf.utils.validation.deepsparse_evaluator import ( | ||
DeepSparseEvaluator, | ||
) | ||
from deepsparse.open_pif_paf.utils.validation.deepsparse_predictor import ( | ||
DeepSparsePredictor, | ||
) | ||
from openpifpaf import __version__, datasets, decoder, logger, network, show, visualizer | ||
from openpifpaf.eval import CustomFormatter | ||
|
||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
__all__ = ["cli"] | ||
|
||
|
||
# adapted from OPENPIFPAF GITHUB: | ||
# https://github.com/openpifpaf/openpifpaf/blob/main/src/openpifpaf/eval.py | ||
# the appropriate edits are marked with # deepsparse edit: <edit comment> | ||
def cli(): | ||
parser = argparse.ArgumentParser( | ||
prog="python3 -m openpifpaf.eval", | ||
usage="%(prog)s [options]", | ||
description=__doc__, | ||
formatter_class=CustomFormatter, | ||
) | ||
parser.add_argument( | ||
"--version", | ||
action="version", | ||
version="OpenPifPaf {version}".format(version=__version__), | ||
) | ||
|
||
datasets.cli(parser) | ||
decoder.cli(parser) | ||
logger.cli(parser) | ||
network.Factory.cli(parser) | ||
DeepSparsePredictor.cli(parser, skip_batch_size=True, skip_loader_workers=True) | ||
show.cli(parser) | ||
visualizer.cli(parser) | ||
DeepSparseEvaluator.cli(parser) | ||
|
||
parser.add_argument("--disable-cuda", action="store_true", help="disable CUDA") | ||
parser.add_argument( | ||
"--output", default=None, help="output filename without file extension" | ||
) | ||
parser.add_argument( | ||
"--watch", | ||
default=False, | ||
const=60, | ||
nargs="?", | ||
type=int, | ||
help=( | ||
"Watch a directory for new checkpoint files. " | ||
"Optionally specify the number of seconds between checks." | ||
), | ||
) | ||
|
||
# deepsparse edit: replace the parse_args call with parse_known_args | ||
args, _ = parser.parse_known_args() | ||
|
||
# add args.device | ||
args.device = torch.device("cpu") | ||
args.pin_memory = False | ||
if not args.disable_cuda and torch.cuda.is_available(): | ||
args.device = torch.device("cuda") | ||
args.pin_memory = True | ||
LOG.debug("neural network device: %s", args.device) | ||
|
||
datasets.configure(args) | ||
decoder.configure(args) | ||
network.Factory.configure(args) | ||
DeepSparsePredictor.configure(args) | ||
show.configure(args) | ||
visualizer.configure(args) | ||
DeepSparseEvaluator.configure(args) | ||
|
||
return args |
75 changes: 75 additions & 0 deletions
75
src/deepsparse/open_pif_paf/utils/validation/deepsparse_decoder.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import logging | ||
import time | ||
from typing import List, Union | ||
|
||
from deepsparse import Pipeline | ||
from deepsparse.open_pif_paf.utils.validation.helpers import deepsparse_fields_to_torch | ||
from openpifpaf.decoder import CifCaf | ||
from openpifpaf.decoder.decoder import DummyPool | ||
|
||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
__all__ = ["DeepSparseCifCaf"] | ||
|
||
|
||
class DeepSparseCifCaf(CifCaf): | ||
def __init__( | ||
self, | ||
head_metas: List[Union["Cif", "Caf"]], # noqa: F821 | ||
pipeline: Pipeline, | ||
): | ||
self.pipeline = pipeline | ||
cif_metas, caf_metas = head_metas | ||
super().__init__([cif_metas], [caf_metas]) | ||
|
||
# adapted from OPENPIFPAF GITHUB: | ||
# https://github.com/openpifpaf/openpifpaf/blob/main/src/openpifpaf/decoder/decoder.py | ||
# the appropriate edits are marked with # deepsparse edit: <edit comment> | ||
|
||
# deepsparse edit: removed model argument (not needed, substituted with '_') | ||
def batch(self, _, image_batch, *, device=None, gt_anns_batch=None): | ||
"""From image batch straight to annotations batch.""" | ||
start_nn = time.perf_counter() | ||
# deepsparse edit: inference using deepsparse pipeline | ||
# instead of torch model | ||
fields_batch = deepsparse_fields_to_torch( | ||
self.pipeline(images=image_batch.numpy()) | ||
) | ||
self.last_nn_time = time.perf_counter() - start_nn | ||
|
||
if gt_anns_batch is None: | ||
gt_anns_batch = [None for _ in fields_batch] | ||
|
||
if not isinstance(self.worker_pool, DummyPool): | ||
# remove debug_images to save time during pickle | ||
image_batch = [None for _ in fields_batch] | ||
gt_anns_batch = [None for _ in fields_batch] | ||
|
||
LOG.debug("parallel execution with worker %s", self.worker_pool) | ||
start_decoder = time.perf_counter() | ||
result = self.worker_pool.starmap( | ||
self._mappable_annotations, zip(fields_batch, image_batch, gt_anns_batch) | ||
) | ||
self.last_decoder_time = time.perf_counter() - start_decoder | ||
|
||
LOG.debug( | ||
"time: nn = %.1fms, dec = %.1fms", | ||
self.last_nn_time * 1000.0, | ||
self.last_decoder_time * 1000.0, | ||
) | ||
return result |
Oops, something went wrong.