Export semantic model fails
See original GitHub issueHello.
Full reproducible code.
import copy
from detectron2 import model_zoo
from detectron2.config import CfgNode
from detectron2.data import build_detection_test_loader
from d2go.export.api import convert_and_export_predictor
from d2go.utils.testing.data_loader_helper import create_fake_detection_data_loader
from d2go.export.d2_meta_arch import patch_d2_meta_arch
from d2go.runner import create_runner
configs = ["COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_1x.yaml","Misc/semantic_R_50_FPN_1x.yaml"]
use_config = 0
cfg = model_zoo.get_config(configs[use_config])
cfg.set_new_allowed(True)
cfg.RCNN_PREPARE_FOR_EXPORT = "default_rcnn_prepare_for_export"
cfg.QUANTIZATION = CfgNode()
cfg.QUANTIZATION.SILICON_QAT = CfgNode({"ENABLED": True})
cfg.QUANTIZATION.QAT = CfgNode({"ENABLED": True})
cfg.MODEL_EMA = CfgNode({"ENABLED": True})
cfg.MODEL.FROZEN_LAYER_REG_EXP = []
cfg.QUANTIZATION.BACKEND = "qnnpack"
runner = create_runner("d2go.runner.GeneralizedRCNNRunner")
if use_config == 0:
pytorch_model = model_zoo.get(configs[use_config], trained=True) # The trained=True is just to verify that the model is working
else:
pytorch_model = model_zoo.get(configs[use_config])
pytorch_model.cpu()
pytorch_model.eval()
patch_d2_meta_arch()
with create_fake_detection_data_loader(224, 320, is_train=False) as data_loader:
predictor_path = convert_and_export_predictor(
cfg,
copy.deepcopy(pytorch_model),
"torchscript@tracing",
'./',
data_loader,
)
### Uncomment to test that model works. ###
#from d2go.utils.demo_predictor import DemoPredictor
#import numpy as np
#import matplotlib.pyplot as plt
#from PIL import Image
#from detectron2.utils.visualizer import Visualizer
#from detectron2.data import MetadataCatalog
#from mobile_cv.predictor.api import create_predictor
#im = np.uint8(Image.open("./000000439715.jpg")) # Some image, for instance one from coco.
#for model in [pytorch_model, "./torchscript@tracing"]:
# if isinstance(model, str): # We load the torchscript model
# model = create_predictor(model)
# predictor = DemoPredictor(model)
# outputs = predictor(im)
# if cfg.MODEL.META_ARCHITECTURE =="SemanticSegmentor":
# We expect to get nonsens since the model isnt trained.
# However the only thing that matters is that the model
# and the torchscript model are agreeing
# result = np.argmax(outputs["sem_seg"].to("cpu").numpy(),axis=0)
# plt.imshow(result)
# plt.show()
# else:
# v = Visualizer(im[:, :, ::-1], MetadataCatalog.get("coco_2017_train"))
# out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
# plt.imshow(out.get_image()[:, :, ::-1])
# plt.show()
You can uncomment the last part to check that both of the mask_rcnn models works.
Changing use_config = 0 to use_config = 1 does the same thing, but for a semantic model in stead of an instance model. Doing this causes an AssertitionError:
File “/home/peter/anaconda3/lib/python3.8/site-packages/d2go/export/api.py”, line 122, in convert_and_export_predictor return export_predictor(cfg, pytorch_model, predictor_type, output_dir, data_loader) File “/home/peter/anaconda3/lib/python3.8/site-packages/d2go/export/api.py”, line 151, in export_predictor return default_export_predictor( File “/home/peter/anaconda3/lib/python3.8/site-packages/d2go/export/api.py”, line 197, in default_export_predictor assert hasattr(pytorch_model, “prepare_for_export”), pytorch_model AssertionError: SemanticSegmentor
It would be very nice to be able to use d2go to export semantic models as well.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
Added in https://github.com/facebookresearch/d2go/pull/83
Cheers!