forked from NVIDIA-AI-IOT/clip-distillation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_clip_images.py
274 lines (218 loc) · 8.3 KB
/
search_clip_images.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
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
# SPDX-FileCopyrightText: Copyright (c) <year> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# 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.
import argparse
import asyncio
import requests
from typing import Optional, List, Optional
from functools import partial
import tqdm.asyncio
import requests
import os
import requests.exceptions
import mimetypes
import glob
import uuid
from typing import Optional
async def clip_search_images_by_text(
text: str,
service_url: str = "https://knn.laion.ai/knn-service",
num_images: int = 40,
indice_name: str = "laion5B-L-14",
num_result_ids: int = 3000,
use_mclip: bool = False,
deduplicate: bool = True,
use_safety_model: bool = True,
use_violence_detector: bool = True,
aesthetic_score: Optional[float] = None,
aesthetic_weight: float = 0.5
):
loop = asyncio.get_event_loop()
response = await loop.run_in_executor(
None,
partial(
requests.post,
service_url,
json={
"text": text,
"image": None,
"image_url": None,
"embedding_input": None,
"modality": "image",
"num_images": num_images,
"indice_name": indice_name,
"num_result_ids": num_result_ids,
"use_mclip": use_mclip,
"deduplicate": deduplicate,
"use_safety_model": use_safety_model,
"use_violence_detector": use_violence_detector,
"aesthetic_score": "" if aesthetic_score is None else str(aesthetic_score),
"aesthetic_weight": str(aesthetic_weight)
}
)
)
urls = [
item['url'] for item in response.json() if 'url' in item
]
return urls
async def clip_search_images_by_multi_text(
texts: List[str],
service_url: str = "https://knn.laion.ai/knn-service",
num_images: int = 40,
indice_name: str = "laion5B-L-14",
num_result_ids: int = 3000,
use_mclip: bool = False,
deduplicate: bool = True,
use_safety_model: bool = True,
use_violence_detector: bool = True,
aesthetic_score: Optional[float] = None,
aesthetic_weight: float = 0.5,
max_workers: int = 1
):
# executor = ThreadPoolExecutor(max_workers=num_workers)
semaphore = asyncio.Semaphore(max_workers)
async def safe_coro(coro):
async with semaphore:
return await coro
coros = []
for text in texts:
coros.append(
safe_coro(
clip_search_images_by_text(
text=text,
service_url=service_url,
num_images=num_images,
indice_name=indice_name,
num_result_ids=num_result_ids,
use_mclip=use_mclip,
deduplicate=deduplicate,
use_safety_model=use_safety_model,
use_violence_detector=use_violence_detector,
aesthetic_score=aesthetic_score,
aesthetic_weight=aesthetic_weight
)
)
)
results = await asyncio.gather(*coros)
return sum(results, [])
def url_to_image_id(url: str):
return str(uuid.uuid5(uuid.NAMESPACE_URL, url))
def find_files_matching_image_id(folder: str, id: str):
return glob.glob(os.path.join(folder, id + ".*"))
def image_with_id_exists(folder: str, id: str):
return len(find_files_matching_image_id(folder, id)) > 0
def download_image(url: str, output_folder: str, timeout = None, verify=True):
image_id = str(uuid.uuid5(uuid.NAMESPACE_URL, url))
response = requests.get(url, timeout=timeout, verify=verify)
content_type = response.headers['content-type']
extension = mimetypes.guess_extension(content_type)
if extension is None:
raise RuntimeError("Extension type could not be determined.")
if extension not in [".jpg", ".png"]:
raise RuntimeError("Invalid image extension.")
filename = image_id + extension
full_path = os.path.join(output_folder, filename)
with open(full_path, 'wb') as f:
f.write(response.content)
return filename
async def download_image_async(url: str, output_folder: str, timeout=None, verify=True):
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(
None,
partial(
download_image,
url,
output_folder,
timeout,
verify
)
)
return result
async def download_many_images_async(
urls: List[str],
output_folder: str,
max_workers: int = 1,
timeout=None,
verify=True
):
skipped_urls = set()
urls_to_download = set()
for url in urls:
if not image_with_id_exists(output_folder, url_to_image_id(url)):
urls_to_download.add(url)
else:
skipped_urls.add(url)
semaphore = asyncio.Semaphore(max_workers)
async def safe_coro(coro, url):
async with semaphore:
try:
result = await coro
return True, result, url
except BaseException as error:
print(url, error)
return False, "", url
tasks = [
asyncio.create_task(safe_coro(download_image_async(url, output_folder, timeout, verify), url))
for url in urls_to_download
]
for f in tqdm.asyncio.tqdm.as_completed(tasks):
await f
failed_urls = set()
downloaded_urls = set()
for task in tasks:
success, filename, url = task.result()
if success:
downloaded_urls.add(url)
else:
failed_urls.add(url)
return downloaded_urls, failed_urls, skipped_urls
def parse_multiple_input(values):
all_values = []
for val in values:
for inner_val in val:
all_values.append(inner_val)
return all_values
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("text_prompts_file", type=str)
parser.add_argument("output_file", type=str, help="Output filepath to store URLS returned from query")
parser.add_argument("-n", "--num_images", type=int, default=50, help="Maximum number of images to download")
parser.add_argument("-m", "--num_result_ids", type=int, default=3000, help="Maximum number of result IDs. Clip retrieval demo uses 3000, set this larger than num images.")
parser.add_argument("--max_workers", type=int, default=16, help="Maximum number of workers to use for multi-prompt queries.")
parser.add_argument("-a", "--append", action="store_true", help="Add urls to existing file")
args = parser.parse_args()
with open(args.text_prompts_file, 'r') as f:
text_prompts = f.readlines()
text_prompts = [tp.strip() for tp in text_prompts]
print(f"Found the following {len(text_prompts)} text prompts in {args.text_prompts_file}")
print(text_prompts)
if os.path.exists(args.output_file) and not args.append:
raise RuntimeError("Output file already exists. If you want to add URLS to it, set the --append flag.")
print("Querying images with the following prompts...")
for text in text_prompts:
print(text)
urls = asyncio.run(
clip_search_images_by_multi_text(
texts=text_prompts,
num_images=args.num_images,
num_result_ids=args.num_result_ids,
max_workers=args.max_workers
)
)
urls = set(urls)
print(f"Found {len(urls)} images related to the above prompts.")
print(f"Writing urls to {args.output_file}")
with open(args.output_file, 'a') as f:
for url in urls:
f.write(f"{url}\n")