tf.shape(图片) returns None 在 Tensorflow 2.0 中
tf.shape(image) returns None in Tensorflow 2.0
我正在使用 Tensorflow 2.0 构建超分辨率模型。在预处理过程中,我想按给定的补丁大小裁剪低分辨率和高分辨率图像。为此,我想获得低分辨率和高分辨率图像的高度和宽度。但是 tf.shape(image) 正在返回 None。
有没有更好的方法?
目前我只是在使用 tf.shape 之前将每张图片调整到某个尺寸,但由于并非所有图片都具有相同的尺寸,这会影响成像质量。期待您的建议。
编辑部分:
这是部分代码
low_r = tf.io.decode_jpeg(lr_filename, 频道=3)
low_r = tf.cast(low_r, dtype=tf.float32)
打印(low_r.shape)
打印语句打印(None, None, 3)
我想要的是得到身高和体重,比如 (240,360,3)
我无法重复您的问题,但这应该为您提供了一种方法来测试您的 Tensorflow 2.0 安装并与您当前获得的结果进行比较。
创建张量并检查其形状:
import tensorflow as tf
t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
tf.shape(t) # [2, 2, 3]
Out[1]: <tf.Tensor: id=1, shape=(3,), dtype=int32, numpy=array([2, 2, 3])>
接下来,检查调用函数 return 的内容:
tf_shape_var = tf.shape(t)
print(tf_shape_var)
输出:
tf.Tensor([2 2 3], shape=(3,), dtype=int32)
最后,在 int 和 string 上调用它以取回有效的 return:
tf.shape(1)
Out[10]: <tf.Tensor: id=12, shape=(0,), dtype=int32, numpy=array([], dtype=int32)>
tf.shape('asd')
Out[11]: <tf.Tensor: id=15, shape=(0,), dtype=int32, numpy=array([], dtype=int32)>
打印语句:
print(tf.shape(1))
print(tf.shape('asd'))
输出:
tf.Tensor([], shape=(0,), dtype=int32)
tf.Tensor([], shape=(0,), dtype=int32)
Link 对于 tf.shape()
https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/shape
我不确定这是否也是你的情况,但在我的 TensorFlow
(v2.4.0rc2
) 中,my_tensor.shape()
也是 returns TensorShape([None, None, None, None])
。这与 TensorShape
张量是在构建期间而不是在执行期间生成的事实有关。
使用 tf.shape()
(在你的问题中提到,但实际上没有在你的代码片段中使用)为我解决了这个问题。
> my_tensor.shape()
TensorShape([None, None, None, None])
> tf.shape(my_tensor)
[10 512 512 8]
我正在使用 Tensorflow 2.0 构建超分辨率模型。在预处理过程中,我想按给定的补丁大小裁剪低分辨率和高分辨率图像。为此,我想获得低分辨率和高分辨率图像的高度和宽度。但是 tf.shape(image) 正在返回 None。 有没有更好的方法?
目前我只是在使用 tf.shape 之前将每张图片调整到某个尺寸,但由于并非所有图片都具有相同的尺寸,这会影响成像质量。期待您的建议。
编辑部分: 这是部分代码
low_r = tf.io.decode_jpeg(lr_filename, 频道=3)
low_r = tf.cast(low_r, dtype=tf.float32)
打印(low_r.shape)
打印语句打印(None, None, 3) 我想要的是得到身高和体重,比如 (240,360,3)
我无法重复您的问题,但这应该为您提供了一种方法来测试您的 Tensorflow 2.0 安装并与您当前获得的结果进行比较。
创建张量并检查其形状:
import tensorflow as tf
t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
tf.shape(t) # [2, 2, 3]
Out[1]: <tf.Tensor: id=1, shape=(3,), dtype=int32, numpy=array([2, 2, 3])>
接下来,检查调用函数 return 的内容:
tf_shape_var = tf.shape(t)
print(tf_shape_var)
输出:
tf.Tensor([2 2 3], shape=(3,), dtype=int32)
最后,在 int 和 string 上调用它以取回有效的 return:
tf.shape(1)
Out[10]: <tf.Tensor: id=12, shape=(0,), dtype=int32, numpy=array([], dtype=int32)>
tf.shape('asd')
Out[11]: <tf.Tensor: id=15, shape=(0,), dtype=int32, numpy=array([], dtype=int32)>
打印语句:
print(tf.shape(1))
print(tf.shape('asd'))
输出:
tf.Tensor([], shape=(0,), dtype=int32)
tf.Tensor([], shape=(0,), dtype=int32)
Link 对于 tf.shape()
https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/shape
我不确定这是否也是你的情况,但在我的 TensorFlow
(v2.4.0rc2
) 中,my_tensor.shape()
也是 returns TensorShape([None, None, None, None])
。这与 TensorShape
张量是在构建期间而不是在执行期间生成的事实有关。
使用 tf.shape()
(在你的问题中提到,但实际上没有在你的代码片段中使用)为我解决了这个问题。
> my_tensor.shape()
TensorShape([None, None, None, None])
> tf.shape(my_tensor)
[10 512 512 8]