Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,38 @@
|
|
| 1 |
import torch
|
| 2 |
-
import
|
| 3 |
-
from torchvision import models, transforms
|
| 4 |
from PIL import Image
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
#
|
| 8 |
class_names = ["Normal", "Cancer", "Malignant"]
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
transforms.ToTensor(),
|
| 13 |
-
transforms.Normalize([0.485, 0.456, 0.406],
|
| 14 |
-
[0.229, 0.224, 0.225])
|
| 15 |
-
])
|
| 16 |
|
| 17 |
-
# Load model
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
model = get_model()
|
| 24 |
-
model.load_state_dict(torch.load("distilled_vgg16.pth", map_location="cpu"))
|
| 25 |
model.eval()
|
| 26 |
|
| 27 |
-
#
|
| 28 |
def predict(img: Image.Image):
|
| 29 |
-
|
| 30 |
with torch.no_grad():
|
| 31 |
-
outputs = model(
|
| 32 |
-
probs = torch.softmax(outputs, dim=1)[0]
|
| 33 |
pred = torch.argmax(probs).item()
|
| 34 |
return f"Prediction: {class_names[pred]}\nPlease consult a doctor for further diagnosis."
|
| 35 |
|
| 36 |
-
# Gradio
|
| 37 |
interface = gr.Interface(
|
| 38 |
fn=predict,
|
| 39 |
-
inputs=gr.Image(type="pil"),
|
| 40 |
outputs=gr.Textbox(label="Diagnosis"),
|
| 41 |
-
title="Lung Cancer
|
| 42 |
-
description="
|
| 43 |
)
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from transformers import AutoModelForImageClassification, AutoFeatureExtractor
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# Class labels in the same order as training
|
| 7 |
class_names = ["Normal", "Cancer", "Malignant"]
|
| 8 |
|
| 9 |
+
# Load the ViT feature extractor
|
| 10 |
+
extractor = AutoFeatureExtractor.from_pretrained("google/vit-base-patch16-224-in21k")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Load the ViT model and custom weights
|
| 13 |
+
model = AutoModelForImageClassification.from_pretrained(
|
| 14 |
+
"google/vit-base-patch16-224-in21k", num_labels=3
|
| 15 |
+
)
|
| 16 |
+
model.classifier = torch.nn.Linear(model.classifier.in_features, 3) # Replace classifier
|
| 17 |
+
model.load_state_dict(torch.load("distill_vgg16.pth", map_location="cpu")) # Your uploaded file
|
|
|
|
|
|
|
| 18 |
model.eval()
|
| 19 |
|
| 20 |
+
# Prediction function
|
| 21 |
def predict(img: Image.Image):
|
| 22 |
+
inputs = extractor(images=img, return_tensors="pt")
|
| 23 |
with torch.no_grad():
|
| 24 |
+
outputs = model(**inputs)
|
| 25 |
+
probs = torch.softmax(outputs.logits, dim=1)[0]
|
| 26 |
pred = torch.argmax(probs).item()
|
| 27 |
return f"Prediction: {class_names[pred]}\nPlease consult a doctor for further diagnosis."
|
| 28 |
|
| 29 |
+
# Gradio interface
|
| 30 |
interface = gr.Interface(
|
| 31 |
fn=predict,
|
| 32 |
+
inputs=gr.Image(type="pil", label="Upload CT Image"),
|
| 33 |
outputs=gr.Textbox(label="Diagnosis"),
|
| 34 |
+
title="Lung Cancer Classifier",
|
| 35 |
+
description="This Vision Transformer model predicts lung conditions from CT scans. Classes: Normal, Cancer, Malignant."
|
| 36 |
)
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|