-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
133 lines (108 loc) · 3.4 KB
/
generate.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
import argparse
import os
from tqdm import tqdm
import numpy as np
import matplotlib.pyplot as plt
import cv2
import torch
from improved_diffusion.script_util import (
NUM_CLASSES,
model_and_diffusion_defaults,
create_model_and_diffusion,
add_dict_to_argparser,
args_to_dict,
)
MODEL_ARGS = {
'image_size': 64,
'num_channels': 128,
'num_res_blocks': 3,
'num_heads': 4,
'num_heads_upsample': -1,
'attention_resolutions': '16,8',
'dropout': 0.0,
'learn_sigma': True,
'sigma_small': False,
'class_cond': False,
'diffusion_steps': 4000,
'noise_schedule': 'cosine',
'timestep_respacing': '',
'use_kl': False,
'predict_xstart': False,
'rescale_timesteps': True,
'rescale_learned_sigmas': True,
'use_checkpoint': False,
'use_scale_shift_norm': True
}
def img2np(img):
img_np = img.detach().cpu().numpy()
img_np = ((img_np + 1) * 127.5).clip(0, 255).astype('uint8')
img_np = img_np[0].transpose([1, 2, 0])
return img_np
def img2torch(img_np):
img = img_np.transpose([2, 0, 1])[None]
img = torch.tensor(img, device='cuda')
img = img / 127.5 - 1
return img.float()
def main(args):
if args.input_image is not None:
input_img = cv2.imread(args.input_image)[:, :, ::-1]
input_img = cv2.resize(
input_img,
(MODEL_ARGS['image_size'], MODEL_ARGS['image_size']),
interpolation=cv2.INTER_NEAREST,
)
else:
input_img = np.zeros(
(MODEL_ARGS['image_size'], MODEL_ARGS['image_size'], 3),
dtype='uint8'
)
input_img_mask = torch.tensor(input_img != 0, dtype=torch.float, device='cuda')
input_img_mask = input_img_mask.max(2).values[None, None]
input_img = img2torch(input_img)
input_img = input_img.tile([args.batch_size, 1, 1, 1])
model, diffusion = create_model_and_diffusion(**MODEL_ARGS)
model.load_state_dict(
torch.load(args.model_path)
)
model.cuda()
model.eval()
img = torch.randn(
[args.batch_size, 3, MODEL_ARGS['image_size'], MODEL_ARGS['image_size']],
device='cuda'
)
history = [img2np(img)]
indices = range(MODEL_ARGS['diffusion_steps'] - 1, -1, -1)
for i in tqdm(indices):
t = torch.tensor([i] * args.batch_size, device='cuda')
with torch.no_grad():
out = diffusion.p_sample_image_completion(
model,
img,
t,
input_img,
input_img_mask,
)
img = out["sample"]
if i % (len(indices) // 22) == 0:
history.append(img2np(img))
if args.batch_size > 1:
history = [
img2np(img[i: i + 1])
for i in range(img.shape[0])
]
np.save('output_images.npy', np.array(history))
h = 4
w = len(history) // h + min(len(history) % h, 1)
fig, axes = plt.subplots(h, w, figsize=(w * 3, h * 3))
for i in range(len(history)):
axes[i // w, i % w].imshow(history[i])
axes[i // w, i % w].axis('off')
plt.tight_layout()
plt.show()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--input_image", type=str, default=None)
parser.add_argument("--model_path", type=str, default=None)
parser.add_argument("--batch_size", type=int, default=1)
args = parser.parse_args()
main(args)