Instructions to use damerajee/GPT-Vision with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use damerajee/GPT-Vision with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import GPTVision model = GPTVision.from_pretrained("damerajee/GPT-Vision", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update modeling_gpt2vision.py
Browse files- modeling_gpt2vision.py +3 -4
modeling_gpt2vision.py
CHANGED
|
@@ -61,7 +61,6 @@ class GPT2Vision(PreTrainedModel):
|
|
| 61 |
if isinstance(text, str):
|
| 62 |
text = [text]
|
| 63 |
input_texts = [f"{IMAGE_TOKEN}{t}" for t in text]
|
| 64 |
-
print(input_texts)
|
| 65 |
text_inputs = self.tokenizer(
|
| 66 |
input_texts,
|
| 67 |
padding='max_length',
|
|
@@ -87,9 +86,9 @@ class GPT2Vision(PreTrainedModel):
|
|
| 87 |
attention_mask = batch['attention_mask'].to(self.device)
|
| 88 |
|
| 89 |
img_embs = self.vision_encoder(images, device=self.device)
|
| 90 |
-
print("img_embs",img_embs.
|
| 91 |
img_embs = self.mlp(img_embs)
|
| 92 |
-
print("img_embs",img_embs.
|
| 93 |
tok_embs = self.language_model.get_input_embeddings()(input_ids)
|
| 94 |
|
| 95 |
inputs_embeds = torch.cat((tok_embs[:, 0:1, :], img_embs, tok_embs[:, 1:, :]), dim=1)
|
|
@@ -110,7 +109,7 @@ class GPT2Vision(PreTrainedModel):
|
|
| 110 |
return outputs
|
| 111 |
|
| 112 |
def generate(self, question, image, max_new_tokens=30, **kwargs):
|
| 113 |
-
prompt = f"
|
| 114 |
batch = {"image": [image], "text": prompt}
|
| 115 |
encoded_batch = self.tokenize_encode(batch, self.device)
|
| 116 |
inputs_embeds, attention_mask = self.preprocess_inputs(encoded_batch)
|
|
|
|
| 61 |
if isinstance(text, str):
|
| 62 |
text = [text]
|
| 63 |
input_texts = [f"{IMAGE_TOKEN}{t}" for t in text]
|
|
|
|
| 64 |
text_inputs = self.tokenizer(
|
| 65 |
input_texts,
|
| 66 |
padding='max_length',
|
|
|
|
| 86 |
attention_mask = batch['attention_mask'].to(self.device)
|
| 87 |
|
| 88 |
img_embs = self.vision_encoder(images, device=self.device)
|
| 89 |
+
print("img_embs",img_embs.shape())
|
| 90 |
img_embs = self.mlp(img_embs)
|
| 91 |
+
print("img_embs",img_embs.shape())
|
| 92 |
tok_embs = self.language_model.get_input_embeddings()(input_ids)
|
| 93 |
|
| 94 |
inputs_embeds = torch.cat((tok_embs[:, 0:1, :], img_embs, tok_embs[:, 1:, :]), dim=1)
|
|
|
|
| 109 |
return outputs
|
| 110 |
|
| 111 |
def generate(self, question, image, max_new_tokens=30, **kwargs):
|
| 112 |
+
prompt = f"\n\nQuestion: {question}\n\nAnswer:"
|
| 113 |
batch = {"image": [image], "text": prompt}
|
| 114 |
encoded_batch = self.tokenize_encode(batch, self.device)
|
| 115 |
inputs_embeds, attention_mask = self.preprocess_inputs(encoded_batch)
|