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

Error in converting saved_model from TF Hub #108

Closed yyuzhong closed 2 years ago

yyuzhong commented 2 years ago

Issue Type

Bug

OS

Ubuntu

OS architecture

x86_64

Programming Language

Python

Framework

OpenVINO

Download URL for ONNX / OpenVINO IR

https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2

Convert Script

  1. Build docker image and run it with sudo docker run -it --rm -v pwd:/home/user/workdir ghcr.io/pinto0309/openvino2tensorflow:latest
  2. Download TF model from https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2, save and unzip it into ./models/
  3. Run this command in docker container: python3 /usr/local/lib/python3.8/dist-packages/openvino/tools/mo/mo_tf.py --saved_model_dir ./models/ --output_dir models/test/

Description

Please see errors.

Do I need to change TF version in Docker?

Thanks!

Relevant Log Output

user@d007e6e5a961:~/workdir$ python3 /usr/local/lib/python3.8/dist-packages/openvino/tools/mo/mo_tf.py --saved_model_dir ./models/ --output_dir models/test/
/usr/local/lib/python3.8/dist-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 0.23ubuntu1 is an invalid version and will not be supported in a future release
  warnings.warn(
/usr/local/lib/python3.8/dist-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 0.23ubuntu1 is an invalid version and will not be supported in a future release
  warnings.warn(
Model Optimizer arguments:
Common parameters:
    - Path to the Input Model:  None
    - Path for generated IR:    /home/user/workdir/models/test/
    - IR output name:   saved_model
    - Log level:    ERROR
    - Batch:    Not specified, inherited from the model
    - Input layers:     Not specified, inherited from the model
    - Output layers:    Not specified, inherited from the model
    - Input shapes:     Not specified, inherited from the model
    - Source layout:    Not specified
    - Target layout:    Not specified
    - Layout:   Not specified
    - Mean values:  Not specified
    - Scale values:     Not specified
    - Scale factor:     Not specified
    - Precision of IR:  FP32
    - Enable fusing:    True
    - User transformations:     Not specified
    - Reverse input channels:   False
    - Enable IR generation for fixed input shape:   False
    - Use the transformations config file:  None
Advanced parameters:
    - Force the usage of legacy Frontend of Model Optimizer for model conversion into IR:   False
    - Force the usage of new Frontend of Model Optimizer for model conversion into IR:  False
TensorFlow specific parameters:
    - Input model in text protobuf format:  False
    - Path to model dump for TensorBoard:   None
    - List of shared libraries with TensorFlow custom layers implementation:    None
    - Update the configuration file with input/output node names:   None
    - Use configuration file used to generate the model with Object Detection API:  None
    - Use the config file:  None
/usr/local/lib/python3.8/dist-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 0.23ubuntu1 is an invalid version and will not be supported in a future release
  warnings.warn(
OpenVINO runtime found in:  /usr/local/lib/python3.8/dist-packages/openvino
OpenVINO runtime version:   custom_HEAD_cdb9bec7210f8c24fde3e416c7ada820faaaa23e
Model Optimizer version:    custom_HEAD_cdb9bec7210f8c24fde3e416c7ada820faaaa23e
[ WARNING ]  
Detected not satisfied dependencies:
    tensorflow: installed: 2.9.0, required: < 2.6
    numpy: installed: 1.22.4, required: < 1.20
    fastjsonschema: not installed, required: ~= 2.15.1

Please install required versions of components or run pip installation
pip install openvino-dev[tensorflow]
[ WARNING ]  The model contains input(s) with partially defined shapes: name="placeholder" shape="[-1, -1, -1, 3]",name="placeholder_1" shape="[-1, -1, -1, 3]". Starting from the 2022.1 release the Model Optimizer can generate an IR with partially defined input shapes ("-1" dimension in the TensorFlow model or dimension with string value in the ONNX model). Some of the OpenVINO plugins require model input shapes to be static, so you should call "reshape" method in the Inference Engine and specify static input shapes. For optimal performance, it is still recommended to update input shapes with fixed ones using "--input" or "--input_shape" command-line parameters.
[ ERROR ]  Cannot infer shapes or values for node "StatefulPartitionedCall/transformer/residual/residual1/conv1/StyleNorm/CheckNumerics_1".
[ ERROR ]  Error converting shape to a TensorShape: Failed to convert 'masked_array(data=[--, 128],
             mask=[ True, False],
       fill_value=-1000000007)' to a shape: 'masked'could not be converted to a dimension. A shape should either be single dimension (e.g. 10), or an iterable of dimensions (e.g. [1, 10, None])..
[ ERROR ]  
[ ERROR ]  It can happen due to bug in custom shape infer function <function tf_native_tf_node_infer at 0x7f6a682ecaf0>.
[ ERROR ]  Or because the node inputs have incorrect values/shapes.
[ ERROR ]  Or because input shapes are incorrect (embedded to the model or passed via --input_shape).
[ ERROR ]  Run Model Optimizer with --log_level=DEBUG for more information.
[ ERROR ]  Exception occurred during running replacer "REPLACEMENT_ID" (<class 'openvino.tools.mo.middle.PartialInfer.PartialInfer'>): Stopped shape/value propagation at "StatefulPartitionedCall/transformer/residual/residual1/conv1/StyleNorm/CheckNumerics_1" node. 
 For more information please refer to Model Optimizer FAQ, question #38. (https://docs.openvino.ai/latest/openvino_docs_MO_DG_prepare_model_Model_Optimizer_FAQ.html?question=38#question-38)
user@d007e6e5a961:~/workdir$

Source code for simple inference testing code

No response

PINTO0309 commented 2 years ago

https://tfhub.dev/google/lite-model/magenta/arbitrary-image-stylization-v1-256/fp16/prediction/1

docker run -it --rm \
-v `pwd`:/home/user/workdir \
ghcr.io/pinto0309/tflite2tensorflow:latest

tflite2tensorflow \
--model_path magenta_arbitrary-image-stylization-v1-256_fp16_prediction_1.tflite \
--flatc_path ../flatc \
--schema_path ../schema.fbs \
--output_pb \
--optimizing_for_openvino_and_myriad

tflite2tensorflow \
--model_path magenta_arbitrary-image-stylization-v1-256_fp16_prediction_1.tflite \
--flatc_path ../flatc \
--schema_path ../schema.fbs \
--output_no_quant_float32_tflite \
--output_dynamic_range_quant_tflite \
--output_weight_quant_tflite \
--output_float16_quant_tflite \
--output_integer_quant_tflite \
--string_formulas_for_normalization 'data / 255.0' \
--output_tfjs \
--output_coreml \
--output_onnx \
--onnx_opset 11 \
--output_openvino_and_myriad
yyuzhong commented 2 years ago

Can I output the model to Keras .h5 format? Thanks!

PINTO0309 commented 2 years ago
openvino2tensorflow \
--model_path saved_model/openvino/FP32/saved_model.xml \
--output_h5