Tensorflow 1.14:映射时tf.numpy_function失去形状吗?

时间:2019-08-06 17:30:45

标签: python tensorflow tensorflow-datasets

此代码主要基于TF GUide Load images with tf.data

这是经过修改的colab,但有错误。

代码中的第一个更改是无害的:

# resize is moved to be an argument
def preprocess_image(image, resize=[192, 192]):
  image = tf.image.decode_jpeg(image, channels=3)
  image = tf.image.resize(image, resize)
  image /= 255.0  # normalize to [0,1] range

  return image

# argument bubbled up 
def load_and_preprocess_image(path, resize=[192, 192]):
  image = tf.read_file(path)
  return preprocess_image(image, resize)

下一个更改是引入问题的地方:

# from tf, with above modifications, works fine
image_ds = path_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE)

# error comes here

# dataset only contains paths, so wrap whatever value for `resize` in lambda
_load_and_preprocess_image = lambda path: load_and_preprocess_image(path, [192,192])

# we "have" numpy functionality for handling images so wrap in `tf.numpy_function`
tf_load_and_preprocess_image = lambda path: tf.numpy_function(_load_and_preprocess_image, [path], tf.float32)

# shape is lost
image_ds2_error_boogaloo = path_ds.map(tf_load_and_preprocess_image, num_parallel_calls=AUTOTUNE)

# no shape
image_ds2_error_boogaloo
# `<DatasetV1Adapter shapes: , types: tf.float32>`

我该如何解决? tf.numpy_function没有针对特定形状的参数,并且tf.data.Dataset的属性output_shapes是只读的

3 个答案:

答案 0 :(得分:0)

尝试在tf.numpy_function之后使用tf.reshape重新初始化形状。

image = tf.read_file(path)
image_shape = tf.shape(image)
numpy_func = lambda image: some_numpy_function(image)
image = tf.numpy_function(numpy_func, [image], tf.float32)
image = tf.reshape(image, image_shape)

答案 1 :(得分:0)

我遇到了一个类似的问题,<html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <link rel="stylesheet" type="text/css" href="css/question3.css" /> </head> <body> <div class="container"> <h1>Name of game</h1> <div class="image" style="background-image: url('https://via.placeholder.com/1000');"></div> <div class="description">Description goes here</div> </div> <script src="js/question3.js"></script> </body> </html>包装了一个在numpy数组而不是张量上工作的函数,签名使人抱怨函数的输出形状未知。

使用Kutay YILDIZ提出的tf.reshape可以解决问题,这是代码段:

tf.numpy_function

答案 2 :(得分:0)

嗨,我刚刚找到了 this 个答案,我认为它可能有用。

<块引用>

请注意,您还可以通过 map(lambda x: tf.set_shape(x, ...)

恢复因应用 numpy_function 而丢失的形状