首页 > 解决方案 > 如何修复“AssertionError:输入图像应该是 float64(32) 并且在 [-1.0, 1.0] 的范围内!”

问题描述

我正在学校 HPC 上的 tensorflow 上运行 CycleGAN 的代码。我运行的代码上周工作,但本周它停止工作。我相信这可能是由于其中一个模块的更新,但我不确定。

Traceback (most recent call last):

File "test.py", line 55, in <module>
im.imwrite(im.immerge(a_img_opt, 1, 3), a_save_dir + '/' + img_name)
File "/home/kseelma/PleaseWork/image_utils.py", line 46, in imwrite
return scipy.misc.imsave(path, _to_range(image, 0, 255, np.uint8))
File "/home/kseelma/PleaseWork/image_utils.py", line 14, in _to_range
'The input images should be float64(32) and in the range of [-1.0, 1.0]!'
AssertionError: The input images should be float64(32) and in the range of [-1.0, 1.0]!

这就是问题所在,下面显示了 imwrite 和 immerge 的方法

def imwrite(image, path):

   # save an [-1.0, 1.0] image

   return scipy.misc.imsave(path, _to_range(image, 0, 255, np.uint8))

def immerge(images, row, col):

    """Merge images.

   merge images into an image with (row * h) * (col * w)

  `images` is in shape of N * H * W(* C=1 or 3)
  """
  if images.ndim == 4:
      c = images.shape[3]
  elif images.ndim == 3:
      c = 1

  h, w = images.shape[1], images.shape[2]
  if c > 1:
      img = np.zeros((h * row, w * col, c))
  else:
      img = np.zeros((h * row, w * col))
  for idx, image in enumerate(images):
      i = idx % col
      j = idx // col
      img[j * h:j * h + h, i * w:i * w + w, ...] = image

  return img

标签: pythontensorflow

解决方案


CycleGAN 正在保存的图像,即模型返回的图像的值小于 -1 或大于 1。CycleGAN 在生成器中的最后一层是tanh,其范围在 -1 到 1 之间。因此,请确保生成器的最后一层是 tanh,或者是一个范围在 -1 到 +1 之间的函数。


推荐阅读