Downloads · 30 days
5
11% of all-time downloads
Shubh769/pmc2_model_m2
pmc2_model_m2 is a machine learning model from Shubh769. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
- Base Model: BLIP-2 OPT 2.7B - Training Device: NVIDIA RTX 4050 - Inference Device: Apple M2 Air (MPS) - Dataset: PMC2 (PubMed Central Medical Images) - Training: 3 epochs on 3,355 medical images - Fine-tuning: LoRA…
Downloads · 30 days
5
11% of all-time downloads
All-time downloads
47
Public
Parameters
3.7B
7.5 GB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors7.5 GB · 100%
From the Hugging Face model README
pip install torch torchvision transformers pillow
import torch
from transformers import Blip2Processor, Blip2ForConditionalGeneration
from PIL import Image
# Load model and processor
print("Loading model...")
processor = Blip2Processor.from_pretrained("./")
model = Blip2ForConditionalGeneration.from_pretrained(
"./",
torch_dtype=torch.float16
).to("mps") # Use Metal Performance Shaders on M2
print("Model loaded!")
# Generate caption for a medical image
image = Image.open("medical_image.jpg")
inputs = processor(images=image, return_tensors="pt").to("mps", torch.float16)
# Generate caption
print("Generating caption...")
with torch.no_grad():
generated_ids = model.generate(**inputs, max_length=50)
caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(f"Caption: {caption}")
from pathlib import Path
# Process all images in a folder
image_folder = Path("./medical_images")
results = []
for img_path in image_folder.glob("*.jpg"):
print(f"Processing: {img_path.name}")
image = Image.open(img_path)
inputs = processor(images=image, return_tensors="pt").to("mps", torch.float16)
with torch.no_grad():
generated_ids = model.generate(**inputs, max_length=50)
caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
results.append({"image": img_path.name, "caption": caption})
print(f" → {caption}")
# Save results
import json
with open("captions.json", "w") as f:
json.dump(results, f, indent=2)
torch.mps.empty_cache()# Adjust these for different caption styles:
generated_ids = model.generate(
**inputs,
max_length=50, # Maximum caption length
num_beams=5, # Beam search (higher = better quality, slower)
temperature=1.0, # Randomness (lower = more deterministic)
top_p=0.9, # Nucleus sampling
repetition_penalty=1.2 # Avoid repetition
)
torch.backends.mps.is_available().to("cpu")torch.float32 → torch.float16torch.mps.empty_cache()