栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Pytorch入门学习-6.图片风格迁移和GAN

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Pytorch入门学习-6.图片风格迁移和GAN

01Neural Style Transfer

图片风格迁移

图片表示

使用VGG network来表示
内容图片
风格图片
风格迁移后的新图片

Content Loss

Style Loss

02 Generative Adversarial Network

Generator: 生成器,目标是让生成的数据接近真实数据

Discriminator: 分类器,目标是能够鉴别真实数据和生成的假数据

DCGAN

03 CycleGAN

Network


模型架构

损失函数


**

模型代码

**

图片风格迁移

读取两张图片

def load_image(image_path, transform=None, max_size=None, shape=None):
    image = Image.open(image_path)
    if max_size:
        scale = max_size / max(image.size)
        size = np.array(image.size) * scale
        image = image.resize(size.astype(int), Image.ANTIALIAS)
    
    if shape:
        image = image.resize(shape, Image.LANCZOS)
    
    if transform:
        image = transform(image).unsqueeze(0)
        
    return image.to(device)

transform = torchvision.transforms.Compose([
    torchvision.transforms.ToTensor(),
    torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])  # 注释这一行以查看原图
])

content = load_image("png/content.jpg", transform, max_size=400)
style = load_image("png/style.jpg", transform, shape=[content.size(2), content.size(3)])
unloader = torchvision.transforms.ToPILImage()
plt.ion()

def imshow(tensor, title=None):
    image = tensor.cpu().clone()
    image = image.squeeze(0)
    image = unloader(image)
    plt.imshow(image)
    if title is not None:
        plt.title(title)
    plt.pause(0.001)
    

plt.figure()
imshow(style[0], title="image")

结果:

数据集下载:https://pan.baidu.com/s/149qNt8EKDcpclUEeoFBhrA

image_size = 64
batch_size = 128
dataroot = "celeba"
num_workers = 2
dataset = torchvision.datasets.ImageFolder(root=dataroot, transform=torchvision.transforms.Compose([
    torchvision.transforms.Resize(image_size),
    torchvision.transforms.CenterCrop(image_size),
    torchvision.transforms.ToTensor(),
    torchvision.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
]))
dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True, num_workers=num_workers)
real_batch = next(iter(dataloader))
im = torchvision.utils.make_grid(real_batch[0].to(device)[:64], padding=2, normalize=True)
plt.figure(figsize=(8, 8))
plt.axis=("off")
plt.title("Traning Images")
plt.imshow(im.permute(1, 2, 0))

结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1011529.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号