Add an optional file to show multiple ways to use BridgeTower
Browse files- README.md +6 -6
- s2-train-data-into-multi-demension-vector.py +0 -43
README.md
CHANGED
|
@@ -99,9 +99,9 @@ _ = MultimodalLanceDB.from_text_image_pairs(
|
|
| 99 |
mode="overwrite",
|
| 100 |
)
|
| 101 |
```
|
| 102 |
-
# Gotchas
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
| 99 |
mode="overwrite",
|
| 100 |
)
|
| 101 |
```
|
| 102 |
+
# Gotchas and Solutions
|
| 103 |
+
Image Processing: When working with base64 encoded images, convert them to PIL.Image format before processing with BridgeTower
|
| 104 |
+
Model Selection: Using BridgeTowerForContrastiveLearning instead of PredictionGuard due to API access limitations
|
| 105 |
+
Model Size: BridgeTower model requires ~3.5GB download
|
| 106 |
+
Image Downloads: Some Flickr images may be unavailable; implement robust error handling
|
| 107 |
+
Token Decoding: BridgeTower contrastive learning model works with embeddings, not token predictions
|
s2-train-data-into-multi-demension-vector.py
CHANGED
|
@@ -61,49 +61,6 @@ def bt_embeddings_from_local(text, image):
|
|
| 61 |
}
|
| 62 |
|
| 63 |
|
| 64 |
-
def bt_scores_with_image_and_text_retrieval():
|
| 65 |
-
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
| 66 |
-
image = Image.open(requests.get(url, stream=True).raw)
|
| 67 |
-
texts = ["An image of two cats chilling on a couch", "A football player scoring a goal"]
|
| 68 |
-
|
| 69 |
-
processor = BridgeTowerProcessor.from_pretrained("BridgeTower/bridgetower-large-itm-mlm-gaudi")
|
| 70 |
-
model = BridgeTowerForImageAndTextRetrieval.from_pretrained("BridgeTower/bridgetower-large-itm-mlm-gaudi")
|
| 71 |
-
|
| 72 |
-
# forward pass
|
| 73 |
-
scores = dict()
|
| 74 |
-
for text in texts:
|
| 75 |
-
# prepare inputs
|
| 76 |
-
encoding = processor(image, text, return_tensors="pt")
|
| 77 |
-
outputs = model(**encoding)
|
| 78 |
-
scores[text] = outputs.logits[0,1].item()
|
| 79 |
-
return scores
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
def bt_with_masked_input():
|
| 83 |
-
url = "http://images.cocodataset.org/val2017/000000360943.jpg"
|
| 84 |
-
image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
|
| 85 |
-
text = "a <mask> looking out of the window"
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
processor = BridgeTowerProcessor.from_pretrained("BridgeTower/bridgetower-large-itm-mlm-gaudi")
|
| 89 |
-
model = BridgeTowerForMaskedLM.from_pretrained("BridgeTower/bridgetower-large-itm-mlm-gaudi")
|
| 90 |
-
|
| 91 |
-
# prepare inputs
|
| 92 |
-
encoding = processor(image, text, return_tensors="pt")
|
| 93 |
-
|
| 94 |
-
# forward pass
|
| 95 |
-
outputs = model(**encoding)
|
| 96 |
-
|
| 97 |
-
token_ids = outputs.logits.argmax(dim=-1).squeeze(0).tolist()
|
| 98 |
-
if isinstance(token_ids, list):
|
| 99 |
-
results = processor.tokenizer.decode(token_ids)
|
| 100 |
-
else:
|
| 101 |
-
results = processor.tokenizer.decode([token_ids])
|
| 102 |
-
|
| 103 |
-
print(results)
|
| 104 |
-
return results
|
| 105 |
-
#res = bt_embeddingsl()
|
| 106 |
-
#print((res['text_embeddings']))
|
| 107 |
for img in [img1, img2, img3]:
|
| 108 |
embeddings = bt_embeddings_from_local(img['caption'], Image.open(img['image_path']))
|
| 109 |
print(embeddings['cross_modal_embeddings'][0].shape)
|
|
|
|
| 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)
|