前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >谷歌“史上最强GAN”,现在有了PyTorch预训练版,可直接玩耍 | 代码

谷歌“史上最强GAN”,现在有了PyTorch预训练版,可直接玩耍 | 代码

作者头像
量子位
发布2019-04-23 11:11:49
7120
发布2019-04-23 11:11:49
举报
文章被收录于专栏:量子位量子位
魔栗 发自 凹非寺 量子位 报道 | 公众号 QbitAI

你知道么,和BigGAN一起玩耍,会上瘾的。

比如,生成了一只狗,再生成了一只汉堡。

那么,狗 × 汉堡 = ?

一看就是亲生的。

现在,身为PyTorch用户的你,也可以拥有一只BigGAN,而且不用自己训练,便能直接玩耍。

一向以造福人类为己任的抱抱脸 (Hugging Face) 团队,用PyTorch复现了这个“史上最强”GAN。

团队开源了预训练模型,只要pip install一下,你有什么大胆想法,就可以实施了。

推特用户纷纷表示欢迎:

还原度极高

开源项目里有三个模型,是不同分辨率的bigGAN:

128×128,256×256,512×512。

抱抱脸团队说,模型的参数都是BigGAN的爸爸DeepMind官方训练的成果。

团队说,他们是用官方的原始计算图 (Computation Graph) 来复现的,与原模型的表现几乎无差:输出差异 (Output Difference) 的方差在10^-5级。

官方模型是放在TensorFlow Hub上,抱抱脸还提供了把TF模型转成PyTorch模型时,用到的脚本。

更加温柔的是,最后会显示生成效果:

肉眼看去,成果喜人。

食用方法

如果只是想随意玩耍的话,pip install就够了。

如果要用前面提到的转换脚本,以及ImageNet实用程序的话,就要再安装一些依赖项。记得要用full_requirements.txt来装:

代码语言:javascript
复制
1git clone https://github.com/huggingface/pytorch-pretrained-BigGAN.git
2cd pytorch-pretrained-BigGAN
3pip install -r full_requirements.txt

128×128模型,有5040多万参数;256×256模型,有5590多万参数;512×512模型,有5620多万参数。三个模型,大小都在200~Mb。

安装之后,正式开始食用:

代码语言:javascript
复制
 1import torch
 2from pytorch_pretrained_biggan import (BigGAN, one_hot_from_names, truncated_noise_sample,
 3                                       save_as_images, display_in_terminal)
 4
 5# OPTIONAL: if you want to have more information on what's happening, activate the logger as follows
 6import logging
 7logging.basicConfig(level=logging.INFO)
 8
 9# Load pre-trained model tokenizer (vocabulary)
10model = BigGAN.from_pretrained('biggan-deep-256')
11
12# Prepare a input
13truncation = 0.4
14class_vector = one_hot_from_names(['soap bubble', 'coffee', 'mushroom'], batch_size=3)
15noise_vector = truncated_noise_sample(truncation=truncation, batch_size=3)
16
17# All in tensors
18noise_vector = torch.from_numpy(noise_vector)
19class_vector = torch.from_numpy(class_vector)
20
21# If you have a GPU, put everything on cuda
22noise_vector = noise_vector.to('cuda')
23class_vector = class_vector.to('cuda')
24model.to('cuda')
25
26# Generate an image
27with torch.no_grad():
28    output = model(noise_vector, class_vector, truncation)
29
30# If you have a GPU put back on CPU
31output = output.to('cpu')
32
33# If you have a sixtel compatible terminal you can display the images in the terminal
34# (see https://github.com/saitoha/libsixel for details)
35display_in_terminal(output)
36
37# Save results as png images
38save_as_images(output)

到这里,图像就愉快地生成了。

那么,你有大胆的想法了么?

举个栗子,“红酒烩鸡”:

代码传送门:

https://github.com/huggingface/pytorch-pretrained-BigGAN

作者系网易新闻·网易号“各有态度”签约作者

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-03-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 量子位 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 还原度极高
  • 食用方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档