【问题标题】:How do I pass output of tf.encode_jpeg() into tf.train.BytesList()?如何将 tf.encode_jpeg() 的输出传递到 tf.train.BytesList()?
【发布时间】:2020-08-01 18:59:11
【问题描述】:

在 Tensorflow 2.2 中,我如何从 tf.encode_jpeg()(它是一个字符串类型的张量)获取输出并将其转换为将被 tf.train.BytesList(value=[xxx]) 接受为输入的字节?最终,我想将它添加到 TFRecord。当我尝试运行一些基本代码时(如下错误所示),我收到以下错误:

--------------------------------------------------------------------------- TypeError                                 Traceback (most recent call last) <ipython-input-25-0a8dec1885d5> in <module>
      4 x = np.array([[[1],[2]],[[3],[4]]], dtype=np.uint8)
      5 x = tf.io.encode_jpeg(x)
----> 6 x = tf.train.BytesList(value=[x])
      7 

TypeError: <tf.Tensor: shape=(), dtype=string, 
numpy=b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x01,\x01,\x 
has type tensorflow.python.framework.ops.EagerTensor, but expected one of: bytes

我已经尝试使用tf.compat.as_bytes 将张量转换为字节,但这只会产生不同的错误。 附言把 tf.encode_jpeg() 和 tf.train.BytesList() 结合起来是不是很傻?

【问题讨论】:

  • 在将 x 传递给 tf.train.BytesList 之前,尝试使用 x = x.numpy() 将张量转换为字符串。在 TF 1.x 中使用 .eval() 来获取张量的值,而在 TF2.X 中 .numpy() 基本上做同样的事情。至少通过这种方式,我设法将我的 tfrecord 压缩到小 5 倍的空间。

标签: jpeg tensorflow2.0 tfrecord


【解决方案1】:

使用tf.io.serialize_tensor(image) 转换为字节。

【讨论】:

    【解决方案2】:
    if isinstance(value, type(tf.constant(0))): #<-Add this. 
         value = value.numpy() #<-Add this. BytesList won't unpack a string from an EagerTensor.
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
    

    【讨论】:

      猜你喜欢
      • 2021-05-31
      • 2012-08-29
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-01
      • 2014-02-21
      相关资源
      最近更新 更多