-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclip_score
executable file
·300 lines (246 loc) · 11 KB
/
clip_score
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python -u
# Author: Mehdi Cherti
# Thanks to @lucidrains for the DALL-E PyTorch repo <https://github.com/lucidrains/DALLE-pytorch>
# this code is based on <https://github.com/lucidrains/DALLE-pytorch/blob/main/generate.py>
import joblib
import json
import argparse
from pathlib import Path
from tqdm import tqdm
import random
import numpy as np
# torch
import torch
from einops import repeat
# vision imports
from PIL import Image
from torchvision.utils import make_grid, save_image
# clip
import clip
# dalle related classes and utils
from dalle_pytorch import DiscreteVAE, OpenAIDiscreteVAE, VQGanVAE1024, DALLE
from dalle_pytorch.tokenizer import tokenizer, HugTokenizer, YttmTokenizer, ChineseTokenizer
from loader import TextImageDataset
CLIP_THRESH_DEFAULT = 25
# shape for real: (1, 1, nb_captions)
# shape for fakes: (num_generate, nb_captions, nb_captions)
# == explanation ==
# - for each real image, we have different captions `nb_captions`
# - for each caption, we generate a number of images `num_generate`
# - for each generated image' and caption, we compute CLIP score
# - in the shape of fakes, second dim is the generated image of a caption i, and third dim is a caption j. So we also compute CLIP score across captions (e.g., CLIP score for generated image of caption i with caption j)
# - shape of real is reshape to have the same dims as shape of fakes, so that we can compute the metrics
# In summary:
# - fakes[k,i,j] means the CLIP score of generated image of caption i with caption j, the k-th attempt (each of the k is an independent sample)
# - real[:,:,j] means the CLIp score of the real image with caption j
def CLIP_score_real(real, fakes):
return real.mean()
def CLIP_atleast(real, fakes, th=CLIP_THRESH_DEFAULT):
if args.clip_thresh:
th = args.clip_thresh
return np.any(fakes > th, axis=0).mean()
def CLIP_score(real, fakes):
return fakes.mean()
def CLIP_score_top1(real, fakes):
return fakes.max(axis=0).mean()
def CLIP_score_relative(real, fakes):
return (fakes.mean(axis=0, keepdims=True) / real).mean()
def CLIP_score_relative_top1(real, fakes):
return (fakes.max(axis=0, keepdims=True) / real).mean()
metrics = [
CLIP_score_real,
CLIP_score,
CLIP_score_top1,
CLIP_score_relative,
CLIP_score_relative_top1,
CLIP_atleast,
]
# argument parsing
parser = argparse.ArgumentParser()
parser.add_argument('--dalle_path', type = str, required = True,
help='path to your trained DALL-E')
parser.add_argument('--out_file', type = str, required =False, default='clip_score.json',
help='Output file')
parser.add_argument('--image_text_folder', type=str, required=True,
help='path to your folder of images and text for learning the DALL-E')
parser.add_argument('--num_generate', type = int, default = 128, required = False,
help='number of images to generate per caption')
parser.add_argument('--clip_thresh', type = float, default = CLIP_THRESH_DEFAULT, required = False,
help='CLIP threshold for computing the "atleast" metric')
parser.add_argument('--nb_examples', type = int, default = None, required = False,
help='number of real images to consider for computing CLIP score (per worker if horovod is used)')
parser.add_argument('--num_captions_per_image', type = int, default = None, required = False,
help='number of captions to retain per real image. if None, ignored. If provided, only select the captions with best CLIP score w.r.t real image')
parser.add_argument('--batch_size', type = int, default = 4, required = False,
help='batch size')
parser.add_argument('--seed', type = int, default = 42, required = False,
help='seed')
parser.add_argument('--top_k', type = float, default = 0.9, required = False,
help='top k filter threshold')
parser.add_argument('--outputs_dir', type = str, default = './outputs', required = False,
help='output directory')
parser.add_argument('--bpe_path', type = str,
help='path to your huggingface BPE json file')
parser.add_argument('--hug', dest='hug', action = 'store_true')
parser.add_argument('--chinese', dest='chinese', action = 'store_true')
parser.add_argument('--taming', dest='taming', action='store_true')
parser.add_argument('--horovod', dest='horovod', action='store_true', help='whether to use horovod for computing the metrics in a distributed manner')
parser.add_argument('--dump', dest='dump', action='store_true', help='whether to dump all the generated images in the output directory')
args = parser.parse_args()
# helper fns
def exists(val):
return val is not None
# tokenizer
if exists(args.bpe_path):
klass = HugTokenizer if args.hug else YttmTokenizer
tokenizer = klass(args.bpe_path)
elif args.chinese:
tokenizer = ChineseTokenizer()
# load DALL-E
dalle_path = Path(args.dalle_path)
assert dalle_path.exists(), 'trained DALL-E must exist'
load_obj = torch.load(str(dalle_path), map_location='cpu')
dalle_params, vae_params, weights = load_obj.pop('hparams'), load_obj.pop('vae_params'), load_obj.pop('weights')
dalle_params.pop('vae', None) # cleanup later
if vae_params is not None:
vae = DiscreteVAE(**vae_params)
elif not args.taming:
vae = OpenAIDiscreteVAE()
else:
vae = VQGanVAE1024()
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print("Using", device)
dalle = DALLE(vae = vae, **dalle_params).to(device)
dalle.load_state_dict(weights)
# generate images
image_size = vae.image_size
ds = TextImageDataset(
args.image_text_folder,
text_len=dalle.text_seq_len,
image_size=vae.image_size,
# resize_ratio=args.resize_ratio,
# truncate_captions=args.truncate_captions,
tokenizer=tokenizer,
shuffle=False,
seed=args.seed,
)
clip_mean = torch.Tensor([0.48145466, 0.4578275, 0.40821073]).view(1,3,1,1).to(device)
clip_std = torch.Tensor([0.26862954, 0.26130258, 0.27577711]).view(1,3,1,1).to(device)
clip_model, clip_preprocess = clip.load("ViT-B/32", device=device, jit=False)
ds.image_transform = clip_preprocess
real_scores = []
fake_scores = []
if args.horovod:
import horovod.torch as hvd
hvd.init()
print("Using horovod to distribute the score computation")
print("Number of workers:", hvd.size())
indices = np.arange(len(ds))
indices = indices[(indices % hvd.size()) == hvd.rank()]
ds = torch.utils.data.Subset(ds, indices)
display = (hvd.rank() == 0)
else:
display = True
nb_examples = args.nb_examples if args.nb_examples else len(ds)
# t0 = time.time()
inds = (range(nb_examples))
if display:
inds = tqdm(inds)
for i in inds:
# clip_model, clip_preprocess = clip.load("ViT-B/32", device=device, jit=False)
text, text_str, image = ds[i]
image = image.to(device)
image = image.unsqueeze(0)
clip_text = clip.tokenize(text_str).to(device)
if args.num_captions_per_image:
with torch.no_grad():
logits, _ = clip_model(image, clip_text)
logits = logits[0]
order = torch.argsort(-logits).cpu().numpy()
order = order[:args.num_captions_per_image]
text = text[order]
clip_text = clip_text[order]
text_str = [text_str[ind] for ind in order]
nb_captions = len(text_str)
# text_str = tokenizer.decode(text)
# print('x',text_str, 'x')
# if display:
# print(i,'/', nb_examples, text_str, time.time() - t0)
text = text.to(device)
# text = tokenizer.tokenize([args.text], dalle.text_seq_len).cuda()
# text = repeat(text, '() n -> b n', b = args.num_generate)
# num_generate * nb_captions, text_seq_len
text = text.repeat(args.num_generate, 1)
outputs = []
for text_chunk in text.split(args.batch_size):
output = dalle.generate_images(text_chunk, filter_thres = args.top_k)
outputs.append(output)
# generated images
# num_generate * nb_captions, c, h, w
outputs = torch.cat(outputs)
# put real and generated images in `outputs`
with torch.no_grad():
outputs = torch.nn.functional.interpolate(outputs, size=(224, 224), mode='bicubic')
outputs = (outputs - clip_mean) / clip_std
outputs = torch.cat((image, outputs), dim=0)
with torch.no_grad():
#logits_per_image: num_generate*nb_captions+1,nb_captions
logits_per_image, logits_per_text = clip_model(outputs, clip_text)
logits_per_image.clamp_min_(0)
# if display:
# print(logits_per_image.shape)
# (1, 1, nb_captions)
real_clip_score = logits_per_image[0:1,:].cpu().numpy().reshape((1, 1, nb_captions))
# num_generate*nb_captions,nb_captions
fake_clip_scores = logits_per_image[1:,:].data.cpu().numpy()
# (num_generate, nb_captions, nb_captions)
fake_clip_scores = fake_clip_scores.reshape((args.num_generate, nb_captions, nb_captions))
if args.dump:
real = outputs[0]
fakes = outputs[1:].view(args.num_generate, nb_captions, outputs.size(1), outputs.size(2), outputs.size(3))
for ind, text_str_cur in enumerate(text_str):
real_clip_score_cur = real_clip_score[:, :, ind]
fake_clip_scores_cur = fake_clip_scores[:, ind, ind]
order = np.argsort(-fake_clip_scores_cur)
fakes_cur = fakes[order, ind]
outputs_dir = Path(args.outputs_dir) / text_str_cur.replace(' ', '_')[:(100)]
outputs_dir.mkdir(parents = True, exist_ok = True)
save_image(real, outputs_dir / f'true.jpg', normalize=True)
for i, image in enumerate(fakes_cur):
save_image(image, outputs_dir / f'{i}.jpg', normalize=True)
results = {}
for metric in metrics:
score = metric(real_clip_score_cur, fake_clip_scores_cur)
score = float(score)
results[metric.__name__] = score
with open(outputs_dir / 'metrics.json', "w") as fd:
json.dump(results, fd)
real_scores.append(real_clip_score)
fake_scores.append(fake_clip_scores)
if args.horovod:
hvd.join()
real_scores = np.array(real_scores)
fake_scores = np.array(fake_scores)
results = {}
for metric in metrics:
score = float(np.mean([metric(r, f) for r, f in zip(real_scores, fake_scores)]))
if args.horovod:
score = hvd.allreduce(torch.Tensor([score])).item()
score = float(score)
if display:
results[metric.__name__] = score
print(metric.__name__, score)
if display:
dump = {
'metrics': results,
'num_generated_images_per_caption': args.num_generate,
'path': args.dalle_path,
'image_text_folder': args.image_text_folder,
'nb_examples': (nb_examples * hvd.size() if args.horovod else nb_examples),
'top_k': args.top_k,
'clip_thresh': args.clip_thresh,
'num_captions_per_image': args.num_captions_per_image,
}
with open(args.out_file, "w") as fd:
json.dump(dump, fd)
joblib.dump({'real_scores': real_scores, 'fake_scores': fake_scores}, args.out_file+'.pkl')