【发布时间】:2021-02-11 19:26:58
【问题描述】:
numpy.empty() 在 Google colab 的默认 numpy 库中返回数据类型不理解异常。我检查了 stackoverflow 中的所有问题,但没有看到与此问题相关的内容,因为我使用的是 google colab。这是完整的代码和异常输出:
完整代码:
def diffMask(img1=None, img2=None, opt=None, dataset=None, args=None):
netG = args[0]
netB = args[1]
netD = args[2]
f = args[3]
res_path = opt.results_Stage3
res_folders = ['temp_masks',
'temp_Stage2',
'temp_ref',
'temp_diff',
'temp_Stage3',
'temp_skel',
'temp_res',
'temp_Stage1',
'temp_src']
for x in res_folders:
if os.path.isdir("{}{}".format(res_path, x)) == False:
os.mkdir("{}{}".format(res_path, x))
save_masks = "{}{}".format(res_path, "temp_masks")
save_Stage2 = "{}{}".format(res_path, "temp_Stage2")
save_ref = "{}{}".format(res_path, "temp_ref")
save_diff = "{}{}".format(res_path, "temp_diff")
save_Stage3 = "{}{}".format(res_path, "temp_Stage3")
save_skel = "{}{}".format(res_path, "temp_skel")
save_res = "{}{}".format(res_path, "temp_res")
save_Stage1 = "{}{}".format(res_path, "temp_Stage1")
save_src = "{}{}".format(res_path, "temp_src")
resize2 = transforms.Resize(size=(128, 128))
src, mask, style_img, target, gt_cloth, skel, cloth = dataset.get_img("{}_0.jpg".format(img1[:-6]),
"{}_1.jpg".format(img1[:-6]))
src, mask, style_img, target, gt_cloth, skel, cloth = src.unsqueeze(0), mask.unsqueeze(0), style_img.unsqueeze(
0), target.unsqueeze(0), gt_cloth.unsqueeze(0), skel.unsqueeze(0), cloth.unsqueeze(0) # , face.unsqueeze(0)
src1, mask1, style_img1, target1, gt_cloth1, skel1, cloth1 = Variable(src.cuda()), Variable(mask.cuda()), Variable(
style_img.cuda()), Variable(target.cuda()), Variable(gt_cloth.cuda()), Variable(skel.cuda()), Variable(
cloth.cuda()) # , Variable(face.cuda())
src, mask, style_img, target, gt_cloth, skel, cloth = dataset.get_img("{}_0.jpg".format(img2[:-6]),
"{}_1.jpg".format(img2[:-6]))
src, mask, style_img, target, gt_cloth, skel, cloth = src.unsqueeze(0), mask.unsqueeze(0), style_img.unsqueeze(
0), target.unsqueeze(0), gt_cloth.unsqueeze(0), skel.unsqueeze(0), cloth.unsqueeze(0) # , face.unsqueeze(0)
src2, mask2, style_img2, target2, gt_cloth2, skel2, cloth2 = Variable(src.cuda()), Variable(mask.cuda()), Variable(
style_img.cuda()), Variable(target.cuda()), Variable(gt_cloth.cuda()), Variable(skel.cuda()), Variable(
cloth.cuda())
gen_targ_Stage1, s_128, s_64, s_32, s_16, s_8, s_4 = netG(skel1, cloth2) # gen_targ11 is structural change cloth
gen_targ_Stage2, s_128, s_64, s_32, s_16, s_8, s_4 = netB(src1, gen_targ_Stage1,
skel1) # gen_targ12 is Stage2 image
# saving structural
pic_Stage2 = (torch.cat([gen_targ_Stage2], dim=0).data + 1) / 2.0
# save_dir = "/home/np9207/PolyGan_res/temp_Stage2/"
save_image(pic_Stage2, '%s/%d_%s_%d.jpg' % (save_Stage2, f, img1[:-6], 0), nrow=1)
msk1 = mask1[0, :, :, :].detach().cpu().permute(1, 2, 0)
plt.imsave("{}/{}_{}_mask.jpg".format(save_masks, f, img1[:-6]), msk1, cmap="gray")
我在这条线上遇到了异常:
plt.imsave("{}/{}_{}_mask.jpg".format(save_masks, f, img1[:-6]), msk1, cmap="gray")
异常输出:
Traceback (most recent call last):
File "test.py", line 208, in test
diffMask(image1, image2, opt, test_loader, args)
File "test.py", line 96, in diffMask
plt.imsave("{}/{}_{}_mask.jpg".format(save_masks, f, img1[:-6]), msk1, cmap="gray")
File "/usr/local/lib/python3.6/dist-packages/matplotlib/pyplot.py", line 2066, in imsave
return matplotlib.image.imsave(fname, arr, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/matplotlib/image.py", line 1550, in imsave
rgba = sm.to_rgba(arr, bytes=True)
File "/usr/local/lib/python3.6/dist-packages/matplotlib/cm.py", line 217, in to_rgba
xx = np.empty(shape=(m, n, 4), dtype=x.dtype)
TypeError: data type not understood
Numpy 版本: 1.18.5
提前谢谢你。
【问题讨论】:
-
这是您传递给
imsave调用的参数的问题。检查您传递的内容,并将这些参数与imsave的文档进行比较。 -
看起来您正在使用某种 GPU 计算库并尝试将其类型与
imsave一起使用。imsave将图像数据作为 NumPy 数组或类数组。你似乎在传递别的东西。 -
您是否忘记在您的 img1 张量上使用
.numpy()? -
我会在单独的一行中定义
filename="{}/{}_{}_mask.jpg".format(save_masks, f, img1[:-6])。这使imsave呼叫更清晰。然后关注msk。文档说应该是“图像数据”,像 2 或 3d 这样的数组。 -
但是
np.asarray(msk1)做了什么。matplotlib不“知道”torch,所以它只是尝试“愚蠢”转换为数组。我建议先自己正确转换为ndarray。
标签: python numpy exception pytorch google-colaboratory