python - 如何修复“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
解决方案
CycleGAN 正在保存的图像,即模型返回的图像的值小于 -1 或大于 1。CycleGAN 在生成器中的最后一层是tanh,其范围在 -1 到 1 之间。因此,请确保生成器的最后一层是 tanh,或者是一个范围在 -1 到 +1 之间的函数。
推荐阅读
- javascript - Vue 数据未在 DOM 中显示
- android - 锁屏音乐控制器意图
- sharepoint-list - 使用javascript从HTML表单中的复选框向SP列表选择列添加多个值
- javascript - 如果我逐个字符串输入脚本,则脚本可以在浏览器中运行。但它不适用于真实的网站
- vuejs2 - 如何在与服务器通信以获取数据请求时从本地构建运行 Capacitor iOS 应用程序
- php - PHP 登录 nouser
- java - 有没有办法在 JPanel 的扩展类中调用 setBackgroud()
- graphql - 如何在 prisma 查询中创建嵌套连接?
- omnet++ - 如何获取cPacket的始发门?
- php - 我从 PHP 页面向 Siebel 发送 SOAP 请求以插入数据。仅供参考 Seibel 不提供 Web 服务的 WSDL URL