如何從0開始訓練企業自己的Diffusion繪圖模型(Part-01)-_第1页
如何從0開始訓練企業自己的Diffusion繪圖模型(Part-01)-_第2页
如何從0開始訓練企業自己的Diffusion繪圖模型(Part-01)-_第3页
如何從0開始訓練企業自己的Diffusion繪圖模型(Part-01)-_第4页
如何從0開始訓練企業自己的Diffusion繪圖模型(Part-01)-_第5页
已阅读5页,还剩46页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

神櫻AI訓練師團隊何從0開始訓練By神櫻AI團隊/高煥堂教授指導I內容:第1篇:基本觀念和技術架構第2篇:範例程式碼及其解析********************************************第1篇:基本觀念和技術架構1.1UNet:StableDiffusion的核心引自:https://jalammar.github.io/illustrated-stable-diffusion/omputmid引自:https://jalammar.github.io/illustrated-stable-diffusion/1.2訓練UNetUNet。在各時間點,將含有不同雜訊(Noise)的圖像輸入給UNet模型。引自:https://jalammar.github.io/illustrated-stable-diffusion/UNet。引自:https://jalammar.github.io/illustrated-stable-diffusion/相當於:earnopencvcomstablediffusiongenerativeai1.3使用UNet(預測)反向操作(Reverse)。httpszhuanlanzhihucomp81186398?UNet模型的內部結構:引自:https://deepsense.ai/diffusion-models-in-practice-part-1-the-tools-of-the-trade/詳細的UNet架構圖:引自:https://deepsense.ai/diffusion-models-in-practice-part-1-the-tools-of-the-trade/1.4準備訓練資料(如圖像)eTimestep引自:Timestep訊量(多寡)。引自:訓練完成了,就進入<使用UNet>階段。進行逆向流程:引自:/liangwqi/article/details/130586777相當於:引自:1.5Encoder的角色VAE模型。引自:/blog/diffusion引自:/projects/dreampose/使用VAE的Encoder模型。引自:/liangwqi/article/details/130586777可視化圖像,經由Encoder轉換,就是下圖裡的Xt:引自:/liangwqi/article/details/130586777這UNet裡的各Layers:引自:/liangwqi/article/details/130586777這裡以輸入的latent為[4,64,64]維度為例。其中encoder部分包括3個CrossAttnDownBlock2D模組和1個DownBlock2D模組。而decoder部分包括1個UpBlock2D模組和3個CrossAttnUpBlock2D模組。中間還有一個UNetMidBlock2DCrossAttn模組。請注意3個CrossAttnDownBlock2D模組最後均有一個2x的downsample操作,而DownBlock2D模組是不包含下採樣的。其中的CrossAttnDownBlock2D模組的結構:ssattentionblocktimestepforward、crossattention元件。的query是UNet的中間特徵,而key和value則是textembeddings。imeembeddinglineartimeembedding變換為和特徵維度一致,第一conv之後通過加上timeembedding來編碼time。2.1認識噪音(Noise)在SD裡,使用數學運算,根據時間步數(time_steps)來決定如何將隨機噪音添加到原圖上。2.2本篇使用的訓練圖像集訓練資料為:程式-1im隨機生成一張噪音(即雜訊)圖像。程式碼:opysnpformsepath='c:/ox_predno/'imagesize128imagesizeimagesizeimageNamepathim0.jpg'tnnoiseshapenoiseshapeyswapaxesnoisefilenamepath'noise_0.jpg'noiseimgsavefilenamee#-------------------------------此程式執行時,輸出:檔案裡。所以執行時,輸出:im所以生成的噪音圖(noise_0)的大小也是[3,128,128]。接下來,介紹一個新的術語和觀念,就是:時間步數。程式碼:opysnpormsepath='c:/ox_predno/'imagesize128imagesizeimagesizeimageNamepathim.jpg'#----------------------------------------timesteps=100#-----------------------------------------petchsizebatchsizeinttsts#---------------------此程式執行時,輸出:此程式先設定一個整數區間的最大值(如100)。亦即,此程式設定取樣區間程式-3間步數(timestep)隨機值。接下來,就使用一項複雜的計算公式,將兩者相加起來,成為一張<含噪音程式碼:opysnpormsepath='c:/ox_predno/'imagesize128imagesizeimagesizeimageNamepathim.jpg'#-----#------------------------#----------------------------------------timesteps=100beta_start=1e-4nspacebetastartbetaendtimestepsphabetadalphadimdefnoiseimagesx,t,Ɛ):rchsqrtalphahattNoneNoneNonesqrtoneminusalphahattorchsqrtalphahattNoneNone,None]#-----------------------------------------pe#-----------------------------------------noisyimgnoiseimagesimagets,Ɛ)noisyimImagefromarraynpuintnoisyim*255))noisy_im.save(filename)e程式執行時,輸出:也繪出圖像:程式-4來訓練UNet預測噪音(不是去除噪音)。這裡的UNet模型程式碼,如下:snptorchnnasnn#-----------------------------------------------edownsampleselfiniteLUTruenelsstridepadding)returnself.down(x)definitself,in_channels,out_channels,drop_out=False):superupsampleselfinit)nelskernelsizestridepadding)urnselfupxpixpixUNetinput28definit__(self):NetselfinitselfincnnConvd4,4,2,1)batchbatchbatchlfdowndownsamplehbatchselfdowndownsample512)batchelfdowndownsampletchlfupupsampletchlfupupsamplebatchselfupupsample)hbatchhbatchelflastnnConvTransposedinchannelsnelsizestridepaddingid#downincselfincxbatch,64]batch2,32]batchbatchdownselfdowndown)batchdownselfdowndown)tchlfupdown#[4,512,8,8]upselfuptorchcatupdowndim1))batchupselfuptorchcatupdowndimhbatchmhbatchlasttorchcatupincdimreturnout#------------------------然後,撰寫主程式。程式碼:opysnpormsetprednounetasPU#------------------------------------------optimizer=torch.optim.Adam(model.parameters(),weight_decay=1e-4,lr=0.0015)#------------------------------------------path='c:/ox_predno/'magesizeimagesizeimagesizeimageNamepathim.jpg'#----------------------------------------timesteps=100beta_start=1e-4nspacebetastartbetaendtimestepsphabetadalphadimdefnoiseimagesx,t,Ɛ):rchsqrtalphahattNoneNoneNonesqrtoneminusalphahattorchsqrtalphahattNoneNone,None]#-----------------------------------------penoisyimgnoiseimagesimagets,Ɛ)#----------------------------------------------printnepochs50其loss值流暢的下降。criterionpredictednoiseloss.backward()ifep==0):print('ep=',ep,',loss=',loss.item())#------------------------其中的指令:criterionpredictednoiseteploss值。此程式執行時,輸出:程式-5UNet測噪音)。程式碼:opysnptorchnnasnnormsetmatplotlibpyplotasplttprednounetasPU#------------------------------------------rametersweightdecayelr#------------------------------------------path='c:/ox_predno/'imagesize128imagesizeimagesizeimageNamepathim.jpg'#----------------------------------------timesteps=100beta_start=1e-4nspacebetastartbetaendtimestepsphabetadalphadimimagesxtrchsqrtalphahattNoneNoneNoneoneminusalphahattorchsqrtalphahattNoneNoneNone#-----------------------------------------peyimgnoisenoiseimagesimagets#----------------------------------------------printnepochs0npredictednoisenoiseloss.backward()ifep==0):print('ep=',ep,',loss=',loss.item())#----save-----------------------------------------isetransposeisetransposeyfilenamefilenamepath+'noise_2.jpg'noiseimgsavefilenameesesqueezefilename=path+'pred_noise_2.jpg'avefilenamee#----show-----------------------------------------sesqueezeplt.figure(figsize=(30,3))pltshow()loss值。如此,持續訓練700回合。此程式執行時,輸出:下圖的pred_noise_2)。也繪出圖像(即pred_noise_2):t俗語說:去蕪存菁。當UNet能預測出噪音(即:蕪)。,就得到清晰的圖像(即:菁)了。程式碼:opysnptorchnnasnnormsetmatplotlibpyplotaspltrtprednounetasPU#------------------------------------------rametersweightdecayelr#------------------------------------------path='c:/ox_predno/'imageimagesize128imagesizeimagesizeimageNamepathim.jpg'#----------------------------------------timesteps=128beta_start=1e-4nspacebetast

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论