Fast Neural Style Transfer
1、简介
在原始风格迁移中,是以一张图片作为参数来训练它。
生成一张图片则需要数分钟不等的时间。
如果以网络来转换图片,我们训练这个网络,那么则能够快速的将图片进行风格转换,而无需迭代一张图片数百至千次。
于是我们所需要做的则是定义这个风格转换网络。
2、网络定义
网络结构如上。类似于自编码解构,先压缩图片,再还原回来。
此外,网络的上采样,采用的是先放大图片再卷积的形式,而非反卷积。
于是构建出网络:
class ResBlock(nn.Module):
def __init__(self,c):
super(ResBlock, self).__init__()
self.layer = nn.Sequential(
nn.Conv2d(c,c,3,1,1, bias=False),
nn.InstanceNorm2d(c),
nn.ReLU(),
nn.Conv2d(c, c, 3, 1, 1, bias=False),
nn.InstanceNorm2d(c),
)
self.relu = nn.ReLU()
def forward(self, x):
return self.relu(self.layer(x)+x)
class TransNet(nn.Module):
def __init__(self):
super(TransNet, self).__init__()
self.layer = nn.Sequential(
nn.Conv2d(3, 32, 9, 1, 4, bias=False),
nn.InstanceNorm2d(32),
nn.ReLU(),
nn.Conv2d(32,64,3,2,1, bias=False),
nn.InstanceNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 128, 3, 2, 1, bias=False),
nn.InstanceNorm2d(128),
nn.ReLU(),
ResBlock(128),
ResBlock(128),
ResBlock(128),
ResBlock(128),
ResBlock(128),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.Conv2d(128,64,3,1,1, bias=False),
nn.InstanceNorm2d(64),
nn.ReLU(),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.Conv2d(64, 32, 3, 1, 1, bias=False),
nn.InstanceNorm2d(32),
nn.ReLU(),
nn.Conv2d(32,3,9,1,4),
nn.Sigmoid()
)
def forward(self, x):
return self.layer(x)
数据集使用COCO数据集来进行网络的训练。
\
相较于一张图片迭代的,风格损失改为带批次的:
def get_gram_matrix(f_map):
n, c, h, w = f_map.shape
f_map = f_map.reshape(n, c, h * w)
gram_matrix = torch.matmul(f_map, f_map.transpose(1, 2))
return gram_matrix
使用VGG16作为损失网络:
class VGG16(nn.Module):
def __init__(self):
super(VGG16, self).__init__