實(shí)踐教程|StableDiffusion圖像生成能力一探!附Int8量化教程與ONNX導出推理
來(lái)源丨h(huán)ttps://zhuanlan.zhihu.com/p/200924181編輯丨計算機視覺(jué)工坊 導讀
只吃瓜怎么能行,當然要上手一試啦!本文送上了一份StableFusion的int8量化教程,以及ONNX導出,甚至是TensorRT加速推理。通過(guò)CPU OpenVINO加速也就3分鐘推理。
最近stablediffusion大火,但很多人都只是吃瓜,最多也就是在huggingface網(wǎng)站上試一下,這其實(shí)并不夠,作為一個(gè)富有商業(yè)嗅覺(jué)的AI從業(yè)者,我****到的更多的是他的商業(yè)能力,不得不說(shuō),現在生成類(lèi)的AI模型,已經(jīng)越來(lái)越來(lái)接近甚至超越人類(lèi)水平。
今天就來(lái)一探究竟,看看StableFusion到底能做啥?并附上這個(gè) 超級大模型的int8量化教程,以及ONNX導出,甚至是TensorRT加速推理。
請注意,一下的所有測試案例,都是我用CPU跑出來(lái)的,你要問(wèn)我多慢?通過(guò)CPU OpenVINO加速也就3分鐘推理。為什么不用GPU?因為顯存不足!
好在我們用CPU也能比較完善的推理,結果并不差。未來(lái)我們還會(huì )繼續使用TensorRT推理,加速整個(gè)推理流程。更進(jìn)一步的將StableDiffusion變成生產(chǎn)力工具!
代碼這次我們直接先上代碼,這里面包含了CPU下推理StableDiffusion,以及OpenVINO加速的代碼,同時(shí),也包含了量化腳本,感覺(jué)興趣的可以再github提issue。
git clone https://github.com/luohao123/gaintmodels
備注:代碼來(lái)自大佬,不是本人寫(xiě)的。不過(guò)有問(wèn)題歡迎提問(wèn)交流。
測試StableDiffusion來(lái)看看生成的效果,由于模型只能編碼英文,我們就以英文作為promopt。
A green car with appearance of Tesla Model 3 and Porsche 911
這長(cháng)得還真的就像是,保時(shí)捷和model3的合體!中間那個(gè)logo你注意看,感覺(jué)并不是特斯拉的logo!有點(diǎn)像保時(shí)捷的logo!~
A robot Elon Musk in cyberpunk, driving on a Tesla Model X
ModelX倒是有點(diǎn)像,但是馬斯克人呢??
StableDiffusion這類(lèi)模型,似乎在生成風(fēng)景畫(huà)上表現不錯
A beautiful mansion beside a lake in the woods, with a clean road front of it.
一棟湖邊的別墅
A beautiful castle beside a waterfall in the woods, detailed, 4k
瀑布旁邊的城堡
真的是非常的優(yōu)美!而且細節清晰可見(jiàn)??!
接下來(lái)我們測試一下,一些NSFW的內容,請注意,高能預警,我們的模型把huggingface的SaftyChecker去掉了,為什么?因為我們并不需要!并且這個(gè)模塊是會(huì )占用很多內存的!
A beautiful sexy girl with red hair, 4k, detailed, without any cloth.
由于你懂得原因,我只截取了一部分??!但是,這個(gè)生成的效果真的令人震驚!過(guò)于真實(shí)??!
再來(lái)測試一些創(chuàng )造性的東西:
A advanced spaceship with Elon Musk driving on it, detailed, in reality style
這個(gè)非常設計的,地區非常的逼真!看這個(gè)反光,這個(gè)倒影,以及上面的mount,甚至有種自動(dòng)駕駛飛船的感覺(jué)。
A red tv in front of sofa, a child is looking at it with a dog.
在比如這個(gè)圖片,這個(gè)真實(shí)度簡(jiǎn)直超乎了我的預料,電視機里面甚至還可以看到倒影??!
代碼講解Experiements on testing GaintModels such as GPT3, StableFusion. We offer TensorRT && Int8 quantization on those gaint models. Make you can inference on a 6GB below GPU mem card!
InstallSome requirements to install:
pip install diffusersModels
pip install transformers
pip install alfred-py
- StableFusion:
First, we need download stablefusion weights from hugging face.
git clone https://huggingface.co/CompVis/stable-diffusion-v1-4
git lfs install
cd stable-diffusion-v1-4
git lfs pull
You should downloading weights using git lfs large file system, the model about 3GB.
To make unet_2d_condition in stablefusion able to export to onnx, make some modification on diffusers, following: link
file: diffuers/models/unet_2d_conditions.py
# L137
timesteps = timesteps.broadcast_to(sample.shape[0])
#timesteps = timesteps.broadcast_to(sample.shape[0])
timesteps = timesteps * torch.ones(sample.shape[0])
output = {"sample": sample}
#output = {"sample": sample}
return output
return sample
After that, move stable-diffusion-v1-4 to weights folder. Run:
python export_df_onnx.py
To generate onnx models.
總結生成模型在之前效果其實(shí)并不好,但是到如今,大模型已經(jīng)展示出了驚人的能力。不管是創(chuàng )作還是設計logo,從某種方面來(lái)說(shuō)可能甚至是超過(guò)常人的。然而大模型都非常大,門(mén)檻比較高,我們將StableDiffusion 降維到int8,你甚至只需要一個(gè)CPU就能推理!
這里面還有無(wú)數種可能等待大家來(lái)探索,歡迎關(guān)注、點(diǎn)贊文章,更多教程更新中。
*博客內容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀(guān)點(diǎn),如有侵權請聯(lián)系工作人員刪除。
pic相關(guān)文章:pic是什么