forked from microsoft/VQ-Diffusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference_VQ_Diffusion.py
146 lines (122 loc) · 5.58 KB
/
inference_VQ_Diffusion.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# ------------------------------------------
# VQ-Diffusion
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# written By Shuyang Gu
# ------------------------------------------
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import platform
import torch
import numpy as np
from PIL import Image
from image_synthesis.utils.io import load_yaml_config
from image_synthesis.modeling.build import build_model
from image_synthesis.utils.misc import get_model_parameters_info
class VQ_Diffusion():
def __init__(self, config, path, imagenet_cf=False):
self.info = self.get_model(ema=True, model_path=path, config_path=config, imagenet_cf=imagenet_cf)
self.model = self.info['model']
self.epoch = self.info['epoch']
self.model_name = self.info['model_name']
self.model = self.model.cuda()
self.model.eval()
for param in self.model.parameters():
param.requires_grad=False
def get_model(self, ema, model_path, config_path, imagenet_cf):
if 'OUTPUT' in model_path: # pretrained model
if(platform.system() == "Windows"):
model_name = model_path.split('/')[-3]
else:
model_name = model_path.split(os.path.sep)[-3]
else:
model_name = os.path.basename(config_path).replace('.yaml', '')
config = load_yaml_config(config_path)
if imagenet_cf:
config['model']['params']['diffusion_config']['params']['transformer_config']['params']['class_number'] = 1001
model = build_model(config)
model_parameters = get_model_parameters_info(model)
print(model_parameters)
if os.path.exists(model_path):
ckpt = torch.load(model_path, map_location="cpu")
else:
print("Model path: {} does not exist.".format(model_path))
exit(0)
if 'last_epoch' in ckpt:
epoch = ckpt['last_epoch']
elif 'epoch' in ckpt:
epoch = ckpt['epoch']
else:
epoch = 0
missing, unexpected = model.load_state_dict(ckpt["model"], strict=False)
print('Model missing keys:\n', missing)
print('Model unexpected keys:\n', unexpected)
if ema==True and 'ema' in ckpt:
print("Evaluate EMA model")
ema_model = model.get_ema_model()
missing, unexpected = ema_model.load_state_dict(ckpt['ema'], strict=False)
return {'model': model, 'epoch': epoch, 'model_name': model_name, 'parameter': model_parameters}
def inference_generate_sample_with_class(self, text, truncation_rate, save_root, batch_size, infer_speed=False, guidance_scale=1.0):
os.makedirs(save_root, exist_ok=True)
self.model.guidance_scale = guidance_scale
data_i = {}
data_i['label'] = [text]
data_i['image'] = None
condition = text
str_cond = str(condition)
save_root_ = os.path.join(save_root, str_cond)
os.makedirs(save_root_, exist_ok=True)
with torch.no_grad():
model_out = self.model.generate_content(
batch=data_i,
filter_ratio=0,
replicate=batch_size,
content_ratio=1,
return_att_weight=False,
sample_type="top"+str(truncation_rate)+'r',
) # B x C x H x W
# save results
content = model_out['content']
content = content.permute(0, 2, 3, 1).to('cpu').numpy().astype(np.uint8)
for b in range(content.shape[0]):
cnt = b
save_base_name = '{}'.format(str(cnt).zfill(6))
save_path = os.path.join(save_root_, save_base_name+'.jpg')
im = Image.fromarray(content[b])
im.save(save_path)
def inference_generate_sample_with_condition(self, text, truncation_rate, save_root, batch_size, infer_speed=False, guidance_scale=1.0, prior_rule=0, prior_weight=0, learnable_cf=True):
os.makedirs(save_root, exist_ok=True)
self.model.guidance_scale = guidance_scale
self.model.learnable_cf = self.model.transformer.learnable_cf = learnable_cf # whether to use learnable classifier-free
self.model.transformer.prior_rule = prior_rule # inference rule: 0 for VQ-Diffusion v1, 1 for only high-quality inference, 2 for purity prior
self.model.transformer.prior_weight = prior_weight # probability adjust parameter, 'r' in Equation.11 of Improved VQ-Diffusion
data_i = {}
data_i['text'] = [text]
data_i['image'] = None
condition = text
str_cond = str(condition)
save_root_ = os.path.join(save_root, str_cond)
os.makedirs(save_root_, exist_ok=True)
if infer_speed != False:
add_string = 'r,time'+str(infer_speed)
else:
add_string = 'r'
with torch.no_grad():
model_out = self.model.generate_content(
batch=data_i,
filter_ratio=0,
replicate=batch_size,
content_ratio=1,
return_att_weight=False,
sample_type="top"+str(truncation_rate)+add_string,
) # B x C x H x W
# save results
content = model_out['content']
content = content.permute(0, 2, 3, 1).to('cpu').numpy().astype(np.uint8)
for b in range(content.shape[0]):
cnt = b
save_base_name = '{}'.format(str(cnt).zfill(6))
save_path = os.path.join(save_root_, save_base_name+'.png')
im = Image.fromarray(content[b])
im.save(save_path)