-
Notifications
You must be signed in to change notification settings - Fork 8
/
full_sampling.py
163 lines (138 loc) · 6.21 KB
/
full_sampling.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import torch
import torch.nn as nn
import argparse
import copy
from torch import optim
from train import setup_logging, Diffusion, EMA
from unet import UNetModel
from diffusers import AutoencoderKL
import os
import random
import torchvision
from PIL import Image
import cv2
import numpy as np
import json
def crop_whitespace(img):
img_gray = img.convert("L")
img_gray = np.array(img_gray)
ret, thresholded = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
coords = cv2.findNonZero(thresholded)
x, y, w, h = cv2.boundingRect(coords)
rect = img.crop((x, y, x + w, y + h))
return np.array(rect)
def save_images(images, path, args, **kwargs):
grid = torchvision.utils.make_grid(images, **kwargs)
if args.latent == True:
im = torchvision.transforms.ToPILImage()(grid)
else:
ndarr = grid.permute(1, 2, 0).to('cpu').numpy()
im = Image.fromarray(ndarr)
im.save(path)
return im
def save_single_images(images, path, args, **kwargs):
#grid = torchvision.utils.make_grid(images, **kwargs)
image = images.squeeze(0)
#print('images', image.shape)
white_crop = False
if args.latent == True:
im = torchvision.transforms.ToPILImage()(image)
#im = image.permute(1, 2, 0).to('cpu').numpy()
if white_crop == True:
im = crop_whitespace(im)
im = Image.fromarray(im)
else:
im = im.convert("L")
#img_gray = np.array(img_gray)
#im = crop_whitespace(im)
else:
print('no latent')
#ndarr = grid.permute(1, 2, 0).to('cpu').numpy()
#im = Image.fromarray(ndarr)
im.save(path)
return im
def main():
'''Main function'''
parser = argparse.ArgumentParser()
parser.add_argument('--device', type=str, default='cuda:7')
parser.add_argument('--img_size', type=int, default=(64, 256))
parser.add_argument('--save_path', type=str, default='/path/to/save/generated/images')
parser.add_argument('--channels', type=int, default=4)
parser.add_argument('--emb_dim', type=int, default=320)
parser.add_argument('--num_heads', type=int, default=4)
parser.add_argument('--num_res_blocks', type=int, default=1)
parser.add_argument('--latent', type=bool, default=True)
parser.add_argument('--single_image', type=bool, default=True)
parser.add_argument('--interpolation', type=bool, default=False)
parser.add_argument('--mix_rate', type=int, default=1)
parser.add_argument('--gt_train', type=str, default='./gt/gan.iam.tr_va.gt.filter27')
parser.add_argument('--stable_dif_path', type=str, default='./stable-diffusion-v1-5')
parser.add_argument('--models_path', type=str, default='/path/to/trained/models')
args = parser.parse_args()
setup_logging(args)
if args.single_image == True:
print('single image')
else:
print('16 classes')
labels = torch.arange(16).long().to(args.device)
diffusion = Diffusion(img_size=args.img_size, args=args)
num_classes = 339
vocab_size = 53
if args.latent == True:
unet = UNetModel(image_size = args.img_size, in_channels=4, model_channels=args.emb_dim, out_channels=4, num_res_blocks=1, attention_resolutions=(1, 1), channel_mult=(1, 1), num_heads=args.num_heads, num_classes=num_classes, context_dim=args.emb_dim, vocab_size=vocab_size, args=args).to(args.device)
else:
unet = UNetModel(image_size = args.img_size, in_channels=3, model_channels=128, out_channels=3, num_res_blocks=1, attention_resolutions=(1, 2), num_heads=1, num_classes=num_classes, context_dim=128, vocab_size=vocab_size).to(args.device)
#unet = nn.DataParallel(unet, device_ids = [7,5]) #,5,6,7])
optimizer = optim.AdamW(unet.parameters(), lr=0.0001)
unet.load_state_dict(torch.load(f'{args.models_path}/models/ckpt.pt'))
optimizer.load_state_dict(torch.load(f'{args.models_path}/models/optim.pt'))
unet.eval()
ema = EMA(0.995)
ema_model = copy.deepcopy(unet).eval().requires_grad_(False)
ema_model.load_state_dict(torch.load(f'{args.models_path}/models/ema_ckpt.pt'))
ema_model.eval()
if args.latent==True:
print('VAE is true')
vae = AutoencoderKL.from_pretrained(args.stable_dif_path, subfolder="vae")
vae = vae.to(args.device)
# Freeze vae and text_encoder
vae.requires_grad_(False)
else:
vae = None
with open(f'{args.gt_train}', 'r') as f:
train_data = f.readlines()
train_data = [i.strip().split(' ') for i in train_data]
style_word_dict = {}
wr_index = 0
idx = 0
for i in train_data:
s_id = i[0].split(',')[0]
image = i[0].split(',')[1] #+ '.png'
transcription = i[1]
style_word_dict[idx] = {'s_id': s_id, 'label':transcription, 'image':image}
idx += 1
print('num of writers', len(style_word_dict))
for idx in style_word_dict:
mix_rate = random.uniform(0, 1)
st = style_word_dict[idx]['s_id']
print('st', st)
image_name = style_word_dict[idx]['image']
print('image_name', image_name)
with open("./writers_dict_train.json", "r") as f:
wr_dict = json.load(f)
new_dict = {value:key for key, value in wr_dict.items()}
#uncomment for RANDOM STYLE
#s=[random.randint(0, 338)]
s = [wr_dict[st]]
x_text = style_word_dict[idx]['label']
labels = torch.tensor(s).long().to(args.device)
if not args.single_image:
ema_sampled_images = diffusion.sample(ema_model, vae, n=len(labels), x_text=x_text, labels=labels, args=args)
sampled_ema = save_images(ema_sampled_images, os.path.join(args.save_path, 'images', f"{x_text}.jpg"), args)
else:
print('final sampling')
ema_sampled_images = diffusion.sampling(ema_model, vae, n=len(labels), x_text=x_text, labels=labels, args=args, mix_rate=mix_rate)
#image_name = f'{st}_{x_text}'
sampled_ema = save_single_images(ema_sampled_images, os.path.join(args.save_path, 'images', f'{image_name}.png'), args)
if __name__ == "__main__":
main()