AttGAN实验复现 2024

AttnGAN 代码复现 2024

文章目录

  • AttnGAN 代码复现 2024
    • 简介
    • 环境
    • python 依赖
    • 数据集
    • Training
      • Pre-train DAMSM
      • Train AttnGAN
    • Sampling
      • B_VALIDATION 为 False (默认)
      • B_VALIDATION 为 True
    • 参考博客

简介

论文地址: https://arxiv.org/pdf/1711.10485.pdf

代码 python2.7(论文源码):https://github.com/taoxugit/AttnGAN

python2.7基本废掉了,不建议

代码 python3:https://github.com/davidstap/AttnGAN

在这里插入图片描述

环境

名称版本备注
NVIDIA-SMI 531.79CUDA Version: 12.1win11
torch2.3.1+cu121最新
torchvision0.18.1+cu121
Python3.10.14
笔记本 40608G 显存

注意 python2.7版本已经无法下载 torch

参考博客:Torch 、torchvision 、Python 版本对应关系

python 依赖

python-dateutil
easydict
pandas
torchfile
nltk
scikit-image
pyyaml

数据集

  1. 鸟类预处理的元数据:https://drive.google.com/file/d/1O_LtUP9sch09QH3s_EBAgLEctBQ5JBSJ/view,保存到 data
  2. 下载鸟类图像数据:http://www.vision.caltech.edu/datasets/cub_200_2011/ 将它们提取到 data/birds/

在这里插入图片描述

在这里插入图片描述

Training

Pre-train DAMSM

运行命令

cd code
python pretrain_DAMSM.py --cfg cfg/DAMSM/bird.yml --gpu 0

配置文件

code/cfg/DAMSM/bird.yml

TRAIN:
    FLAG: True
    NET_E: ''
    BATCH_SIZE: 48  # RuntimeError: CUDA out of memory,可减少batch_size
    MAX_EPOCH: 600  # 训练轮数目
    SNAPSHOT_INTERVAL: 50  # 每训练50轮保存模型

yml 文件中不要写中文,中文会出现读文件错误!

问题1

re_img = transforms.Resize(imsize[i])(img)

IndexError: list index out of range

code/datasets.py

# 旧代码
if i < (cfg.TREE.BRANCH_NUM - 1)

# 新代码 (如果你修改成 TREE.BRANCH_NUM -2 后续会导致很多问题)
if i < len(imsize)

在这里插入图片描述

问题2

IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

code/pretrain_DAMSM.py:提示我们将 loss[0] 改为 loss.item()

在这里插入图片描述

问题3

TypeError: load() missing 1 required positional argument: ‘Loader’

code/miscc/config.py

# 旧代码
yaml_cfg = edict(yaml.load(f))

# 新代码
yaml_cfg = edict(yaml.safe_load(f))

问题4

RuntimeError: masked_fill only supports boolean masks, but got dtype Byte

code/miscc/losses.py

# 旧代码(windows下不适用,但liunx下适用)
data.masked_fill_(masks, -float('inf'))

# 新代码(相反)
data.masked_fill_(masks.bool(), -float('inf'))

问题5

TypeError: pyramid_expand() got an unexpected keyword argument ‘multichannel’

code/miscc/utils.py

# 旧代码
skimage.transform.pyramid_expand(one_map, sigma=20,
                                                     upscale=vis_size // att_sze,
                                                     multichannel=True)

# 新代码
skimage.transform.pyramid_expand(one_map, sigma=20,
                                                     upscale=vis_size // att_sze,
                                                     channel_axis=-1)

问题6

OSError: cannot open resource

code/miscc/utils.py

# 旧代码
fnt = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 50)

# 新代码
script_dir = os.path.dirname(os.path.abspath(__file__))
font_path = os.path.join(script_dir, 'FreeMono.ttf')
fnt = ImageFont.truetype(font_path, 50)

eval/FreeMono.ttf 复制到 code/miscc/FreeMono.ttf

在这里插入图片描述

Train AttnGAN

运行命令

python main.py --cfg cfg/bird_attn2.yml --gpu 0

预训练模型下载

如果没有进行 Pre-train DAMSM 步骤,可直接下载 DAMSM 预训练模型 保存到 DAMSMencoders

在这里插入图片描述

配置文件

code/cfg/bird_attn2.yml

TRAIN:
    FLAG: True
	...
    NET_E: '../DAMSMencoders/bird/text_encoder200.pth'  # DAMSM 预训练模型存放位置,可自定义

