如何将tf.encode_jpeg()的输出传递到tf.train.BytesList()



在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'xffxd8xffxe0x00x10JFIFx00x01x01x01x01,x01,x 
has type tensorflow.python.framework.ops.EagerTensor, but expected one of: bytes

我已经尝试过使用tf.compat.as_bytes将张量转换为字节,但这只会产生不同的错误。附言:将tf.encode_jpeg((和tf.train.BytesList((组合在一起是愚蠢的吗?

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

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]))

最新更新