matlab-deep-learning / pix2pix

Image to Image Translation Using Generative Adversarial Networks
Other
30 stars 12 forks source link

deploy trained dlnetwork to onnx file? #13

Open kail85 opened 4 years ago

kail85 commented 4 years ago

exportONNXNetwork() doesn't support dlnetwork object. Is there a way to export the trained dlnetwork object to onnx for further deployment to other frameworks/production?

justinpinkney commented 4 years ago

In theory you can get around this limitation by calling layerGraph on the dlnetwork, add the required output layer, then call assembleNetwork to create the DAGNetwork for export to ONNX. In theory the following code can be used to export to ONNX.

But one subtle point is that the network below needs to be used in training mode, not inference mode, i.e. the dropout should happen, and the batchnorms should not used the trained mean and variances. So the DAG network will not give the expected outputs if you use it in MATLAB, and I'm not sure what the behaviour will be in ONNX.

lg = layerGraph(p2pModel.g);

% add a regression output layer to make it a valid layerGraph
regressionLayerName = "imageOut";
lg = lg.addLayers(regressionLayer("Name", regressionLayerName));
lg = lg.connectLayers("Output", regressionLayerName);

% call asseble to create a DAG network
net = assembleNetwork(lg);

% don't forget the image normalisation for input and output and the tanh
% are not part of the above DAG network

% export to onnx
exportONNXNetwork(net, "p2p.onnx")