Commit
·
2ecc0b9
1
Parent(s):
acc5fea
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,42 @@
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
| 3 |
---
|
| 4 |
+
|
| 5 |
+
# OFA-base
|
| 6 |
+
This is the **base** version of OFA pretrained model. OFA is a unified multimodal pretrained model that unifies modalities (i.e., cross-modality, vision, language) and tasks (e.g., image generation, visual grounding, image captioning, image classification, text generation, etc.) to a simple sequence-to-sequence learning framework.
|
| 7 |
+
|
| 8 |
+
The directory includes 4 files, namely `config.json` which consists of model configuration, `vocab.json` and `merge.txt` for our OFA tokenizer, and lastly `pytorch_model.bin` which consists of model weights. There is no need to worry about the mismatch between Fairseq and transformers, since we have addressed the issue yet.
|
| 9 |
+
|
| 10 |
+
To use it in transformers, please refer to https://github.com/OFA-Sys/OFA/tree/feature/add_transformers. Install the transformers and download the models as shown below.
|
| 11 |
+
```
|
| 12 |
+
git clone --single-branch --branch feature/add_transformers https://github.com/OFA-Sys/OFA.git
|
| 13 |
+
pip install OFA/transformers/
|
| 14 |
+
it clone https://huggingface.co/OFA-Sys/OFA-base
|
| 15 |
+
```
|
| 16 |
+
After, refer the path to OFA-base to `ckpt_dir`, and prepare an image for the testing example below. Also, ensure that you have pillow and torchvision in your environment.
|
| 17 |
+
|
| 18 |
+
```
|
| 19 |
+
>>> from PIL import Image
|
| 20 |
+
>>> from torchvision import transforms
|
| 21 |
+
>>> from transformers import OFATokenizer, OFAForConditionalGeneration
|
| 22 |
+
|
| 23 |
+
>>> mean, std = [0.5, 0.5, 0.5], [0.5, 0.5, 0.5]
|
| 24 |
+
>>> resolution = 256
|
| 25 |
+
>>> patch_resize_transform = transforms.Compose([
|
| 26 |
+
lambda image: image.convert("RGB"),
|
| 27 |
+
transforms.Resize((resolution, resolution), interpolation=Image.BICUBIC),
|
| 28 |
+
transforms.ToTensor(),
|
| 29 |
+
transforms.Normalize(mean=mean, std=std)
|
| 30 |
+
])
|
| 31 |
+
|
| 32 |
+
>>> model = OFAForConditionalGeneration.from_pretrained(ckpt_dir)
|
| 33 |
+
>>> tokenizer = OFATokenizer.from_pretrained(ckpt_dir)
|
| 34 |
+
|
| 35 |
+
>>> txt = " what is the description of the image?"
|
| 36 |
+
>>> inputs = tokenizer([txt], max_length=1024, return_tensors="pt")["input_ids"]
|
| 37 |
+
>>> img = Image.open(path_to_image)
|
| 38 |
+
>>> patch_img = patch_resize_transform(img).unsqueeze(0)
|
| 39 |
+
|
| 40 |
+
>>> gen = model.generate(inputs, patch_images=patch_img, num_beams=4)
|
| 41 |
+
>>> print(tokenizer.batch_decode(gen, skip_special_tokens=True))
|
| 42 |
+
```
|