【问题标题】:tf.shape(image) returns None in Tensorflow 2.0tf.shape(image) 在 Tensorflow 2.0 中返回 None
【发布时间】:2019-12-12 02:19:46
【问题描述】:

我正在使用 Tensorflow 2.0 构建超分辨率模型。在预处理期间,我想通过给定的补丁大小裁剪低分辨率和高分辨率图像。为此,我想获得低分辨率和高分辨率图像的高度和宽度。但是 tf.shape(image) 正在返回 None。 有更好的方法吗?

目前我只是在使用 tf.shape 之前将每个图像调整为某种大小,但由于并非所有图像都具有相同的大小,因此会影响图像的质量。期待您的建议。

编辑部分: 以下是部分代码

low_r = tf.io.decode_jpeg(lr_filename, channels=3)

low_r = tf.cast(low_r, dtype=tf.float32)

打印(low_r.shape)

打印语句打印 (None, None, 3) 我想要的是得到身高和体重,比如 (240,360,3)

【问题讨论】:

  • 如果您不提供代码,我们将无法帮助您。
  • 请看一下。我提供了我的代码。

标签: tensorflow tensorflow-datasets tensorflow2.0


【解决方案1】:

我不确定这是否也是你的情况,但在我的 TensorFlow (v2.4.0rc2) 中,my_tensor.shape() 也返回 TensorShape([None, None, None, None])。这与 TensorShape 张量是在构建期间而不是在执行期间生成的事实有关。

使用tf.shape()(在您的问题中提到,但实际上并未在您的代码 sn-p 中使用)为我解决了这个问题。

> my_tensor.shape()
TensorShape([None, None, None, None])
> tf.shape(my_tensor)
[10 512 512 8]

【讨论】:

    【解决方案2】:

    我无法重复您的问题,但这应该可以让您测试您的 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])>
    

    接下来,检查函数调用时返回的内容:

    tf_shape_var = tf.shape(t)
    print(tf_shape_var)
    

    输出:

    tf.Tensor([2 2 3], shape=(3,), dtype=int32)
    

    最后,在 int 和 string 上调用它以获取有效返回:

    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)
    

    tf.shape()https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/shape的链接

    【讨论】:

    • 我认为 OP 正在解码延迟计算的图像,因此导致无,无。
    猜你喜欢
    • 1970-01-01
    • 2022-01-18
    • 2020-05-14
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 2020-10-08
    相关资源
    最近更新 更多