Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,18 +5,22 @@ from PIL import Image
|
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
import joblib
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# Load labels
|
| 10 |
label_df = pd.read_csv("labels.csv") # columns: filename,label
|
| 11 |
test_dir = "test_images"
|
| 12 |
|
| 13 |
-
# Preprocessing: convert image to feature vector
|
| 14 |
def image_to_features(image: Image.Image) -> np.ndarray:
|
| 15 |
-
image = image.resize((64, 64))
|
| 16 |
-
array = np.asarray(image.convert("L"))
|
| 17 |
return array.flatten()
|
| 18 |
|
| 19 |
-
# Build test dataset
|
| 20 |
X_test = []
|
| 21 |
y_test = []
|
| 22 |
|
|
@@ -31,25 +35,83 @@ for _, row in label_df.iterrows():
|
|
| 31 |
X_test = np.array(X_test)
|
| 32 |
y_test = np.array(y_test)
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
LEADERBOARD_PATH = "leaderboard.csv"
|
| 35 |
if not os.path.exists(LEADERBOARD_PATH):
|
| 36 |
pd.DataFrame(columns=["Name", "Accuracy", "Avg Time (ms)"]).to_csv(LEADERBOARD_PATH, index=False)
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
def evaluate_model(file, name):
|
| 39 |
try:
|
| 40 |
-
|
| 41 |
-
model = joblib.load(file.name)
|
| 42 |
|
| 43 |
-
# Measure time for prediction
|
| 44 |
start = time.time()
|
| 45 |
y_pred = model.predict(X_test)
|
| 46 |
-
elapsed = (time.time() - start) * 1000 # ms
|
| 47 |
|
| 48 |
-
# Calculate accuracy
|
| 49 |
accuracy = 100 * (y_pred == y_test).mean()
|
| 50 |
avg_time = elapsed / len(X_test)
|
| 51 |
|
| 52 |
-
# Update leaderboard
|
| 53 |
leaderboard = pd.read_csv(LEADERBOARD_PATH)
|
| 54 |
leaderboard = pd.concat([leaderboard, pd.DataFrame([{
|
| 55 |
"Name": name,
|
|
@@ -63,15 +125,19 @@ def evaluate_model(file, name):
|
|
| 63 |
except Exception as e:
|
| 64 |
return f"❌ Error during evaluation: {e}"
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
interface = gr.Interface(
|
| 67 |
fn=evaluate_model,
|
| 68 |
inputs=[
|
| 69 |
-
gr.File(label="Upload your `.joblib` model"),
|
| 70 |
gr.Text(label="Your (group) name(s)")
|
| 71 |
],
|
| 72 |
outputs=gr.Dataframe(label="Leaderboard"),
|
| 73 |
-
title="
|
| 74 |
-
description="Upload a `.joblib
|
| 75 |
)
|
| 76 |
|
| 77 |
interface.launch()
|
|
|
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
import joblib
|
| 8 |
+
import pickle
|
| 9 |
+
import torch
|
| 10 |
+
import torch.nn as nn
|
| 11 |
+
|
| 12 |
+
# ===============================
|
| 13 |
+
# Step 1: Load Test Dataset
|
| 14 |
+
# ===============================
|
| 15 |
|
|
|
|
| 16 |
label_df = pd.read_csv("labels.csv") # columns: filename,label
|
| 17 |
test_dir = "test_images"
|
| 18 |
|
|
|
|
| 19 |
def image_to_features(image: Image.Image) -> np.ndarray:
|
| 20 |
+
image = image.resize((64, 64))
|
| 21 |
+
array = np.asarray(image.convert("L"))
|
| 22 |
return array.flatten()
|
| 23 |
|
|
|
|
| 24 |
X_test = []
|
| 25 |
y_test = []
|
| 26 |
|
|
|
|
| 35 |
X_test = np.array(X_test)
|
| 36 |
y_test = np.array(y_test)
|
| 37 |
|
| 38 |
+
# ===============================
|
| 39 |
+
# Step 2: Define Leaderboard File
|
| 40 |
+
# ===============================
|
| 41 |
+
|
| 42 |
LEADERBOARD_PATH = "leaderboard.csv"
|
| 43 |
if not os.path.exists(LEADERBOARD_PATH):
|
| 44 |
pd.DataFrame(columns=["Name", "Accuracy", "Avg Time (ms)"]).to_csv(LEADERBOARD_PATH, index=False)
|
| 45 |
|
| 46 |
+
# ===============================
|
| 47 |
+
# Step 3: Define Model Wrappers
|
| 48 |
+
# ===============================
|
| 49 |
+
|
| 50 |
+
class SimpleTorchModel(nn.Module):
|
| 51 |
+
def __init__(self):
|
| 52 |
+
super().__init__()
|
| 53 |
+
self.fc = nn.Sequential(
|
| 54 |
+
nn.Linear(64 * 64, 128),
|
| 55 |
+
nn.ReLU(),
|
| 56 |
+
nn.Linear(128, 2)
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
def forward(self, x):
|
| 60 |
+
return self.fc(x)
|
| 61 |
+
|
| 62 |
+
class SKLearnWrapper:
|
| 63 |
+
def __init__(self, model):
|
| 64 |
+
self.model = model
|
| 65 |
+
|
| 66 |
+
def predict(self, X: np.ndarray) -> np.ndarray:
|
| 67 |
+
return self.model.predict(X)
|
| 68 |
+
|
| 69 |
+
class TorchPredictWrapper:
|
| 70 |
+
def __init__(self, model_path: str):
|
| 71 |
+
self.model = SimpleTorchModel()
|
| 72 |
+
self.model.load_state_dict(torch.load(model_path, map_location="cpu"))
|
| 73 |
+
self.model.eval()
|
| 74 |
+
|
| 75 |
+
def predict(self, X: np.ndarray) -> np.ndarray:
|
| 76 |
+
with torch.no_grad():
|
| 77 |
+
tensor = torch.tensor(X, dtype=torch.float32)
|
| 78 |
+
logits = self.model(tensor)
|
| 79 |
+
return torch.argmax(logits, dim=1).numpy()
|
| 80 |
+
|
| 81 |
+
# ===============================
|
| 82 |
+
# Step 4: Load Model From File
|
| 83 |
+
# ===============================
|
| 84 |
+
|
| 85 |
+
def load_model(file_path: str):
|
| 86 |
+
try:
|
| 87 |
+
model = joblib.load(file_path)
|
| 88 |
+
return SKLearnWrapper(model)
|
| 89 |
+
except:
|
| 90 |
+
try:
|
| 91 |
+
with open(file_path, "rb") as f:
|
| 92 |
+
model = pickle.load(f)
|
| 93 |
+
return SKLearnWrapper(model)
|
| 94 |
+
except:
|
| 95 |
+
try:
|
| 96 |
+
return TorchPredictWrapper(file_path)
|
| 97 |
+
except Exception as e:
|
| 98 |
+
raise RuntimeError(f"Unrecognized model format or failed to load: {e}")
|
| 99 |
+
|
| 100 |
+
# ===============================
|
| 101 |
+
# Step 5: Evaluation Logic
|
| 102 |
+
# ===============================
|
| 103 |
+
|
| 104 |
def evaluate_model(file, name):
|
| 105 |
try:
|
| 106 |
+
model = load_model(file.name)
|
|
|
|
| 107 |
|
|
|
|
| 108 |
start = time.time()
|
| 109 |
y_pred = model.predict(X_test)
|
| 110 |
+
elapsed = (time.time() - start) * 1000 # in ms
|
| 111 |
|
|
|
|
| 112 |
accuracy = 100 * (y_pred == y_test).mean()
|
| 113 |
avg_time = elapsed / len(X_test)
|
| 114 |
|
|
|
|
| 115 |
leaderboard = pd.read_csv(LEADERBOARD_PATH)
|
| 116 |
leaderboard = pd.concat([leaderboard, pd.DataFrame([{
|
| 117 |
"Name": name,
|
|
|
|
| 125 |
except Exception as e:
|
| 126 |
return f"❌ Error during evaluation: {e}"
|
| 127 |
|
| 128 |
+
# ===============================
|
| 129 |
+
# Step 6: Gradio Interface
|
| 130 |
+
# ===============================
|
| 131 |
+
|
| 132 |
interface = gr.Interface(
|
| 133 |
fn=evaluate_model,
|
| 134 |
inputs=[
|
| 135 |
+
gr.File(label="Upload your `.joblib`, `.pkl`, or `.pth` model"),
|
| 136 |
gr.Text(label="Your (group) name(s)")
|
| 137 |
],
|
| 138 |
outputs=gr.Dataframe(label="Leaderboard"),
|
| 139 |
+
title="Insect Classifier Leaderboard",
|
| 140 |
+
description="Upload a `.joblib`, `.pkl`, or `.pth` model that implements `.predict(X)` on 64×64 grayscale image vectors. We will evaluate your model and update the leaderboard."
|
| 141 |
)
|
| 142 |
|
| 143 |
interface.launch()
|