88hours commited on
Commit
643eb75
·
1 Parent(s): f7828c6

Save Embeddings in a file to save process every time

Browse files
s2-download-data.py CHANGED
@@ -1,5 +1,6 @@
1
  import requests
2
  from PIL import Image
 
3
  # You can use your own uploaded images and captions.
4
  # You will be responsible for the legal use of images that
5
  # you are going to use.
@@ -42,6 +43,8 @@ def download_images():
42
  for img in [img1, img2, img3]:
43
  image = Image.open(img['image_path'])
44
  caption = img['caption']
45
- print(image)
46
  print(caption)
 
 
47
 
 
1
  import requests
2
  from PIL import Image
3
+ from IPython.display import display
4
  # You can use your own uploaded images and captions.
5
  # You will be responsible for the legal use of images that
6
  # you are going to use.
 
43
  for img in [img1, img2, img3]:
44
  image = Image.open(img['image_path'])
45
  caption = img['caption']
46
+ display(image)
47
  print(caption)
48
+
49
+ download_images()
50
 
s2-train-data-into-multi-demension-vector.py CHANGED
@@ -1,18 +1,9 @@
1
- import json
2
- import os
3
  import numpy as np
4
  from numpy.linalg import norm
5
- import cv2
6
- from io import StringIO, BytesIO
7
- from umap import UMAP
8
- from sklearn.preprocessing import MinMaxScaler
9
- import pandas as pd
10
- from tqdm import tqdm
11
- import base64
12
- from transformers import BridgeTowerProcessor, BridgeTowerForContrastiveLearning, BridgeTowerForImageAndTextRetrieval, BridgeTowerForMaskedLM
13
- import requests
14
- from PIL import Image
15
  import torch
 
 
16
 
17
  url1='http://farm3.staticflickr.com/2519/4126738647_cc436c111b_z.jpg'
18
  cap1='A motorcycle sits parked across from a herd of livestock'
@@ -26,19 +17,22 @@ cap3='a cat laying down stretched out near a laptop'
26
  img1 = {
27
  'flickr_url': url1,
28
  'caption': cap1,
29
- 'image_path' : './shared_data/motorcycle_1.jpg'
 
30
  }
31
 
32
  img2 = {
33
  'flickr_url': url2,
34
  'caption': cap2,
35
- 'image_path' : './shared_data/motorcycle_2.jpg'
 
36
  }
37
 
38
  img3 = {
39
  'flickr_url' : url3,
40
  'caption': cap3,
41
- 'image_path' : './shared_data/cat_1.jpg'
 
42
  }
43
 
44
  def bt_embeddings_from_local(text, image):
@@ -48,7 +42,6 @@ def bt_embeddings_from_local(text, image):
48
 
49
  processed_inputs = processor(image, text, padding=True, return_tensors="pt")
50
 
51
- #inputs = processor(prompt, base64_image, padding=True, return_tensors="pt")
52
  outputs = model(**processed_inputs)
53
 
54
  cross_modal_embeddings = outputs.cross_embeds
@@ -59,8 +52,16 @@ def bt_embeddings_from_local(text, image):
59
  'text_embeddings': text_embeddings,
60
  'image_embeddings': image_embeddings
61
  }
62
-
63
 
64
- for img in [img1, img2, img3]:
65
- embeddings = bt_embeddings_from_local(img['caption'], Image.open(img['image_path']))
66
- print(embeddings['cross_modal_embeddings'][0].shape)
 
 
 
 
 
 
 
 
 
 
 
 
1
  import numpy as np
2
  from numpy.linalg import norm
3
+ from transformers import BridgeTowerProcessor, BridgeTowerForContrastiveLearning
 
 
 
 
 
 
 
 
 
4
  import torch
5
+ from PIL import Image
6
+
7
 
8
  url1='http://farm3.staticflickr.com/2519/4126738647_cc436c111b_z.jpg'
9
  cap1='A motorcycle sits parked across from a herd of livestock'
 
17
  img1 = {
18
  'flickr_url': url1,
19
  'caption': cap1,
20
+ 'image_path' : './shared_data/motorcycle_1.jpg',
21
+ 'tensor_path' : './shared_data/motorcycle_1'
22
  }
23
 
24
  img2 = {
25
  'flickr_url': url2,
26
  'caption': cap2,
27
+ 'image_path' : './shared_data/motorcycle_2.jpg',
28
+ 'tensor_path' : './shared_data/motorcycle_2'
29
  }
30
 
31
  img3 = {
32
  'flickr_url' : url3,
33
  'caption': cap3,
34
+ 'image_path' : './shared_data/cat_1.jpg',
35
+ 'tensor_path' : './shared_data/cat_1'
36
  }
37
 
38
  def bt_embeddings_from_local(text, image):
 
42
 
43
  processed_inputs = processor(image, text, padding=True, return_tensors="pt")
44
 
 
45
  outputs = model(**processed_inputs)
46
 
47
  cross_modal_embeddings = outputs.cross_embeds
 
52
  'text_embeddings': text_embeddings,
53
  'image_embeddings': image_embeddings
54
  }
 
55
 
56
+ def cosine_similarity(vec1, vec2):
57
+ similarity = np.dot(vec1,vec2)/(norm(vec1)*norm(vec2))
58
+ return similarity
59
+
60
+
61
+ def save_embeddings():
62
+ for img in [img1, img2, img3]:
63
+ embedding = bt_embeddings_from_local(img['caption'], Image.open(img['image_path']))
64
+ print(embedding['cross_modal_embeddings'][0].shape) #<class 'torch.Tensor'>
65
+ torch.save(embedding['cross_modal_embeddings'][0], img['tensor_path'] + '.pt')
66
+
67
+ save_embeddings()