Kshitij-369 commited on
Commit
d5aaff2
·
1 Parent(s): 80e776e

Initial commit

Browse files
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import os
4
+ import torchvision.transforms as transforms
5
+ from timeit import default_timer as timer
6
+
7
+ # ResNet9 model definition
8
+ def conv_block(in_channels, out_channels, pool=False):
9
+ layers = [torch.nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
10
+ torch.nn.BatchNorm2d(out_channels),
11
+ torch.nn.ReLU(inplace=True)]
12
+ if pool: layers.append(torch.nn.MaxPool2d(2))
13
+ return torch.nn.Sequential(*layers)
14
+
15
+ class ResNet9(torch.nn.Module):
16
+ def __init__(self, in_channels, num_classes):
17
+ super().__init__()
18
+
19
+ self.conv1 = conv_block(in_channels, 64)
20
+ self.conv2 = conv_block(64, 128, pool=True)
21
+ self.res1 = torch.nn.Sequential(conv_block(128, 128), conv_block(128, 128))
22
+
23
+ self.conv3 = conv_block(128, 256, pool=True)
24
+ self.conv4 = conv_block(256, 512, pool=True)
25
+ self.res2 = torch.nn.Sequential(conv_block(512, 512), conv_block(512, 512))
26
+
27
+ self.classifier = torch.nn.Sequential(torch.nn.MaxPool2d(4),
28
+ torch.nn.Flatten(),
29
+ torch.nn.Dropout(0.2),
30
+ torch.nn.Linear(512, num_classes))
31
+
32
+ def forward(self, xb):
33
+ out = self.conv1(xb)
34
+ out = self.conv2(out)
35
+ out = self.res1(out) + out
36
+ out = self.conv3(out)
37
+ out = self.conv4(out)
38
+ out = self.res2(out) + out
39
+ out = self.classifier(out)
40
+ return out
41
+
42
+ # Load the trained model
43
+ model = ResNet9(3, 10)
44
+ model.load_state_dict(torch.load('cifar10-resnet9.pth', map_location=torch.device('cpu')))
45
+ model.eval()
46
+
47
+ # Define the CIFAR-10 classes
48
+ class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
49
+
50
+ # Define the image transformations
51
+ transform = transforms.Compose([
52
+ transforms.Resize((32, 32)),
53
+ transforms.ToTensor(),
54
+ transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
55
+ ])
56
+
57
+ def predict(img):
58
+ start_time = timer() # Start the timer
59
+ img = transform(img).unsqueeze(0) # Apply transforms and add batch dimension
60
+ with torch.no_grad():
61
+ preds = model(img)
62
+ probabilities = torch.nn.functional.softmax(preds, dim=1)
63
+ top_prob, top_catid = torch.topk(probabilities, 5)
64
+ end_time = timer() # End the timer
65
+
66
+ prediction_time = end_time - start_time
67
+
68
+ # Ensure that we use the correct dimensions
69
+ top_prob = top_prob.squeeze().tolist()
70
+ top_catid = top_catid.squeeze().tolist()
71
+
72
+ # Construct the prediction dictionary
73
+ prediction = {class_names[idx]: prob for idx, prob in zip(top_catid, top_prob)}
74
+
75
+ return prediction, prediction_time
76
+
77
+
78
+ # Example images for the Gradio interface
79
+ examples = [
80
+ ["/content/data/cifar10/test/airplane/0001.png"],
81
+ ["/content/data/cifar10/test/bird/0007.png"],
82
+ ["/content/data/cifar10/test/dog/0004.png"],
83
+ ["/content/data/cifar10/test/ship/0009.png"]
84
+ ]
85
+
86
+ # Create the Gradio interface
87
+ demo = gr.Interface(fn=predict,
88
+ inputs=gr.Image(type="pil"),
89
+ outputs=[gr.Label(num_top_classes=5, label="Predictions"),
90
+ gr.Number(label="Prediction time (s)")],
91
+ examples=examples,
92
+ title="CIFAR-10 Image Classifier",
93
+ description="A computer Vision Model to Classify images 10 classes from CIFAR10 Dataset.",
94
+ allow_flagging="never")
95
+
96
+ demo.launch()
cifar10-resnet9.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a4c8ba77746a6e0b511d97c32920e82d06b83e3f2bd6c6f6bf6ba79f4f82e818
3
+ size 26339032
examples/aeroplane.jpeg ADDED
examples/automobile.jpg ADDED
examples/dog.jpg ADDED
examples/frog.jpeg ADDED
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch
2
+ torchvision
3
+ gradio
4
+ Pillow