1、使用PIL保存图片,保存代码的前几行是
im_h = im_h*255.
im_h = np.clip(im_h, 0., 255.)
im_h = im_h.transpose(1,2,0).astype(np.float32)
im = Image.fromarray(im_h)
im.save('out.png')
出现了下面的报错
Traceback (most recent call last):
File "/anaconda3/envs/py36/lib/python3.6/site-packages/PIL/Image.py", line 2749, in fromarray
mode, rawmode = _fromarray_typemap[typekey]
KeyError: ((1, 1, 3), '<f4')
Traceback (most recent call last):
File "eval.py", line 89, in <module>
im = Image.fromarray(im_h)
File "/home/lab-cao.qiaoyu/anaconda3/envs/py36/lib/python3.6/site-packages/PIL/Image.py", line 2751, in fromarray
The above exception was the direct cause of the following exception:
raise TypeError("Cannot handle this data type: %s, %s" % typekey) from e
TypeError: Cannot handle this data type: (1, 1, 3), <f4
这里的错误位置是:im = Image.fromarray(im_h)。前面的代码对im_h进行了复杂的图像预处理,但是转为图像出了错。这是因为fromarray默认会将图像转为uint8类型(也就是RGB每个通道由8bit表示的图像格式),一旦输入的np数组不是这个格式,就无法正常转化。因此我们需要改成
im = Image.fromarray(np.uint8(im_h))
或者是
im = Image.fromarray(np.uint8(im_h)).convert('RGB')
2、激活函数ReLU、Leaky ReLU、PReLU和RReLU
这篇文章讲得不错 激活函数ReLU、Leaky ReLU、PReLU和RReLU