question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Export semantic model fails

See original GitHub issue

Hello.

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:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
wat3rBrocommented, Jun 13, 2021
0reactions
petoorcommented, Jun 13, 2021

Cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Known Issues in Semantic Modeler Preview
To work around this issue, try to deploy the semantic model again. If a second deployment attempted fails, then try to deploy the...
Read more >
Semantic model lost after restart - openHAB Community
So now I will try to transfer things/items configuration from old crazy installation to new installation. I hope, that export / import will ......
Read more >
[2103.00259] Exposing Semantic Segmentation Failures via ...
In this paper, we take steps toward answering the question by exposing failures of existing semantic segmentation methods in the open visual ...
Read more >
EXPORT_TOO_MUCH_DATA" while trying to load mass data ...
Short Text: Too much data for export. Error analysis: An exception occurred that is explained in detail below. This exception cannot be caught...
Read more >
How to solve the "module has no exported member" error?
Remove the line below from your statement export module Demo. and use it like export class ViewModel { constructor(public test: string) { }...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found