Commit
·
c1aa9ef
1
Parent(s):
c4bac4c
Update README.md
Browse files
README.md
CHANGED
|
@@ -15,10 +15,12 @@ git 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,
|
|
|
|
| 22 |
|
| 23 |
>>> mean, std = [0.5, 0.5, 0.5], [0.5, 0.5, 0.5]
|
| 24 |
>>> resolution = 384
|
|
@@ -29,14 +31,29 @@ After, refer the path to OFA-base to `ckpt_dir`, and prepare an image for the te
|
|
| 29 |
transforms.Normalize(mean=mean, std=std)
|
| 30 |
])
|
| 31 |
|
| 32 |
-
|
| 33 |
>>> tokenizer = OFATokenizer.from_pretrained(ckpt_dir)
|
| 34 |
|
| 35 |
-
>>> txt = " what
|
| 36 |
-
>>> inputs = tokenizer([txt],
|
| 37 |
>>> img = Image.open(path_to_image)
|
| 38 |
>>> patch_img = patch_resize_transform(img).unsqueeze(0)
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
>>> print(tokenizer.batch_decode(gen, skip_special_tokens=True))
|
| 42 |
```
|
|
|
|
| 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 |
```
|
| 20 |
>>> from PIL import Image
|
| 21 |
>>> from torchvision import transforms
|
| 22 |
+
>>> from transformers import OFATokenizer, OFAModel
|
| 23 |
+
>>> from generate import sequence_generator
|
| 24 |
|
| 25 |
>>> mean, std = [0.5, 0.5, 0.5], [0.5, 0.5, 0.5]
|
| 26 |
>>> resolution = 384
|
|
|
|
| 31 |
transforms.Normalize(mean=mean, std=std)
|
| 32 |
])
|
| 33 |
|
| 34 |
+
|
| 35 |
>>> tokenizer = OFATokenizer.from_pretrained(ckpt_dir)
|
| 36 |
|
| 37 |
+
>>> txt = " what does the image describe?"
|
| 38 |
+
>>> inputs = tokenizer([txt], return_tensors="pt").input_ids
|
| 39 |
>>> img = Image.open(path_to_image)
|
| 40 |
>>> patch_img = patch_resize_transform(img).unsqueeze(0)
|
| 41 |
|
| 42 |
+
|
| 43 |
+
>>> # using the generator of fairseq version
|
| 44 |
+
>>> model = OFAModel.from_pretrained(ckpt_dir, use_cache=True)
|
| 45 |
+
>>> generator = sequence_generator.SequenceGenerator(tokenizer=tokenizer,beam_size=5,
|
| 46 |
+
max_len_b=16,
|
| 47 |
+
min_len=0,
|
| 48 |
+
no_repeat_ngram_size=3) # using the generator of fairseq version
|
| 49 |
+
>>> data = {}
|
| 50 |
+
>>> data["net_input"] = {"input_ids": inputs, 'patch_images': patch_img, 'patch_masks':torch.tensor([True])}
|
| 51 |
+
>>> gen_output = generator.generate([model], data)
|
| 52 |
+
>>> gen = [gen_output[i][0]["tokens"] for i in range(len(gen_output))]
|
| 53 |
+
|
| 54 |
+
>>> # using the generator of huggingface version
|
| 55 |
+
>>> model = OFAModel.from_pretrained(ckpt_dir, use_cache=False)
|
| 56 |
+
>>> gen = model.generate(inputs, patch_images=patch_img, num_beams=5, no_repeat_ngram_size=3)
|
| 57 |
+
|
| 58 |
>>> print(tokenizer.batch_decode(gen, skip_special_tokens=True))
|
| 59 |
```
|