注意:配置文件中不要进行中文注释

问题1

FileNotFoundError: [Errno 2] No such file or directory: '../DAMSMencoders/bird/image_encoder200.pth'

找不到.pth文件,比对路径发现是 …/DAMSMencoders/birds/image_encoder200.pth

  • 修改后

在这里插入图片描述

问题2

AttributeError: ‘_MultiProcessingDataLoaderIter’ object has no attribute ‘next’

code/trainer.py

# 旧代码
data = data_iter.next()

# 新代码
data = next(data_iter)

问题3

torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 20.00 MiB. GPU

code/cfg/bird_attn2.yml

# 旧代码
BATCH_SIZE: 20  # 22

# 新代码
BATCH_SIZE: 10  # 20 22

警告

UserWarning: This overload of add_ is deprecated

code/trainer.py

# 修改前
avg_p.mul_(0.999).add_(0.001, p.data)

# 修改后
avg_p.mul_(0.999).add_(p.data, alpha=0.001)

注意:警告可以不进行修改

如果你不想进行Trian AttnGAN 步骤,可以下载已训练好的 AttnGAN 模型 保存到 models/

在这里插入图片描述

Sampling

运行命令

python main.py --cfg cfg/eval_bird.yml --gpu 0

B_VALIDATION 为 False (默认)

./data/birds/example_filenames.txt 中的文本(可自定义句子)作为输入生成样本图像,没啥用还会干扰评价指标测量

配置文件

code/cfg/eval_bird.yml

B_VALIDATION: False

BATCH_SIZE: 100

错误1

Length of all samples has to be greater than 0, but found an element in ‘lengths’ that is <= 0

  • 进行控制变量法
    • data/birds/example_filenames.txt 进行不断删减,检测存在问题的输入样本

正确运行

example_captions
text/180.Wilson_Warbler/Wilson_Warbler_0007_175618
text/180.Wilson_Warbler/Wilson_Warbler_0024_175278
text/180.Wilson_Warbler/Wilson_Warbler_0074_175645
text/180.Wilson_Warbler/Wilson_Warbler_0107_175320
text/165.Chestnut_sided_Warbler/Chestnut_Sided_Warbler_0001_163813
text/165.Chestnut_sided_Warbler/Chestnut_Sided_Warbler_0008_164001
text/165.Chestnut_sided_Warbler/Chestnut_Sided_Warbler_0016_164060
text/165.Chestnut_sided_Warbler/Chestnut_Sided_Warbler_0035_163587
text/165.Chestnut_sided_Warbler/Chestnut_Sided_Warbler_0101_164324
text/165.Chestnut_sided_Warbler/Chestnut_Sided_Warbler_0103_163669

在这里插入图片描述

运行报错

example_captions
text/138.Tree_Swallow/Tree_Swallow_0002_136792
text/138.Tree_Swallow/Tree_Swallow_0008_135352
text/138.Tree_Swallow/Tree_Swallow_0030_134942
text/138.Tree_Swallow/Tree_Swallow_0050_135104
text/138.Tree_Swallow/Tree_Swallow_0117_134925
text/098.Scott_Oriole/Scott_Oriole_0002_795829
text/098.Scott_Oriole/Scott_Oriole_0014_795827
text/098.Scott_Oriole/Scott_Oriole_0018_795840
text/098.Scott_Oriole/Scott_Oriole_0046_92371

在这里插入图片描述

反手查看源文件

data/birds/text/138.Tree_Swallow/Tree_Swallow_0030_134942.txt

the blue backed, white bellied baby bird has a very fat little belly
this is a bird with a white belly and breast and a blue back and head.
the bird has a small white body with blue and green colored crown and coverts.
a small round bird with a white and blue body.
this��bird��has��a��white��belly,��dark��blue��wings,��and��a��small,��short��bill.
this bird is blue with white and has a very short beak.
this is a blue bird with a white throat, breast, belly and abdomen and a small black pointed beak
this small bird has a white beast and blue crest and back.
this bird has wings that are blue and has a white belly
this bird has wings that are blue and has a white belly

偷真的🐕,将��替换成空格后正常运行!!!

B_VALIDATION 为 True

data/birds/text 中文本作为输入,生成样本图像,用于评价指标测量

code/cfg/eval_bird.yml

# 新代码
B_VALIDATION: True

