paulbauriegel / tensorflow-tools

Python Scripts for working with Tensorflow
MIT License
48 stars 7 forks source link

Failed to use convert-model-to-NHWC.py #5

Closed MaratZakirov closed 4 years ago

MaratZakirov commented 4 years ago

I failed to use your script as is, because of aasert out_trans.shape == sess.graph.get_tensor_by_name(n_org.name+':0').shape failure When I instigate the issue I found that somehow op = sess.graph.create_op(op_type=n_org.type, inputs=op_inputs, name=n_org.name+'_new', attrs=atts) ignores the first batch dimension so shit bellow helps

        if n_org.type == 'Conv2D':
            print('Bef:', atts['strides'].list.i)
            atts['strides'].list.i[0] = atts['strides'].list.i[1]
            atts['strides'].list.i[1] = atts['strides'].list.i[2]
            atts['strides'].list.i[2] = atts['strides'].list.i[3]
            atts['strides'].list.i[3] = 1
            print('Aft:', atts['strides'].list.i)

        op = sess.graph.create_op(op_type=n_org.type, inputs=op_inputs, name=n_org.name+'_new', attrs=atts) 
        out_tens = sess.graph.get_tensor_by_name(n_org.name+'_new'+':0')

After _toco model "succesfully" generated on the tflite generation phase I face weird problem Here is a list of operators for which you will need custom implementations: Dilation2D. Which is strange because resnet18 does not have any fancy dilation values (Conv2d has dilation = 1)

paulbauriegel commented 4 years ago

@MaratZakirov Yes, forgot to transfer the attributes for the Conv2D layer. I added the fix to the script. As for the Dilation2D error look at the solution from https://github.com/tensorflow/tensorflow/issues/38102 maybe that helps:

converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,
                                       tf.lite.OpsSet.SELECT_TF_OPS]

Let me know if that works for you.

MaratZakirov commented 4 years ago

@MaratZakirov Yes, forgot to transfer the attributes for the Conv2D layer. I added the fix to the script. As for the Dilation2D error look at the solution from tensorflow/tensorflow#38102 maybe that helps:

converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,
                                       tf.lite.OpsSet.SELECT_TF_OPS]

Let me know if that works for you.

I currently used New fix from onnx_tf contributor #613 it sems work ok for me, but I am still in transition path between pytorch yolo3 to tflite.

paulbauriegel commented 4 years ago

Alright, in this case I would close the issue. Feel free to reopen it, if any other error occurs with this script.

MaratZakirov commented 4 years ago

Alright, in this case I would close the issue. Feel free to reopen it, if any other error occurs with this script.

@paulbauriegel Have you ever seen pytorch F.interpolate conversion to TFlite issue? Somehow I have it and then I tried to make its analog to avoid TFlite converter 'If convertion error' using torch.view I obtain 'Segmentaion fault' from same converter.

paulbauriegel commented 4 years ago

@MaratZakirov From pytorch to onnx or from onnx to tensorflow or from tensorflow to tflite. I'm not sure what kind of workflow you have so it's hard to help.

MaratZakirov commented 4 years ago

@MaratZakirov From pytorch to onnx or from onnx to tensorflow or from tensorflow to tflite. I'm not sure what kind of workflow you have so it's hard to help.

My workflow is pytorch - onnx - pb - tflite. Pb file exucutes correctly in all cases, all problem I have currently in tflite converter