提取中间层输出
除了在我前面的文章pytorch获取中间层参数、输出与可视化
提到的使用hook函数获取中间层输出以外,还可以用更直接的办法,不使用hook函数
方法一:将model中的每层拆解出来依次在监控下让数据流过
可以借助isinstance(layer, nn.Conv2d)
判断层类型
def extract_layers(layers,img,model):
layer_index = 0
res = []
for layer in model:
if layer_index == 0:
out = layer(img)
else:
out = layer(out)
if layer_index in layers:
res.append(out)
layer_index += 1
return res
方法二:直接在定义模型的时候输出中间层数据
class model(nn.Module):
def __init__(self):
super().__init__()
self.linear1 = nn.Linear(64,32)
self.linear2 = nn.Linear(32,16)
def forward(self,input):
linear1_out = self.linear1(input)
linear2_out = self.linear2(linear1_out)
return linear1_out,linear2_out
对于噪点的改进-降噪
修改方程,向方程中加入总变差损失(total variation denoising)
class TotalVariationLoss(nn.Module):
def __init__(self,weight):
super().__init__()
self.weight = weight
def forward(self,inputs):
#矩阵计算,一步到位
loss = 0.5*((inputs[:, :, 1:, :]-inputs[:, :, :-1, :]).abs().mean()+(inputs[:, :, :, 1:]-inputs[:, :, :, :-1]).abs().mean())
return loss * self.weight
在loss function
中对应作修改
#总变差损失计算
noise_loss = TotalVariationLoss(noise_weight)
noise_loss_this = noise_loss(input_param)
loss = style_loss_this + content_loss_this + noise_loss_this
风格迁移的其它损失函数优化算法
我最先采用的LBFGS算法是因为原论文是使用的该算法,使用LBFGS的优点在于能够快速收敛,但缺点也很明显,非常耗显存,图片大一点的话泰坦的12G显存迅速被耗尽
另外以下为两种优化算法对同样风格层迁移效果的对比
原图:
风格图:
LBFGS:
Adam:
Denoise Adam:
加入降噪训练之后效果好很多