Tensorflow 1.14:tf.numpy_函数映射时会丢失形状?

2024-04-19 08:22:42 发布

您现在位置:Python中文网/ 问答频道 /正文

此代码主要基于TF指南Load images with ^{}。在

这是修改后的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)

下一个变化是引入问题的地方:

^{2}$

我怎么才能避开这个?tf.numpy_function没有特定定义形状的参数,tf.data.Dataset的属性output_shapes是只读的


Tags: topath代码imagereturntfdefwith
1条回答
网友
1楼 · 发布于 2024-04-19 08:22:42

尝试使用变形就在那之后tf.numpy_函数重新初始化形状。在

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)

相关问题 更多 >