sohojoe commited on
Commit
cdfc363
1 Parent(s): 99ea77b

refactor clip_app_client into an api class and clip_app_performance_test

Browse files
experimental/clip_app_client.py CHANGED
@@ -1,146 +1,100 @@
1
- # File name: graph_client.py
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).cpu()
42
- return prepro
43
-
44
- preprocessed_image = preprocess_image(test_image_url)
45
-
46
-
47
- def text_to_embedding(text):
48
- payload = {
49
- "text": ('str', text, 'application/octet-stream'),
50
- }
51
- url = os.environ.get("HTTP_ADDRESS", "http://127.0.0.1:8000/")
52
- response = requests.post(url, files=payload)
53
- embeddings = response.text
54
- return embeddings
55
-
56
- def image_url_to_embedding(image_url):
57
- payload = {
58
- "image_url": ('str', test_image_url, 'application/octet-stream'),
59
- }
60
- url = os.environ.get("HTTP_ADDRESS", "http://127.0.0.1:8000/")
61
- response = requests.post(url, files=payload)
62
- embeddings = response.text
63
- return embeddings
64
-
65
- def preprocessed_image_to_embedding(image):
66
- key = "preprocessed_image"
67
- data_bytes = image.numpy().tobytes()
68
- shape_bytes = np.array(image.shape).tobytes()
69
- dtype_bytes = str(image.dtype).encode()
70
- payload = {
71
- key: ('tensor', data_bytes, 'application/octet-stream'),
72
- 'shape': ('shape', shape_bytes, 'application/octet-stream'),
73
- 'dtype': ('dtype', dtype_bytes, 'application/octet-stream'),
74
- }
75
- url = os.environ.get("HTTP_ADDRESS", "http://127.0.0.1:8000/")
76
- response = requests.post(url, files=payload)
77
- embeddings = response.text
78
- return embeddings
79
-
80
- def _send_text_request(number):
81
- embeddings = text_to_embedding(english_text)
82
- return number, embeddings
83
-
84
- def _send_image_url_request(number):
85
- embeddings = image_url_to_embedding(test_image_url)
86
- return number, embeddings
87
-
88
- def _send_preprocessed_image_request(number):
89
- embeddings = preprocessed_image_to_embedding(preprocessed_image)
90
- return number, embeddings
91
-
92
- def process(numbers, send_func, max_workers=10):
93
- with ThreadPoolExecutor(max_workers=max_workers) as executor:
94
- futures = [executor.submit(send_func, number) for number in numbers]
95
- for future in as_completed(futures):
96
- n_result, result = future.result()
97
- result = json.loads(result)
98
- print (f"{n_result} : {len(result[0])}")
99
-
100
- # def process_text(numbers, max_workers=10):
101
- # for n in numbers:
102
- # n_result, result = send_text_request(n)
103
- # result = json.loads(result)
104
- # print (f"{n_result} : {len(result[0])}")
105
-
106
- if __name__ == "__main__":
107
- n_calls = 300
108
-
109
- # test text
110
- # n_calls = 1
111
- numbers = list(range(n_calls))
112
- start_time = time.monotonic()
113
- process(numbers, _send_text_request)
114
- end_time = time.monotonic()
115
- total_time = end_time - start_time
116
- avg_time_ms = total_time / n_calls * 1000
117
- calls_per_sec = n_calls / total_time
118
- print(f"Text...")
119
- print(f" Average time taken: {avg_time_ms:.2f} ms")
120
- print(f" Number of calls per second: {calls_per_sec:.2f}")
121
-
122
- # test image url
123
- # n_calls = 1
124
- numbers = list(range(n_calls))
125
- start_time = time.monotonic()
126
- process(numbers, _send_image_url_request)
127
- end_time = time.monotonic()
128
- total_time = end_time - start_time
129
- avg_time_ms = total_time / n_calls * 1000
130
- calls_per_sec = n_calls / total_time
131
- print(f"Image passing url...")
132
- print(f" Average time taken: {avg_time_ms:.2f} ms")
133
- print(f" Number of calls per second: {calls_per_sec:.2f}")
134
-
135
- # test image as vector
136
- # n_calls = 1
137
- numbers = list(range(n_calls))
138
- start_time = time.monotonic()
139
- process(numbers, _send_preprocessed_image_request)
140
- end_time = time.monotonic()
141
- total_time = end_time - start_time
142
- avg_time_ms = total_time / n_calls * 1000
143
- calls_per_sec = n_calls / total_time
144
- print(f"Preprocessed image...")
145
- print(f" Average time taken: {avg_time_ms:.2f} ms")
146
- print(f" Number of calls per second: {calls_per_sec:.2f}")
 
 
 
 
1
  import os
