Tensorflow tf.squeeze 个备选方案

Tensorflow tf.squeeze alternatives

我有以下图片:

import tensorflow as tf
tf.enable_eager_execution()
tf.executing_eagerly()    

img = Image.open('image.jpg')
try:
    data = np.asarray(img, dtype='uint8' )
except SystemError:
    data = np.asarray(img.getdata(), dtype='uint8' )

重塑:

tf.shape(data)
<tf.Tensor: id=2, shape=(3,), dtype=int32, numpy=array([263, 320,   3], dtype=int32)>

image = tf.expand_dims(data, 0)
tf.shape(image)
<tf.Tensor: id=16, shape=(4,), dtype=int32, numpy=array([  1, 263, 320,   3], dtype=int32)>

 tf.squeeze(image, squeeze_dims=[0])
<tf.Tensor: id=22, shape=(263, 320, 3), dtype=uint8, numpy=...>

如何用类似的命令替换最后的tf.squeeze(例如:tf.reshape)?

您可以使用 image[0] 到 select 图像的第一个 "row"。如果 image 是形状 [1, w, h, c] 这将 return 一个 [w, h, c] 张量。虽然我不明白 tf.squeeze 的问题是什么。 squeeze(image, axis=0) 做同样的事情并防止其他轴(例如通道轴)也为 1 的情况。

另一种选择,如果你知道第一个维度的大小是1,你可以这样做:

tf.reshape(image, tf.shape(image)[1:])

不过,如前所述,tf.squeeze 似乎是您的情况的直接解决方案。