np.array to PIL image --> Typerror: 无法处理此数据类型: (1, 1, 12), |u1



我正在尝试将np.array转换为PIL图像,但我一直遇到警告"Cannot handle this data type: (1, 1, 12), |u1"。我的输入数组的大小是(25625612(。正如这里所说,我已经尝试将其重塑为np.array(256256(,但这给了我更多的警告(ValueError: cannot reshape array of size 786432 into shape (256,256)(

部分代码(来自AttnGAN(:

for j in range(num_attn):
one_map = row_beforeNorm[j]
one_map *= 255
PIL_im = Image.fromarray(np.uint8(img))
PIL_att = Image.fromarray(np.uint8(one_map))

完整错误:

/content/drive/My Drive/AttnGAN/code/miscc/utils.py:240: RuntimeWarning: invalid value encountered in true_divide
one_map = (one_map - minV) / (maxV - minV)
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/PIL/Image.py", line 2680, in fromarray
mode, rawmode = _fromarray_typemap[typekey]
KeyError: ((1, 1, 12), '|u1')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 146, in <module>
gen_example(dataset.wordtoix, algo)  # generate images for customized captions
File "main.py", line 83, in gen_example
algo.gen_example(data_dic)
File "/content/drive/My Drive/AttnGAN/code/trainer.py", line 518, in gen_example
[attn_maps[j]], att_sze)
File "/content/drive/My Drive/AttnGAN/code/miscc/utils.py", line 254, in build_super_images2
PIL_att = Image.fromarray(np.uint8(one_map))
File "/usr/local/lib/python3.6/dist-packages/PIL/Image.py", line 2682, in fromarray
raise TypeError("Cannot handle this data type: %s, %s" % typekey)
TypeError: Cannot handle this data type: (1, 1, 12), |u1

您能用Image.fromarray(img[:,:,0])选择第一个图像吗?

如果有效的话,你可以在它们上面循环。

我和你们一样也有同样的错误。我发现发生了这个错误因为one_map数组一旦上采样(或扩展(就具有与原始数组不同的维度。阵列的第3维度(例如(1,1,12((可能需要为RGB颜色通道的3维度。

所以我试图找到一种在不扩展第三维度(例如z方向(的情况下进行上采样的方法。

我终于找到了一个解决方案并修改了代码仅添加参数";multichannel=True(默认为False(";在函数skimage.transform.pyramid_expand((中。我的完整代码如下。

one_map = skimage.transform.pyramid_expand(one_map, sigma=20, upscale=vis_size // att_sze, multichannel=True)

我发现上面的代码并没有改变颜色通道的三维。请试试这个代码如果你从github运行attngan。

我犯的错误如下。

Traceback (most recent call last):
File "pretrain_DAMSM.py", line 274, in <module>
dataset.ixtoword, image_dir)
File "apretrain_DAMSM.py", line 125, in train
ixtoword, attn_maps, att_sze)
File "/content/AttnGAN/code/miscc/utils.py", line 148, in build_super_images
PIL_att = Image.fromarray(np.uint8(one_map))
File "/usr/local/lib/python3.6/dist-packages/PIL/Image.py", line 2682, in fromarray
raise TypeError("Cannot handle this data type: %s, %s" % typekey)
TypeError: Cannot handle this data type: (1, 1, 48), |u1

最新更新