python - 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
?
解决方案
您可以使用image[0]
选择图像的第一“行”。如果image
是形状[1, w, h, c]
,这将返回一个[w, h, c]
张量。虽然我不明白问题tf.squeeze
是什么。squeeze(image, axis=0)
做同样的事情并防止其他轴(例如通道轴)也是大小为 1 的情况。
推荐阅读
- python - 对齐可迭代:错误“字符串无法从手动字段规范切换到自动字段编号”
- facebook - 使用 Fb SDK 使用 Facebook 进行 Cognito 登录
- typescript - 如何从 index.d.ts 中的@types 导入接口?
- javascript - 不能增加 - 减少字体大小
- reactjs - Reactjs + Redux Form FieldArray 条件渲染
- javascript - 过滤角材料组合框
- node.js - Mongodb - 连接池大小未反映
- plot - 为什么当我应用丢失的数据截止时,gnuplot 会抱怨空的 x 范围(并破坏图表)?
- c++ - 如何在预处理阶段获取 c++ 函数调用者名称
- javascript - 屏蔽替换对象数组中的随机字符串字符