图像风格迁移也有框架了:使用Python编写,与PyTorch完美兼容,外行也能用
AI算法与图像处理
共 6489字,需浏览 13分钟
·
2020-12-23 20:47
点击上方“AI算法与图像处理”,选择加"星标"或“置顶”
重磅干货,第一时间送达
易于使用的神经风格迁移框架 pystiche。
论文地址:https://joss.theoj.org/papers/10.21105/joss.02761
项目地址:https://github.com/pmeier/pystiche
import torch
import pystiche
from pystiche import demo, enc, loss, ops, optim
print(f"pystiche=={pystiche.__version__}")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
pystiche==0.7.0
multi_layer_encoder = enc.vgg19_multi_layer_encoder()
print(multi_layer_encoder)
VGGMultiLayerEncoder(
arch=vgg19, framework=torch, allow_inplace=True
(preprocessing): TorchPreprocessing(
(0): Normalize(
mean=('0.485', '0.456', '0.406'),
std=('0.229', '0.224', '0.225')
)
)
(conv1_1): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu1_1): ReLU(inplace=True)
(conv1_2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu1_2): ReLU(inplace=True)
(pool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(conv2_1): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu2_1): ReLU(inplace=True)
(conv2_2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu2_2): ReLU(inplace=True)
(pool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(conv3_1): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu3_1): ReLU(inplace=True)
(conv3_2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu3_2): ReLU(inplace=True)
(conv3_3): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu3_3): ReLU(inplace=True)
(conv3_4): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu3_4): ReLU(inplace=True)
(pool3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(conv4_1): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu4_1): ReLU(inplace=True)
(conv4_2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu4_2): ReLU(inplace=True)
(conv4_3): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu4_3): ReLU(inplace=True)
(conv4_4): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu4_4): ReLU(inplace=True)
(pool4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(conv5_1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu5_1): ReLU(inplace=True)
(conv5_2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu5_2): ReLU(inplace=True)
(conv5_3): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu5_3): ReLU(inplace=True)
(conv5_4): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu5_4): ReLU(inplace=True)
(pool5): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
content_layer = "relu4_2"
encoder = multi_layer_encoder.extract_encoder(content_layer)
content_loss = ops.FeatureReconstructionOperator(encoder)
style_layers = ("relu1_1", "relu2_1", "relu3_1", "relu4_1", "relu5_1")
style_weight = 1e3
def get_encoding_op(encoder, layer_weight):
return ops.GramOperator(encoder, score_weight=layer_weight)
style_loss = ops.MultiLayerEncodingOperator(
multi_layer_encoder, style_layers, get_encoding_op, score_weight=style_weight,
)
criterion = loss.PerceptualLoss(content_loss, style_loss).to(device)
print(criterion)
PerceptualLoss(
FeatureReconstructionOperator( :
score_weight=1,
encoder=VGGMultiLayerEncoder(
layer=relu4_2,
arch=vgg19,
framework=torch,
allow_inplace=True
)
)
MultiLayerEncodingOperator( :
encoder=VGGMultiLayerEncoder(
arch=vgg19,
framework=torch,
allow_inplace=True
),
score_weight=1000
GramOperator(score_weight=0.2) :
GramOperator(score_weight=0.2) :
GramOperator(score_weight=0.2) :
GramOperator(score_weight=0.2) :
GramOperator(score_weight=0.2) :
)
)
size = 500
images = demo.images()
content_image = images["bird1"].read(size=size, device=device)
criterion.set_content_image(content_image)
style_image = images["paint"].read(size=size, device=device)
criterion.set_style_image(style_image)
input_image = content_image.clone()
output_image = optim.image_optimization(input_image, criterion, num_steps=500)
下载1:何恺明顶会分享
在「AI算法与图像处理」公众号后台回复:何恺明,即可下载。总共有6份PDF,涉及 ResNet、Mask RCNN等经典工作的总结分析
下载2:leetcode 开源书
在「AI算法与图像处理」公众号后台回复:leetcode,即可下载。每题都 runtime beats 100% 的开源好书,你值得拥有!
下载3 CVPR2020 在「AI算法与图像处理」公众号后台回复:CVPR2020,即可下载1467篇CVPR 2020论文 个人微信(如果没有备注不拉群!) 请注明:地区+学校/企业+研究方向+昵称
觉得不错就点亮在看吧
评论