From db33ced14313c97e12ec1afc841a1fcd0dbf0e9f Mon Sep 17 00:00:00 2001 From: Cathy0908 <30484308+Cathy0908@users.noreply.github.com> Date: Fri, 18 Aug 2023 14:05:15 +0800 Subject: [PATCH 1/2] fix numpy version compatibility (#327) * fix numpy version compatibility --- .../evaluation/custom_cocotools/cocoeval.py | 4 +- .../face/pipelines/face_keypoint_transform.py | 2 +- easycv/models/utils/pos_embed.py | 2 +- .../thirdparty/mot/bytetrack/byte_tracker.py | 2 +- easycv/thirdparty/mot/bytetrack/matching.py | 12 ++--- easycv/version.py | 4 +- tests/test_core/evaluation/test_coco_tools.py | 52 ++++++++++++++----- .../test_models/detection/yolox/test_yolox.py | 4 +- .../detection/yolox_edge/test_yolox_edge.py | 4 +- .../test_panoptic_segmentation_pipeline.py | 8 +-- .../pipelines/test_segmentation_pipeline.py | 8 +-- 11 files changed, 58 insertions(+), 44 deletions(-) diff --git a/easycv/core/evaluation/custom_cocotools/cocoeval.py b/easycv/core/evaluation/custom_cocotools/cocoeval.py index 4606f5c4..7400e13b 100644 --- a/easycv/core/evaluation/custom_cocotools/cocoeval.py +++ b/easycv/core/evaluation/custom_cocotools/cocoeval.py @@ -467,8 +467,8 @@ def accumulate(self, p=None): fps = np.logical_and( np.logical_not(dtm), np.logical_not(dtIg)) - tp_sum = np.cumsum(tps, axis=1).astype(dtype=np.float) - fp_sum = np.cumsum(fps, axis=1).astype(dtype=np.float) + tp_sum = np.cumsum(tps, axis=1).astype(dtype=np.float32) + fp_sum = np.cumsum(fps, axis=1).astype(dtype=np.float32) for t, (tp, fp) in enumerate(zip(tp_sum, fp_sum)): tp = np.array(tp) fp = np.array(fp) diff --git a/easycv/datasets/face/pipelines/face_keypoint_transform.py b/easycv/datasets/face/pipelines/face_keypoint_transform.py index bda83859..0add55e4 100644 --- a/easycv/datasets/face/pipelines/face_keypoint_transform.py +++ b/easycv/datasets/face/pipelines/face_keypoint_transform.py @@ -252,7 +252,7 @@ def aug_clr_noise_blur(self, img): skin_factor_list = [0.6, 0.8, 1.0, 1.2, 1.4] skin_factor = np.random.choice(skin_factor_list) img_ycrcb_raw[:, :, 0:1] = np.clip( - img_ycrcb_raw[:, :, 0:1].astype(np.float) * skin_factor, 0, + img_ycrcb_raw[:, :, 0:1].astype(np.float32) * skin_factor, 0, 255).astype(np.uint8) img = cv2.cvtColor(img_ycrcb_raw, cv2.COLOR_YCR_CB2BGR) diff --git a/easycv/models/utils/pos_embed.py b/easycv/models/utils/pos_embed.py index e8616486..853a3ed2 100644 --- a/easycv/models/utils/pos_embed.py +++ b/easycv/models/utils/pos_embed.py @@ -47,7 +47,7 @@ def get_1d_sincos_pos_embed_from_grid(embed_dim, pos): out: (M, D) """ assert embed_dim % 2 == 0 - omega = np.arange(embed_dim // 2, dtype=np.float) + omega = np.arange(embed_dim // 2, dtype=np.float32) omega /= embed_dim / 2. omega = 1. / 10000**omega # (D/2,) diff --git a/easycv/thirdparty/mot/bytetrack/byte_tracker.py b/easycv/thirdparty/mot/bytetrack/byte_tracker.py index c7576448..dab52b7b 100644 --- a/easycv/thirdparty/mot/bytetrack/byte_tracker.py +++ b/easycv/thirdparty/mot/bytetrack/byte_tracker.py @@ -38,7 +38,7 @@ class STrack(BaseTrack): def __init__(self, tlwh, score): # wait activate - self._tlwh = np.asarray(tlwh, dtype=np.float) + self._tlwh = np.asarray(tlwh, dtype=np.float32) self.kalman_filter = None self.mean, self.covariance = None, None self.is_activated = False diff --git a/easycv/thirdparty/mot/bytetrack/matching.py b/easycv/thirdparty/mot/bytetrack/matching.py index 301740ac..bda5265e 100644 --- a/easycv/thirdparty/mot/bytetrack/matching.py +++ b/easycv/thirdparty/mot/bytetrack/matching.py @@ -86,15 +86,15 @@ def ious(atlbrs, btlbrs): :rtype ious np.ndarray """ - ious = np.zeros((len(atlbrs), len(btlbrs)), dtype=np.float) + ious = np.zeros((len(atlbrs), len(btlbrs)), dtype=np.float32) if ious.size == 0: return ious from cython_bbox import bbox_overlaps as bbox_ious ious = bbox_ious( - np.ascontiguousarray(atlbrs, dtype=np.float), - np.ascontiguousarray(btlbrs, dtype=np.float)) + np.ascontiguousarray(atlbrs, dtype=np.float32), + np.ascontiguousarray(btlbrs, dtype=np.float32)) return ious @@ -151,15 +151,15 @@ def embedding_distance(tracks, detections, metric='cosine'): :return: cost_matrix np.ndarray """ - cost_matrix = np.zeros((len(tracks), len(detections)), dtype=np.float) + cost_matrix = np.zeros((len(tracks), len(detections)), dtype=np.float32) if cost_matrix.size == 0: return cost_matrix det_features = np.asarray([track.curr_feat for track in detections], - dtype=np.float) + dtype=np.float32) #for i, track in enumerate(tracks): #cost_matrix[i, :] = np.maximum(0.0, cdist(track.smooth_feat.reshape(1,-1), det_features, metric)) track_features = np.asarray([track.smooth_feat for track in tracks], - dtype=np.float) + dtype=np.float32) cost_matrix = np.maximum(0.0, cdist(track_features, det_features, metric)) # Nomalized features return cost_matrix diff --git a/easycv/version.py b/easycv/version.py index b9f3853d..6db1a976 100644 --- a/easycv/version.py +++ b/easycv/version.py @@ -2,5 +2,5 @@ # GENERATED VERSION FILE # TIME: Thu Nov 5 14:17:50 2020 -__version__ = '0.11.3' -short_version = '0.11.3' +__version__ = '0.11.4' +short_version = '0.11.4' diff --git a/tests/test_core/evaluation/test_coco_tools.py b/tests/test_core/evaluation/test_coco_tools.py index ef30b33e..a15d7ae7 100644 --- a/tests/test_core/evaluation/test_coco_tools.py +++ b/tests/test_core/evaluation/test_coco_tools.py @@ -89,8 +89,8 @@ def testCocoWrappers(self): def testExportGroundtruthToCOCO(self): image_ids = ['first', 'second'] groundtruth_boxes = [ - np.array([[100, 100, 200, 200]], np.float), - np.array([[50, 50, 100, 100]], np.float) + np.array([[100, 100, 200, 200]], np.float32), + np.array([[50, 50, 100, 100]], np.float32) ] groundtruth_classes = [ np.array([1], np.int32), @@ -126,12 +126,12 @@ def testExportGroundtruthToCOCO(self): def testExportDetectionsToCOCO(self): image_ids = ['first', 'second'] detections_boxes = [ - np.array([[100, 100, 200, 200]], np.float), - np.array([[50, 50, 100, 100]], np.float) + np.array([[100, 100, 200, 200]], np.float32), + np.array([[50, 50, 100, 100]], np.float32) ] detections_scores = [ - np.array([.8], np.float), - np.array([.7], np.float) + np.array([.8], np.float32), + np.array([.7], np.float32) ] detections_classes = [np.array([1], np.int32), np.array([1], np.int32)] categories = [{ @@ -152,7 +152,17 @@ def testExportDetectionsToCOCO(self): detections_classes, categories, output_path=output_path) - self.assertListEqual(result, self._detections_list) + + self.assertEqual(len(result), len(detections_boxes)) + self.assertEqual(len(detections_boxes), len(detections_boxes)) + + score_list = [] + for i in range(len(detections_boxes)): + score = self._detections_list[i].pop('score') + score_list.append(score) + self.assertAlmostEqual(result[i].pop('score'), score) + self.assertDictEqual(result[i], self._detections_list[i]) + with io.open(output_path, 'r') as f: written_result = f.read() # The json output should have floats written to 4 digits of precision. @@ -160,7 +170,10 @@ def testExportDetectionsToCOCO(self): re.MULTILINE) self.assertTrue(matcher.findall(written_result)) written_result = json.loads(written_result) - self.assertAlmostEqual(result, written_result) + for i in range(len(result)): + self.assertAlmostEqual(written_result[i].pop('score'), + score_list[i]) + self.assertDictEqual(result[i], written_result[i]) def testExportSegmentsToCOCO(self): image_ids = ['first', 'second'] @@ -176,7 +189,10 @@ def testExportSegmentsToCOCO(self): for i, detection_mask in enumerate(detection_masks): detection_masks[i] = detection_mask[:, :, :, None] - detection_scores = [np.array([.8], np.float), np.array([.7], np.float)] + detection_scores = [ + np.array([.8], np.float32), + np.array([.7], np.float32) + ] detection_classes = [np.array([1], np.int32), np.array([1], np.int32)] categories = [{ @@ -202,7 +218,12 @@ def testExportSegmentsToCOCO(self): written_result = json.loads(written_result) mask_load = mask.decode([written_result[0]['segmentation']]) self.assertTrue(np.allclose(mask_load, detection_masks[0])) - self.assertAlmostEqual(result, written_result) + self.assertEqual(len(result), len(detection_masks)) + self.assertEqual(len(written_result), len(detection_masks)) + for i in range(len(detection_masks)): + self.assertAlmostEqual(result[i].pop('score'), + written_result[i].pop('score')) + self.assertDictEqual(result[i], written_result[i]) def testExportKeypointsToCOCO(self): image_ids = ['first', 'second'] @@ -216,8 +237,8 @@ def testExportKeypointsToCOCO(self): ] detection_scores = [ - np.array([.8, 0.2], np.float), - np.array([.7, 0.3], np.float) + np.array([.8, 0.2], np.float32), + np.array([.7, 0.3], np.float32) ] detection_classes = [ np.array([1, 1], np.int32), @@ -248,7 +269,12 @@ def testExportKeypointsToCOCO(self): with io.open(output_path, 'r') as f: written_result = f.read() written_result = json.loads(written_result) - self.assertAlmostEqual(result, written_result) + self.assertEqual(len(result), 4) + self.assertEqual(len(written_result), 4) + for i in range(4): + self.assertAlmostEqual(result[i].pop('score'), + written_result[i].pop('score')) + self.assertDictEqual(result[i], written_result[i]) def testSingleImageDetectionBoxesExport(self): boxes = np.array([[0, 0, 1, 1], [0, 0, .5, .5], [.5, .5, 1, 1]], diff --git a/tests/test_models/detection/yolox/test_yolox.py b/tests/test_models/detection/yolox/test_yolox.py index 98325c73..7826b714 100644 --- a/tests/test_models/detection/yolox/test_yolox.py +++ b/tests/test_models/detection/yolox/test_yolox.py @@ -40,9 +40,9 @@ def test_yolox(self): } output = model(imgs, mode='train', **kwargs) self.assertEqual(output['img_h'].cpu().numpy(), - np.array(640, dtype=np.float)) + np.array(640, dtype=np.float32)) self.assertEqual(output['img_w'].cpu().numpy(), - np.array(640, dtype=np.float)) + np.array(640, dtype=np.float32)) self.assertEqual(output['total_loss'].shape, torch.Size([])) self.assertEqual(output['iou_l'].shape, torch.Size([])) self.assertEqual(output['conf_l'].shape, torch.Size([])) diff --git a/tests/test_models/detection/yolox_edge/test_yolox_edge.py b/tests/test_models/detection/yolox_edge/test_yolox_edge.py index 2eeed017..cc868646 100644 --- a/tests/test_models/detection/yolox_edge/test_yolox_edge.py +++ b/tests/test_models/detection/yolox_edge/test_yolox_edge.py @@ -45,9 +45,9 @@ def test_yolox_edge(self): } output = model(imgs, mode='train', **kwargs) self.assertEqual(output['img_h'].cpu().numpy(), - np.array(640, dtype=np.float)) + np.array(640, dtype=np.float32)) self.assertEqual(output['img_w'].cpu().numpy(), - np.array(640, dtype=np.float)) + np.array(640, dtype=np.float32)) self.assertEqual(output['total_loss'].shape, torch.Size([])) self.assertEqual(output['iou_l'].shape, torch.Size([])) self.assertEqual(output['conf_l'].shape, torch.Size([])) diff --git a/tests/test_toolkit/modelscope/pipelines/test_panoptic_segmentation_pipeline.py b/tests/test_toolkit/modelscope/pipelines/test_panoptic_segmentation_pipeline.py index 4de349b1..8766e642 100644 --- a/tests/test_toolkit/modelscope/pipelines/test_panoptic_segmentation_pipeline.py +++ b/tests/test_toolkit/modelscope/pipelines/test_panoptic_segmentation_pipeline.py @@ -8,13 +8,11 @@ from modelscope.pipelines import pipeline from modelscope.utils.constant import Tasks from modelscope.utils.cv.image_utils import panoptic_seg_masks_to_image -from modelscope.utils.demo_utils import DemoCompatibilityCheck from modelscope.utils.test_utils import test_level from tests.ut_config import BASE_LOCAL_PATH -class EasyCVPanopticSegmentationPipelineTest(unittest.TestCase, - DemoCompatibilityCheck): +class EasyCVPanopticSegmentationPipelineTest(unittest.TestCase): img_path = os.path.join( BASE_LOCAL_PATH, 'data/test_images/image_semantic_segmentation.jpg') @@ -32,10 +30,6 @@ def test_r50(self): cv2.imwrite(tmp_save_path, draw_img) print('print ' + self.model_id + ' success') - @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') - def test_demo_compatibility(self): - self.compatibility_check() - if __name__ == '__main__': unittest.main() diff --git a/tests/test_toolkit/modelscope/pipelines/test_segmentation_pipeline.py b/tests/test_toolkit/modelscope/pipelines/test_segmentation_pipeline.py index 5e2ac3ca..dade7ef9 100644 --- a/tests/test_toolkit/modelscope/pipelines/test_segmentation_pipeline.py +++ b/tests/test_toolkit/modelscope/pipelines/test_segmentation_pipeline.py @@ -9,14 +9,12 @@ from modelscope.pipelines import pipeline from modelscope.utils.constant import Tasks from modelscope.utils.cv.image_utils import semantic_seg_masks_to_image -from modelscope.utils.demo_utils import DemoCompatibilityCheck from modelscope.utils.test_utils import test_level from PIL import Image from tests.ut_config import BASE_LOCAL_PATH -class EasyCVSegmentationPipelineTest(unittest.TestCase, - DemoCompatibilityCheck): +class EasyCVSegmentationPipelineTest(unittest.TestCase): img_path = os.path.join(BASE_LOCAL_PATH, 'data/test_images/image_segmentation.jpg') @@ -82,10 +80,6 @@ def test_segformer_b5(self): model_id = 'damo/cv_segformer-b5_image_semantic-segmentation_coco-stuff164k' self._internal_test_(model_id) - @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') - def test_demo_compatibility(self): - self.compatibility_check() - if __name__ == '__main__': unittest.main() From 8c3ba59aaf189b5a7e6ce88763f2d9acdf90a3cc Mon Sep 17 00:00:00 2001 From: gulou <113007768+liaogulou@users.noreply.github.com> Date: Tue, 31 Oct 2023 14:56:08 +0800 Subject: [PATCH 2/2] Features/self test onnx (#330) add yolox onnx export method --- configs/config_templates/yolox_itag.py | 78 +++++++++++++------------- easycv/apis/export.py | 31 +++++++++- easycv/predictors/detector.py | 37 ++++++++++-- requirements/runtime.txt | 1 + tests/test_tools/test_predict.py | 6 +- 5 files changed, 101 insertions(+), 52 deletions(-) diff --git a/configs/config_templates/yolox_itag.py b/configs/config_templates/yolox_itag.py index fff720b2..b190edfb 100644 --- a/configs/config_templates/yolox_itag.py +++ b/configs/config_templates/yolox_itag.py @@ -49,14 +49,14 @@ mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True) train_pipeline = [ - dict(type='MMMosaic', img_scale='${img_scale}', pad_val=114.0), + dict(type='MMMosaic', img_scale=tuple(img_scale), pad_val=114.0), dict( type='MMRandomAffine', - scaling_ratio_range='${scale_ratio}', - border=['-${img_scale}[0] // 2', '-${img_scale}[1] // 2']), + scaling_ratio_range=scale_ratio, + border=[img_scale[0] // 2, img_scale[1] // 2]), dict( type='MMMixUp', # s m x l; tiny nano will detele - img_scale='${img_scale}', + img_scale=tuple(img_scale), ratio_range=(0.8, 1.6), pad_val=114.0), dict( @@ -70,45 +70,43 @@ dict(type='MMPad', pad_to_square=True, pad_val=(114.0, 114.0, 114.0)), dict( type='MMNormalize', - mean='${img_norm_cfg.mean}', - std='${img_norm_cfg.std}', - to_rgb='${img_norm_cfg.to_rgb}'), + mean=img_norm_cfg['mean'], + std=img_norm_cfg['std'], + to_rgb=img_norm_cfg['to_rgb']), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']) ] test_pipeline = [ - dict(type='MMResize', img_scale='${img_scale}', keep_ratio=True), + dict(type='MMResize', img_scale=img_scale, keep_ratio=True), dict(type='MMPad', pad_to_square=True, pad_val=(114.0, 114.0, 114.0)), dict( type='MMNormalize', - mean='${img_norm_cfg.mean}', - std='${img_norm_cfg.std}', - to_rgb='${img_norm_cfg.to_rgb}'), + mean=img_norm_cfg['mean'], + std=img_norm_cfg['std'], + to_rgb=img_norm_cfg['to_rgb']), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img']) ] +train_path = 'data/coco/train2017.manifest' +val_path = 'data/coco/val2017.manifest' + +train_dataset = dict( + type='DetImagesMixDataset', + data_source=dict(type='DetSourcePAI', path=train_path, classes=CLASSES), + pipeline=train_pipeline, + dynamic_scale=tuple(img_scale)) + +val_dataset = dict( + type='DetImagesMixDataset', + imgs_per_gpu=2, + data_source=dict(type='DetSourcePAI', path=val_path, classes=CLASSES), + pipeline=test_pipeline, + dynamic_scale=None, + label_padding=False) + data = dict( - imgs_per_gpu=16, - workers_per_gpu=4, - train=dict( - type='DetImagesMixDataset', - data_source=dict( - type='DetSourcePAI', - path='data/coco/train2017.manifest', - classes='${CLASSES}'), - pipeline='${train_pipeline}', - dynamic_scale='${img_scale}'), - val=dict( - type='DetImagesMixDataset', - imgs_per_gpu=2, - data_source=dict( - type='DetSourcePAI', - path='data/coco/val2017.manifest', - classes='${CLASSES}'), - pipeline='${test_pipeline}', - dynamic_scale=None, - label_padding=False)) + imgs_per_gpu=16, workers_per_gpu=4, train=train_dataset, val=val_dataset) # additional hooks interval = 10 @@ -120,14 +118,14 @@ priority=48), dict( type='SyncRandomSizeHook', - ratio_range='${random_size}', - img_scale='${img_scale}', - interval='${interval}', + ratio_range=random_size, + img_scale=img_scale, + interval=interval, priority=48), dict( type='SyncNormHook', num_last_epochs=15, - interval='${interval}', + interval=interval, priority=48) ] @@ -135,23 +133,23 @@ vis_num = 20 score_thr = 0.5 eval_config = dict( - interval='${interval}', + interval=interval, gpu_collect=False, visualization_config=dict( - vis_num='${vis_num}', - score_thr='${score_thr}', + vis_num=vis_num, + score_thr=score_thr, ) # show by TensorboardLoggerHookV2 ) eval_pipelines = [ dict( mode='test', - data='${data.val}', + data=val_dataset, evaluators=[dict(type='CocoDetectionEvaluator', classes=CLASSES)], ) ] -checkpoint_config = dict(interval='${interval}') +checkpoint_config = dict(interval=interval) # optimizer # basic_lr_per_img = 0.01 / 64.0 optimizer = dict( diff --git a/easycv/apis/export.py b/easycv/apis/export.py index 3f8ffb07..0cdc4da7 100644 --- a/easycv/apis/export.py +++ b/easycv/apis/export.py @@ -247,10 +247,10 @@ def _export_yolox(model, cfg, filename): if hasattr(cfg, 'export'): export_type = getattr(cfg.export, 'export_type', 'raw') - default_export_type_list = ['raw', 'jit', 'blade'] + default_export_type_list = ['raw', 'jit', 'blade', 'onnx'] if export_type not in default_export_type_list: logging.warning( - 'YOLOX-PAI only supports the export type as [raw,jit,blade], otherwise we use raw as default' + 'YOLOX-PAI only supports the export type as [raw,jit,blade,onnx], otherwise we use raw as default' ) export_type = 'raw' @@ -276,7 +276,7 @@ def _export_yolox(model, cfg, filename): len(img_scale) == 2 ), 'Export YoloX predictor config contains img_scale must be (int, int) tuple!' - input = 255 * torch.rand((batch_size, 3) + img_scale) + input = 255 * torch.rand((batch_size, 3) + tuple(img_scale)) # assert use_trt_efficientnms only happens when static_opt=True if static_opt is not True: @@ -355,6 +355,31 @@ def _export_yolox(model, cfg, filename): json.dump(config, ofile) + if export_type == 'onnx': + + with io.open( + filename + '.config.json' if filename.endswith('onnx') + else filename + '.onnx.config.json', 'w') as ofile: + config = dict( + model=cfg.model, + export=cfg.export, + test_pipeline=cfg.test_pipeline, + classes=cfg.CLASSES) + + json.dump(config, ofile) + + torch.onnx.export( + model, + input.to(device), + filename if filename.endswith('onnx') else filename + + '.onnx', + export_params=True, + opset_version=12, + do_constant_folding=True, + input_names=['input'], + output_names=['output'], + ) + if export_type == 'jit': with io.open(filename + '.jit', 'wb') as ofile: torch.jit.save(yolox_trace, ofile) diff --git a/easycv/predictors/detector.py b/easycv/predictors/detector.py index 35d62e22..ed7dd908 100644 --- a/easycv/predictors/detector.py +++ b/easycv/predictors/detector.py @@ -23,6 +23,12 @@ from .interface import PredictorInterface +# 将张量转化为ndarray格式 +def onnx_to_numpy(tensor): + return tensor.detach().cpu().numpy( + ) if tensor.requires_grad else tensor.cpu().numpy() + + class DetInputProcessor(InputProcessor): def build_processor(self): @@ -349,9 +355,11 @@ def __init__(self, self.model_type = 'jit' elif model_path.endswith('blade'): self.model_type = 'blade' + elif model_path.endswith('onnx'): + self.model_type = 'onnx' else: self.model_type = 'raw' - assert self.model_type in ['raw', 'jit', 'blade'] + assert self.model_type in ['raw', 'jit', 'blade', 'onnx'] if self.model_type == 'blade' or self.use_trt_efficientnms: import torch_blade @@ -381,8 +389,16 @@ def __init__(self, def _build_model(self): if self.model_type != 'raw': - with io.open(self.model_path, 'rb') as infile: - model = torch.jit.load(infile, self.device) + if self.model_type != 'onnx': + with io.open(self.model_path, 'rb') as infile: + model = torch.jit.load(infile, self.device) + else: + import onnxruntime + if onnxruntime.get_device() == 'GPU': + model = onnxruntime.InferenceSession( + self.model_path, providers=['CUDAExecutionProvider']) + else: + model = onnxruntime.InferenceSession(self.model_path) else: from easycv.utils.misc import reparameterize_models model = super()._build_model() @@ -394,8 +410,9 @@ def prepare_model(self): If the model is not loaded from a configuration file, e.g. torch jit model, you need to reimplement it. """ model = self._build_model() - model.to(self.device) - model.eval() + if self.model_type != 'onnx': + model.to(self.device) + model.eval() if self.model_type == 'raw': load_checkpoint(model, self.model_path, map_location='cpu') return model @@ -406,7 +423,15 @@ def model_forward(self, inputs): """ if self.model_type != 'raw': with torch.no_grad(): - outputs = self.model(inputs['img']) + if self.model_type != 'onnx': + outputs = self.model(inputs['img']) + else: + outputs = self.model.run( + None, { + self.model.get_inputs()[0].name: + onnx_to_numpy(inputs['img']) + })[0] + outputs = torch.from_numpy(outputs) outputs = {'results': outputs} # convert to dict format else: outputs = super().model_forward(inputs) diff --git a/requirements/runtime.txt b/requirements/runtime.txt index edff212e..2aa63ac3 100644 --- a/requirements/runtime.txt +++ b/requirements/runtime.txt @@ -13,6 +13,7 @@ lmdb numba numpy nuscenes-devkit +onnxruntime opencv-python oss2 packaging diff --git a/tests/test_tools/test_predict.py b/tests/test_tools/test_predict.py index 74c163fd..69f9a3e9 100644 --- a/tests/test_tools/test_predict.py +++ b/tests/test_tools/test_predict.py @@ -83,12 +83,12 @@ def test_predict_oss_path(self): oss_config = get_oss_config() ak_id = oss_config['ak_id'] ak_secret = oss_config['ak_secret'] - hosts = oss_config['hosts'] + ['oss-cn-hangzhou.aliyuncs.com'] + hosts = oss_config['hosts'] hosts = ','.join(_ for _ in hosts) - buckets = oss_config['buckets'] + ['easycv'] + buckets = oss_config['buckets'] buckets = ','.join(_ for _ in buckets) - input_file = 'oss://easycv/data/small_test_data/test_images/http_image_list.txt' + input_file = 'oss://pai-vision-data-hz/unittest/local_backup/easycv_nfs/data/test_images/http_image_list.txt' output_file = tempfile.NamedTemporaryFile('w').name cmd = f'PYTHONPATH=. python tools/predict.py \ --input_file {input_file} \