docs/si/diffusion/stable_diffusion/scripts/text_to_image.html
homediffusionstable_diffusionscripts
11importargparse12importos13frompathlibimportPath1415importtorch1617fromlabmlimportlab,monit18fromlabml\_nn.diffusion.stable\_diffusion.latent\_diffusionimportLatentDiffusion19fromlabml\_nn.diffusion.stable\_diffusion.sampler.ddimimportDDIMSampler20fromlabml\_nn.diffusion.stable\_diffusion.sampler.ddpmimportDDPMSampler21fromlabml\_nn.diffusion.stable\_diffusion.utilimportload\_model,save\_images,set\_seed
24classTxt2Img:
28model:LatentDiffusion
checkpoint_path යනු මුරපොලේ මාර්ගයයිsampler_nameනියැදිකරුගේ නමයිn_steps නියැදි පියවර ගණන වේddim_etaDDIM නියැදිη නියතය30def\_\_init\_\_(self,\*,31checkpoint\_path:Path,32sampler\_name:str,33n\_steps:int=50,34ddim\_eta:float=0.0,35):
ගුප්ත විසරණය කිරීමේ ආකෘතිය පටවන්න
43self.model=load\_model(checkpoint\_path)
උපාංගය ලබා ගන්න
45self.device=torch.device("cuda:0")iftorch.cuda.is\_available()elsetorch.device("cpu")
ආකෘතිය උපාංගයට ගෙන යන්න
47self.model.to(self.device)
නියැදිකරු ආරම්භ කරන්න
50ifsampler\_name=='ddim':51self.sampler=DDIMSampler(self.model,52n\_steps=n\_steps,53ddim\_eta=ddim\_eta)54elifsampler\_name=='ddpm':55self.sampler=DDPMSampler(self.model)
dest_path ජනනය කරන ලද රූප ගබඩා කිරීමේ මාර්ගයයිbatch_size යනු කණ්ඩායමක් තුළ ජනනය කළ යුතු රූප ගණනprompt සමඟ රූප ජනනය කිරීමේ විමසුමh යනු රූපයේ උසw රූපයේ පළල වේuncond_scale යනු කොන්දේසි විරහිත මාර්ගෝපදේශs පරිමාණයයි. මෙය භාවිතා වේϵθ(xt,c)=sϵcond(xt,c)+(s−1)ϵcond(xt,cu)[email protected]\_grad()58def\_\_call\_\_(self,\*,59dest\_path:str,60batch\_size:int=3,61prompt:str,62h:int=512,w:int=512,63uncond\_scale:float=7.5,64):
රූපයේ නාලිකා ගණන
75c=4
ගුප්ත අභ්යවකාශ විභේදන අඩු කිරීමට රූපය
77f=8
විමසුම් කණ්ඩායමක් සාදන්න
80prompts=batch\_size\*[prompt]
AMP වාහන වාත්තු
83withtorch.cuda.amp.autocast():
කොන්දේසි විරහිත පරිමාණය තුළ හිස් විමසීම් සඳහා කාවැද්දීම්1 ලබා නොගනී (කන්ඩිෂනේෂන් නැත).
85ifuncond\_scale!=1.0:86un\_cond=self.model.get\_text\_conditioning(batch\_size\*[""])87else:88un\_cond=None
කඩිනම් කාවැද්දීම් ලබා ගන්න
90cond=self.model.get\_text\_conditioning(prompts)
ගුප්ත අවකාශයේ නියැදිය. x හැඩය වනු ඇත[batch_size, c, h / f, w / f]
93x=self.sampler.sample(cond=cond,94shape=[batch\_size,c,h//f,w//f],95uncond\_scale=uncond\_scale,96uncond\_cond=un\_cond)
ස්වයංක්රීය එන්කෝඩරයෙන් රූපය විකේතනය කරන්න
98images=self.model.autoencoder\_decode(x)
පින්තූර සුරකින්න
101save\_images(images,dest\_path,'txt\_')
104defmain():
108parser=argparse.ArgumentParser()109110parser.add\_argument(111"--prompt",112type=str,113nargs="?",114default="a painting of a virus monster playing guitar",115help="the prompt to render"116)117118parser.add\_argument("--batch\_size",type=int,default=4,help="batch size")119120parser.add\_argument(121'--sampler',122dest='sampler\_name',123choices=['ddim','ddpm'],124default='ddim',125help=f'Set the sampler.',126)127128parser.add\_argument("--flash",action='store\_true',help="whether to use flash attention")129130parser.add\_argument("--steps",type=int,default=50,help="number of sampling steps")131132parser.add\_argument("--scale",type=float,default=7.5,133help="unconditional guidance scale: "134"eps = eps(x, empty) + scale \* (eps(x, cond) - eps(x, empty))")135136opt=parser.parse\_args()137138set\_seed(42)
ෆ්ලෑෂ් අවධානය සකසන්න
141fromlabml\_nn.diffusion.stable\_diffusion.model.unet\_attentionimportCrossAttention142CrossAttention.use\_flash\_attention=opt.flash
145txt2img=Txt2Img(checkpoint\_path=lab.get\_data\_path()/'stable-diffusion'/'sd-v1-4.ckpt',146sampler\_name=opt.sampler\_name,147n\_steps=opt.steps)148149withmonit.section('Generate'):150txt2img(dest\_path='outputs',151batch\_size=opt.batch\_size,152prompt=opt.prompt,153uncond\_scale=opt.scale)
157if\_\_name\_\_=="\_\_main\_\_":158main()