Views
No views yet
68e1a8e0ebb9b30f3c45c1ef6195980f29063ae2 of the MPT-7B modeling code. We suggest using this copy of the model to ensure the code is loaded at that commit.1from open_flamingo import create_model_and_transforms
2
3model, image_processor, tokenizer = create_model_and_transforms(
4 clip_vision_encoder_path="ViT-L-14",
5 clip_vision_encoder_pretrained="openai",
6 lang_encoder_path="anas-awadalla/mpt-7b",
7 tokenizer_path="anas-awadalla/mpt-7b",
8 cross_attn_every_n_layers=4
9)
10
11# grab model checkpoint from huggingface hub
12from huggingface_hub import hf_hub_download
13import torch
14
15checkpoint_path = hf_hub_download("openflamingo/OpenFlamingo-9B-vitl-mpt7b", "checkpoint.pt")
16model.load_state_dict(torch.load(checkpoint_path), strict=False)1from PIL import Image
2import requests
3
4"""
5Step 1: Load images
6"""
7demo_image_one = Image.open(
8 requests.get(
9 "http://images.cocodataset.org/val2017/000000039769.jpg", stream=True
10 ).raw
11)
12
13demo_image_two = Image.open(
14 requests.get(
15 "http://images.cocodataset.org/test-stuff2017/000000028137.jpg",
16 stream=True
17 ).raw
18)
19
20query_image = Image.open(
21 requests.get(
22 "http://images.cocodataset.org/test-stuff2017/000000028352.jpg",
23 stream=True
24 ).raw
25)
26
27
28"""
29Step 2: Preprocessing images
30Details: For OpenFlamingo, we expect the image to be a torch tensor of shape
31 batch_size x num_media x num_frames x channels x height x width.
32 In this case batch_size = 1, num_media = 3, num_frames = 1,
33 channels = 3, height = 224, width = 224.
34"""
35vision_x = [image_processor(demo_image_one).unsqueeze(0), image_processor(demo_image_two).unsqueeze(0), image_processor(query_image).unsqueeze(0)]
36vision_x = torch.cat(vision_x, dim=0)
37vision_x = vision_x.unsqueeze(1).unsqueeze(0)
38
39"""
40Step 3: Preprocessing text
41Details: In the text we expect an <image> special token to indicate where an image is.
42 We also expect an <|endofchunk|> special token to indicate the end of the text
43 portion associated with an image.
44"""
45tokenizer.padding_side = "left" # For generation padding tokens should be on the left
46lang_x = tokenizer(
47 ["<image>An image of two cats.<|endofchunk|><image>An image of a bathroom sink.<|endofchunk|><image>An image of"],
48 return_tensors="pt",
49)
50
51
52"""
53Step 4: Generate text
54"""
55generated_text = model.generate(
56 vision_x=vision_x,
57 lang_x=lang_x["input_ids"],
58 attention_mask=lang_x["attention_mask"],
59 max_new_tokens=20,
60 num_beams=3,
61)
62
63print("Generated text: ", tokenizer.decode(generated_text[0]))| 0-shot | 4-shot | 8-shot | 16-shot | 32-shot | |
|---|---|---|---|---|---|
| COCO (CIDEr) | 79.5 (0.2) | 89.0 (0.3) | 96.3 (0.1) | 98.8 (0.7) | 99.5 (0.1) |
| VQAv2 (Accuracy) | 50.3 (0.7) | 50.5 (0.5) | 52.8 (0.3) | 52.3 (0.3) | 50.5 (0.0) |
| Flickr-30K (CIDEr) | 59.5 (1.0) | 65.8 (0.6) | 62.9 (1.0) | 62.8 (1.0) | 61.3 (0.7) |
| OK-VQA (Accuracy) | 34.7 (0.1) | 34.3 (0.1) | 38.4 (0.0) | 39.5 (0.1) | 38.1 (0.0) |
| TextVQA (Accuracy) | 24.2 (0.5) | 28.2 (0.4) | 29.1 (0.1) | 27.3 (0.1) | 23.8 (0.2) |
| Vizwiz (Accuracy) | 17.7 (0.7) | 23.1 (0.9) | 31.6 (1.5) | 38.0 (1.1) | 40.2 (0.7) |
| Hateful Memes (ROC AUC) | 50.8 (4.7) | 47.5 (2.2) | 45.2 (2.7) | 46.9 (3.8) | 52.0 (2.1) |