-
Notifications
You must be signed in to change notification settings - Fork 0
/
pic2word.patch
443 lines (431 loc) · 18.2 KB
/
pic2word.patch
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
diff --git a/src/data.py b/pic2word/data.py
similarity index 98%
rename from src/data.py
rename to pic2word/data.py
index 6f807dc..3eb3487 100644
--- a/src/data.py
+++ b/pic2word/data.py
@@ -1,3 +1,9 @@
+# ---------------------------------------------------------------
+# This file has been modified from the file in the following repository:
+# https://github.com/google-research/composed_image_retrieval
+# See below for the original license.
+# ---------------------------------------------------------------
+
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -38,7 +44,7 @@ import torchvision.datasets as datasets
from torchvision.datasets.folder import DatasetFolder
import torchvision.datasets as datasets
import torchvision.transforms as T
-from third_party.open_clip.clip import tokenize
+from pic2word.third_party.open_clip.clip import tokenize
## Structure of dataset directory
diff --git a/src/demo.py b/pic2word/demo.py
similarity index 97%
rename from src/demo.py
rename to pic2word/demo.py
index 2e36e5d..78d98a7 100644
--- a/src/demo.py
+++ b/pic2word/demo.py
@@ -1,3 +1,9 @@
+# ---------------------------------------------------------------
+# This file has been modified from the file in the following repository:
+# https://github.com/google-research/composed_image_retrieval
+# See below for the original license.
+# ---------------------------------------------------------------
+
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
diff --git a/src/eval_retrieval.py b/pic2word/eval_retrieval.py
similarity index 93%
rename from src/eval_retrieval.py
rename to pic2word/eval_retrieval.py
index 530fd61..f6b2d16 100644
--- a/src/eval_retrieval.py
+++ b/pic2word/eval_retrieval.py
@@ -1,3 +1,9 @@
+# ---------------------------------------------------------------
+# This file has been modified from the file in the following repository:
+# https://github.com/google-research/composed_image_retrieval
+# See below for the original license.
+# ---------------------------------------------------------------
+
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -32,13 +38,13 @@ import torchvision.datasets as datasets
import torchvision.transforms as T
from PIL import Image
-from model.clip import _transform, load
-from model.model import convert_weights, CLIP, IM2TEXT
-from eval_utils import evaluate_imgnet_retrieval, evaluate_coco, evaluate_fashion, evaluate_cirr, evaluate_cirr_test
-from data import CsvDataset, CustomFolder, ImageList, CsvCOCO, FashionIQ, CIRR
-from params import parse_args, get_project_root
-from logger import setup_primary_logging, setup_worker_logging
-from utils import is_master, convert_models_to_fp32, TargetPad
+from pic2word.model.clip import _transform, load
+from pic2word.model.model import convert_weights, CLIP, IM2TEXT
+from pic2word.eval_utils import evaluate_imgnet_retrieval, evaluate_coco, evaluate_fashion, evaluate_cirr, evaluate_cirr_test
+from pic2word.data import CsvDataset, CustomFolder, ImageList, CsvCOCO, FashionIQ, CIRR
+from pic2word.params import parse_args, get_project_root
+from pic2word.logger import setup_primary_logging, setup_worker_logging
+from pic2word.utils import is_master, convert_models_to_fp32, TargetPad
def load_model(args):
model, _, preprocess_val = load(
@@ -109,7 +115,7 @@ def load_model(args):
f"=> loaded checkpoint '{args.resume}' (epoch {checkpoint['epoch']})"
)
else:
- logging.info("=> no checkpoint found at '{}'".format(args.resume))
+ raise FileNotFoundError("=> no checkpoint found at '{}'".format(args.resume))
return model, img2text, preprocess_val
def setup_log_save(args):
@@ -346,10 +352,8 @@ def main():
args.log_level = logging.DEBUG if args.debug else logging.INFO
log_queue = setup_primary_logging(args.log_path, args.log_level)
args.world_size = 1
- try:
- main_worker(args.gpu, None, log_queue, args)
- except:
- print('evaluation done')
+
+ main_worker(args.gpu, None, log_queue, args)
if __name__ == "__main__":
diff --git a/src/eval_utils.py b/pic2word/eval_utils.py
similarity index 98%
rename from src/eval_utils.py
rename to pic2word/eval_utils.py
index cad8cba..5f9da12 100644
--- a/src/eval_utils.py
+++ b/pic2word/eval_utils.py
@@ -1,3 +1,9 @@
+# ---------------------------------------------------------------
+# This file has been modified from the file in the following repository:
+# https://github.com/google-research/composed_image_retrieval
+# See below for the original license.
+# ---------------------------------------------------------------
+
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -28,10 +34,10 @@ import sys
import pdb
import logging
import torch.nn.functional as F
-from third_party.open_clip.clip import tokenize, _transform
+from pic2word.third_party.open_clip.clip import tokenize, _transform
import pickle
-from utils import is_master
+from pic2word.utils import is_master
def prepare_img(img_file, transform):
return transform(Image.open(img_file))
diff --git a/src/logger.py b/pic2word/logger.py
similarity index 90%
rename from src/logger.py
rename to pic2word/logger.py
index ac40aca..522c0c4 100644
--- a/src/logger.py
+++ b/pic2word/logger.py
@@ -1,5 +1,11 @@
+# ---------------------------------------------------------------
+# This file has been modified from the file in the following repository:
+# https://github.com/google-research/composed_image_retrieval
+# See below for the original license.
+# ---------------------------------------------------------------
+
# Copyright 2022 Google LLC
-#
+#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
diff --git a/src/main.py b/pic2word/main.py
similarity index 97%
rename from src/main.py
rename to pic2word/main.py
index 2f8d1b3..45dc20d 100644
--- a/src/main.py
+++ b/pic2word/main.py
@@ -1,3 +1,9 @@
+# ---------------------------------------------------------------
+# This file has been modified from the file in the following repository:
+# https://github.com/google-research/composed_image_retrieval
+# See below for the original license.
+# ---------------------------------------------------------------
+
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
diff --git a/pic2word/model/__init__.py b/pic2word/model/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/model/clip.py b/pic2word/model/clip.py
similarity index 96%
rename from model/clip.py
rename to pic2word/model/clip.py
index 02625b9..500fb46 100644
--- a/model/clip.py
+++ b/pic2word/model/clip.py
@@ -1,5 +1,11 @@
+# ---------------------------------------------------------------
+# This file has been modified from the file in the following repository:
+# https://github.com/google-research/composed_image_retrieval
+# See below for the original license.
+# ---------------------------------------------------------------
+
# Copyright 2022 Google LLC
-#
+#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@@ -22,9 +28,8 @@ import torch
from PIL import Image
from torchvision.transforms import Compose, Resize, CenterCrop, ToTensor, Normalize, RandomResizedCrop
from tqdm import tqdm
-from model.model import build_model
-from third_party.open_clip.simple_tokenizer import SimpleTokenizer as _Tokenizer
-
+from pic2word.model.model import build_model
+from pic2word.third_party.open_clip.simple_tokenizer import SimpleTokenizer as _Tokenizer
from functools import *
try:
from huggingface_hub import hf_hub_download
diff --git a/model/model.py b/pic2word/model/model.py
similarity index 98%
rename from model/model.py
rename to pic2word/model/model.py
index 7525fc0..9bd3c8b 100644
--- a/model/model.py
+++ b/pic2word/model/model.py
@@ -1,5 +1,11 @@
+# ---------------------------------------------------------------
+# This file has been modified from the file in the following repository:
+# https://github.com/google-research/composed_image_retrieval
+# See below for the original license.
+# ---------------------------------------------------------------
+
# Copyright 2022 Google LLC
-#
+#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@@ -452,7 +458,7 @@ class CLIP(nn.Module):
else:
img_tokens = img_tokens.view(1, 1, -1)
ind_insert = ind_insert.nonzero()[0]
- sample = torch.cat([sample[:, :ind_insert], img_tokens, sample[:, ind_insert+1:]], dim=1)
+ sample = torch.cat([sample[:, :ind_insert], img_tokens, sample[:, ind_insert+1:]], dim=1)
new_x.append(sample)
x = torch.cat(new_x, dim=0)
x = x + self.positional_embedding.type(self.dtype)
@@ -461,7 +467,7 @@ class CLIP(nn.Module):
x = x.permute(1, 0, 2) # LND -> NLD
x = self.ln_final(x).type(self.dtype)
# x.shape = [batch_size, n_ctx, transformer.width]
- # take features from the eot embedding (eot_token is the highest number in each sequence)
+ # take features from the eot embedding (eot_token is the highest number in each sequence)
x = x[torch.arange(x.size(0)), collect_ind] @ self.text_projection
return x
diff --git a/src/params.py b/pic2word/params.py
similarity index 95%
rename from src/params.py
rename to pic2word/params.py
index 5f44d39..7486994 100755
--- a/src/params.py
+++ b/pic2word/params.py
@@ -1,3 +1,9 @@
+# ---------------------------------------------------------------
+# This file has been modified from the file in the following repository:
+# https://github.com/google-research/composed_image_retrieval
+# See below for the original license.
+# ---------------------------------------------------------------
+
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -247,6 +253,11 @@ def parse_args():
help="Use the openai pretrained models.",
)
# arguments for distributed training
+ parser.add_argument("--distributed",
+ default=False,
+ action="store_true",
+ help="Whether to do distribute the training on multiple machines")
+
parser.add_argument(
"--dist-url",
default="tcp://127.0.0.1:6100",
diff --git a/third_party/open_clip/LICENSE b/pic2word/third_party/open_clip/LICENSE
similarity index 100%
rename from third_party/open_clip/LICENSE
rename to pic2word/third_party/open_clip/LICENSE
diff --git a/third_party/open_clip/bpe_simple_vocab_16e6.txt.gz b/pic2word/third_party/open_clip/bpe_simple_vocab_16e6.txt.gz
similarity index 100%
rename from third_party/open_clip/bpe_simple_vocab_16e6.txt.gz
rename to pic2word/third_party/open_clip/bpe_simple_vocab_16e6.txt.gz
diff --git a/third_party/open_clip/clip.py b/pic2word/third_party/open_clip/clip.py
similarity index 97%
rename from third_party/open_clip/clip.py
rename to pic2word/third_party/open_clip/clip.py
index 2e44431..c11c75b 100644
--- a/third_party/open_clip/clip.py
+++ b/pic2word/third_party/open_clip/clip.py
@@ -1,3 +1,8 @@
+# ---------------------------------------------------------------
+# This file has been modified from the file in the following repository:
+# https://github.com/google-research/composed_image_retrieval
+# ---------------------------------------------------------------
+
import hashlib
import os
import urllib
diff --git a/third_party/open_clip/environment.yml b/pic2word/third_party/open_clip/environment.yml
similarity index 94%
rename from third_party/open_clip/environment.yml
rename to pic2word/third_party/open_clip/environment.yml
index a75144f..110267b 100644
--- a/third_party/open_clip/environment.yml
+++ b/pic2word/third_party/open_clip/environment.yml
@@ -1,3 +1,8 @@
+# ---------------------------------------------------------------
+# This file has been modified from the file in the following repository:
+# https://github.com/google-research/composed_image_retrieval
+# ---------------------------------------------------------------
+
name: open_clip
channels:
- pytorch
diff --git a/third_party/open_clip/model.py b/pic2word/third_party/open_clip/model.py
similarity index 98%
rename from third_party/open_clip/model.py
rename to pic2word/third_party/open_clip/model.py
index cda8161..824a6ff 100644
--- a/third_party/open_clip/model.py
+++ b/pic2word/third_party/open_clip/model.py
@@ -1,3 +1,8 @@
+# ---------------------------------------------------------------
+# This file has been modified from the file in the following repository:
+# https://github.com/google-research/composed_image_retrieval
+# ---------------------------------------------------------------
+
from collections import OrderedDict
from typing import Tuple, Union
diff --git a/third_party/open_clip/model_configs/RN101.json b/pic2word/third_party/open_clip/model_configs/RN101.json
similarity index 100%
rename from third_party/open_clip/model_configs/RN101.json
rename to pic2word/third_party/open_clip/model_configs/RN101.json
diff --git a/third_party/open_clip/model_configs/RN50.json b/pic2word/third_party/open_clip/model_configs/RN50.json
similarity index 100%
rename from third_party/open_clip/model_configs/RN50.json
rename to pic2word/third_party/open_clip/model_configs/RN50.json
diff --git a/third_party/open_clip/model_configs/RN50_a2.json b/pic2word/third_party/open_clip/model_configs/RN50_a2.json
similarity index 100%
rename from third_party/open_clip/model_configs/RN50_a2.json
rename to pic2word/third_party/open_clip/model_configs/RN50_a2.json
diff --git a/third_party/open_clip/model_configs/RN50_a2s.json b/pic2word/third_party/open_clip/model_configs/RN50_a2s.json
similarity index 100%
rename from third_party/open_clip/model_configs/RN50_a2s.json
rename to pic2word/third_party/open_clip/model_configs/RN50_a2s.json
diff --git a/third_party/open_clip/model_configs/RN50x16.json b/pic2word/third_party/open_clip/model_configs/RN50x16.json
similarity index 100%
rename from third_party/open_clip/model_configs/RN50x16.json
rename to pic2word/third_party/open_clip/model_configs/RN50x16.json
diff --git a/third_party/open_clip/model_configs/RN50x4.json b/pic2word/third_party/open_clip/model_configs/RN50x4.json
similarity index 100%
rename from third_party/open_clip/model_configs/RN50x4.json
rename to pic2word/third_party/open_clip/model_configs/RN50x4.json
diff --git a/third_party/open_clip/model_configs/ViT-B-16.json b/pic2word/third_party/open_clip/model_configs/ViT-B-16.json
similarity index 100%
rename from third_party/open_clip/model_configs/ViT-B-16.json
rename to pic2word/third_party/open_clip/model_configs/ViT-B-16.json
diff --git a/third_party/open_clip/model_configs/ViT-B-32.json b/pic2word/third_party/open_clip/model_configs/ViT-B-32.json
similarity index 100%
rename from third_party/open_clip/model_configs/ViT-B-32.json
rename to pic2word/third_party/open_clip/model_configs/ViT-B-32.json
diff --git a/third_party/open_clip/scheduler.py b/pic2word/third_party/open_clip/scheduler.py
similarity index 68%
rename from third_party/open_clip/scheduler.py
rename to pic2word/third_party/open_clip/scheduler.py
index 200cec8..147e813 100644
--- a/third_party/open_clip/scheduler.py
+++ b/pic2word/third_party/open_clip/scheduler.py
@@ -1,3 +1,8 @@
+# ---------------------------------------------------------------
+# This file has been modified from the file in the following repository:
+# https://github.com/google-research/composed_image_retrieval
+# ---------------------------------------------------------------
+
import numpy as np
def assign_learning_rate(optimizer, new_lr):
diff --git a/third_party/open_clip/simple_tokenizer.py b/pic2word/third_party/open_clip/simple_tokenizer.py
similarity index 94%
rename from third_party/open_clip/simple_tokenizer.py
rename to pic2word/third_party/open_clip/simple_tokenizer.py
index 876f08f..6665c33 100644
--- a/third_party/open_clip/simple_tokenizer.py
+++ b/pic2word/third_party/open_clip/simple_tokenizer.py
@@ -1,3 +1,9 @@
+# ---------------------------------------------------------------
+# This file has been modified from the file in the following repository:
+# https://github.com/google-research/composed_image_retrieval
+# ---------------------------------------------------------------
+
+
import gzip
import html
import os
diff --git a/src/trainer.py b/pic2word/trainer.py
similarity index 96%
rename from src/trainer.py
rename to pic2word/trainer.py
index 60eb2f5..476872e 100644
--- a/src/trainer.py
+++ b/pic2word/trainer.py
@@ -1,5 +1,11 @@
+# ---------------------------------------------------------------
+# This file has been modified from the file in the following repository:
+# https://github.com/google-research/composed_image_retrieval
+# See below for the original license.
+# ---------------------------------------------------------------
+
# Copyright 2022 Google LLC
-#
+#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
diff --git a/src/utils.py b/pic2word/utils.py
similarity index 82%
rename from src/utils.py
rename to pic2word/utils.py
index 52dbb2c..64b93a1 100644
--- a/src/utils.py
+++ b/pic2word/utils.py
@@ -1,5 +1,11 @@
+# ---------------------------------------------------------------
+# This file has been modified from the file in the following repository:
+# https://github.com/google-research/composed_image_retrieval
+# See below for the original license.
+# ---------------------------------------------------------------
+
# Copyright 2022 Google LLC
-#
+#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..8baf351
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,3 @@
+from setuptools import setup, find_packages
+
+setup(name='pic2word', version='1.0', packages=find_packages())
\ No newline at end of file