Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from transformers import CLIPTokenizerFast, CLIPProcessor, CLIPModel
|
|
| 4 |
import torch
|
| 5 |
from tqdm.auto import tqdm
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
|
| 8 |
device = 'cpu' # 'cuda' if torch.cuda.is_available() else "cpu"
|
| 9 |
model_id = 'openai/clip-vit-base-patch32'
|
|
@@ -30,31 +31,22 @@ def embedding_input(text_input):
|
|
| 30 |
|
| 31 |
def embedding_img():
|
| 32 |
global images
|
| 33 |
-
|
| 34 |
-
images = [imagenette[i]['image'] for i in sample_idx]
|
| 35 |
-
batch_sie = 5
|
| 36 |
-
image_arr = None
|
| 37 |
-
for i in tqdm(range(0, len(images), batch_sie)):
|
| 38 |
-
batch = images[i:i+batch_sie]
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
image_arr = batch_emb
|
| 51 |
-
|
| 52 |
-
else:
|
| 53 |
-
image_arr = np.concatenate((image_arr, batch_emb), axis = 0)
|
| 54 |
-
return image_arr
|
| 55 |
|
| 56 |
def norm_val(text_input):
|
| 57 |
image_arr = embedding_img()
|
|
|
|
| 58 |
text_emb = embedding_input(text_input)
|
| 59 |
|
| 60 |
image_arr = (image_arr.T / np.linalg.norm(image_arr, axis = 1)).T
|
|
|
|
| 4 |
import torch
|
| 5 |
from tqdm.auto import tqdm
|
| 6 |
import numpy as np
|
| 7 |
+
import time
|
| 8 |
|
| 9 |
device = 'cpu' # 'cuda' if torch.cuda.is_available() else "cpu"
|
| 10 |
model_id = 'openai/clip-vit-base-patch32'
|
|
|
|
| 31 |
|
| 32 |
def embedding_img():
|
| 33 |
global images
|
| 34 |
+
img_batch = imagenette['image']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
images = processor(
|
| 37 |
+
text = None,
|
| 38 |
+
images = img_batch,
|
| 39 |
+
return_tensors = 'pt'
|
| 40 |
+
)['pixel_values'].to(device)
|
| 41 |
+
batch_emb = model.get_image_features(pixel_values =img_batch)
|
| 42 |
+
batch_emb = batch_emb.squeeze(0)
|
| 43 |
+
image_arr = batch_emb.cpu().detach().numpy()
|
| 44 |
+
|
| 45 |
+
return image_arr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
def norm_val(text_input):
|
| 48 |
image_arr = embedding_img()
|
| 49 |
+
time.sleep(5)
|
| 50 |
text_emb = embedding_input(text_input)
|
| 51 |
|
| 52 |
image_arr = (image_arr.T / np.linalg.norm(image_arr, axis = 1)).T
|