get image working
Browse files- experimental/clip_app.py +3 -2
- experimental/clip_app_client.py +83 -8
experimental/clip_app.py
CHANGED
|
@@ -54,12 +54,13 @@ class CLIPTransform:
|
|
| 54 |
if "text" in request:
|
| 55 |
prompt = request["text"]
|
| 56 |
embeddings = self.text_to_embeddings(prompt)
|
| 57 |
-
elif "
|
| 58 |
image_url = request["image_url"]
|
| 59 |
# download image from url
|
| 60 |
import requests
|
| 61 |
from io import BytesIO
|
| 62 |
-
|
|
|
|
| 63 |
input_image = input_image.convert('RGB')
|
| 64 |
input_image = np.array(input_image)
|
| 65 |
embeddings = self.image_to_embeddings(input_image)
|
|
|
|
| 54 |
if "text" in request:
|
| 55 |
prompt = request["text"]
|
| 56 |
embeddings = self.text_to_embeddings(prompt)
|
| 57 |
+
elif "image_url" in request:
|
| 58 |
image_url = request["image_url"]
|
| 59 |
# download image from url
|
| 60 |
import requests
|
| 61 |
from io import BytesIO
|
| 62 |
+
image_bytes = requests.get(image_url).content
|
| 63 |
+
input_image = Image.open(BytesIO(image_bytes))
|
| 64 |
input_image = input_image.convert('RGB')
|
| 65 |
input_image = np.array(input_image)
|
| 66 |
embeddings = self.image_to_embeddings(input_image)
|
experimental/clip_app_client.py
CHANGED
|
@@ -2,27 +2,72 @@
|
|
| 2 |
from concurrent.futures import ThreadPoolExecutor
|
| 3 |
import json
|
| 4 |
import os
|
|
|
|
| 5 |
import requests
|
| 6 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 7 |
import time
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
test_image_url = "https://static.wixstatic.com/media/4d6b49_42b9435ce1104008b1b5f7a3c9bfcd69~mv2.jpg/v1/fill/w_454,h_333,fp_0.50_0.50,q_90/4d6b49_42b9435ce1104008b1b5f7a3c9bfcd69~mv2.jpg"
|
| 10 |
english_text = (
|
| 11 |
"It was the best of times, it was the worst of times, it was the age "
|
| 12 |
"of wisdom, it was the age of foolishness, it was the epoch of belief"
|
| 13 |
)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def send_text_request(number):
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
url = os.environ.get("HTTP_ADDRESS", "http://127.0.0.1:8000/")
|
| 19 |
-
response = requests.post(url, json=
|
| 20 |
embeddings = response.text
|
| 21 |
return number, embeddings
|
| 22 |
|
| 23 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
| 25 |
-
futures = [executor.submit(
|
| 26 |
for future in as_completed(futures):
|
| 27 |
n_result, result = future.result()
|
| 28 |
result = json.loads(result)
|
|
@@ -35,15 +80,45 @@ def process_text(numbers, max_workers=10):
|
|
| 35 |
# print (f"{n_result} : {len(result[0])}")
|
| 36 |
|
| 37 |
if __name__ == "__main__":
|
| 38 |
-
# n_calls = 100000
|
| 39 |
n_calls = 10000
|
|
|
|
|
|
|
|
|
|
| 40 |
numbers = list(range(n_calls))
|
| 41 |
start_time = time.monotonic()
|
| 42 |
-
|
| 43 |
end_time = time.monotonic()
|
| 44 |
total_time = end_time - start_time
|
| 45 |
avg_time_ms = total_time / n_calls * 1000
|
| 46 |
calls_per_sec = n_calls / total_time
|
| 47 |
-
print(f"
|
| 48 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from concurrent.futures import ThreadPoolExecutor
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
+
import numpy as np
|
| 6 |
import requests
|
| 7 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 8 |
import time
|
| 9 |
|
| 10 |
+
import torch
|
| 11 |
+
|
| 12 |
+
# hack for debugging, set HTTP_ADDRESS to "http://127.0.0.1:8000/"
|
| 13 |
+
# os.environ["HTTP_ADDRESS"] = "http://192.168.7.79:8000"
|
| 14 |
+
|
| 15 |
test_image_url = "https://static.wixstatic.com/media/4d6b49_42b9435ce1104008b1b5f7a3c9bfcd69~mv2.jpg/v1/fill/w_454,h_333,fp_0.50_0.50,q_90/4d6b49_42b9435ce1104008b1b5f7a3c9bfcd69~mv2.jpg"
|
| 16 |
english_text = (
|
| 17 |
"It was the best of times, it was the worst of times, it was the age "
|
| 18 |
"of wisdom, it was the age of foolishness, it was the epoch of belief"
|
| 19 |
)
|
| 20 |
|
| 21 |
+
clip_model="ViT-L/14"
|
| 22 |
+
clip_model_id ="laion5B-L-14"
|
| 23 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 24 |
+
print ("using device", device)
|
| 25 |
+
from clip_retrieval.load_clip import load_clip, get_tokenizer
|
| 26 |
+
# from clip_retrieval.clip_client import ClipClient, Modality
|
| 27 |
+
model, preprocess = load_clip(clip_model, use_jit=True, device=device)
|
| 28 |
+
tokenizer = get_tokenizer(clip_model)
|
| 29 |
+
|
| 30 |
+
def preprocess_image(image_url):
|
| 31 |
+
# download image from url
|
| 32 |
+
import requests
|
| 33 |
+
from PIL import Image
|
| 34 |
+
from io import BytesIO
|
| 35 |
+
response = requests.get(test_image_url)
|
| 36 |
+
input_image = Image.open(BytesIO(response.content))
|
| 37 |
+
input_image = input_image.convert('RGB')
|
| 38 |
+
# convert image to numpy array
|
| 39 |
+
input_image = np.array(input_image)
|
| 40 |
+
input_im = Image.fromarray(input_image)
|
| 41 |
+
prepro = preprocess(input_im).unsqueeze(0).to(device)
|
| 42 |
+
return prepro
|
| 43 |
+
|
| 44 |
+
preprocessed_image = preprocess_image(test_image_url)
|
| 45 |
|
| 46 |
def send_text_request(number):
|
| 47 |
+
data = {"text": english_text}
|
| 48 |
+
url = os.environ.get("HTTP_ADDRESS", "http://127.0.0.1:8000/")
|
| 49 |
+
response = requests.post(url, json=data)
|
| 50 |
+
embeddings = response.text
|
| 51 |
+
return number, embeddings
|
| 52 |
+
|
| 53 |
+
def send_image_url_request(number):
|
| 54 |
+
data = {"image_url": test_image_url}
|
| 55 |
url = os.environ.get("HTTP_ADDRESS", "http://127.0.0.1:8000/")
|
| 56 |
+
response = requests.post(url, json=data)
|
| 57 |
embeddings = response.text
|
| 58 |
return number, embeddings
|
| 59 |
|
| 60 |
+
def send_preprocessed_image_request(number):
|
| 61 |
+
nested_list = preprocessed_image.tolist()
|
| 62 |
+
data = {"preprocessed_image": nested_list}
|
| 63 |
+
url = os.environ.get("HTTP_ADDRESS", "http://127.0.0.1:8000/")
|
| 64 |
+
response = requests.post(url, json=data)
|
| 65 |
+
embeddings = response.text
|
| 66 |
+
return number, embeddings
|
| 67 |
+
|
| 68 |
+
def process(numbers, send_func, max_workers=10):
|
| 69 |
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
| 70 |
+
futures = [executor.submit(send_func, number) for number in numbers]
|
| 71 |
for future in as_completed(futures):
|
| 72 |
n_result, result = future.result()
|
| 73 |
result = json.loads(result)
|
|
|
|
| 80 |
# print (f"{n_result} : {len(result[0])}")
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
|
|
|
| 83 |
n_calls = 10000
|
| 84 |
+
|
| 85 |
+
# test text
|
| 86 |
+
# n_calls = 1
|
| 87 |
numbers = list(range(n_calls))
|
| 88 |
start_time = time.monotonic()
|
| 89 |
+
process(numbers, send_text_request)
|
| 90 |
end_time = time.monotonic()
|
| 91 |
total_time = end_time - start_time
|
| 92 |
avg_time_ms = total_time / n_calls * 1000
|
| 93 |
calls_per_sec = n_calls / total_time
|
| 94 |
+
print(f"Text...")
|
| 95 |
+
print(f" Average time taken: {avg_time_ms:.2f} ms")
|
| 96 |
+
print(f" Number of calls per second: {calls_per_sec:.2f}")
|
| 97 |
+
|
| 98 |
+
n_calls = 100
|
| 99 |
|
| 100 |
+
# test image url
|
| 101 |
+
# n_calls = 1
|
| 102 |
+
numbers = list(range(n_calls))
|
| 103 |
+
start_time = time.monotonic()
|
| 104 |
+
process(numbers, send_image_url_request)
|
| 105 |
+
end_time = time.monotonic()
|
| 106 |
+
total_time = end_time - start_time
|
| 107 |
+
avg_time_ms = total_time / n_calls * 1000
|
| 108 |
+
calls_per_sec = n_calls / total_time
|
| 109 |
+
print(f"Image passing url...")
|
| 110 |
+
print(f" Average time taken: {avg_time_ms:.2f} ms")
|
| 111 |
+
print(f" Number of calls per second: {calls_per_sec:.2f}")
|
| 112 |
+
|
| 113 |
+
# test image as vector
|
| 114 |
+
# n_calls = 1
|
| 115 |
+
numbers = list(range(n_calls))
|
| 116 |
+
start_time = time.monotonic()
|
| 117 |
+
process(numbers, send_preprocessed_image_request)
|
| 118 |
+
end_time = time.monotonic()
|
| 119 |
+
total_time = end_time - start_time
|
| 120 |
+
avg_time_ms = total_time / n_calls * 1000
|
| 121 |
+
calls_per_sec = n_calls / total_time
|
| 122 |
+
print(f"Text...")
|
| 123 |
+
print(f" Average time taken: {avg_time_ms:.2f} ms")
|
| 124 |
+
print(f" Number of calls per second: {calls_per_sec:.2f}")
|