Spaces:
Sleeping
Sleeping
Upload 9 files
Browse files- .gitattributes +5 -0
- Screenshot 2026-01-31 150516.png +3 -0
- Screenshot 2026-01-31 150636.png +3 -0
- Screenshot 2026-01-31 150840.png +3 -0
- Screenshot 2026-01-31 151118.png +3 -0
- Screenshot 2026-01-31 151244.png +3 -0
- app.py +126 -0
- requirements.txt +5 -0
- stl10_cnn_model.pkl +3 -0
- stl10_target_names.pkl +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,8 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
Screenshot[[:space:]]2026-01-31[[:space:]]150516.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
Screenshot[[:space:]]2026-01-31[[:space:]]150636.png filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
Screenshot[[:space:]]2026-01-31[[:space:]]150840.png filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
Screenshot[[:space:]]2026-01-31[[:space:]]151118.png filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
Screenshot[[:space:]]2026-01-31[[:space:]]151244.png filter=lfs diff=lfs merge=lfs -text
|
Screenshot 2026-01-31 150516.png
ADDED
|
Git LFS Details
|
Screenshot 2026-01-31 150636.png
ADDED
|
Git LFS Details
|
Screenshot 2026-01-31 150840.png
ADDED
|
Git LFS Details
|
Screenshot 2026-01-31 151118.png
ADDED
|
Git LFS Details
|
Screenshot 2026-01-31 151244.png
ADDED
|
Git LFS Details
|
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
import torch.nn.functional as F
|
| 6 |
+
import joblib
|
| 7 |
+
import torchvision.transforms as transforms
|
| 8 |
+
from PIL import Image
|
| 9 |
+
|
| 10 |
+
class STL10Net(nn.Module):
|
| 11 |
+
def __init__(self):
|
| 12 |
+
super(STL10Net, self).__init__()
|
| 13 |
+
self.conv1 = nn.Conv2d(3, 32, 3, padding=1)
|
| 14 |
+
self.pool = nn.MaxPool2d(2, 2)
|
| 15 |
+
self.bn1 = nn.BatchNorm2d(32)
|
| 16 |
+
self.conv2 = nn.Conv2d(32, 64, 3, padding=1)
|
| 17 |
+
self.bn2 = nn.BatchNorm2d(64)
|
| 18 |
+
self.conv3 = nn.Conv2d(64, 128, 3, padding=1)
|
| 19 |
+
self.bn3 = nn.BatchNorm2d(128)
|
| 20 |
+
self.conv4 = nn.Conv2d(128, 256, 3, padding=1)
|
| 21 |
+
self.bn4 = nn.BatchNorm2d(256)
|
| 22 |
+
self.fc1 = nn.Linear(256 * 6 * 6, 512)
|
| 23 |
+
self.dropout = nn.Dropout(0.5)
|
| 24 |
+
self.fc2 = nn.Linear(512, 10)
|
| 25 |
+
|
| 26 |
+
def forward(self, x):
|
| 27 |
+
x = self.pool(F.relu(self.bn1(self.conv1(x))))
|
| 28 |
+
x = self.pool(F.relu(self.bn2(self.conv2(x))))
|
| 29 |
+
x = self.pool(F.relu(self.bn3(self.conv3(x))))
|
| 30 |
+
x = self.pool(F.relu(self.bn4(self.conv4(x))))
|
| 31 |
+
x = x.view(-1, 256 * 6 * 6)
|
| 32 |
+
x = F.relu(self.fc1(x))
|
| 33 |
+
x = self.dropout(x)
|
| 34 |
+
x = self.fc2(x)
|
| 35 |
+
return x
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
original_torch_load = torch.load
|
| 39 |
+
|
| 40 |
+
def cpu_load(*args, **kwargs):
|
| 41 |
+
kwargs['map_location'] = torch.device('cpu')
|
| 42 |
+
return original_torch_load(*args, **kwargs)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
torch.load = cpu_load
|
| 46 |
+
try:
|
| 47 |
+
model = joblib.load('stl10_cnn_model.pkl')
|
| 48 |
+
finally:
|
| 49 |
+
torch.load = original_torch_load
|
| 50 |
+
|
| 51 |
+
model.to('cpu')
|
| 52 |
+
model.eval()
|
| 53 |
+
|
| 54 |
+
classes = joblib.load('stl10_target_names.pkl')
|
| 55 |
+
|
| 56 |
+
transform = transforms.Compose([
|
| 57 |
+
transforms.Resize((96, 96)),
|
| 58 |
+
transforms.ToTensor(),
|
| 59 |
+
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
|
| 60 |
+
])
|
| 61 |
+
|
| 62 |
+
def predict_image(image):
|
| 63 |
+
if image is None: return None
|
| 64 |
+
image_tensor = transform(image).unsqueeze(0)
|
| 65 |
+
with torch.no_grad():
|
| 66 |
+
outputs = model(image_tensor)
|
| 67 |
+
probabilities = torch.nn.functional.softmax(outputs[0], dim=0)
|
| 68 |
+
confidences = {classes[i]: float(probabilities[i]) for i in range(10)}
|
| 69 |
+
return confidences
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
image_paths = [
|
| 73 |
+
"Screenshot 2026-01-31 150516.png",
|
| 74 |
+
"Screenshot 2026-01-31 150636.png",
|
| 75 |
+
"Screenshot 2026-01-31 150840.png",
|
| 76 |
+
"Screenshot 2026-01-31 151118.png",
|
| 77 |
+
"Screenshot 2026-01-31 151244.png"
|
| 78 |
+
]
|
| 79 |
+
|
| 80 |
+
example_images = [[path] for path in image_paths]
|
| 81 |
+
|
| 82 |
+
def load_example_image(evt: gr.SelectData):
|
| 83 |
+
"""Load the selected example image into the input component."""
|
| 84 |
+
selected_path = image_paths[evt.index]
|
| 85 |
+
return Image.open(selected_path)
|
| 86 |
+
|
| 87 |
+
class_colors = ["#FF6B6B", "#4ECDC4", "#45B7D1", "#96CEB4", "#FFEAA7",
|
| 88 |
+
"#DDA0DD", "#98D8C8", "#F7DC6F", "#BB8FCE", "#85C1E9"]
|
| 89 |
+
class_badges = " ".join([
|
| 90 |
+
f'<span style="background-color: {class_colors[i]}; color: #000; padding: 4px 12px; border-radius: 15px; margin: 2px; display: inline-block; font-weight: 500;">{cls}</span>'
|
| 91 |
+
for i, cls in enumerate(classes)
|
| 92 |
+
])
|
| 93 |
+
|
| 94 |
+
with gr.Blocks(title="STL-10 Image Classifier") as demo:
|
| 95 |
+
gr.Markdown("# STL-10 Image Classifier")
|
| 96 |
+
gr.Markdown("""
|
| 97 |
+
Upload an image to classify it into one of the 10 STL-10 categories.
|
| 98 |
+
|
| 99 |
+
The model is based on a CNN architecture trained on the STL-10 dataset.
|
| 100 |
+
""")
|
| 101 |
+
gr.Markdown("**Supported Classes:**")
|
| 102 |
+
gr.HTML(f'<div style="margin: 10px 0; line-height: 2.2;">{class_badges}</div>')
|
| 103 |
+
|
| 104 |
+
with gr.Row():
|
| 105 |
+
with gr.Column():
|
| 106 |
+
image_input = gr.Image(type="pil", label="Input Image")
|
| 107 |
+
submit_btn = gr.Button("Classify", variant="primary")
|
| 108 |
+
|
| 109 |
+
gr.Markdown("### Click an example image to select it:")
|
| 110 |
+
example_gallery = gr.Gallery(
|
| 111 |
+
value=image_paths,
|
| 112 |
+
label="Examples",
|
| 113 |
+
columns=5,
|
| 114 |
+
rows=1,
|
| 115 |
+
object_fit="contain",
|
| 116 |
+
height="auto",
|
| 117 |
+
allow_preview=False
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
with gr.Column():
|
| 121 |
+
label_output = gr.Label(num_top_classes=3, label="Predictions")
|
| 122 |
+
|
| 123 |
+
example_gallery.select(fn=load_example_image, inputs=None, outputs=image_input)
|
| 124 |
+
submit_btn.click(fn=predict_image, inputs=image_input, outputs=label_output)
|
| 125 |
+
|
| 126 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
torch
|
| 3 |
+
torchvision
|
| 4 |
+
joblib
|
| 5 |
+
Pillow
|
stl10_cnn_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e23e3fa28e06965880cccb4fe7bdbc7f42a72657a77370c8ebea4bedfa1615cc
|
| 3 |
+
size 20470652
|
stl10_target_names.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:212e10f8c61b41b8bab4018b015fc353a2d2fb66f2c5147cc3444c2a101cb615
|
| 3 |
+
size 91
|