深度学习系列43:引入注意力的SAGAN/BigGAN和big_sleep

1. 从SAGAN到BigGAN

sa_gan是Self-Attention Generative Adversarial Networks的缩写。
动机:一般的dc_gan(deep convolution)模型擅长处理含有大量纹理的类型,比如天空、风景等,但在结构上的表现比较差,比如不能正确生成人脸、四肢等。其原因是卷积核不足以覆盖较大的区域。因此,我们加入attention机制。
下图中的fgh类似注意力机制中的kqv
在这里插入图片描述

convolution feature maps的尺寸为[C, W, H]
f/g后的尺寸为[C/8, W*H], h后的尺寸仍旧是[C, W, H]
attention map的尺寸为[W*H,W*H]
attention map和h相乘得到的o,尺寸为[C, W, H]

BigGAN是SAGAN的升级版,包括:

  • batchsize*8
  • parameters*2~4
  • noise truncate:截断先验分布z,在保证多样性的同时,防止生成坏图片。
    在这里插入图片描述

2. big_sleep模型介绍

big_sheep是结合了CLIP的多模态版本big_gan。git地址为:https://github.com/lucidrains/big-sleep。
由于没有论文,我们简单看下代码:

# 使用biggan生成图像
model = BigGAN.from_pretrained('biggan-deep-512')
out = model(*lats(), 1)

# 使用clip计算图像和文字的损失,加入到discriminator的损失函数中
perceptor, preprocess = clip.load('ViT-B/32')
tx = clip.tokenize('''a cityscape in the style of Van Gogh''')
t = perceptor.encode_text(tx.cuda())
i = perceptor.encode_image(out)
loss1 = latents损失
loss2 = 分类损失
loss3 =-100*torch.cosine_similarity(t, i, dim=-1).mean() # 图像与文本相似度损失

# 其他的步骤和biggan相同

3. 使用方法

在有gpu的机器上,调用pip install big-sleep

然后直接执行$ dream "a pyramid made of ice"就可以获得图片了。在colab上使用3个半小时,生成的图片如下:
在这里插入图片描述

如果内存足够,可以用大模型:$ dream "storm clouds rolling in over a white barnyard" --larger-model
想要保存的话,添加下面的参数:$ dream "a bowl of apples next to the fireplace" --save-progress --save-every 100
或者保存最佳:$ dream "a room with a view of the ocean" --save-best

在python中调用方法如下:

from big_sleep import Imagine

dream = Imagine(
    text = "fire in the sky",
    lr = 5e-2,
    save_every = 25,
    save_progress = True
)

dream()

高阶玩法:建立一个pipline,逐步生成更高级的图片。用|分割即可:

from big_sleep import Imagine

dream = Imagine(
    text = "an armchair in the form of pikachu|an armchair imitating pikachu|abstract",
    lr = 5e-2,
    save_every = 25,
    save_progress = True
)

dream()

避免模糊和放大:可以添加text_min参数:

from big_sleep import Imagine

dream = Imagine(
    text = "an armchair in the form of pikachu|an armchair imitating pikachu|abstract",
    text_min = "blur|zoom",
)
dream()

如果你用的是windows机器,这里还有一个界面:
https://softology.pro/voc.htm
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值