alibaba / TinyNeuralNetwork

TinyNeuralNetwork is an efficient and easy-to-use deep learning model compression framework.
MIT License
744 stars 115 forks source link

Error from `avg_pool3d` & `Conv3D ` in 3D_CNN #49

Closed Le0v1n closed 2 years ago

Le0v1n commented 2 years ago

非常棒的Repo,但我想使用convert.py将PyTorch训练好的3DCNN分类模型转换为.tflite模型,发生了错误:

问题1:x = F.avg_pool3d(x, x.data.size()[-3:])不支持

ERROR (tinynn.converter.base) Unsupported ops: aten::avg_pool3d

问题2:我将Temporal通道Squeeze掉了,但还是发生了错误:

Traceback (most recent call last):
  File "/home/leovin/Dynamic Gesture Detection/3D-CNNs/TinyNeuralNetwork/examples/converter/convert_for_3DCNNs.py", line 50, in <module>
    main_worker()
  File "/home/leovin/Dynamic Gesture Detection/3D-CNNs/TinyNeuralNetwork/examples/converter/convert_for_3DCNNs.py", line 46, in main_worker
    converter.convert()
  File "/home/leovin/anaconda3/envs/dgr/lib/python3.9/site-packages/TinyNeuralNetwork-0.1.20220310142810-py3.9.egg/tinynn/converter/base.py", line 347, in convert
    optimizer.optimize()
  File "/home/leovin/anaconda3/envs/dgr/lib/python3.9/site-packages/TinyNeuralNetwork-0.1.20220310142810-py3.9.egg/tinynn/converter/operators/optimize.py", line 1377, in optimize
    self.transform_graph()
  File "/home/leovin/anaconda3/envs/dgr/lib/python3.9/site-packages/TinyNeuralNetwork-0.1.20220310142810-py3.9.egg/tinynn/converter/operators/optimize.py", line 210, in transform_graph
    op.transform(self.graph, mapping)
  File "/home/leovin/anaconda3/envs/dgr/lib/python3.9/site-packages/TinyNeuralNetwork-0.1.20220310142810-py3.9.egg/tinynn/converter/operators/tflite/transformable.py", line 235, in transform
    assert False, "Only Conv[Transpose]1d/2d is supported"
AssertionError: Only Conv[Transpose]1d/2d is supported

Process finished with exit code 1

Only Conv[Transpose]1d/2d is supported请问是因为不支持Conv3d吗?

问题3:如果支持Conv3d,我该怎么做?

期待您的回答!

peterjc123 commented 2 years ago

问题1: avg_pool3d 目前不支持,新版本支持 问题2: 是的,目前不支持conv3d 问题3:你是问要怎么支持conv3d吗?

Le0v1n commented 2 years ago

我想将我的3D-CNN分类模型运行在手机上,所以如何将含有Conv3d的PyTorch模型转换为.tflite模型呢?

peterjc123 commented 2 years ago

需要converter这边支持conv3d的转换,但是麻烦的是这个会牵扯到tflite schema的升级,因为2.4和2.3完全不兼容

Le0v1n commented 2 years ago

需要converter这边支持conv3d的转换,但是麻烦的是这个会牵扯到tflite schema的升级,因为2.4和2.3完全不兼容

意思是这个问题占时无解,只能等待后续支持Conv3d了,是吗?

请问还有其他办法来实现同样的功能吗?

peterjc123 commented 2 years ago

除非你kernel的维度有1,可以降成2D来跑,否则好像是不行,只能等升级了

Le0v1n commented 2 years ago

除非你kernel的维度有1,可以降成2D来跑,否则好像是不行,只能等升级了

嗯嗯,等升级吧 :joy:

非常感谢你的回答!

peterjc123 commented 2 years ago

@Le0v1n 抱歉,纠正一下,看这里https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/kernels/pooling3d.cc 3d pooling在新版本TFLite中是支持的

Le0v1n commented 2 years ago

收到

peterjc123 commented 2 years ago

相关问题:https://github.com/alibaba/TinyNeuralNetwork/issues/50

peterjc123 commented 2 years ago

TFLite schema升级相关PR: https://github.com/alibaba/TinyNeuralNetwork/pull/57

peterjc123 commented 2 years ago

@Le0v1n 3D卷积和反卷积已经支持了 https://github.com/alibaba/TinyNeuralNetwork/commit/468801a4179506109bebbceb67e7b13af11020bb

Le0v1n commented 2 years ago

@Le0v1n 3D卷积和反卷积已经支持了 468801a

谢谢大佬!大佬辛苦了!

peterjc123 commented 2 years ago

@Le0v1n 帮忙试下模型现在可以转换了不?

Le0v1n commented 2 years ago

@Le0v1n 帮忙试下模型现在可以转换了不?

Some ops are not supported by the native TFLite runtime, you can enable TF kernels fallback using TF Select. See instructions: https://www.tensorflow.org/lite/guide/ops_select 
TF Select ops: AvgPool3D
Details:
    tf.AvgPool3D(tensor<1x1x6x6x1024xf32>) -> (tensor<1x1x1x1x1024xf32>) : {data_format = "NDHWC", device = "", ksize = [1, 1, 6, 6, 1], padding = "VALID", strides = [1, 1, 6, 6, 1]}

nn.AvgPool3d仍然不支持,但3D-CNN可以了。

非常感谢大佬!大佬辛苦了!