This model is a fine-tuned version of
google/medgemma-4b-it
specifically optimized for medical image analysis tasks in the FLARE 2025 2D Medical Multimodal Dataset challenge.
The model was fine-tuned on 19 diverse medical imaging datasets from FLARE 2025, including:
1# LoRA Configuration
2lora_r: 16\nlora_alpha: 32
3lora_dropout: 0.1
4target_modules: ['gate_proj', 'up_proj', 'o_proj', 'down_proj', 'v_proj', 'q_proj', 'k_proj']
5task_type: CAUSAL_LM
6bias: none
7
This model has been evaluated across multiple medical imaging tasks using FLARE 2025 evaluation metrics:
1import torch
2from transformers import AutoTokenizer, AutoProcessor, AutoModelForImageTextToText
3from peft import PeftModel
4from PIL import Image
5
6# Load the fine-tuned model
7base_model_name = "google/medgemma-4b-it"
8adapter_model_name = "leoyinn/flare25-medgemma"
9
10# Load tokenizer and processor
11tokenizer = AutoTokenizer.from_pretrained(base_model_name, trust_remote_code=True)
12processor = AutoProcessor.from_pretrained(base_model_name, trust_remote_code=True)
13
14# Load base model
15base_model = AutoModelForImageTextToText.from_pretrained(
16 base_model_name,
17 torch_dtype=torch.bfloat16,
18 device_map="auto",
19 trust_remote_code=True,
20 attn_implementation="eager"
21)
22
23# Load the fine-tuned adapter
24model = PeftModel.from_pretrained(base_model, adapter_model_name)
25
26# Prepare input with MedGemma chat format
27image = Image.open("medical_image.jpg").convert("RGB")
28image = image.resize((448, 448)) # MedGemma standard size
29
30# Create proper message format
31messages = [
32 {
33 "role": "system",
34 "content": [{
35 "type": "text",
36 "text": "You are an expert medical AI assistant specialized in analyzing medical images and providing accurate diagnostic insights."
37 }]
38 },
39 {
40 "role": "user",
41 "content": [
42 {"type": "image"},
43 {"type": "text", "text": "Describe the medical findings in this image and provide a diagnostic assessment."}
44 ]
45 }
46]
47
48# Apply chat template
49full_text = tokenizer.apply_chat_template(
50 messages,
51 tokenize=False,
52 add_generation_prompt=True
53)
54
55# Process and generate
56inputs = processor(
57 images=[image],
58 text=full_text,
59 return_tensors="pt",
60 padding=True,
61 truncation=False
62).to(model.device, dtype=torch.bfloat16)
63
64# Generate medical response
65with torch.inference_mode():
66 outputs = model.generate(
67 **inputs,
68 max_new_tokens=300,
69 do_sample=False, # Deterministic for medical applications
70 use_cache=True,
71 cache_implementation="dynamic"
72 )
73
74# Decode response
75input_len = inputs["input_ids"].shape[-1]
76response = processor.decode(outputs[0][input_len:], skip_special_tokens=True)
77print(response)
1@misc{medgemma-flare2025,
2 title={MedGemma Fine-tuned for FLARE 2025 Medical Image Analysis},
3 author={Shuolin Yin},
4 year={2025},
5 publisher={Hugging Face},
6 url={https://huggingface.co/leoyinn/flare25-medgemma}
7}
8
9@misc{medgemma-base,
10 title={MedGemma: Medical Gemma Models for Healthcare},
11 author={Google Research},
12 year={2024},
13 publisher={Hugging Face},
14 url={https://huggingface.co/google/medgemma-4b-it}
15}
16
17@misc{flare2025,
18 title={FLARE 2025: A Multi-Modal Foundation Model Challenge for Medical AI},
19 year={2025},
20 url={https://huggingface.co/datasets/FLARE-MedFM/FLARE-Task5-MLLM-2D}
21}
For questions or issues, please open an issue in the model repository or contact the authors.