Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +94 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from torchvision import transforms
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from huggingface_hub import snapshot_download
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
|
| 12 |
+
# === Model Definition ===
|
| 13 |
+
class CNNModel(nn.Module):
|
| 14 |
+
def __init__(self, dropout_rate=0.5, hidden_size=512, use_batchnorm=True):
|
| 15 |
+
super(CNNModel, self).__init__()
|
| 16 |
+
self.use_batchnorm = use_batchnorm
|
| 17 |
+
|
| 18 |
+
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
|
| 19 |
+
self.bn1 = nn.BatchNorm2d(32) if use_batchnorm else nn.Identity()
|
| 20 |
+
|
| 21 |
+
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
|
| 22 |
+
self.bn2 = nn.BatchNorm2d(64) if use_batchnorm else nn.Identity()
|
| 23 |
+
|
| 24 |
+
self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
|
| 25 |
+
self.bn3 = nn.BatchNorm2d(128) if use_batchnorm else nn.Identity()
|
| 26 |
+
|
| 27 |
+
self.conv4 = nn.Conv2d(128, 256, kernel_size=3, padding=1)
|
| 28 |
+
self.bn4 = nn.BatchNorm2d(256) if use_batchnorm else nn.Identity()
|
| 29 |
+
|
| 30 |
+
self.conv5 = nn.Conv2d(256, 256, kernel_size=3, padding=1)
|
| 31 |
+
self.bn5 = nn.BatchNorm2d(256) if use_batchnorm else nn.Identity()
|
| 32 |
+
|
| 33 |
+
self.pool = nn.MaxPool2d(2, 2)
|
| 34 |
+
self.dropout = nn.Dropout(dropout_rate)
|
| 35 |
+
|
| 36 |
+
self.fc1 = nn.Linear(256 * 7 * 7, hidden_size)
|
| 37 |
+
self.fc2 = nn.Linear(hidden_size, 1)
|
| 38 |
+
|
| 39 |
+
def forward(self, x):
|
| 40 |
+
x = self.pool(F.relu(self.bn1(self.conv1(x))))
|
| 41 |
+
x = self.pool(F.relu(self.bn2(self.conv2(x))))
|
| 42 |
+
x = self.pool(F.relu(self.bn3(self.conv3(x))))
|
| 43 |
+
x = self.pool(F.relu(self.bn4(self.conv4(x))))
|
| 44 |
+
x = self.pool(F.relu(self.bn5(self.conv5(x))))
|
| 45 |
+
x = torch.flatten(x, 1)
|
| 46 |
+
x = F.relu(self.fc1(x))
|
| 47 |
+
x = self.dropout(x)
|
| 48 |
+
x = self.fc2(x)
|
| 49 |
+
return x
|
| 50 |
+
|
| 51 |
+
# === Load Model from Hugging Face ===
|
| 52 |
+
def load_model(repo_id="ZacToh/RandomSearchCNN"):
|
| 53 |
+
download_dir = snapshot_download(repo_id)
|
| 54 |
+
model_path = os.path.join(download_dir, "cnn_final_model.pth")
|
| 55 |
+
|
| 56 |
+
model = CNNModel()
|
| 57 |
+
checkpoint = torch.load(model_path, map_location=DEVICE)
|
| 58 |
+
state_dict = checkpoint.get("model_state_dict", checkpoint)
|
| 59 |
+
model.load_state_dict(state_dict, strict=False)
|
| 60 |
+
|
| 61 |
+
model.to(DEVICE)
|
| 62 |
+
model.eval()
|
| 63 |
+
return model
|
| 64 |
+
|
| 65 |
+
model = load_model()
|
| 66 |
+
|
| 67 |
+
# === Image Transform ===
|
| 68 |
+
transform = transforms.Compose([
|
| 69 |
+
transforms.Resize((224, 224)),
|
| 70 |
+
transforms.ToTensor(),
|
| 71 |
+
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
|
| 72 |
+
])
|
| 73 |
+
|
| 74 |
+
# === Prediction Function ===
|
| 75 |
+
def predict(img: Image.Image) -> str:
|
| 76 |
+
img_tensor = transform(img).unsqueeze(0).to(DEVICE)
|
| 77 |
+
|
| 78 |
+
with torch.no_grad():
|
| 79 |
+
output = model(img_tensor)
|
| 80 |
+
prob = torch.sigmoid(output).item()
|
| 81 |
+
|
| 82 |
+
label = "hf" if prob > 0.5 else "cc"
|
| 83 |
+
confidence = prob * 100 if label == "hf" else (1 - prob) * 100
|
| 84 |
+
|
| 85 |
+
return f"Prediction: {label.upper()} ({confidence:.2f}%)"
|
| 86 |
+
|
| 87 |
+
# === Gradio UI ===
|
| 88 |
+
gr.Interface(
|
| 89 |
+
fn=predict,
|
| 90 |
+
inputs=gr.Image(type="pil"),
|
| 91 |
+
outputs="text",
|
| 92 |
+
title="CNN Classifier: CC vs HF",
|
| 93 |
+
description="Upload an image to classify whether it belongs to the 'cc' or 'hf' category using a CNN model."
|
| 94 |
+
).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
gradio
|
| 4 |
+
huggingface_hub
|