controlnet前向代码解析

ControlNet|使用教程 各模型算法说明以及使用解析 - openAI本本介绍了如何在Stable Diffusion中使用ControlNet生成高质量图片的方法,包括骨骼提取、边缘线处理、引导设置、语义分割、涂鸦等功能的详细介绍,帮助用户快速上手使用ControlNet。https://openai.wiki/controlnet-guide.htmlcldm:controlnet版本的ldm

apply_uniformer=Uniformerdetector()
model=create_model('')
model.load_state_dict(load_state_dict('',location='cuda'))
ddim_sampler=DDIMSampler(model) 默认ddim

img:输入图片
prompt:
a_prompt:默认的好的prompt
n_prompt: 负面prompt
num_sample: 出几张图
image_resolution: 对controlnet中输入的图片进行最长边等比resize
detect_resolution: 
ddim_steps: 采样步数,一般20-30,值越大越精细
guess_mode:可以不写提示词
strength(control scales):  这里就是对应webui中的weights,代表controlnet生成图片的权重占比
影响,在controlnet代码中表示13步中control侧的影响,一共13个网络control侧weights=0,即不对
原始的sd进行梯度更新,但是如果对cond中的c_concat设为None,则默认不使用control,不会触发
weights,优先级高一点
guidance scale:   在webui中的这个参数是guidance和cfg有关系, 
1,中文为强度引导,在理解此功能之前,应该知道生成图片的步数功能,步数代表生成一张图片刷新计算
多少次,假设你设置的生成步数为20步,引导强度设置为1时,代表这20步中的每1步都会被controlnet
引导1次,个人认为强度数值为1,效果最佳。  
在contrilnet作者代码中是如下作用:
if unconditional_conditioning is None or unconditional_guidance_scale == 1.:
   model_output = self.model.apply_model(x, t, c)
else:
   model_t = self.model.apply_model(x, t, c)
   model_uncond = self.model.apply_model(x, t, unconditional_conditioning)
   model_output = model_uncond + unconditional_guidance_scale * (model_t - model_uncond)

process->
input_image=HWC3(input_image)->
detected_map=apply_uniformer(resize_image(input_image,detect_resolution))->
detected_img=cv2.resize(detected_map)->
control=torch.from_numpy(detected_img)->

cond={'c_concat':[control],'c_crossattn':
[model.get_learned_conditioning([prompt+a_prompt])]}->
un_cond={'c_concat':[None if guess_mode else [control],
'c_crossattn':[model.get_learned_conditioning([n_prompt])]]}->
model.control_scales=[strength*(0.825**float(12-i)) for i in range(13)] 
if guess_mode else ([strength]*13)->
samples,_=ddim_sampler.sample(ddim_steps,num_samples,shape,cond,verbose=False,
eta=eta,unconditional_guidance_scale=scale,unconditional_conditioning=uncond)->
= make_schedule(ddim_num_steps=ddim_steps,ddim_eta=eta)->
== ddim_timesteps=make_ddim_timesteps()->
= samplers,intermediates=ddim_sampler(condition,size...unconditional_guidance_scale,
unconditional_conditioning)->
== img=torch.randn(shape)->
== ts=torch.full((b,),step,device)->
== timesteps=ddpm_num_timesteps->
== outs=p_sample_ddim(img,cond,ts,...)->
=== model_t=model.apply_model(x(img),t(ts),c(cond))->
- diffusion_model=model.diffusion_model->
- cond_txt=torch.cat(cond['c_crossattn',1])->
- control=control_model(x_noisy,hint=torch.cat(cond['c_concat'],1),t,cond_txt)->
-- t_emb=timestep_embedding(timesteps,model_channels,repeat_only=False)->
-- emb=time_embed(t_embed)->
-- guided_hint=TimestepEmbedSequential(hint,emd,context)->
-- input_blocks,sero_convs->
-- h=middle_block(h,emb,context)->
-- outs.append(middle_block_out(h,emb,context))->
- control=[c*scale for c,scale in zip(control,control_scales)]->
- eps=diffusion_model(x_noisy,t,cond_txt,control,only_mid_control)->
-- t_emb=timestep_embedding(timesteps,model_channels,repeat_only)->
-- emb=time_embed(t_emb)->
-- h=module(h,emb,context)->
-- h=middle_block(h,emb,context)->
-- only_mid_control->只在中间阶段添加control,但是control=None,则都不添加->
-- h=torch.cat([h,hs.pop()+control.pop()],dim=1)->
-- h=module(h,emb,context)->
-- out(h)->
=== model_uncond=model.apply_model(x,t,unconditional_conditioning)->
=== model_output=model_uncond+unconditional_guidance_scale*(model_t-model_uncond)->
=== pred_x0,-,-=model.first_stage_model.quantize(pred_x0)->
x_samples=model.decode_first_stage(samples) vae中的decode部分->
= z=1./scale_factor*z->
= first_stage_model.decode(z)->
== z = post_quant_conv(z)->
== dec=decoder(z)->
x_samples=(einops.rearrange(x_samples,'b c h w -> b h w c')*127.5+127.5)
.cpu().numpy().clip(0,255).astype(np/unint8)->
results

要分析下controlnet的yaml文件,在params中分成了4个部分,分别是control_stage_config、unnet_config、first_stage_config、cond_stage_config。其中control_stage_config对应的是13层的controlnet,unet_config对应的是diffusion model,first_stage_config对应的是vae中的decode部分。

因此当control=None时,就是webui中的sd1.5/2.1。

猜你喜欢

转载自blog.csdn.net/u012193416/article/details/129956546