我有以下图像:
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
)?
答案 0 :(得分:2)
您可以使用image[0]
选择图像的第一个“行”。如果image
的形状为[1, w, h, c]
,这将返回一个[w, h, c]
张量。尽管我不了解tf.squeeze
的问题是什么。 squeeze(image, axis=0)
做同样的事情,并防止其他轴(例如通道轴)的尺寸也为1。
答案 1 :(得分:2)
另一种选择,如果您知道第一维的大小为1,则可以执行以下操作:
tf.reshape(image, tf.shape(image)[1:])
但是,如前所述,tf.squeeze
似乎是您情况下的直接解决方案。