2
  import numpy as np
3
  import requests
4
  from concurrent.futures import ThreadPoolExecutor, as_completed
5
+ from PIL import Image
6
+ from io import BytesIO
7
  import torch
8
 
9
+ from clip_retrieval.load_clip import load_clip, get_tokenizer
10
+
11
+
12
+ class ClipAppClient:
13
+ """
14
+ A class to handle generating embeddings using the OpenAI CLIP model.
15
+
16
+ clip_embeddings = ClipEmbeddings()
17
+
18
+ test_image_url = "https://example.com/image.jpg"
19
+ preprocessed_image = clip_embeddings.preprocess_image(test_image_url)
20
+
21
+ text = "A beautiful landscape"
22
+ text_embeddings = clip_embeddings.text_to_embedding(text)
23
+
24
+ image_embeddings = clip_embeddings.image_url_to_embedding(test_image_url)
25
+
26
+ preprocessed_image_embeddings = clip_embeddings.preprocessed_image_to_embedding(preprocessed_image)
27
+ """
28
+
29
+ def __init__(self, clip_model="ViT-L/14", device=None):
30
+ self.clip_model = clip_model
31
+ self.device = device or ("cuda:0" if torch.cuda.is_available() else "cpu")
32
+ print("using device", self.device)
33
+ self.model, self.preprocess = load_clip(clip_model, use_jit=True, device=self.device)
34
+ self.tokenizer = get_tokenizer(clip_model)
35
+
36
+ def preprocess_image(self, image_url):
37
+ """
38
+ Preprocess an image from a given URL.
39
+
40
+ :param image_url: str, URL of the image to preprocess
41
+ :return: torch.Tensor, preprocessed image
42
+ """
43
+ response = requests.get(image_url)
44
+ input_image = Image.open(BytesIO(response.content)).convert('RGB')
45
+ input_image = np.array(input_image)
46
+ input_im = Image.fromarray(input_image)
47
+ prepro = self.preprocess(input_im).unsqueeze(0).cpu()
48
+ return prepro
49
+
50
+ def text_to_embedding(self, text):
51
+ """
52
+ Convert a given text to an embedding using the OpenAI CLIP model.
53
+
54
+ :param text: str, text to convert to an embedding
55
+ :return: str, text embeddings
56
+ """
57
+ payload = {
58
+ "text": ('str', text, 'application/octet-stream'),
59
+ }
60
+ url = os.environ.get("HTTP_ADDRESS", "http://127.0.0.1:8000/")
61
+ response = requests.post(url, files=payload)
62
+ embeddings = response.text
63
+ return embeddings
64
+
65
+ def image_url_to_embedding(self, image_url):
66
+ """
67
+ Convert an image URL to an embedding using the OpenAI CLIP model.
68
+
69
+ :param image_url: str, URL of the image to convert to an embedding
70
+ :return: str, image embeddings
71
+ """
72
+ payload = {
73
+ "image_url": ('str', image_url, 'application/octet-stream'),
74
+ }
75
+ url = os.environ.get("HTTP_ADDRESS", "http://127.0.0.1:8000/")
76
+ response = requests.post(url, files=payload)
77
+ embeddings = response.text
78
+ return embeddings
79
+
80
+ def preprocessed_image_to_embedding(self, image):
81
+ """
82
+ Convert a preprocessed image to an embedding using the OpenAI CLIP model.
83
+
84
+ :param image: torch.Tensor, preprocessed image
85
+ :return: str, image embeddings
86
+ """
87
+ key = "preprocessed_image"
88
+ data_bytes = image.numpy().tobytes()
89
+ shape_bytes = np.array(image.shape).tobytes()
90
+ dtype_bytes = str(image.dtype).encode()
91
+ payload = {
92
+ key: ('tensor', data_bytes, 'application/octet-stream'),
93
+ 'shape': ('shape', shape_bytes, 'application/octet-stream'),
94
+ 'dtype': ('dtype', dtype_bytes, 'application/octet-stream'),
95
+ }
96
+ url = os.environ.get("HTTP_ADDRESS", "http://127.0.0.1:8000/")
97
+ response = requests.post(url, files=payload)
98
+ embeddings = response.text
99
+ return embeddings
100
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
experimental/clip_app_performance_test.py ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from concurrent.futures import ThreadPoolExecutor, as_completed
2
+ import json
3
+ import os
4
+ import time
5
+
6
+ import numpy as np
7
+ import requests
8
+ import torch
9
+
10
+ from clip_app_client import ClipAppClient
11
+
12
+ 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"
13
+ english_text = (
14
+ "It was the best of times, it was the worst of times, it was the age "
15
+ "of wisdom, it was the age of foolishness, it was the epoch of belief"
16
+ )
17
+
18
+ app_client = ClipAppClient()
19
+ preprocessed_image = app_client.preprocess_image(test_image_url)
20
+
21
+ def _send_text_request(number):
22
+ embeddings = app_client.text_to_embedding(english_text)
23
+ return number, embeddings
24
+
25
+ def _send_image_url_request(number):
26
+ embeddings = app_client.image_url_to_embedding(test_image_url)
27
+ return number, embeddings
28
+
29
+ def _send_preprocessed_image_request(number):
30
+ embeddings = app_client.preprocessed_image_to_embedding(preprocessed_image)
31
+ return number, embeddings
32
+
33
+ def process(numbers, send_func, max_workers=10):
34
+ with ThreadPoolExecutor(max_workers=max_workers) as executor:
35
+ futures = [executor.submit(send_func, number) for number in numbers]
36
+ for future in as_completed(futures):
37
+ n_result, result = future.result()
38
+ result = json.loads(result)
39
+ print (f"{n_result} : {len(result[0])}")
40
+
41
+ if __name__ == "__main__":
42
+ n_calls = 300
43
+
44
+ # test text
45
+ numbers = list(range(n_calls))
46
+ start_time = time.monotonic()
47
+ process(numbers, _send_text_request)
48
+ end_time = time.monotonic()
49
+ total_time = end_time - start_time
50
+ avg_time_ms = total_time / n_calls * 1000
51
+ calls_per_sec = n_calls / total_time
52
+ print(f"Text...")
53
+ print(f" Average time taken: {avg_time_ms:.2f} ms")
54
+ print(f" Number of calls per second: {calls_per_sec:.2f}")
55
+
56
+ # test image url
57
+ numbers = list(range(n_calls))
58
+ start_time = time.monotonic()
59
+ process(numbers, _send_image_url_request)
60
+ end_time = time.monotonic()
61
+ total_time = end_time - start_time
62
+ avg_time_ms = total_time / n_calls * 1000
63
+ calls_per_sec = n_calls / total_time
64
+ print(f"Image passing url...")
65
+ print(f" Average time taken: {avg_time_ms:.2f} ms")
66
+ print(f" Number of calls per second: {calls_per_sec:.2f}")
67
+
68
+ # test image as vector
69
+ numbers = list(range(n_calls))
70
+ start_time = time.monotonic()
71
+ process(numbers, _send_preprocessed_image_request)
72
+ end_time = time.monotonic()
73
+ total_time = end_time - start_time
74
+ avg_time_ms = total_time / n_calls * 1000
75
+ calls_per_sec = n_calls / total_time
76
+ print(f"Preprocessed image...")
77
+ print(f" Average time taken: {avg_time_ms:.2f} ms")
78
+ print(f" Number of calls per second: {calls_per_sec:.2f}")
79
+
80
+
81
+ # from concurrent.futures import ThreadPoolExecutor
82
+ # import json
83
+ # import os
84
+ # import numpy as np
85
+ # import requests
86
+ # from concurrent.futures import ThreadPoolExecutor, as_completed
87
+ # import time
88
+
89
+ # import torch
90
+
91
+ # # hack for debugging, set HTTP_ADDRESS to "http://127.0.0.1:8000/"
92
+ # # os.environ["HTTP_ADDRESS"] = "http://192.168.7.79:8000"
93
+
94
+ # 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"
95
+ # english_text = (
96
+ # "It was the best of times, it was the worst of times, it was the age "
97
+ # "of wisdom, it was the age of foolishness, it was the epoch of belief"
98
+ # )
99
+
100
+ # preprocessed_image = preprocess_image(test_image_url)
101
+
102
+ # def _send_text_request(number):
103
+ # embeddings = text_to_embedding(english_text)
104
+ # return number, embeddings
105
+
106
+ # def _send_image_url_request(number):
107
+ # embeddings = image_url_to_embedding(test_image_url)
108
+ # return number, embeddings
109
+
110
+ # def _send_preprocessed_image_request(number):
111
+ # embeddings = preprocessed_image_to_embedding(preprocessed_image)
112
+ # return number, embeddings
113
+
114
+ # def process(numbers, send_func, max_workers=10):
115
+ # with ThreadPoolExecutor(max_workers=max_workers) as executor:
116
+ # futures = [executor.submit(send_func, number) for number in numbers]
117
+ # for future in as_completed(futures):
118
+ # n_result, result = future.result()
119
+ # result = json.loads(result)
120
+ # print (f"{n_result} : {len(result[0])}")
121
+
122
+ # # def process_text(numbers, max_workers=10):
123
+ # # for n in numbers:
124
+ # # n_result, result = send_text_request(n)
125
+ # # result = json.loads(result)
126
+ # # print (f"{n_result} : {len(result[0])}")
127
+
128
+ # if __name__ == "__main__":
129
+ # n_calls = 300
130
+
131
+ # # test text
132
+ # # n_calls = 1
133
+ # numbers = list(range(n_calls))
134
+ # start_time = time.monotonic()
135
+ # process(numbers, _send_text_request)
136
+ # end_time = time.monotonic()
137
+ # total_time = end_time - start_time
138
+ # avg_time_ms = total_time / n_calls * 1000
139
+ # calls_per_sec = n_calls / total_time
140
+ # print(f"Text...")
141
+ # print(f" Average time taken: {avg_time_ms:.2f} ms")
142
+ # print(f" Number of calls per second: {calls_per_sec:.2f}")
143
+
144
+ # # test image url
145
+ # # n_calls = 1
146
+ # numbers = list(range(n_calls))
147
+ # start_time = time.monotonic()
148
+ # process(numbers, _send_image_url_request)
149
+ # end_time = time.monotonic()
150
+ # total_time = end_time - start_time
151
+ # avg_time_ms = total_time / n_calls * 1000
152
+ # calls_per_sec = n_calls / total_time
153
+ # print(f"Image passing url...")
154
+ # print(f" Average time taken: {avg_time_ms:.2f} ms")
155
+ # print(f" Number of calls per second: {calls_per_sec:.2f}")
156
+
157
+ # # test image as vector
158
+ # # n_calls = 1
159
+ # numbers = list(range(n_calls))
160
+ # start_time = time.monotonic()
161
+ # process(numbers, _send_preprocessed_image_request)
162
+ # end_time = time.monotonic()
163
+ # total_time = end_time - start_time
164
+ # avg_time_ms = total_time / n_calls * 1000
165
+ # calls_per_sec = n_calls / total_time
166
+ # print(f"Preprocessed image...")
167
+ # print(f" Average time taken: {avg_time_ms:.2f} ms")
168
+ # print(f" Number of calls per second: {calls_per_sec:.2f}")