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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
#!/usr/bin/env python3
import argparse
import datasets
import json
import os
import requests
import sys
import torch
from glob import glob
from PIL import Image
from transformers import AutoProcessor, Gemma3ForConditionalGeneration
def captioning_prompt(image):
return [
{
"role": "system",
"content": [{"type": "text", "text": "You are a professional English-German translator and also a renowned photography critic."}]
},
{
"role": "user",
"content": [
{"type": "image", "image": image},
{"type": "text", "text": "Write a detailed caption for this image in a single sentence. Translate the caption into German. The output needs to be JSON, the keys being 'English' and 'German' for the respective captions. Only output the JSON, nothing else."}
]
}
]
def captioning_prompt_with_source(image, source):
caption = captioning_prompt(image)
prefix = json.dumps({"English": source}).removesuffix("}") + ', "German": "'
caption.append({"role": "assistant", "content": [{"type": "text", "text": prefix}]})
return caption
def translation_prompt(source):
return [
{
"role": "system",
"content": [{"type": "text", "text": "You are a professional English-German translator."}]
},
{
"role": "user",
"content": [
{"type": "text", "text": f"Translate the following caption into German. The output needs to be JSON, the only being 'Translation' for the translation. Only output the JSON, nothing else. Caption: {source}"}
]
}
]
def make_inputs(processor,
messages,
device):
return processor.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt"
).to(device, dtype=torch.bfloat16)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--model", default="google/gemma-3-4b-it")
parser.add_argument("--lora-adapter", default=None)
parser.add_argument("--mode", choices=["from_scratch", "with_prefix", "translate"])
parser.add_argument("--dataset", default="asdf2k/caption_translation", type=str)
parser.add_argument("--data-subset", choices=["train", "dev", "test"], default="test")
args = parser.parse_args()
model = Gemma3ForConditionalGeneration.from_pretrained(
args.model,
device_map="cuda",
dtype=torch.bfloat16,
attn_implementation="eager",
).eval()
processor = AutoProcessor.from_pretrained(args.model, use_fast=True)
if args.lora_adapter:
from peft import PeftModel
model = PeftModel.from_pretrained(model, args.lora_adapter)
dataset = datasets.load_dataset(args.dataset)[args.data_subset]
if args.mode == "translate": # Generate German translation given English source
for x in dataset:
sys.stderr.write(f"Processing {x['image']=}\n")
data = json.loads(x["assistant"])
exit()
inputs = make_inputs(processor,
translation_prompt(data["English"]),
model.device)
input_len = inputs["input_ids"].shape[-1]
with torch.inference_mode():
generation = model.generate(**inputs,
max_new_tokens=300,
do_sample=True,
top_p=1.0,
top_k=50)
generation = generation[0][input_len:]
decoded = processor.decode(generation, skip_special_tokens=True).removeprefix("```json").removesuffix("```").replace("\n", "").strip()
try:
new_data = json.loads(decoded)
except:
sys.stderr.write(f"Error loading JSON from string '{decoded}' for {filename=}\n")
data.update(new_data)
f.write(json.dumps(data))
f.truncate()
sys.stderr.write(f"{decoded=}\n")
elif args.mode == "from_scratch": # Generate caption & translation from scratch
for filename in glob("../d/Images/*.jpg"):
image = "../d/Images/" + os.path.basename(filename).removesuffix(".jsonl") + ".jpg"
sys.stderr.write(f"Processing {filename=}\n")
inputs = make_inputs(processor,
captioning_prompt(Image.open(filename)),
model.device)
input_len = inputs["input_ids"].shape[-1]
with torch.inference_mode():
generation = model.generate(**inputs,
max_new_tokens=300,
do_sample=True,
temperature=0.8,
top_p=1.0,
top_k=50,
eos_token_id=stop_token_ids,
disable_compile=True)
generation = generation[0][input_len:]
decoded = processor.decode(generation, skip_special_tokens=True).removeprefix("```json").removesuffix("```").replace("\n", "").strip()
try:
_ = json.loads(decoded)
except:
sys.stderr.write(f"Error loading JSON from string '{decoded}' for {filename=}\n")
sys.stderr.write(f"{decoded=}\n")
with open(f"{os.path.basename(filename).removesuffix('.jpg')}.jsonl", "w") as f:
f.write(f"{decoded}\n")
elif args.mode == "with_prefix": # Generate German translation given English caption and image
for filename in glob("./baseline/files_test/*.jsonl"):
image = "../d/Images/" + os.path.basename(filename).removesuffix(".jsonl") + ".jpg"
sys.stderr.write(f"Processing {filename=}\n")
with open(filename, "r+") as f:
data = json.loads(f.read())
prompt = captioning_prompt_with_source(Image.open(image), data["English"])
inputs = make_inputs(processor,
prompt,
model.device)
input_len = inputs["input_ids"].shape[-1] # Will not cut off assistant prefix
with torch.inference_mode():
generation = model.generate(**inputs,
max_new_tokens=300,
do_sample=True,
top_p=1.0,
top_k=50)
generation = generation[0] # batch size 1
truncated_generation = generation[input_len:]
decoded = processor.decode(truncated_generation, skip_special_tokens=True).removeprefix("```json").removesuffix("```").replace("\n", "").strip()
try:
_ = json.loads(decoded)
except:
sys.stderr.write(f"Error loading JSON from string '{decoded}' for {filename=}\n")
sys.stderr.write(f"{decoded=}\n")
with open(f"{os.path.basename(filename)}", "w") as f:
f.write(f"{decoded}\n")
else:
sys.stderr.write(f"Unkown mode '{args.mode}'")
if __name__ == "__main__":
main()
|