为了比对,还需要导出各个中间层数据。
这个和《从 SRGAN(TensorFlow) 导出中间层图像数据到文本文件》一文差不多,当然 TensorFlow 的流程是很难理解的,灵活性很差,要导出也不容易,Pytorch就没有这个缺点了。
定义保存函数:
def _save_mat(name, tensor_x): # 保存多通道图像数据到txt文件
#print(tensor_x.shape)
x=tensor_x.numpy() #转换为数组
#print(x.shape)
f=open(name+'.txt','w')
f.write(str(x.shape)) # 数据尺寸
f.write('\n')
for i in range(x.shape[1]):#图像通道数
#print(i)
v_2d=x[0, i, :, :] #取出一个通道
#print(v_2d.shape)
w=v_2d.shape[0] #图像宽高
h=v_2d.shape[1]
for Ii in range(w):
for Ji in range(h):
strNum = str(v_2d[Ii,Ji]) #每一点数据
f.write(strNum)
f.write(' ')#数据间隔一空格
f.write('\n')
f.write('\n')#通道间隔一空行
f.close()
return
然而放在保存的位置,比如:
_save_mat("rgb", x) #(1, 3, 60, 64)
x = self.feat0(x)
_save_mat("feat0", x) #(1, 256, 60, 64)
x = self.feat1(x)
_save_mat("feat1", x) #(1, 64, 60, 64)
h1 = self.up1(x)#(1, 64, 120, 128)
_save_mat("up1", h1)
这样就会出现几个文本文件了
rgb.txt, feat0.txt, ...
内容:
(1, 256, 60, 64)
0.6313726 0.6313726 0.6313726 0.627451 0.6156863 省略...
0.6392157 0.627451 0.6156863 0.60784316 0.59607846 ...
0.627451 0.6156863 0.6 0.59607846 0.5921569 0.5921569 ...
省略...
0.5882353 0.5882353 0.5882353 0.58431375 0.57254905 ...
0.59607846 0.58431375 0.57254905 0.5647059 0.5529412 ...
...
这样,如果你要转化为图像也是可以的
结束
本文介绍如何使用PyTorch定义保存函数,将神经网络各中间层的图像数据导出到文本文件,相较于TensorFlow,PyTorch在中间层数据导出方面更加灵活简便。通过实例演示,展示如何保存多通道图像数据,并提供转化回图像的方法。
4万+





