一、前言
這篇文章是AttnGAN: Fine-Grained TexttoImage Generation with Attention(帶有注意的生成對(duì)抗網(wǎng)絡(luò)細(xì)化文本到圖像生成)的代碼復(fù)現(xiàn)博文,我邊做邊寫,展示詳細(xì)步驟、踩坑和debug的過(guò)程。
論文地址: https://arxiv.org/pdf/1711.10485.pdf
論文閱讀筆記:Text to image論文精讀 AttnGAN
二、下載代碼和數(shù)據(jù)集
(下載鏈接如果打不開,翻到文末)
1、首先在github上下載模型代碼:https://github.com/taoxugit/AttnGAN(此為Python2.7版本)
??????最近在github上找到了AttnGAN的python3版本,可以有效避免很多語(yǔ)法錯(cuò)誤,推薦下載(22年2月28日更新)??????:
https://github.com/davidstap/AttnGAN
2、下載為鳥類預(yù)處理的元數(shù)據(jù):https://drive.google.com/open?id=1O_LtUP9sch09QH3s_EBAgLEctBQ5JBSJ
并將其保存到data/
3、下載鳥類圖像數(shù)據(jù):http://www.vision.caltech.edu/visipedia/CUB-200-2011.html 將它們提取到data/birds/。
??????若該鏈接打不開可下載這個(gè),內(nèi)容是一樣的(22年2月28日更新)??????:https://drive.google.com/file/d/1hbzc_P1FuxMkcabkgn9ZKinBwW683j45/view
4、下載完后目錄如下:
三、搭建環(huán)境
1、首先配置好解釋器
2、然后安裝環(huán)境
pip install python-dateutil
pip install easydict
pip install pandas
pip install torchfile nltk
pip install scikit-image
可能需要額外安裝的環(huán)境,根據(jù)提示進(jìn)行補(bǔ)充:
pip install torchvision
四、預(yù)訓(xùn)練DAMSM 模型(也可以跳過(guò)這步驟,直接下載預(yù)訓(xùn)練模型)
python pretrain_DAMSM.py --cfg cfg/DAMSM/bird.yml --gpu 0
可能出現(xiàn)的問(wèn)題1:‘EasyDict’ object has no attribute ‘iteritems’
問(wèn)題原因:Python3中:iteritems變?yōu)閕tems
解決方案:根據(jù)提示將iteritems改為items
可能出現(xiàn)的問(wèn)題2: ‘EasyDict’ object has no attribute ‘has_key’
問(wèn)題原因:Python3以后刪除了has_key()方法
解決方案:將 b.has_key(k):改為if k in b
可能出現(xiàn)的問(wèn)題3: module ‘torch._C’ has no attribute ‘_cuda_setDevice’
問(wèn)題原因:環(huán)境問(wèn)題,環(huán)境沒配好
解決方案:卸載原環(huán)境,重新配置pytorch
可能出現(xiàn)的問(wèn)題4:name ‘xrange’ is not defined
問(wèn)題原因:xrange是python2的用法,在python3中range與xrange已經(jīng)合并為range了。
解決方案:把用到的程序里的xrange( )函數(shù)全部換為range( )
可能出現(xiàn)的問(wèn)題5: ‘a(chǎn)scii’ codec can’t decode byte 0x80 in position 0: ordinal not in range(128)
問(wèn)題原因:讀取文件時(shí)的解碼問(wèn)題
解決方案:更改為:
class_id = pickle.load(f, encoding=‘bytes’)
可能出現(xiàn)的問(wèn)題6:IndexError: list index out of range
問(wèn)題原因:代碼問(wèn)題,數(shù)組超限
解決方案:
將 if i < (cfg.TREE.BRANCH_NUM - 1):改為
if i < (cfg.TREE.BRANCH_NUM - 2):
可能出現(xiàn)的問(wèn)題7:IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number
問(wèn)題原因:在pytorch高版本用item()
解決方案:將【0】改為.item()
可能出現(xiàn)的問(wèn)題8:OSError: cannot open resource
問(wèn)題原因:ImageFont.truetype(‘Pillow/Tests/fonts/FreeMono.ttf’, 50),環(huán)境里沒有FreeMono這個(gè)字體
解決方案:更換字體,更改為:
fnt = ImageFont.truetype(‘Pillow/Tests/fonts/arial.ttf’, 40)
五、運(yùn)行
1、預(yù)訓(xùn)練模型的下載(選做)
如果做了第四步,可以直接進(jìn)入下一小節(jié)
如果沒有做第四步,首先下載別人已經(jīng)訓(xùn)練好的預(yù)訓(xùn)練模型:
https://drive.google.com/open?id=1GNUKjVeyWYBJ8hEU-yrfYQpDOkxEyP3V將其保存到DAMSMencoders/
下載https://drive.google.com/open?id=1lqNG75suOuR_8gjoEPYNp8VyT_ufPPig并將其保存到models/
2、運(yùn)行
訓(xùn)練GAN:python main.py --cfg cfg/bird_attn2.yml --gpu 1
運(yùn)行:python main.py --cfg cfg/eval_bird.yml --gpu 1
以從“./data/birds/example_filenames.txt”中列出的文件中的標(biāo)題生成示例。結(jié)果保存到DAMSMencoders/.
可能出現(xiàn)的問(wèn)題1:‘str‘ object has no attribute ‘decode
問(wèn)題原因:Python2和Python3在字符串編碼上的區(qū)別。
解決方案:.encode(‘utf-8’). decode(‘utf-8’) ) #先編碼再解碼:
filenames = f.read().encode(‘utf8’).decode(‘utf8’).split(’\n’)
sentences = f.read().encode(‘utf8’).decode(‘utf8’).split(’\n’)
可能出現(xiàn)的問(wèn)題2:FileNotFoundError: [Errno 2] No such file or directory:’…/data/birds/text/180.Wilson_Warbler/Wilson_Warbler_0007_175618.txt’
問(wèn)題原因:該文件沒找到, 路徑問(wèn)題
解決方案:更改為正確的路徑,如果text是處于壓縮狀態(tài)要解壓。
可能出現(xiàn)的問(wèn)題3:RuntimeError: CUDA out of memory. Tried to allocate 40.00 MiB (GPU 0; 4.00 GiB total capacity; 2.86 GiB already allocated; 33.84 MiB free; 20.86 MiB cached)
問(wèn)題原因:GPU性能不足(但依然還是能跑出結(jié)果,結(jié)果在AttnGAN-master\models\bird_AttnGAN2\example_captions中)
解決方案:花錢升級(jí)硬件或者放到服務(wù)器
六、實(shí)驗(yàn)結(jié)果
可以在這里輸入相應(yīng)測(cè)試的文本
然后在這里就可以看到生成的各個(gè)階段的圖像和注意力機(jī)制的應(yīng)用。
在這里可以選擇采樣數(shù)據(jù)集
然后在這里可以看到采樣生成的圖像。
部分實(shí)驗(yàn)結(jié)果如下:
1.this bird is yellow with white and has a very long beak
2.this bird has wings that are blue and has a red belly
3.this bird is yellow with white on its head and has a very short beak文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-440865.html
六、資源下載
打不開網(wǎng)址的可以點(diǎn)擊:https://download.csdn.net/download/air__Heaven/85067478
該文件除了需要下載配置好圖像數(shù)據(jù)集(二-3),其他都是配置好了的文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-440865.html
到了這里,關(guān)于AttnGAN代碼復(fù)現(xiàn)(詳細(xì)步驟+避坑指南)文本生成圖像的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!