Spaces:
Running
Running
File size: 8,338 Bytes
fe1195b d13bccc fe1195b d13bccc fe1195b d13bccc fe1195b d13bccc fe1195b d13bccc fe1195b d13bccc fe1195b d13bccc fe1195b d13bccc fe1195b d13bccc fe1195b d13bccc fe1195b d13bccc fe1195b 1082a29 fe1195b 1082a29 fe1195b d13bccc fe1195b d13bccc fe1195b e1ab149 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
import os
import gradio as gr
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from PIL import Image
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import timm
class BaseModel(nn.Module):
def predict(self, x: torch.Tensor) -> torch.Tensor:
with torch.no_grad():
logits = self(x)
return F.softmax(logits, dim=1)
def get_num_classes(self) -> int:
raise NotImplementedError
class CNNModel(BaseModel):
def __init__(self, num_classes: int, input_size: int = 224):
super(CNNModel, self).__init__()
self.conv_layers = nn.Sequential(
# First block: 32 filters
nn.Conv2d(3, 32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(2),
# Second block: 64 filters
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(2),
# Third block: 128 filters
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(2),
# Global Average Pooling
nn.AdaptiveAvgPool2d(1)
)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Dropout(0.5),
nn.Linear(128, 256),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256, num_classes)
)
self._initialize_weights()
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
nn.init.constant_(m.bias, 0)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.conv_layers(x)
return self.classifier(x)
def get_num_classes(self) -> int:
return self.classifier[-1].out_features
class EfficientNetModel(BaseModel):
def __init__(
self,
num_classes: int,
model_name: str = "efficientnet_b0",
pretrained: bool = True
):
super(EfficientNetModel, self).__init__()
self.base_model = timm.create_model(
model_name,
pretrained=pretrained,
num_classes=0
)
with torch.no_grad():
dummy_input = torch.randn(1, 3, 224, 224)
features = self.base_model(dummy_input)
feature_dim = features.shape[1]
self.classifier = nn.Sequential(
nn.Dropout(0.2),
nn.Linear(feature_dim, num_classes)
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
features = self.base_model(x)
return self.classifier(features)
def get_num_classes(self) -> int:
return self.classifier[-1].out_features
class AnimalClassifierApp:
def __init__(self):
"""Initialize the application."""
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.labels = ["bird", "cat", "dog", "horse"]
self.transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
])
self.models = self.load_models()
if not self.models:
print("Warning: No models found in checkpoints directory!")
def load_models(self):
"""Load both trained models."""
models = {}
try:
efficientnet = EfficientNetModel(num_classes=len(self.labels))
efficientnet_path = os.path.join("checkpoints", "efficientnet", "efficientnet_best_model.pth")
if os.path.exists(efficientnet_path):
checkpoint = torch.load(efficientnet_path, map_location=self.device, weights_only=True)
state_dict = checkpoint.get('model_state_dict', checkpoint)
efficientnet.load_state_dict(state_dict, strict=False)
efficientnet.eval()
models['EfficientNet'] = efficientnet
print("Successfully loaded EfficientNet model")
except Exception as e:
print(f"Error loading EfficientNet model: {str(e)}")
try:
cnn = CNNModel(num_classes=len(self.labels))
cnn_path = os.path.join("checkpoints", "cnn", "cnn_best_model.pth")
if os.path.exists(cnn_path):
checkpoint = torch.load(cnn_path, map_location=self.device, weights_only=True)
state_dict = checkpoint.get('model_state_dict', checkpoint)
cnn.load_state_dict(state_dict, strict=False)
cnn.eval()
models['CNN'] = cnn
print("Successfully loaded CNN model")
except Exception as e:
print(f"Error loading CNN model: {str(e)}")
return models
def predict(self, image: Image.Image):
if not self.models:
return ["No trained models found. Please train the models first.", ""]
# Preprocess image
img_tensor = self.transform(image).unsqueeze(0).to(self.device)
results = {}
probabilities = {}
for model_name, model in self.models.items():
with torch.no_grad():
output = model(img_tensor)
probs = F.softmax(output, dim=1).squeeze().cpu().numpy()
probabilities[model_name] = probs
pred_idx = np.argmax(probs)
pred_label = self.labels[pred_idx]
pred_prob = probs[pred_idx]
results[model_name] = (pred_label, pred_prob)
fig = plt.figure(figsize=(12, 5))
if 'EfficientNet' in probabilities:
plt.subplot(1, 2, 1)
plt.bar(self.labels, probabilities['EfficientNet'], color='skyblue')
plt.title('EfficientNet Predictions')
plt.ylim(0, 1)
plt.xticks(rotation=45)
plt.ylabel('Probability')
if 'CNN' in probabilities:
plt.subplot(1, 2, 2)
plt.bar(self.labels, probabilities['CNN'], color='lightcoral')
plt.title('CNN Predictions')
plt.ylim(0, 1)
plt.xticks(rotation=45)
plt.ylabel('Probability')
plt.tight_layout()
text_results = "Model Predictions:\n\n"
for model_name, (label, prob) in results.items():
text_results += f"{model_name}:\n"
text_results += f"Top prediction: {label} ({prob:.2%})\n"
text_results += "All probabilities:\n"
for label, prob in zip(self.labels, probabilities[model_name]):
text_results += f" {label}: {prob:.2%}\n"
text_results += "\n"
return [fig, text_results]
def create_interface(self):
"""Create Gradio interface."""
return gr.Interface(
fn=self.predict,
inputs=gr.Image(type="pil"),
outputs=[
gr.Plot(label="Prediction Probabilities"),
gr.Textbox(label="Detailed Results", lines=10)
],
title="Animal Classifier - Model Comparison",
description=(
"Upload an image of one of these animals: Bird, Cat, Dog, or Horse.\n"
"The app will compare predictions from both EfficientNet and CNN models.\n\n"
"Note: For best results, ensure the animal is clearly visible in the image."
)
)
def main():
app = AnimalClassifierApp()
interface = app.create_interface()
interface.launch()
if __name__ == "__main__":
main() |