File size: 3,066 Bytes
0b8f387
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time
import numpy as np
import torch

num_steps = 1000
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"
clip_model="ViT-L/14"
clip_model_id ="laion5B-L-14"

device = "cuda:0" if torch.cuda.is_available() else "cpu"
print ("using device", device)

from clip_retrieval.load_clip import load_clip, get_tokenizer 
# from clip_retrieval.clip_client import ClipClient, Modality
model, preprocess = load_clip(clip_model, use_jit=True, device=device)
tokenizer = get_tokenizer(clip_model)

def test_text(prompt):
    text = tokenizer([prompt]).to(device)
    with torch.no_grad():
        prompt_embededdings = model.encode_text(text)
    prompt_embededdings /= prompt_embededdings.norm(dim=-1, keepdim=True)
    return(prompt_embededdings)

def test_image(input_im):
    input_im = Image.fromarray(input_im)
    prepro = preprocess(input_im).unsqueeze(0).to(device)
    with torch.no_grad():
        image_embeddings = model.encode_image(prepro)
    image_embeddings /= image_embeddings.norm(dim=-1, keepdim=True)
    return(image_embeddings)

def test_preprocessed_image(prepro):
    with torch.no_grad():
        image_embeddings = model.encode_image(prepro)
    image_embeddings /= image_embeddings.norm(dim=-1, keepdim=True)
    return(image_embeddings)


# performance test for text
start = time.time()
for i in range(num_steps):
    test_text("todo")
end = time.time()
average_time_seconds = (end - start) / num_steps
average_time_seconds = average_time_seconds if average_time_seconds > 0 else 0.0000001
print("Average time for text: ", average_time_seconds, "s")
print("Average time for text: ", average_time_seconds * 1000, "ms")
print("Number of predictions per second for text: ", 1 / average_time_seconds)

# download image from url
import requests
from PIL import Image
from io import BytesIO
response = requests.get(test_image_url)
input_image = Image.open(BytesIO(response.content))
input_image = input_image.convert('RGB')
# convert image to numpy array
input_image = np.array(input_image)

# performance test for image
start = time.time()
for i in range(num_steps):
    test_image(input_image)
end = time.time()
average_time_seconds = (end - start) / num_steps
print("Average time for image: ", average_time_seconds, "s")
print("Average time for image: ", average_time_seconds * 1000, "ms")
print("Number of predictions per second for image: ", 1 / average_time_seconds)

# performance test for preprocessed image
input_im = Image.fromarray(input_image)
prepro = preprocess(input_im).unsqueeze(0).to(device)

start = time.time()
for i in range(num_steps):
    test_preprocessed_image(prepro)
end = time.time()
average_time_seconds = (end - start) / num_steps
print("Average time for preprocessed image: ", average_time_seconds, "s")
print("Average time for preprocessed image: ", average_time_seconds * 1000, "ms")
print("Number of predictions per second for preprocessed image: ", 1 / average_time_seconds)