- Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathretrieve.py
89 lines (78 loc) · 3.23 KB
/
retrieve.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
# Copyright 2024 Custom Diffusion authors. All rights reserved.
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
importargparse
importos
fromioimportBytesIO
frompathlibimportPath
importrequests
fromclip_retrieval.clip_clientimportClipClient
fromPILimportImage
fromtqdmimporttqdm
defretrieve(class_prompt, class_data_dir, num_class_images):
factor=1.5
num_images=int(factor*num_class_images)
client=ClipClient(
url="https://knn.laion.ai/knn-service", indice_name="laion_400m", num_images=num_images, aesthetic_weight=0.1
)
os.makedirs(f"{class_data_dir}/images", exist_ok=True)
iflen(list(Path(f"{class_data_dir}/images").iterdir())) >=num_class_images:
return
whileTrue:
class_images=client.query(text=class_prompt)
iflen(class_images) >=factor*num_class_imagesornum_images>1e4:
break
else:
num_images=int(factor*num_images)
client=ClipClient(
url="https://knn.laion.ai/knn-service",
indice_name="laion_400m",
num_images=num_images,
aesthetic_weight=0.1,
)
count=0
total=0
pbar=tqdm(desc="downloading real regularization images", total=num_class_images)
with (
open(f"{class_data_dir}/caption.txt", "w") asf1,
open(f"{class_data_dir}/urls.txt", "w") asf2,
open(f"{class_data_dir}/images.txt", "w") asf3,
):
whiletotal<num_class_images:
images=class_images[count]
count+=1
try:
img=requests.get(images["url"], timeout=30)
ifimg.status_code==200:
_=Image.open(BytesIO(img.content))
withopen(f"{class_data_dir}/images/{total}.jpg", "wb") asf:
f.write(img.content)
f1.write(images["caption"] +"\n")
f2.write(images["url"] +"\n")
f3.write(f"{class_data_dir}/images/{total}.jpg"+"\n")
total+=1
pbar.update(1)
else:
continue
exceptException:
continue
return
defparse_args():
parser=argparse.ArgumentParser("", add_help=False)
parser.add_argument("--class_prompt", help="text prompt to retrieve images", required=True, type=str)
parser.add_argument("--class_data_dir", help="path to save images", required=True, type=str)
parser.add_argument("--num_class_images", help="number of images to download", default=200, type=int)
returnparser.parse_args()
if__name__=="__main__":
args=parse_args()
retrieve(args.class_prompt, args.class_data_dir, args.num_class_images)