基础的风格迁移只能对其中一种风格进行迁移。该方法无法实现,需要再重新再对模型进行训练。
风格迁移算法:在保证内容图像的完整性的前提下,讲风格图像的风格融入内容图像中,使得内容图像的原始风格最后发生转变,实现内容和风格的融合
import torch
from torch.autograd import Variable
import torchvision
from torchvision import transforms, models
import copy
from PIL import Image
import matplotlib.pyplot as plt
# 数据预处理,加载数据
transform = transforms.Compose([transforms.Resize([224,224]),
transforms.ToTensor()])
def loading(path = None):
img = Image.open(path)
img = transform(img)
img = img.unsqueeze(0)
return img
content_img = loading('D:/neirong.jpg')
content_img = Variable(content_img).cuda()
style_img = loading('D:/fengge.jpg')
style_img = Variable(style_img).cuda()
# 内容度量值可以使用均方误差作为损失函数:
class Content_loss(torch.nn.Module):
def __init__(self, weight, target):
super(Content_loss, self).__init__()
self.weight =