Toonies commited on
Commit
6ae99b6
·
1 Parent(s): 33a4812

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -21
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
- sample_idx= np.random.randint(0, len(imagenette)+1, 100).tolist()
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
- batch = processor(
41
- text = None,
42
- images = batch,
43
- return_tensors= 'pt',
44
- padding = True
45
- )['pixel_values'].to(device)
46
- batch_emb = model.get_image_features(pixel_values = batch)
47
- batch_emb = batch_emb.squeeze(0)
48
- batch_emb = batch_emb.cpu().detach().numpy()
49
- if image_arr is None:
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