【问题标题】:why tensorflow TFLiteConverter.from_session require the same size for input and output为什么 tensorflow TFLiteConverter.from_session 需要相同的输入和输出大小
【发布时间】:2019-09-25 02:37:54
【问题描述】:

我正在尝试使用 TFLiteConverter 来转换我的网络。所以我先尝试了示例代码。有用。但是经过一些修改后,它会发回错误。似乎 input_array 和 output_array 必须是相同的大小。我只是不明白为什么。有人可以帮帮我吗?

我将 img from 的大小和 var 的大小从 [1,64,64,3 修改为 [1,64,3,1] 完整代码粘贴在下面enter code here

import tensorflow as tf

img = tf.placeholder(name="img", dtype=tf.float32, shape=(1, 64, 64, 1))
var = tf.get_variable("weights", dtype=tf.float32, shape=(1, 64, 3, 1))
val = tf.matmul(img,var)
out = tf.identity(val, name="out")

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(val.shape)
    converter = tf.lite.TFLiteConverter.from_session(sess, [img], [out])
    tflite_model = converter.convert()
    open("converted_model.tflite", "wb").write(tflite_model)

错误消息: ValueError:维度必须相等,但对于输入形状为 [1,64,64,1]、[1,64,3,1] 的“MatMul”(操作:“BatchMatMulV2”),维度必须是 1 和 3。

【问题讨论】:

    标签: tensorflow tensorflow-lite


    【解决方案1】:

    问题不在于 TFLite 转换,而在于首先构建图表。

    tf.matmul 对张量中最内层的二维矩阵进行运算。因此,在您的情况下,您尝试将形状为64x1 的矩阵乘以大小为3x1 的矩阵,这是无效的。矩阵乘法要求第一个操作数的列等于第二个操作数的行数,但是这里1 != 3 所以它不起作用。

    例如,将 3 替换为 1 即可:

    import tensorflow as tf
    
    img = tf.placeholder(name="img", dtype=tf.float32, shape=(1, 64, 64, 1))
    var = tf.get_variable("weights", dtype=tf.float32, shape=(1, 64, 3, 1))
    val = tf.matmul(img,var)
    out = tf.identity(val, name="out")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      • 2018-02-05
      • 1970-01-01
      • 2012-08-08
      相关资源
      最近更新 更多