0%

paper and code reading:Adding Conditional Control to Text-to-Image Diffusion Models

abstract and introduction

control net:为大型预训练的文本到图像扩散模型(文中使用的是Stable Diffusion)添加空间条件控制(空间定位的输入条件)

“zero convolutions”: ensure that no harmful noise could affect the finetuning

大概就是说锁住主干的不变,然后复制一份,把这两个用零卷积层连接到一起

finetune

Image Diffusion

//todo

Image-to-Image Translation

image-20240428201348313

一个神经块以特征图x作为输入,输出另一个特征图y,如图(a)所示。为了给这样的块添加ControlNet,我们锁定原始块,创建一个可训练的副本,并使用零卷积层将它们连接在一起,即权重和偏置都初始化为零的1×1卷积。这里的c是我们希望添加到网络中的条件向量,如图(b)所示。

对于右图,在实际coding的时候,使用

1
python tool_add_control.py ./models/v1-5-pruned.ckpt ./models/control_sd15_ini.ckpt

来完成这两个模型的拼接

Method

image-20240428213908624

灰色的锁定块显示了Stable Diffusion的结构。可训练的蓝色块和白色的零卷积层被添加以构建ControlNet。

code reading:canny2img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
apply_canny = CannyDetector()

model = create_model('./models/cldm_v15.yaml').cpu()
model.load_state_dict(load_state_dict('./models/control_sd15_canny.pth', location='cuda'))
model = model.cuda()
ddim_sampler = DDIMSampler(model)


def process(input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, guess_mode, strength, scale, seed, eta, low_threshold, high_threshold):
with torch.no_grad():
#输入图像的预处理
img = resize_image(HWC3(input_image), image_resolution)
H, W, C = img.shape

detected_map = apply_canny(img, low_threshold, high_threshold)
detected_map = HWC3(detected_map)

control = torch.from_numpy(detected_map.copy()).float().cuda() / 255.0
control = torch.stack([control for _ in range(num_samples)], dim=0)
control = einops.rearrange(control, 'b h w c -> b c h w').clone()

if seed == -1:
seed = random.randint(0, 65535)
seed_everything(seed)

if config.save_memory:
model.low_vram_shift(is_diffusing=False)
#模型的输入
#c_concat部分:主要为control的引导模块,仅在guess_mode中un_cond为空
#c_crossattn交叉注意力模块:主要为prompt的引导部分
cond = {"c_concat": [control], "c_crossattn": [model.get_learned_conditioning([prompt + ', ' + a_prompt] * num_samples)]}
un_cond = {"c_concat": None if guess_mode else [control], "c_crossattn": [model.get_learned_conditioning([n_prompt] * num_samples)]}
shape = (4, H // 8, W // 8)

if config.save_memory:
model.low_vram_shift(is_diffusing=True)
#进行DDIM采样过程
model.control_scales = [strength * (0.825 ** float(12 - i)) for i in range(13)] if guess_mode else ([strength] * 13) # Magic number. IDK why. Perhaps because 0.825**12<0.01 but 0.826**12>0.01
samples, intermediates = ddim_sampler.sample(ddim_steps, num_samples,
shape, cond, verbose=False, eta=eta,
unconditional_guidance_scale=scale,
unconditional_conditioning=un_cond)

if config.save_memory:
model.low_vram_shift(is_diffusing=False)
#将特征解码还原成图像
x_samples = model.decode_first_stage(samples)
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.uint8)

results = [x_samples[i] for i in range(num_samples)]
return [255 - detected_map] + results

代码其他部分的分析

//todo