openvinotoolkit / nncf

Neural Network Compression Framework for enhanced OpenVINO™ inference
Apache License 2.0
910 stars 227 forks source link

mmdetection onnx convertation #80

Closed HFVladimir closed 3 years ago

HFVladimir commented 4 years ago

Hi! I've trained Cascade RCNN model with mmdetection by using your patch and modifying config. So for converting to onnx I've used in-build converter script in mmdetection. Looks like I've got the same model as before optimization.

Is it necessary to use your converter to onnx? How I can do it with mmdetection training pipeline?

I decided what models are same because after converting to opevino IR I've got the same inference performance. Also my onnx graph hasn't contain anything like 'FakeQuantize' layers.

Thanks, Vladimir

vshampor commented 4 years ago

Greetings, @HFVladimir!

Indeed, it is required in general to export the NNCF-compressed models to ONNX using the compression controller's method, and not via the regular torch.onnx.export path. Look towards the bottom of the usage code snippet in the README.md.

It seems that the corresponding code is missing in the mmdetection integration patch, which we will be fixing soon. What you would have to do in the mmdetection code is basically this - replace the mmdetection's original torch.onnx.export call with the comp_ctrl.export_model taking a single parameter specifying the path to the output ONNX model. Make sure you pass the comp_ctrl compression controller object to wherever that call occurs. See sample code for examples on where this is done in various training pipelines.

lzrvch commented 4 years ago

Hi @HFVladimir, actually you can have a look at the export_model method of the compression controller: https://github.com/openvinotoolkit/nncf_pytorch/blob/develop/nncf/compression_method_api.py#L197-221

We use this method to export quantized models - as you can see, it's not much different from the tools/pytorch2onnx.py script in mmdetection. You can use the mmdetection script to export you model, just mind the torch.onnx.export arguments (e.g. opset_version).

BTW, do you have the FakeQuantize layers properly inserted in your model graph in PyTorch? Could you please share the compressed_graph.dot file which is dumped to your log directory (you should be able to see FakeQuantize layers present there)?

kshpv commented 3 years ago

@HFVladimir

Our patch for mmdetection is just a simple demonstration of how to include NNCF into a big project. This patch doesn't aim to provide a full function to a user. It is just a demonstration.

If you would like to get full NNCF support in mmdetection I highly recommend you to use mmdetection fork, on which our team is working. There is a readme that expresses instructions on how to export a model to ONNX or OpenVINO and to evaluate these models.

While currently we haven't tested Cascade R-CNN support, in our mmdetection fork we will cover all possible models.

@vshampor I suppose we can close this issue

vshampor commented 3 years ago

@aleksei-kashapov is this resolved by #363?

kshpv commented 3 years ago

@aleksei-kashapov is this resolved by #363?

We can not guarantee the support of Cascade R-CNN as export to ONNX is currently unavailable in the official mmdetetction repo. But this model will have an export in our fork, which is here https://github.com/openvinotoolkit/mmdetection