kai-2054 commited on
Commit
6eb7da6
1 Parent(s): 81ec919

Create cool_models.py

Browse files
Files changed (1) hide show
  1. cool_models.py +123 -0
cool_models.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from guided_diffusion.script_util import create_model_and_diffusion, model_and_diffusion_defaults
3
+ import lpips
4
+ import clip
5
+
6
+
7
+ from encoders.modules import BERTEmbedder
8
+ from models.clipseg import CLIPDensePredT
9
+
10
+ from huggingface_hub import hf_hub_download
11
+
12
+ STEPS = 100
13
+ USE_DDPM = False
14
+ USE_DDIM = False
15
+ USE_CPU = False
16
+ CLIP_SEG_PATH = './weights/rd64-uni.pth'
17
+ CLIP_GUIDANCE = False
18
+
19
+ def make_models():
20
+ segmodel = CLIPDensePredT(version='ViT-B/16', reduce_dim=64)
21
+ segmodel.eval()
22
+
23
+ # non-strict, because we only stored decoder weights (not CLIP weights)
24
+ segmodel.load_state_dict(torch.load(CLIP_SEG_PATH, map_location=torch.device('cpu')), strict=False)
25
+ # segmodel.save_pretrained("./weights/hf_clipseg")
26
+
27
+ device = torch.device('cuda:0' if (torch.cuda.is_available() and not USE_CPU) else 'cpu')
28
+ print('Using device:', device)
29
+
30
+ hf_inpaint_path = hf_hub_download("alvanlii/rdm_inpaint", "inpaint.pt")
31
+ model_state_dict = torch.load(hf_inpaint_path, map_location='cpu')
32
+
33
+ # print(
34
+ # 'hey',
35
+ # 'clip_proj.weight' in model_state_dict, # True
36
+ # model_state_dict['input_blocks.0.0.weight'].shape[1] == 8, # True
37
+ # 'external_block.0.0.weight' in model_state_dict # False
38
+ # )
39
+
40
+ model_params = {
41
+ 'attention_resolutions': '32,16,8',
42
+ 'class_cond': False,
43
+ 'diffusion_steps': 1000,
44
+ 'rescale_timesteps': True,
45
+ 'timestep_respacing': STEPS, # Modify this value to decrease the number of
46
+ # timesteps.
47
+ 'image_size': 32,
48
+ 'learn_sigma': False,
49
+ 'noise_schedule': 'linear',
50
+ 'num_channels': 320,
51
+ 'num_heads': 8,
52
+ 'num_res_blocks': 2,
53
+ 'resblock_updown': False,
54
+ 'use_fp16': False,
55
+ 'use_scale_shift_norm': False,
56
+ 'clip_embed_dim': 768,
57
+ 'image_condition': True,
58
+ 'super_res_condition': False,
59
+ }
60
+
61
+ if USE_DDPM:
62
+ model_params['timestep_respacing'] = '1000'
63
+ if USE_DDIM:
64
+ if STEPS:
65
+ model_params['timestep_respacing'] = 'ddim'+str(STEPS)
66
+ else:
67
+ model_params['timestep_respacing'] = 'ddim50'
68
+ elif STEPS:
69
+ model_params['timestep_respacing'] = str(STEPS)
70
+
71
+ model_config = model_and_diffusion_defaults()
72
+ model_config.update(model_params)
73
+
74
+ if USE_CPU:
75
+ model_config['use_fp16'] = False
76
+
77
+
78
+ model, diffusion = create_model_and_diffusion(**model_config)
79
+ model.load_state_dict(model_state_dict, strict=False)
80
+
81
+ model.requires_grad_(CLIP_GUIDANCE).eval().to(device)
82
+
83
+ if model_config['use_fp16']:
84
+ model.convert_to_fp16()
85
+ else:
86
+ model.convert_to_fp32()
87
+
88
+ def set_requires_grad(model, value):
89
+ for param in model.parameters():
90
+ param.requires_grad = value
91
+
92
+
93
+ lpips_model = lpips.LPIPS(net="vgg").to(device)
94
+ hf_kl_path = hf_hub_download("alvanlii/rdm_inpaint", "kl-f8.pt")
95
+
96
+ ldm = torch.load(hf_kl_path, map_location="cpu")
97
+
98
+ # torch.save(ldm, "./weights/hf_ldm")
99
+ ldm.to(device)
100
+ ldm.eval()
101
+ ldm.requires_grad_(CLIP_GUIDANCE)
102
+ set_requires_grad(ldm, CLIP_GUIDANCE)
103
+
104
+ bert = BERTEmbedder(1280, 32)
105
+ hf_bert_path = hf_hub_download("alvanlii/rdm_inpaint", 'bert.pt')
106
+ # bert = BERTEmbedder.from_pretrained("alvanlii/rdm_bert")
107
+ sd = torch.load(hf_bert_path, map_location="cpu")
108
+ bert.load_state_dict(sd)
109
+ # bert.save_pretrained("./weights/hf_bert")
110
+
111
+ bert.to(device)
112
+ bert.half().eval()
113
+ set_requires_grad(bert, False)
114
+
115
+
116
+ clip_model, clip_preprocess = clip.load('ViT-L/14', device=device, jit=False)
117
+ clip_model.eval().requires_grad_(False)
118
+
119
+ return segmodel, model, diffusion, ldm, bert, clip_model, model_params
120
+
121
+
122
+ if __name__ == "__main__":
123
+ make_models()