【问题标题】:ValueError: None is only supported in the 1st dimension. Tensor 'input_tensor' has invalid shape '[1, None, None, 3]'ValueError: None 仅在第一维中受支持。张量“input_tensor”的形状无效“[1, None, None, 3]”
【发布时间】:2020-10-22 13:32:00
【问题描述】:

我训练了一个自定义 MobileNetV2 SSD 模型来进行对象检测。我保存了 .pb 文件,现在我想将其转换为 .tflite 文件,以便与 Coral edge-tpu 一起使用。

我在 CPU 上的 Windows 10 上使用 Tensorflow 2.2。

我正在使用的代码:

import tensorflow as tf

saved_model_dir = r"C:/Tensorflow/Backup_Training/my_MobileNetV2_fpnlite_320x320/saved_model"
num_calibration_steps = 100

def representative_dataset_gen():
    for _ in range(num_calibration_steps):
        # Get sample input data as a numpy array
        yield [np.random.uniform(0.0, 1.0, size=(1,416,416, 3)).astype(np.float32)]

converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.optimizations = [tf.lite.Optimize.DEFAULT]

converter.experimental_new_converter = True

converter.representative_dataset = representative_dataset_gen
converter.target_spec.supported_ops = [
    #tf.lite.OpsSet.TFLITE_BUILTINS_INT8,
    tf.lite.OpsSet.TFLITE_BUILTINS,
    tf.lite.OpsSet.SELECT_TF_OPS
    ]
converter.inference_input_type = tf.int8
converter.inference_output_type = tf.int8

tflite_quant_model = converter.convert()

with open('model.tflite', 'wb') as f:
    f.write(tflite_model)

我尝试了其他线程提出的几种解决方案,还尝试了 tf-nightly、tf2.3 和 tf1.14,但都没有奏效(总是有另一个我无法处理的错误消息)。由于我使用 tf2.2 进行了培训,因此我认为继续使用 tf2.2 可能是个好主意。

由于我是 Tensorflow 的新手,我有几个问题:输入张量到底是什么,我在哪里定义它?是否有可能查看或提取此输入张量? 有人知道如何解决这个问题吗?

整个错误信息:

(tf22) C:\Tensorflow\Backup_Training>python full_int_quant.py
2020-10-22 14:51:20.460948: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-10-22 14:51:20.466366: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2020-10-22 14:51:29.231404: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2020-10-22 14:51:29.239003: E tensorflow/stream_executor/cuda/cuda_driver.cc:313] failed call to cuInit: UNKNOWN ERROR (303)
2020-10-22 14:51:29.250497: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: ip3536
2020-10-22 14:51:29.258432: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: ip3536
2020-10-22 14:51:29.269261: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-10-22 14:51:29.291457: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x2ae2ac3ffc0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-10-22 14:51:29.298043: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-10-22 14:52:03.785341: I tensorflow/core/grappler/devices.cc:55] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0
2020-10-22 14:52:03.790251: I tensorflow/core/grappler/clusters/single_machine.cc:356] Starting new session
2020-10-22 14:52:04.559832: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:797] Optimization results for grappler item: graph_to_optimize
2020-10-22 14:52:04.564529: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:799]   function_optimizer: Graph size after: 3672 nodes (3263), 5969 edges (5553), time = 136.265ms.
2020-10-22 14:52:04.570187: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:799]   function_optimizer: function_optimizer did nothing. time = 2.637ms.
2020-10-22 14:52:10.742013: I tensorflow/core/grappler/devices.cc:55] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0
2020-10-22 14:52:10.746868: I tensorflow/core/grappler/clusters/single_machine.cc:356] Starting new session
2020-10-22 14:52:12.358897: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:797] Optimization results for grappler item: graph_to_optimize
2020-10-22 14:52:12.363657: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:799]   constant_folding: Graph size after: 1714 nodes (-1958), 2661 edges (-3308), time = 900.347ms.
2020-10-22 14:52:12.369137: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:799]   constant_folding: Graph size after: 1714 nodes (0), 2661 edges (0), time = 60.628ms.
Traceback (most recent call last):
  File "full_int_quant.py", line 40, in <module>
    tflite_model = converter.convert()
  File "C:\Users\schulzyk\Anaconda3\envs\tf22\lib\site-packages\tensorflow\lite\python\lite.py", line 480, in convert
    raise ValueError(
ValueError: None is only supported in the 1st dimension. Tensor 'input_tensor' has invalid shape '[1, None, None, 3]'.

无论我对代码进行什么更改,总是会出现相同的错误消息。我不知道这是否表明在训练过程中出现了问题但没有引人注目的事件发生。

我很乐意收到任何反馈!

【问题讨论】:

    标签: tensorflow tensorflow-lite google-coral edge-tpu


    【解决方案1】:

    啊,用于珊瑚的带有 tensorflow 2.0 的对象检测 API 仍然是 WIP。我们遇到了很多障碍,可能不会很快看到此功能。我建议现在使用 tf1.x API。这是一个很好的教程:) https://github.com/Namburger/edgetpu-ssdlite-mobiledet-retrain

    【讨论】:

    • 嗨,南,感谢您的快速回复。可惜TF2还不能用于Coral。我已经为此付出了很多努力。我还尝试在 Ubuntu20.04 上的 Virtualbox 中使用 edgetpu_compiler 编译该文件,并想为它打开另一个线程(因为还有另一个错误消息)。有机会这样转换吗?
    • 你看,问题是即使是我们也遇到了 2.x api 的问题。只是我们的工程团队仍在处理未实现的操作。如果您愿意,可以分享您的 tflite 模型,我可以尝试快速运行一下我们最新的编译器二进制文件,看看是否有改进?
    • 感谢您的提议。不幸的是,我不允许分享模型。我想我必须用 TF1.x 尝试一下:/ 感谢您之前提到的链接,我试图让自己定位于那个...
    猜你喜欢
    • 2020-07-04
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多