JustinLin610 commited on
Commit
ca2ea37
1 Parent(s): 2484e4a

Update README.md

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