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