# 新代码(8G显存)
BATCH_SIZE: 20

运行命令

python main.py --cfg cfg/eval_bird.yml --gpu 0

结果

Make a new folder:  ../models/bird_AttnGAN2/valid/single/156.White_eyed_Vireo
step:  100
Total time for training: 91.08317875862122

在这里插入图片描述

参考博客

AttnGAN代码复现(详细步骤+避坑指南)文本生成图像

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/759682.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

springboot使用测试类报空指针异常

检查了Service注解&#xff0c;还有Autowired注解&#xff0c;还有其他注解&#xff0c;后面放心没能解决问题&#xff0c;最后使用RunWith(SpringRunner.class)解决了问题&#xff01;&#xff01; 真的是✓8了&#xff0c;烦死了这个✓8报错&#xff01;

DIVE INTO DEEP LEARNING 56-60

文章目录 56 Gated Recurrent Unit(GRU)56.1 Motivation: How to focus on a sequence56.2 The concept of doors56.3 Candidate hidden state56.4 hidden state56.5 summarize56.6 QA 57 Long short-term memory network57.1 Basic concepts57.2 Long short-term memory netwo…

240630_昇思学习打卡-Day12-Transformer中的Multiple-Head Attention

240630_昇思学习打卡-Day12-Transformer中的Multiple-Head Attention 以下为观看大佬课程及查阅资料总结所得&#xff0c;附大佬视频链接&#xff1a;Transformer中Self-Attention以及Multi-Head Attention详解_哔哩哔哩_bilibili&#xff0c;强烈建议先去看大佬视频&#xff…

BGE M3-Embedding 模型介绍

BGE M3-Embedding来自BAAI和中国科学技术大学&#xff0c;是BAAI开源的模型。相关论文在https://arxiv.org/abs/2402.03216&#xff0c;论文提出了一种新的embedding模型&#xff0c;称为M3-Embedding&#xff0c;它在多语言性&#xff08;Multi-Linguality&#xff09;、多功能…

【MySQL】库的操作【创建和操纵】

文章目录 1.创建数据库1.1字符集和校验规则1.查看系统默认字符集以及校验规则2.查看数据库支持的字符集以及校验规则 1.2校验规则对数据库的影响1.创建一个数据库&#xff0c;校验规则使用utf8_ general_ ci[不区分大小写]2.创建一个数据库&#xff0c;校验规则使用utf8_ bin[区…

如何借助 LLM 设计和实现任务型对话 Agent

1 引言 在人工智能的快速发展中&#xff0c;任务型对话 Agent 正成为提升用户体验和工作效率的关键技术。这类系统通过自然语言交互&#xff0c;专注于高效执行特定任务&#xff0c;如预订酒店或查询天气。尽管市场上的开源框架如 Rasa 和 Microsoft Bot Framework 在对话理解…

24年诺瓦星云入职认知能力测验Verify + 职业性格问卷OPQ可搜索带解析求职SHL题库

一、走进西安诺瓦星云科技股份有限公司 西安诺瓦星云科技股份有限公司(简称诺瓦星云) 是全球极具竞争力的LED显示解决方案供应商&#xff0c;实施"基于西安&#xff0c;围绕北京与深圳&#xff0c;辐射全球"的全球化布局&#xff0c;总部位于西安&#xff0c;西安、…

嵌入式Linux系统编程 — 5.3 times、clock函数获取进程时间

目录 1 进程时间概念 2 times 函数 2.1 times 函数介绍 2.2 示例程序 3 clock 函数 3.1 clock 函数介绍 3.2 示例程序 1 进程时间概念 进程时间指的是进程从创建后&#xff08;也就是程序运行后&#xff09;到目前为止这段时间内使用 CPU 资源的时间总数&#xff0c;出…

足球虚拟越位线技术FIFA OT(一)

此系列文章用于记录和回顾开发越位线系统的过程&#xff0c;平时工作较忙&#xff0c;有空时更新。 越位线技术 越位技术已被用于图形化分析足球中潜在的越位情况。 自 2018 年将视频助理裁判 &#xff08;VAR&#xff09; 引入比赛规则以来&#xff0c;人们越来越关注准确确…

力扣 单词规律

所用数据结构 哈希表 核心方法 判断字符串pattern 和字符串s 是否存在一对一的映射关系&#xff0c;按照题意&#xff0c;双向连接的对应规律。 思路以及实现步骤 1.字符串s带有空格&#xff0c;因此需要转换成字符数组进行更方便的操作&#xff0c;将字符串s拆分成单词列表…

