首页 > 解决方案 > Tensorflow tf.squeeze 替代品

问题描述

我有以下图像:

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

标签: pythontensorflowreshape

解决方案


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


推荐阅读