Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +55 -0
- best_model.pth +3 -0
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torchvision import models, transforms
|
3 |
+
from PIL import Image
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# Mendefinisikan nama kelas
|
7 |
+
class_names = [
|
8 |
+
"calculus",
|
9 |
+
"caries",
|
10 |
+
"gingivitis",
|
11 |
+
"hypodontia",
|
12 |
+
"mouth_ulcer",
|
13 |
+
"tooth_discoloration"
|
14 |
+
]
|
15 |
+
|
16 |
+
# Mengatur jumlah kelas
|
17 |
+
num_classes = len(class_names)
|
18 |
+
|
19 |
+
# Membuat dan mengkonfigurasi model
|
20 |
+
model = models.resnet50(pretrained=False)
|
21 |
+
model.fc = torch.nn.Linear(model.fc.in_features, num_classes)
|
22 |
+
|
23 |
+
# Memuat bobot model (sesuaikan path jika diperlukan)
|
24 |
+
model.load_state_dict(torch.load('best_model.pth', map_location=torch.device('cpu')))
|
25 |
+
model.eval()
|
26 |
+
|
27 |
+
# Mengatur transformasi preprocessing
|
28 |
+
preprocess = transforms.Compose([
|
29 |
+
transforms.Resize((224, 224)),
|
30 |
+
transforms.ToTensor(),
|
31 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
32 |
+
])
|
33 |
+
|
34 |
+
# Fungsi untuk melakukan prediksi
|
35 |
+
def predict_image(image, model, preprocess, class_names):
|
36 |
+
processed_image = preprocess(image).unsqueeze(0)
|
37 |
+
|
38 |
+
with torch.no_grad():
|
39 |
+
outputs = model(processed_image)
|
40 |
+
_, predicted = torch.max(outputs, 1)
|
41 |
+
predicted_class = class_names[predicted.item()]
|
42 |
+
|
43 |
+
return predicted_class
|
44 |
+
|
45 |
+
# Membuat interface Gradio
|
46 |
+
iface = gr.Interface(
|
47 |
+
fn=predict_image,
|
48 |
+
inputs=[gr.inputs.Image(type='pil')],
|
49 |
+
outputs=gr.outputs.Label(num_top_classes=1),
|
50 |
+
title="Klasifikasi Gambar Medis",
|
51 |
+
description="Upload gambar untuk memprediksi kelasnya."
|
52 |
+
)
|
53 |
+
|
54 |
+
# Menjalankan aplikasi Gradio
|
55 |
+
iface.launch()
|
best_model.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b10fd8542b9770dd45be680bf393e1406d2460a0a1a4e6010ec6e0f4ca757821
|
3 |
+
size 94399870
|