Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import torch | |
| import torch.nn as nn | |
| import torchvision.transforms as transforms | |
| from PIL import Image | |
| from safetensors.torch import load_file | |
| from huggingface_hub import hf_hub_download | |
| # Model architecture | |
| class AdvancedCNN(nn.Module): | |
| def __init__(self, num_classes=10): | |
| super(AdvancedCNN, self).__init__() | |
| self.conv1 = nn.Conv2d(in_channels=3, out_channels=32 * 2, kernel_size=3, padding=1) | |
| self.conv2 = nn.Conv2d(in_channels=32 * 2, out_channels=64 * 2, kernel_size=3, padding=1) | |
| self.conv3 = nn.Conv2d(in_channels=64 * 2, out_channels=64 * 2, kernel_size=3, padding=1) | |
| self.conv4 = nn.Conv2d(in_channels=64 * 2, out_channels=128 * 2, kernel_size=3, padding=1) | |
| self.conv5 = nn.Conv2d(in_channels=128 * 2, out_channels=128 * 2, kernel_size=3, padding=1) | |
| self.conv6 = nn.Conv2d(in_channels=128 * 2, out_channels=128 * 2, kernel_size=3, padding=1) | |
| self.conv7 = nn.Conv2d(in_channels=128 * 2, out_channels=256 * 2, kernel_size=3, padding=1) | |
| self.conv8 = nn.Conv2d(in_channels=256 * 2, out_channels=256 * 2, kernel_size=3, padding=1) | |
| self.conv9 = nn.Conv2d(in_channels=256 * 2, out_channels=256 * 2, kernel_size=3, padding=1) | |
| self.bn1 = nn.BatchNorm2d(32 * 2) | |
| self.bn2 = nn.BatchNorm2d(128 * 2) | |
| self.bn3 = nn.BatchNorm2d(256 * 2) | |
| self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2) | |
| self.dropout = nn.Dropout2d(0.2) | |
| self.fc1 = nn.Linear(4096 * 2, 4096 * 2) | |
| self.fc2 = nn.Linear(4096 * 2, 2048 * 2) | |
| self.fc3 = nn.Linear(2048 * 2, num_classes) | |
| self.relu = nn.ReLU() | |
| def forward(self, x): | |
| x = self.relu(self.bn1(self.conv1(x))) | |
| x = self.relu(self.conv2(x)) | |
| x = self.relu(self.conv3(x)) | |
| x = self.maxpool(x) | |
| x = self.relu(self.bn2(self.conv4(x))) | |
| x = self.relu(self.conv5(x)) | |
| x = self.relu(self.conv6(x)) | |
| x = self.maxpool(x) | |
| x = self.dropout(x) | |
| x = self.relu(self.bn3(self.conv7(x))) | |
| x = self.relu(self.conv8(x)) | |
| x = self.relu(self.conv9(x)) | |
| x = self.maxpool(x) | |
| x = self.dropout(x) | |
| x = torch.flatten(x, start_dim=1) | |
| x = self.relu(self.fc1(x)) | |
| x = self.relu(self.fc2(x)) | |
| x = self.dropout(x) | |
| x = self.fc3(x) | |
| return x | |
| # Load model with try/except | |
| device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") | |
| try: | |
| model = AdvancedCNN(num_classes=10).to(device) | |
| model_path = hf_hub_download(repo_id="chandu1617/CIFAR10-CNN_Model", filename="model.safetensors") | |
| state_dict = load_file(model_path) | |
| model.load_state_dict(state_dict) | |
| model.eval() | |
| load_status = "Model loaded successfully!" | |
| except Exception as e: | |
| load_status = f"Model load error: {str(e)}" | |
| # CIFAR-10 classes | |
| classes = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] | |
| # Image Preprocessing | |
| def preprocess_image(image): | |
| transform = transforms.Compose([ | |
| transforms.Resize((32, 32)), | |
| transforms.ToTensor(), | |
| transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) | |
| ]) | |
| image = transform(image).unsqueeze(0) | |
| return image.to(device) | |
| # Predict function | |
| def predict(image): | |
| if image is None: | |
| return "Please upload an image." | |
| try: | |
| image = preprocess_image(image) | |
| with torch.no_grad(): | |
| output = model(image) | |
| prob = torch.softmax(output, dim=1) | |
| conf, predicted = torch.max(prob, 1) | |
| return f"Predicted: {classes[predicted.item()]} --> (Confidence: {conf.item()*100:.2f}%) => Overall Model Acc: 92.59%" | |
| except Exception as e: | |
| return f"Prediction error: {str(e)}\\n{load_status}" | |
| # Gradio interface | |
| interface = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(type="pil", label="Upload an image (32x32 RGB recommended)"), | |
| outputs=gr.Textbox(label="Prediction"), | |
| title="π CIFAR-10 CNN Classifier Demo", | |
| description="Upload a small RGB image to classify it. Model Acc: 92.59% on CIFAR-10 test set!", | |
| examples=[ | |
| ["https://www.cs.toronto.edu/~kriz/cifar-10-sample/airplane1.png"], | |
| ["https://www.cs.toronto.edu/~kriz/cifar-10-sample/cat1.png"], | |
| ["https://www.cs.toronto.edu/~kriz/cifar-10-sample/dog1.png"] | |
| ] | |
| ) | |
| if __name__ == "__main__": | |
| interface.launch() |