NifulIslam commited on
Commit
74d8e02
·
1 Parent(s): 68fb9fd

Upload 5 files

Browse files
Files changed (3) hide show
  1. app.py +84 -0
  2. encoder.pkl +3 -0
  3. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import ImageFile, Image
2
+ import gradio as gr
3
+ import torch
4
+ import torch.nn as nn
5
+ import torch.nn.functional as F
6
+ import torchvision.transforms as transforms
7
+ import pickle
8
+ from torchvision import transforms, models
9
+
10
+ class FineTunedVGG(nn.Module):
11
+ def __init__(self, num_classes, input_size=224):
12
+ super(FineTunedVGG, self).__init__()
13
+ self.vgg = models.vgg16(pretrained=True)
14
+
15
+ self.st = 8
16
+ self.blocks = []
17
+ for param in self.vgg.parameters():
18
+ param.requires_grad = False
19
+
20
+ x = torch.randn(1, 3, input_size, input_size)
21
+ for idx, layer in enumerate(self.vgg.features):
22
+ if isinstance(layer, nn.Conv2d):
23
+ x = layer(x)
24
+ if idx in [12, 22, 32]:
25
+ self.blocks.append(x)
26
+
27
+
28
+ for idx, block in enumerate(self.blocks):
29
+
30
+ filters = block.size(1)
31
+ depthwise_conv = nn.Conv2d(filters, filters, kernel_size=3, padding=1, groups=filters)
32
+ depthwise_sep_conv = nn.Conv2d(filters, 128, kernel_size=1, padding=0)
33
+ bn = nn.BatchNorm2d(128)
34
+ pooled_block = nn.MaxPool2d(kernel_size=self.st, stride=self.st)
35
+ self.st = self.st // 2
36
+ self.blocks[idx] = nn.Sequential(depthwise_conv, depthwise_sep_conv, bn, pooled_block)
37
+
38
+ self.vgg.add_module('ConcatenatedBlocks', nn.Sequential(*self.blocks))
39
+
40
+ self.avgpool = nn.AdaptiveAvgPool2d((1,1))
41
+ self.fc = nn.Linear(1000, num_classes)
42
+
43
+ def forward(self, x):
44
+ x = self.vgg(x)
45
+ x = x.view(x.size(0), -1)
46
+ x = self.fc(x)
47
+ return x
48
+
49
+ model = torch.load("model.pth",map_location ='cpu')
50
+ with open("encoder.pkl", "rb") as encoder_file:
51
+ label_encoder = pickle.load(encoder_file)
52
+
53
+ def preprocess_image(image_path):
54
+ transform = transforms.Compose([
55
+ transforms.Resize((224, 224)),
56
+ transforms.ToTensor(),
57
+ transforms.Normalize(mean=[0.0, 0.0, 0.0], std=[1.0, 1.0, 1.0]),
58
+ ])
59
+ image = transform(image)
60
+ image = image.unsqueeze(0)
61
+ return image
62
+
63
+
64
+
65
+ def recognize_image(image):
66
+ output = model.predict(image)
67
+ probs = torch.softmax(output, dim=1)[0].tolist()
68
+ class_labels = label_encoder.classes_
69
+ output_dict = dict(zip(class_labels, map(float, probs)))
70
+ return output_dict
71
+
72
+ image = gr.inputs.Image(shape=(224,224))
73
+ label = gr.outputs.Label(num_top_classes=10)
74
+
75
+ examples = [
76
+ 'test_imgs/bike.jpg',
77
+ 'test_imgs/boat.jpg',
78
+ 'test_imgs/boat_2.png',
79
+ 'test_imgs/easybike.jpg',
80
+ ]
81
+
82
+
83
+ iface = gr.Interface(fn=recognize_image, inputs=image, outputs=label, examples=examples)
84
+ iface.launch(inline=False)
encoder.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8203be10c669394ff576ee62c8cb927713b89a361f21fbe3fae32ef09da85b22
3
+ size 292
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio==3.16.0
2
+ ipywidgets==8.0.7
3
+ torch==2.0.1