Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,12 +7,18 @@ import gradio as gr
|
|
| 7 |
import joblib
|
| 8 |
import os
|
| 9 |
|
| 10 |
-
# β
|
| 11 |
label_map = {
|
| 12 |
'G': 0, 'DR': 1, 'WD': 2, 'ND': 3, 'OTHER': 4
|
| 13 |
}
|
| 14 |
reverse_label_map = {v: k for k, v in label_map.items()}
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# β
Load model
|
| 18 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
@@ -37,13 +43,13 @@ def predict(img):
|
|
| 37 |
try:
|
| 38 |
img_tensor = transform(img).unsqueeze(0).to(device)
|
| 39 |
|
| 40 |
-
#
|
| 41 |
with torch.no_grad():
|
| 42 |
output = model(img_tensor)
|
| 43 |
_, predicted = torch.max(output, 1)
|
| 44 |
damage_class = reverse_label_map[predicted.item()]
|
| 45 |
|
| 46 |
-
# Feature extraction
|
| 47 |
x = model.conv1(img_tensor)
|
| 48 |
x = model.bn1(x)
|
| 49 |
x = model.relu(x)
|
|
@@ -55,9 +61,11 @@ def predict(img):
|
|
| 55 |
x = model.avgpool(x)
|
| 56 |
x = torch.flatten(x, 1).detach().cpu().numpy()
|
| 57 |
|
| 58 |
-
#
|
| 59 |
x_pca = pca.transform(x)
|
| 60 |
severity_cluster = kmeans.predict(x_pca)[0]
|
|
|
|
|
|
|
| 61 |
severity_label = severity_map.get(severity_cluster, f"Cluster {severity_cluster}")
|
| 62 |
|
| 63 |
return f"Damage: {damage_class}", f"Severity: {severity_label}"
|
|
@@ -65,7 +73,7 @@ def predict(img):
|
|
| 65 |
except Exception as e:
|
| 66 |
return f"Error: {str(e)}", ""
|
| 67 |
|
| 68 |
-
# β
Gradio
|
| 69 |
gr.Interface(
|
| 70 |
fn=predict,
|
| 71 |
inputs=gr.Image(type="pil"),
|
|
|
|
| 7 |
import joblib
|
| 8 |
import os
|
| 9 |
|
| 10 |
+
# β
Label maps
|
| 11 |
label_map = {
|
| 12 |
'G': 0, 'DR': 1, 'WD': 2, 'ND': 3, 'OTHER': 4
|
| 13 |
}
|
| 14 |
reverse_label_map = {v: k for k, v in label_map.items()}
|
| 15 |
+
|
| 16 |
+
# β οΈ Adjust this based on cluster-to-severity relation (see notes below)
|
| 17 |
+
severity_map = {
|
| 18 |
+
0: 'Low', # e.g., yellow cluster
|
| 19 |
+
1: 'Medium', # e.g., pink cluster
|
| 20 |
+
2: 'High' # e.g., blue cluster
|
| 21 |
+
}
|
| 22 |
|
| 23 |
# β
Load model
|
| 24 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 43 |
try:
|
| 44 |
img_tensor = transform(img).unsqueeze(0).to(device)
|
| 45 |
|
| 46 |
+
# π Predict damage class
|
| 47 |
with torch.no_grad():
|
| 48 |
output = model(img_tensor)
|
| 49 |
_, predicted = torch.max(output, 1)
|
| 50 |
damage_class = reverse_label_map[predicted.item()]
|
| 51 |
|
| 52 |
+
# π Feature extraction for severity
|
| 53 |
x = model.conv1(img_tensor)
|
| 54 |
x = model.bn1(x)
|
| 55 |
x = model.relu(x)
|
|
|
|
| 61 |
x = model.avgpool(x)
|
| 62 |
x = torch.flatten(x, 1).detach().cpu().numpy()
|
| 63 |
|
| 64 |
+
# π PCA + Clustering
|
| 65 |
x_pca = pca.transform(x)
|
| 66 |
severity_cluster = kmeans.predict(x_pca)[0]
|
| 67 |
+
print(f"Predicted Severity Cluster: {severity_cluster}") # Debug log
|
| 68 |
+
|
| 69 |
severity_label = severity_map.get(severity_cluster, f"Cluster {severity_cluster}")
|
| 70 |
|
| 71 |
return f"Damage: {damage_class}", f"Severity: {severity_label}"
|
|
|
|
| 73 |
except Exception as e:
|
| 74 |
return f"Error: {str(e)}", ""
|
| 75 |
|
| 76 |
+
# β
Gradio Interface
|
| 77 |
gr.Interface(
|
| 78 |
fn=predict,
|
| 79 |
inputs=gr.Image(type="pil"),
|