博客專欄

        EEPW首頁 > 博客 > 實踐教程|StableDiffusion圖像生成能力一探!附Int8量化教程與ONNX導出推理

        實踐教程|StableDiffusion圖像生成能力一探!附Int8量化教程與ONNX導出推理

        發(fā)布人:計算機視覺工坊 時間:2022-09-17 來源:工程師 發(fā)布文章
        作者丨吃飯機@知乎

        來源丨h(huán)ttps://zhuanlan.zhihu.com/p/200924181編輯丨計算機視覺工坊

        導讀

         

        只吃瓜怎么能行,當然要上手一試啦!本文送上了一份StableFusion的int8量化教程,以及ONNX導出,甚至是TensorRT加速推理。通過CPU OpenVINO加速也就3分鐘推理。 

        最近stablediffusion大火,但很多人都只是吃瓜,最多也就是在huggingface網(wǎng)站上試一下,這其實并不夠,作為一個富有商業(yè)嗅覺的AI從業(yè)者,我****到的更多的是他的商業(yè)能力,不得不說,現(xiàn)在生成類的AI模型,已經(jīng)越來越來接近甚至超越人類水平。

        今天就來一探究竟,看看StableFusion到底能做啥?并附上這個 超級大模型的int8量化教程,以及ONNX導出,甚至是TensorRT加速推理。

        請注意,一下的所有測試案例,都是我用CPU跑出來的,你要問我多慢?通過CPU OpenVINO加速也就3分鐘推理。為什么不用GPU?因為顯存不足!

        好在我們用CPU也能比較完善的推理,結(jié)果并不差。未來我們還會繼續(xù)使用TensorRT推理,加速整個推理流程。更進一步的將StableDiffusion變成生產(chǎn)力工具!

        代碼

        這次我們直接先上代碼,這里面包含了CPU下推理StableDiffusion,以及OpenVINO加速的代碼,同時,也包含了量化腳本,感覺興趣的可以再github提issue。

        git clone https://github.com/luohao123/gaintmodels

        備注:代碼來自大佬,不是本人寫的。不過有問題歡迎提問交流。

        測試StableDiffusion

        來看看生成的效果,由于模型只能編碼英文,我們就以英文作為promopt。

        A green car with appearance of Tesla Model 3 and Porsche 911

        圖片

        這長得還真的就像是,保時捷和model3的合體!中間那個logo你注意看,感覺并不是特斯拉的logo!有點像保時捷的logo!~

        A robot Elon Musk in cyberpunk, driving on a Tesla Model X

        圖片

        ModelX倒是有點像,但是馬斯克人呢??

        StableDiffusion這類模型,似乎在生成風景畫上表現(xiàn)不錯

        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é)清晰可見!!

        接下來我們測試一下,一些NSFW的內(nèi)容,請注意,高能預警,我們的模型把huggingface的SaftyChecker去掉了,為什么?因為我們并不需要!并且這個模塊是會占用很多內(nèi)存的!

        A beautiful sexy girl with red hair, 4k, detailed, without any cloth.

        圖片

        由于你懂得原因,我只截取了一部分!!但是,這個生成的效果真的令人震驚!過于真實!!

        再來測試一些創(chuàng)造性的東西:

        A advanced spaceship with Elon Musk driving on it, detailed, in reality style

        圖片

        這個非常設計的,地區(qū)非常的逼真!看這個反光,這個倒影,以及上面的mount,甚至有種自動駕駛飛船的感覺。

        A red tv in front of sofa, a child is looking at it with a dog.

        圖片

        在比如這個圖片,這個真實度簡直超乎了我的預料,電視機里面甚至還可以看到倒影!!

        代碼講解

        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!

        Install

        Some requirements to install:

        pip install diffusers
        pip install transformers
        pip install alfred-py

        Models
        1. 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.

        總結(jié)

        生成模型在之前效果其實并不好,但是到如今,大模型已經(jīng)展示出了驚人的能力。不管是創(chuàng)作還是設計logo,從某種方面來說可能甚至是超過常人的。然而大模型都非常大,門檻比較高,我們將StableDiffusion 降維到int8,你甚至只需要一個CPU就能推理!

        這里面還有無數(shù)種可能等待大家來探索,歡迎關(guān)注、點贊文章,更多教程更新中。


        *博客內(nèi)容為網(wǎng)友個人發(fā)布,僅代表博主個人觀點,如有侵權(quán)請聯(lián)系工作人員刪除。

        pic相關(guān)文章:pic是什么




        關(guān)鍵詞: AI

        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 普兰县| 洪雅县| 清新县| 巢湖市| 南澳县| 宁波市| 芜湖县| 壶关县| 榕江县| 钟山县| 芮城县| 苏尼特右旗| 都兰县| 丽水市| 高要市| 隆昌县| 渭南市| 酒泉市| 普格县| 桂林市| 汶上县| 榆社县| 三都| 东乡| 从江县| 于都县| 河津市| 毕节市| 临澧县| 武清区| 崇阳县| 闻喜县| 淮滨县| 屯留县| 绩溪县| 合山市| 丹寨县| 临西县| 莱阳市| 贡嘎县| 全南县|