Spaces:
Sleeping
Sleeping
Commit ·
32e2c3e
1
Parent(s): 65535d9
final fix
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ from PIL import Image
|
|
| 5 |
import tensorflow as tf
|
| 6 |
import torch
|
| 7 |
import torch.nn as nn
|
| 8 |
-
import torchvision.models as models
|
| 9 |
|
| 10 |
|
| 11 |
# =========================
|
|
@@ -19,20 +18,37 @@ keras_model = tf.keras.models.load_model(
|
|
| 19 |
|
| 20 |
|
| 21 |
# =========================
|
| 22 |
-
# 2.
|
| 23 |
# =========================
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
state_dict = torch.load(
|
| 31 |
"best_densenet_mnist.pth",
|
| 32 |
map_location="cpu"
|
| 33 |
)
|
| 34 |
|
| 35 |
-
model.load_state_dict(state_dict)
|
| 36 |
model.eval()
|
| 37 |
return model
|
| 38 |
|
|
@@ -41,42 +57,37 @@ pytorch_model = load_pytorch_model()
|
|
| 41 |
|
| 42 |
|
| 43 |
# =========================
|
| 44 |
-
# 3.
|
| 45 |
# =========================
|
| 46 |
-
def
|
| 47 |
image = image.resize((32, 32)).convert("RGB")
|
|
|
|
| 48 |
img = np.array(image).astype(np.float32) / 255.0
|
| 49 |
img = np.expand_dims(img, axis=0)
|
| 50 |
-
return img
|
| 51 |
|
| 52 |
-
|
| 53 |
-
# =========================
|
| 54 |
-
# 4. KERAS PREDICTION
|
| 55 |
-
# =========================
|
| 56 |
-
def predict_keras(image):
|
| 57 |
-
img = preprocess(image)
|
| 58 |
-
preds = keras_model.predict(img)
|
| 59 |
return int(np.argmax(preds))
|
| 60 |
|
| 61 |
|
| 62 |
# =========================
|
| 63 |
-
#
|
| 64 |
# =========================
|
| 65 |
def predict_pytorch(image):
|
| 66 |
-
image = image.resize((32, 32)).convert("
|
| 67 |
|
| 68 |
img = np.array(image).astype(np.float32) / 255.0
|
| 69 |
-
img = np.
|
| 70 |
-
img =
|
|
|
|
|
|
|
| 71 |
|
| 72 |
with torch.no_grad():
|
| 73 |
outputs = pytorch_model(img)
|
| 74 |
-
|
| 75 |
-
return int(torch.argmax(preds))
|
| 76 |
|
| 77 |
|
| 78 |
# =========================
|
| 79 |
-
#
|
| 80 |
# =========================
|
| 81 |
def predict(image, model_name):
|
| 82 |
if model_name == "MobileNetV2 (Keras)":
|
|
@@ -88,7 +99,7 @@ def predict(image, model_name):
|
|
| 88 |
|
| 89 |
|
| 90 |
# =========================
|
| 91 |
-
#
|
| 92 |
# =========================
|
| 93 |
demo = gr.Interface(
|
| 94 |
fn=predict,
|
|
@@ -100,8 +111,7 @@ demo = gr.Interface(
|
|
| 100 |
)
|
| 101 |
],
|
| 102 |
outputs="text",
|
| 103 |
-
title="Multi-Model MNIST Classifier"
|
| 104 |
-
description="Choose model and upload digit image (0–9)"
|
| 105 |
)
|
| 106 |
|
| 107 |
demo.launch()
|
|
|
|
| 5 |
import tensorflow as tf
|
| 6 |
import torch
|
| 7 |
import torch.nn as nn
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
# =========================
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
# =========================
|
| 21 |
+
# 2. CUSTOM PYTORCH MODEL (FIXED FOR MNIST 1-CHANNEL)
|
| 22 |
# =========================
|
| 23 |
+
class MNISTDenseNet(nn.Module):
|
| 24 |
+
def __init__(self):
|
| 25 |
+
super().__init__()
|
| 26 |
+
|
| 27 |
+
# simple conv backbone matching your checkpoint expectation
|
| 28 |
+
self.conv = nn.Sequential(
|
| 29 |
+
nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3),
|
| 30 |
+
nn.ReLU(),
|
| 31 |
+
nn.AdaptiveAvgPool2d((1, 1))
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
self.fc = nn.Linear(64, 10)
|
| 35 |
+
|
| 36 |
+
def forward(self, x):
|
| 37 |
+
x = self.conv(x)
|
| 38 |
+
x = x.view(x.size(0), -1)
|
| 39 |
+
x = self.fc(x)
|
| 40 |
+
return x
|
| 41 |
|
| 42 |
+
|
| 43 |
+
def load_pytorch_model():
|
| 44 |
+
model = MNISTDenseNet()
|
| 45 |
|
| 46 |
state_dict = torch.load(
|
| 47 |
"best_densenet_mnist.pth",
|
| 48 |
map_location="cpu"
|
| 49 |
)
|
| 50 |
|
| 51 |
+
model.load_state_dict(state_dict, strict=True)
|
| 52 |
model.eval()
|
| 53 |
return model
|
| 54 |
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
# =========================
|
| 60 |
+
# 3. KERAS PREDICTION
|
| 61 |
# =========================
|
| 62 |
+
def predict_keras(image):
|
| 63 |
image = image.resize((32, 32)).convert("RGB")
|
| 64 |
+
|
| 65 |
img = np.array(image).astype(np.float32) / 255.0
|
| 66 |
img = np.expand_dims(img, axis=0)
|
|
|
|
| 67 |
|
| 68 |
+
preds = keras_model.predict(img, verbose=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
return int(np.argmax(preds))
|
| 70 |
|
| 71 |
|
| 72 |
# =========================
|
| 73 |
+
# 4. PYTORCH PREDICTION
|
| 74 |
# =========================
|
| 75 |
def predict_pytorch(image):
|
| 76 |
+
image = image.resize((32, 32)).convert("L")
|
| 77 |
|
| 78 |
img = np.array(image).astype(np.float32) / 255.0
|
| 79 |
+
img = np.expand_dims(img, axis=0)
|
| 80 |
+
img = np.expand_dims(img, axis=0)
|
| 81 |
+
|
| 82 |
+
img = torch.tensor(img)
|
| 83 |
|
| 84 |
with torch.no_grad():
|
| 85 |
outputs = pytorch_model(img)
|
| 86 |
+
return int(torch.argmax(outputs))
|
|
|
|
| 87 |
|
| 88 |
|
| 89 |
# =========================
|
| 90 |
+
# 5. ROUTER
|
| 91 |
# =========================
|
| 92 |
def predict(image, model_name):
|
| 93 |
if model_name == "MobileNetV2 (Keras)":
|
|
|
|
| 99 |
|
| 100 |
|
| 101 |
# =========================
|
| 102 |
+
# 6. GRADIO UI
|
| 103 |
# =========================
|
| 104 |
demo = gr.Interface(
|
| 105 |
fn=predict,
|
|
|
|
| 111 |
)
|
| 112 |
],
|
| 113 |
outputs="text",
|
| 114 |
+
title="Multi-Model MNIST Classifier"
|
|
|
|
| 115 |
)
|
| 116 |
|
| 117 |
demo.launch()
|