pix2pix icon indicating copy to clipboard operation
pix2pix copied to clipboard

deploy trained dlnetwork to onnx file?

Open kail85 opened this issue 4 years ago • 1 comments

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?

kail85 avatar May 13 '20 07:05 kail85

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")

justinpinkney avatar May 13 '20 18:05 justinpinkney