PINTO0309 / openvino2tensorflow

This script converts the ONNX/OpenVINO IR model to Tensorflow's saved_model, tflite, h5, tfjs, tftrt(TensorRT), CoreML, EdgeTPU, ONNX and pb. PyTorch (NCHW) -> ONNX (NCHW) -> OpenVINO (NCHW) -> openvino2tensorflow -> Tensorflow/Keras (NHWC/NCHW) -> TFLite (NHWC/NCHW). And the conversion from .pb to saved_model and from saved_model to .pb and from .pb to .tflite and saved_model to .tflite and saved_model to onnx. Support for building environments with Docker. It is possible to directly access the host PC GUI and the camera to verify the operation. NVIDIA GPU (dGPU) support. Intel iHD GPU (iGPU) support.
MIT License
334 stars 40 forks source link

single output in u2net onnx model from pytorch #25

Closed milestonezero closed 3 years ago

milestonezero commented 3 years ago

@PINTO0309 amazing work, I am performing U2NET conversion from pytorch to onnx with single output i.e d0 instead of 7(d0,d1,d2,d3,d4,d5,d6).

I followed the blog you shared, and another blog post which follows your blog. But after conversion instead of single sigmoid function for d0, it gives me 7 sigmoid function for all 7 output variables even i have explicitly mentioned only d0 sigmoid function in output names in the command used to convert into onnx. Initially I tried this command:

python3 ${INTEL_OPENVINO_DIR}/deployment_tools/tools/model_downloader/pytorch_to_onnx.py \ --import-module model.u2net \ --model-name U2NETP \ --input-shape 1,3,320,320 \ --weights saved_models/u2netp/u2netp.pth \ --output-file u2netp_320x320.onnx \ --input-names "x" \ --output-names "F.sigmoid(d0)"

https://qiita.com/PINTO/items/ed06e03eb5c007c2e102#6-6-2-generate-onnx-using-pytorch_to_onnxpy-a-backend-module-of-openvinos-model_downloader

Then I tried this command from another blog post.

python3 /opt/intel/openvino_2021/deployment_tools/tools/model_downloader/pytorch_to_onnx.py \ --import-module model.u2net \ --model-name U2NETP \ --input-shape 1,3,${SIZE},${SIZE} \ --weights savedmodels/u2netp/u2netp.pth \ --output-file u2netp${SIZE}x${SIZE}.onnx --input-names "x" \ --output-names "a/F.sigmoid(d0)"

https://dannadori.medium.com/convert-pytorch-model-to-tensorflowjs-fb3bc8e90589

But after successful conversion into onnx, i opened the model in NETRON(a network visualization package) to see the output but instead of 1 output variable it's converting with all 7 variables.

PINTO0309 commented 3 years ago

--output-names is only meant to change the name of the output as desired. If you want to change the number of outputs, just change the return of the forward function.

NaeemKhan333 commented 3 years ago

I am also confusion that the command given in the Dannadori's blog

python3 /opt/intel/openvino_2021/deployment_tools/tools/model_downloader/pytorch_to_onnx.py
--import-module model.u2net
--model-name U2NETP
--input-shape 1,3,${SIZE},${SIZE}
--weights saved_models/u2netp/u2netp.pth
--output-file u2netp_${SIZE}x${SIZE}.onnx --input-names "x"
--output-names "a/F.sigmoid(d0)"

is it use for custom train u2net model or you can extract the output variable during conversion, becuase when I open in Netron its converted u2net-portrait tensorflow js model , its giving me as following network output

your-blog-model

Can you help me out in this matter?

PINTO0309 commented 3 years ago

What I have stated is all I have to say. Move your own hands to see what happens at each step.

NaeemKhan333 commented 3 years ago

@PINTO0309 I have followed each and every steps given in your blog as well as given in the Dannadori's Blog . I can convert the model sucessfully but when I check the structure of the output , my model output is difference than Dannadori converted model as I mention .I have used the same u2net-portrait model for conversion as Dannadori converted but output is not same.

PINTO0309 commented 3 years ago

That Dannadori's blog is not my blog, so I have no way of knowing the background that is not mentioned. Just try it first.

From:

return F.sigmoid(d0), F.sigmoid(d1), F.sigmoid(d2), F.sigmoid(d3), F.sigmoid(d4), F.sigmoid(d5), F.sigmoid(d6)

To:

return F.sigmoid(d0) #, F.sigmoid(d1), F.sigmoid(d2), F.sigmoid(d3), F.sigmoid(d4), F.sigmoid(d5), F.sigmoid(d6)
NaeemKhan333 commented 3 years ago

@PINTO0309 thank you for response, I have done the same commit of code as you mentioned but it is not working for me and secondly understand your point, will consult to Dannadori about prerequisite have done for converting the model to get the same output as Dannadori Tensorflow Js converted model have.