ESP32实现UDP连接——micropython版本

代码&#xff1a; import network import socket import timedef wifiInit(name, port):ap network.WLAN(network.AP_IF) # 创建一个热点ap.config(essidname, authmodenetwork.AUTH_OPEN) # 无需密码ap.active(True) # 激活热点ip ap.ifconfig()[0] # 获取ip地址print(…

C++(Python)肥皂泡沫普拉托边界膜曲面模型算法

&#x1f3af;要点 &#x1f3af;肥皂泡二维流体模拟 | &#x1f3af;泡沫普拉托边界膜曲面模型算法演化厚度变化 | &#x1f3af;螺旋曲面三周期最小结构生成 &#x1f4dc;皂膜用例&#xff1a;Python计算物理粒子及拉格朗日和哈密顿动力学 | Python和MATLAB粘性力接触力动…

WordPress中文网址导航栏主题风格模版HaoWa

模板介绍 WordPress响应式网站中文网址导航栏主题风格模版HaoWa1.3.1源码 HaoWA主题风格除行为主体导航栏目录外&#xff0c;对主题风格需要的小控制模块都开展了敞开式的HTML在线编辑器方式的作用配备&#xff0c;另外预埋出默认设置的编码构造&#xff0c;便捷大伙儿在目前…

【python刷题】蛇形方阵

题目描述 给出一个不大于 99 的正整数n&#xff0c;输出n*n的蛇形方阵。从左上角填上1开始&#xff0c;顺时针方向依次填入数字&#xff0c;如同样例所示。注意每个数字有都会占用3个字符&#xff0c;前面使用空格补齐。 输入 输入一个正整数n,含义如题所述 输出 输出符合…

【每日刷题】Day77

【每日刷题】Day77 &#x1f955;个人主页&#xff1a;开敲&#x1f349; &#x1f525;所属专栏&#xff1a;每日刷题&#x1f34d; &#x1f33c;文章目录&#x1f33c; 1. LCR 159. 库存管理 III - 力扣&#xff08;LeetCode&#xff09; 2. LCR 075. 数组的相对排序 - 力…

vue中【事件修饰符号】详解

在Vue中&#xff0c;事件修饰符是一种特殊的后缀&#xff0c;用于修改事件触发时的默认行为。以下是Vue中常见的事件修饰符的详细解释&#xff1a; .stop 调用event.stopPropagation()&#xff0c;阻止事件冒泡。当你在嵌套元素中都有相同的事件监听器&#xff08;如click事件…

Hadoop3:Yarn容量调度器配置多队列案例

一、情景描述 需求1&#xff1a; default队列占总内存的40%&#xff0c;最大资源容量占总资源60%&#xff0c;hive队列占总内存的60%&#xff0c;最大资源容量占总资源80%。 二、多队列优点 &#xff08;1&#xff09;因为担心员工不小心&#xff0c;写递归死循环代码&#…

5.x86游戏实战-CE定位基地址

免责声明&#xff1a;内容仅供学习参考&#xff0c;请合法利用知识&#xff0c;禁止进行违法犯罪活动&#xff01; 本次游戏没法给 内容参考于&#xff1a;微尘网络安全 上一个内容&#xff1a;4.x86游戏实战-人物状态标志位 上一个内容通过CE未知的初始值、未变动的数值、…

AI绘画 Stable Diffusion【实战进阶】:图片的创成式填充,竖图秒变横屏壁纸!想怎么扩就怎么扩!

大家好&#xff0c;我是向阳。 所谓图片的创成式填充&#xff0c;就是基于原有图片进行扩展或延展&#xff0c;在保证图片合理性的同时实现与原图片的高度契合。是目前图像处理中常见应用之一。之前大部分都是通过PS工具来处理的。今天我们来看看在AI绘画工具 Stable Diffusio…

利用GPT-4o秒杀100块的开题报告,让你轻松接私活

GPT4o秒杀100块的开题报告 使用网址 https://chatgpt-plus.top/ 需求 文档上传给GPT 让gpt提供下载链接 成品如下&#xff0c;只需要稍微排版即可。 本科毕业设计&#xff08;论文&#xff09;开题报告 1. 选题目的、意义及研究现状 选题目的&#xff1a; 建立一个基于Pyt…