File size: 3,539 Bytes
1825f0a
 
 
4dc4216
1825f0a
 
 
 
 
 
 
 
 
 
 
 
 
cb0b9da
 
 
 
0f2f364
cb0b9da
cf5ca0f
cb0b9da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc4907d
 
 
0f2f364
cba582e
 
 
0f2f364
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
---
license: gemma
datasets:
  - BUAADreamer/llava-en-zh-300k
language:
  - en
  - zh
library_name: transformers
pipeline_tag: image-text-to-text
base_model: google/paligemma-3b-mix-448
inference: false
tags:
  - paligemma
  - llama-factory
  - mllm
  - vlm
---
# PaliGemma-3B-Chat-v0.2

This model is fine-tuned from [google/paligemma-3b-mix-448](https://huggingface.co/google/paligemma-3b-mix-448) for multiturn chat completions.

Try our live demo at: https://huggingface.co/spaces/llamafactory/PaliGemma-3B-Chat-v0.2

![example_en](assets/example_en.png)
![example_zh](assets/example_zh.png)

## Usage

```python
import requests
import torch
from PIL import Image
from transformers import AutoModelForVision2Seq, AutoProcessor, AutoTokenizer, TextStreamer

model_id = "BUAADreamer/PaliGemma-3B-Chat-v0.2"
tokenizer = AutoTokenizer.from_pretrained(model_id)
processor = AutoProcessor.from_pretrained(model_id)
model = AutoModelForVision2Seq.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)

url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
image = Image.open(requests.get(url, stream=True).raw)
pixel_values = processor(images=[image], return_tensors="pt").to(model.device)["pixel_values"]

messages = [
    {"role": "user", "content": "What is in this image?"}
]
input_ids = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
image_token_id = tokenizer.convert_tokens_to_ids("<image>")
image_prefix = torch.empty((1, getattr(processor, "image_seq_length")), dtype=input_ids.dtype).fill_(image_token_id)
input_ids = torch.cat((image_prefix, input_ids), dim=-1).to(model.device)

generate_ids = model.generate(input_ids, pixel_values=pixel_values, streamer=streamer, max_new_tokens=50)
```

## Training procedure

We used [LLaMA Factory](https://github.com/hiyouga/LLaMA-Factory) to fine-tune this model. During fine-tuning, we freezed the vision tower and adjusted the parameters in the language model and projector layer.

### Training hyperparameters

The following hyperparameters were used during training:
- learning_rate: 0.000003
- num_train_epochs: 2.0
- train_batch_size: 4
- gradient_accumulation_steps: 16
- total_train_batch_size: 64
- seed: 42
- lr_scheduler_type: cosine
- mixed_precision_training: bf16

<details>
<summary><b>Show Llama Factory Config [CLICK TO EXPAND]</b></summary>

```yaml
### model
model_name_or_path: google/paligemma-3b-mix-448
visual_inputs: true

### method
stage: sft
do_train: true
finetuning_type: full

### ddp
ddp_timeout: 180000000
deepspeed: examples/deepspeed/ds_z3_config.json

### dataset
dataset: identity,llava_150k_en,llava_150k_zh
template: gemma
cutoff_len: 1536
overwrite_cache: true
preprocessing_num_workers: 16
tokenized_path: cache/paligemma-identity-llava-zh-en-300k

### output
output_dir: models/paligemma-3b-chat-v0.2
logging_steps: 10
save_steps: 1000
plot_loss: true

### train
per_device_train_batch_size: 1
gradient_accumulation_steps: 16
learning_rate: 0.000003
num_train_epochs: 2.0
lr_scheduler_type: cosine
warmup_steps: 50
bf16: true
do_eval: false
```

</details>

### Framework versions

- Pytorch 2.3.0
- Transformers 4.41.0

## Evaluation Results

|         Model          | MMMU_Val | CMMMU_Val |
| :--------------------: | :------: | :-------: |
|        Yi-VL-6B        |   36.8   |   32.2    |
| Paligemma-3B-Chat-v0.2 |   33.0   |   29.0    |