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

2019 年 3 月 22 日 量子位
魔栗 发自 凹非寺 
量子位 报道 | 公众号 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来装:

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。

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

 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

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

订阅AI内参,获取AI行业资讯

加入社群

量子位AI社群开始招募啦,量子位社群分:AI讨论群、AI+行业群、AI技术群;


欢迎对AI感兴趣的同学,在量子位公众号(QbitAI)对话界面回复关键字“微信群”,获取入群方式。(技术群与AI+行业群需经过审核,审核较严,敬请谅解)

诚挚招聘

量子位正在招募编辑/记者,工作地点在北京中关村。期待有才气、有热情的同学加入我们!相关细节,请在量子位公众号(QbitAI)对话界面,回复“招聘”两个字。

量子位 QbitAI · 头条号签约作者

վ'ᴗ' ի 追踪AI技术和产品新动态

喜欢就点「好看」吧 !

登录查看更多
16

相关内容

Transformer文本分类代码
专知会员服务
116+阅读 · 2020年2月3日
【干货】用BRET进行多标签文本分类(附代码)
专知会员服务
84+阅读 · 2019年12月27日
【书籍】深度学习框架:PyTorch入门与实践(附代码)
专知会员服务
160+阅读 · 2019年10月28日
开源书:PyTorch深度学习起步
专知会员服务
49+阅读 · 2019年10月11日
DeepMind开源最牛无监督学习BigBiGAN预训练模型
新智元
10+阅读 · 2019年10月10日
Github 项目推荐 | PyTorch 实现的 GAN 文本生成框架
AI研习社
33+阅读 · 2019年6月10日
基于PyTorch/TorchText的自然语言处理库
专知
27+阅读 · 2019年4月22日
手把手教你由TensorFlow上手PyTorch(附代码)
数据派THU
5+阅读 · 2017年10月1日
Conditional BERT Contextual Augmentation
Arxiv
8+阅读 · 2018年12月17日
Bidirectional Attention for SQL Generation
Arxiv
4+阅读 · 2018年6月21日
Arxiv
8+阅读 · 2018年5月21日
VIP会员
Top
微信扫码咨询专知VIP会员