Downloads · 30 days
14
29% of all-time downloads
anonhs/vit-cancer-classifier
vit-cancer-classifier is a machine learning model from anonhs. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
--- libraryname: transformers pipelinetag: image-classification ---
Downloads · 30 days
14
29% of all-time downloads
All-time downloads
48
Public
Parameters
85.8M
343 MB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors343 MB · 100%
From the Hugging Face model README
A Vision Transformer (ViT) model trained for cancer image classification.
import torch
from transformers import AutoImageProcessor, AutoModelForImageClassification
from PIL import Image
# Load model from Hugging Face
MODEL_ID = "anonhs/vit-cancer-classifier"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = AutoModelForImageClassification.from_pretrained(MODEL_ID)
processor = AutoImageProcessor.from_pretrained(MODEL_ID)
model.to(device)
model.eval()
def predict_image(image_path):
# Load image
image = Image.open(image_path).convert("RGB")
# Preprocess
inputs = processor(images=image, return_tensors="pt")
inputs = {k: v.to(device) for k, v in inputs.items()}
# Inference
with torch.no_grad():
outputs = model(**inputs)
probs = torch.softmax(outputs.logits, dim=1)
pred_idx = torch.argmax(probs, dim=1).item()
confidence = probs[0][pred_idx].item()
label = model.config.id2label[pred_idx]
return label, confidence
# Example usage
image_path = "sample_image.jpg"
label, confidence = predict_image(image_path)
print(f"Prediction: {label}")
print(f"Confidence: {confidence:.4f}")