Mangara01's picture
Update app.py
fcbbec0
import torch
import torch.nn as nn
import torchvision.transforms as transforms
from PIL import Image
import gradio as gr
class_labels = ['Dog', 'Horse', 'Elephant', 'Butterfly', 'Chicken', 'Cat', 'Cow', 'Sheep', 'Spider', 'Squirrel']
transform = transforms.Compose([
transforms.Resize((128, 128)),
transforms.ToTensor(),
])
class Animal(nn.Module):
def __init__(self, num_classes=10):
super(Animal, self).__init__()
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm2d(32)
self.relu1 = nn.ReLU()
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm2d(64)
self.relu2 = nn.ReLU()
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
self.bn3 = nn.BatchNorm2d(128)
self.relu3 = nn.ReLU()
self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(128 * 16 * 16, 512)
self.relu4 = nn.ReLU()
self.dropout = nn.Dropout(0.5)
self.fc2 = nn.Linear(512, num_classes)
def forward(self, x):
x = self.pool1(self.relu1(self.bn1(self.conv1(x))))
x = self.pool2(self.relu2(self.bn2(self.conv2(x))))
x = self.pool3(self.relu3(self.bn3(self.conv3(x))))
x = x.view(x.size(0), -1)
x = self.relu4(self.fc1(x))
x = self.dropout(x)
x = self.fc2(x)
return x
model = Animal()
model.load_state_dict(torch.load('model.pth', map_location=torch.device('cpu')))
model.eval()
def predict_class_with_confidence(input_image):
input_image = Image.fromarray(input_image)
input_image = transform(input_image).unsqueeze(0)
with torch.no_grad():
output = model(input_image)
_, predicted_class = torch.max(output.data, 1)
predicted_label = class_labels[predicted_class.item()]
return predicted_label
app = gr.Interface(
fn=predict_class_with_confidence,
inputs=gr.inputs.Image(),
outputs="text",
live=True,
capture_session=True,
title="Animal Classification App",
description="Upload an image of an animal to classify it",
)
if __name__ == '__main__':
app.launch(share=True)