-
Notifications
You must be signed in to change notification settings - Fork 43
/
inference_VQ_Diffusion.py
148 lines (117 loc) · 5.29 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
# ------------------------------------------
# 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 torch
import cv2
import argparse
import numpy as np
import torchvision
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):
self.info = self.get_model(ema=True, model_path=path, config_path=config)
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):
if 'OUTPUT' in model_path: # pretrained model
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)
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")
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,fast=False):
os.makedirs(save_root, exist_ok=True)
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,fast=False):
os.makedirs(save_root, exist_ok=True)
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 fast != False:
add_string = 'r,fast'+str(fast-1)
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)
if __name__ == '__main__':
# VQ_Diffusion = VQ_Diffusion(config='OUTPUT/pretrained_model/config_text.yaml', path='OUTPUT/pretrained_model/human_pretrained.pth')
# VQ_Diffusion.inference_generate_sample_with_condition("a man with beard",truncation_rate=0.86, save_root="RESULT",batch_size=2,fast=2) # fast is a int from 2 to 10
# VQ_Diffusion.inference_generate_sample_with_condition("a beautiful smiling woman",truncation_rate=0.85, save_root="RESULT",batch_size=8)
VQ_Diffusion = VQ_Diffusion(config='OUTPUT/pretrained_model/config_imagenet.yaml', path='OUTPUT/pretrained_model/imagenet_pretrained.pth')
VQ_Diffusion.inference_generate_sample_with_class(493,truncation_rate=0.86, save_root="RESULT",batch_size=8)