Views
No views yet
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.git clone --single-branch --branch feature/add_transformers https://github.com/OFA-Sys/OFA.git
pip install OFA/transformers/
git clone https://huggingface.co/OFA-Sys/OFA-large-captionckpt_dir, and prepare an image for the testing example below. Also, ensure that you have pillow and torchvision in your environment.1>>> from PIL import Image
2>>> from torchvision import transforms
3>>> from transformers import OFATokenizer, OFAModel
4>>> from generate import sequence_generator
5
6>>> mean, std = [0.5, 0.5, 0.5], [0.5, 0.5, 0.5]
7>>> resolution = 480
8>>> patch_resize_transform = transforms.Compose([
9 lambda image: image.convert("RGB"),
10 transforms.Resize((resolution, resolution), interpolation=Image.BICUBIC),
11 transforms.ToTensor(),
12 transforms.Normalize(mean=mean, std=std)
13 ])
14
15
16>>> tokenizer = OFATokenizer.from_pretrained(ckpt_dir)
17
18>>> txt = " what does the image describe?"
19>>> inputs = tokenizer([txt], return_tensors="pt").input_ids
20>>> img = Image.open(path_to_image)
21>>> patch_img = patch_resize_transform(img).unsqueeze(0)
22
23
24# using the generator of fairseq version
25>>> model = OFAModel.from_pretrained(ckpt_dir, use_cache=True)
26>>> generator = sequence_generator.SequenceGenerator(
27 tokenizer=tokenizer,
28 beam_size=5,
29 max_len_b=16,
30 min_len=0,
31 no_repeat_ngram_size=3,
32 )
33>>> data = {}
34>>> data["net_input"] = {"input_ids": inputs, 'patch_images': patch_img, 'patch_masks':torch.tensor([True])}
35>>> gen_output = generator.generate([model], data)
36>>> gen = [gen_output[i][0]["tokens"] for i in range(len(gen_output))]
37
38# using the generator of huggingface version
39>>> model = OFAModel.from_pretrained(ckpt_dir, use_cache=False)
40>>> gen = model.generate(inputs, patch_images=patch_img, num_beams=5, no_repeat_ngram_size=3)
41
42>>> print(tokenizer.batch_decode(gen, skip_special_tokens=True))