2021-11-30 15:00:37 +08:00
|
|
|
# Copyright (c) OpenMMLab. All rights reserved.
|
2022-09-14 20:08:52 +08:00
|
|
|
import mmengine
|
2021-11-25 09:57:05 +08:00
|
|
|
import torch
|
|
|
|
|
|
|
|
import mmdeploy.backend.onnxruntime as ort_apis
|
2021-12-10 11:34:22 +08:00
|
|
|
from mmdeploy.codebase import import_codebase
|
|
|
|
from mmdeploy.utils import Backend, Codebase
|
2021-12-01 10:19:11 +08:00
|
|
|
from mmdeploy.utils.test import SwitchBackendWrapper, backend_checker
|
2021-11-25 09:57:05 +08:00
|
|
|
|
2021-12-10 11:34:22 +08:00
|
|
|
import_codebase(Codebase.MMSEG)
|
|
|
|
|
2022-09-14 20:08:52 +08:00
|
|
|
from .utils import generate_datasample # noqa: E402
|
|
|
|
from .utils import generate_mmseg_deploy_config # noqa: E402
|
|
|
|
|
2021-11-25 09:57:05 +08:00
|
|
|
NUM_CLASS = 19
|
|
|
|
IMAGE_SIZE = 32
|
|
|
|
|
|
|
|
|
2021-12-01 10:19:11 +08:00
|
|
|
@backend_checker(Backend.ONNXRUNTIME)
|
2021-11-25 09:57:05 +08:00
|
|
|
class TestEnd2EndModel:
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setup_class(cls):
|
|
|
|
# force add backend wrapper regardless of plugins
|
|
|
|
from mmdeploy.backend.onnxruntime import ORTWrapper
|
|
|
|
ort_apis.__dict__.update({'ORTWrapper': ORTWrapper})
|
|
|
|
|
|
|
|
# simplify backend inference
|
|
|
|
cls.wrapper = SwitchBackendWrapper(ORTWrapper)
|
|
|
|
cls.outputs = {
|
2022-09-14 20:08:52 +08:00
|
|
|
'output': torch.rand(1, 1, IMAGE_SIZE, IMAGE_SIZE),
|
2021-11-25 09:57:05 +08:00
|
|
|
}
|
|
|
|
cls.wrapper.set(outputs=cls.outputs)
|
2022-09-14 20:08:52 +08:00
|
|
|
deploy_cfg = generate_mmseg_deploy_config()
|
2021-11-25 09:57:05 +08:00
|
|
|
|
2022-02-09 17:27:50 +08:00
|
|
|
from mmdeploy.codebase.mmseg.deploy.segmentation_model import \
|
|
|
|
End2EndModel
|
2021-11-25 09:57:05 +08:00
|
|
|
cls.end2end_model = End2EndModel(
|
2022-09-14 20:08:52 +08:00
|
|
|
Backend.ONNXRUNTIME, [''], device='cpu', deploy_cfg=deploy_cfg)
|
2021-11-25 09:57:05 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def teardown_class(cls):
|
|
|
|
cls.wrapper.recover()
|
|
|
|
|
2022-09-14 20:08:52 +08:00
|
|
|
def test_forward(self):
|
|
|
|
from mmseg.structures import SegDataSample
|
|
|
|
imgs = torch.rand(1, 3, IMAGE_SIZE, IMAGE_SIZE)
|
|
|
|
data_samples = [generate_datasample(IMAGE_SIZE, IMAGE_SIZE)]
|
|
|
|
results = self.end2end_model.forward(imgs, data_samples)
|
|
|
|
assert len(results) == 1
|
|
|
|
assert isinstance(results[0], SegDataSample)
|
2021-11-25 09:57:05 +08:00
|
|
|
|
|
|
|
|
2021-12-01 10:19:11 +08:00
|
|
|
@backend_checker(Backend.ONNXRUNTIME)
|
2021-11-25 09:57:05 +08:00
|
|
|
def test_build_segmentation_model():
|
2022-09-14 20:08:52 +08:00
|
|
|
model_cfg = mmengine.Config(
|
2021-11-25 09:57:05 +08:00
|
|
|
dict(data=dict(test={'type': 'CityscapesDataset'})))
|
2022-09-14 20:08:52 +08:00
|
|
|
deploy_cfg = generate_mmseg_deploy_config()
|
2021-11-25 09:57:05 +08:00
|
|
|
|
|
|
|
from mmdeploy.backend.onnxruntime import ORTWrapper
|
|
|
|
ort_apis.__dict__.update({'ORTWrapper': ORTWrapper})
|
|
|
|
|
|
|
|
# simplify backend inference
|
|
|
|
with SwitchBackendWrapper(ORTWrapper) as wrapper:
|
|
|
|
wrapper.set(model_cfg=model_cfg, deploy_cfg=deploy_cfg)
|
2022-02-09 17:27:50 +08:00
|
|
|
from mmdeploy.codebase.mmseg.deploy.segmentation_model import (
|
|
|
|
End2EndModel, build_segmentation_model)
|
2021-11-25 09:57:05 +08:00
|
|
|
segmentor = build_segmentation_model([''], model_cfg, deploy_cfg,
|
|
|
|
'cpu')
|
|
|
|
assert isinstance(segmentor, End2EndModel)
|