实现模型输出图无失真保存
关键剖析
After Squeeze(0): (Tensors)
img.shape = (3,H,W)
output.shape = (1,H,W)
但是
plt.imsave的形式(H,W,C)
所以写了两个子函数(维度转化,矩阵元素归一化)
# 将Tensor (C,H,W) -> Numpy (H,W,C)
def array_transpose(array):
temp = np.transpose(array,(1,2,0))
return temp
# 像素归一化
def normalization(data):
max_val = []
min_val = []
for i in range(3):
max_val.append(np.max(data[i]))
min_val.append(np.min(data[i]))
data[i] = (data[i] - min_val[i]) / (max_val[i] - min_val[i])
return data
def main():
im = normalization(im)
im = array_transpose(im)
upper_store = (upper.squeeze(0)).squeeze(0)
# lower类似同理
plt.imsave('./clean/IMG_{}.png'.format(i), im, format='png', cmap=plt.cm.jet)
plt.imsave('./upper/IMG_{}.png'.format(i), upper_store, format='png', cmap=plt.cm.jet)
plt.imsave('./lower/IMG_{}.png'.format(i), lower_store, format='png', cmap=plt.cm.jet)