Cannot convert tf.keras.layers.ConvLSTM2D layer to open vino intermediate representation

狂风中的少年 提交于 2021-01-28 19:06:17

问题


I am trying to convert a trained model in tensorflow to Open VINO Intermediate Representation.

I have a model of the form given below

class Conv3DModel(tf.keras.Model):
    def __init__(self):
        super(Conv3DModel, self).__init__()
        # Convolutions
        self.conv1 = tf.compat.v2.keras.layers.Conv3D(32, (3, 3, 3), activation='relu', name="conv1", data_format='channels_last')
        self.pool1 = tf.keras.layers.MaxPool3D(pool_size=(2, 2, 2), data_format='channels_last')
        self.conv2 = tf.compat.v2.keras.layers.Conv3D(64, (3, 3, 3), activation='relu', name="conv1", data_format='channels_last')
        self.pool2 = tf.keras.layers.MaxPool3D(pool_size=(2, 2,2), data_format='channels_last')

        # LSTM & Flatten
        self.convLSTM =tf.keras.layers.ConvLSTM2D(40, (3, 3))
        self.flatten =  tf.keras.layers.Flatten(name="flatten")

        # Dense layers
        self.d1 = tf.keras.layers.Dense(128, activation='relu', name="d1")
        self.out = tf.keras.layers.Dense(6, activation='softmax', name="output")


    def call(self, x):
        x = self.conv1(x)
        x = self.pool1(x)
        x = self.conv2(x)
        x = self.pool2(x)
        x = self.convLSTM(x)
        x = self.flatten(x)
        x = self.d1(x)
        return self.out(x)

I tried to convert the model into IR. The model is here .

I have trained this model in tensorflow 1.15. Tensorflow 2.0 is currently not supported.

Now I tried to run the command

python3 /opt/intel/openvino/deployment_tools/model_optimizer/mo_tf.py --saved_model_dir jester_trained_models/3dcnn-basic/ --output_dir /home/deepanshu/open_vino/udacity_project_custom_model/

Now i got the following error

Model Optimizer arguments:

Common parameters:

  • Path to the Input Model: None

  • Path for generated IR: /home/deepanshu/open_vino/udacity_project_custom_model/

  • 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

  • Mean values: Not specified

  • Scale values: Not specified

  • Scale factor: Not specified

  • Precision of IR: FP32

  • Enable fusing: True

  • Enable grouped convolutions fusing: True

  • Move mean values to preprocess section: False

  • Reverse input channels: 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

  • Operations to offload: None

  • Patterns to offload: None

  • Use the config file: None

Model Optimizer version: 2020.1.0-61-gd349c3ba4a

[ ERROR ] Unexpected exception happened during extracting attributes for node conv3d_model/conv_lst_m2d/bias/Read/ReadVariableOp. Original exception message: 'ascii' codec can't decode byte 0xc9 in position 1: ordinal not in range(128)

As far as I can see it is the tf.keras.layers.ConvLSTM2D(40, (3, 3)) causing problems . I am kind of stuck here . Can anyone tell me where can I proceed further ?

Thanks

Edit to the question

Now I rejected the above tensorflow implementation and used keras . My h5 model developed was converted into .pb format using this post.

Now I ran the model optimizer on this .pb file. Using the command

python3 /opt/intel/openvino/deployment_tools/model_optimizer/mo_tf.py --input_model /home/deepanshu/ml_playground/jester_freezed/tf_model.pb --output_dir /home/deepanshu/open_vino/udacity_project_custom_model/ --input_shape=[1,30,64,64,1] --data_type FP32

Now i am facing another issue . The issue here is point no. 97 on this post.

So my model contains a cycle and model optimizer does not know a way to convert it. Has anybody faced this issue before ?

Please help.

Here is the model .

Here is the defination of the model in keras


from keras.models import Sequential

from keras.layers import Conv3D , MaxPool3D,Flatten ,Dense

from keras.layers.convolutional_recurrent import ConvLSTM2D

import keras


model = Sequential()

model.add(Conv3D(32, (3, 3, 3), 

         name="conv1" , input_shape=(30, 64, 64,1) ,  data_format='channels_last',

        activation='relu') )

model.add(MaxPool3D(pool_size=(2, 2, 2), data_format='channels_last'))

model.add(Conv3D(64, (3, 3, 3), activation='relu', name="conv2", data_format='channels_last'))

model.add(MaxPool3D(pool_size=(2, 2,2), data_format='channels_last'))

model.add(ConvLSTM2D(40, (3, 3)))

model.add(Flatten(name="flatten"))

model.add(Dense(128, activation='relu', name="d1"))

model.add(Dense(6, activation='softmax', name="output"))

回答1:


Actually the script to convert from h5 to .pb suggested by intel was not good enough. Always use the code from here to convert your keras model to .pb.

Once you obtain your .pb file now convert your model to IR using

python3 /opt/intel/openvino/deployment_tools/model_optimizer/mo_tf.py --input_model ml_playground/try_directory/tf_model.pb   --output_dir /home/deepanshu/open_vino/udacity_project_custom_model/  --input_shape=[1,30,64,64,1] --data_type FP32

After the execution of this script we can obtain the intermediate representation of the keras model.



来源:https://stackoverflow.com/questions/60606297/cannot-convert-tf-keras-layers-convlstm2d-layer-to-open-vino-intermediate-repres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!