Spaces:
Sleeping
Sleeping
Commit
·
cbec0aa
1
Parent(s):
0739808
cnn-classifier-app added
Browse files- app.py +30 -0
- core/__init__.py +0 -0
- core/__pycache__/__init__.cpython-311.pyc +0 -0
- core/__pycache__/predict.cpython-311.pyc +0 -0
- core/predict.py +103 -0
- model/cnn-trained-model.pth +3 -0
- requirments.txt +5 -0
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from core.predict import ImageClassifier
|
3 |
+
import os
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
cwd = os.getcwd()
|
7 |
+
model_path = os.path.join(cwd,'model','cnn-trained-model.pth')
|
8 |
+
class_name = {0 : 'Cat' ,1 : 'Dog' ,2 : 'person'}
|
9 |
+
classifier = ImageClassifier(model_path=model_path,class_name=None)
|
10 |
+
|
11 |
+
|
12 |
+
def classify_image(image):
|
13 |
+
image_path = 'uploaded_image.jpg'
|
14 |
+
image.save(image_path)
|
15 |
+
|
16 |
+
label,output_path = classifier.predict(image_path)
|
17 |
+
|
18 |
+
return label,Image.open(output_path)
|
19 |
+
|
20 |
+
demo = gr.Interface(
|
21 |
+
fn=classify_image,
|
22 |
+
inputs = gr.Image(type='pil'),
|
23 |
+
outputs=[gr.Textbox(label="Prediction"),gr.Image(label="Labeled Image")],
|
24 |
+
title="Image Classification Gradio app",
|
25 |
+
description="Upload an Image to classify it as Dog,Cat or Person"
|
26 |
+
)
|
27 |
+
|
28 |
+
|
29 |
+
if __name__ == '__main__':
|
30 |
+
demo.launch()
|
core/__init__.py
ADDED
File without changes
|
core/__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (178 Bytes). View file
|
|
core/__pycache__/predict.cpython-311.pyc
ADDED
Binary file (6.86 kB). View file
|
|
core/predict.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torchvision.transforms as transforms
|
4 |
+
from PIL import Image
|
5 |
+
import cv2 as cv
|
6 |
+
import os
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
class CustomCnnModel(nn.Module):
|
11 |
+
def __init__(self,input_dim,num_classes):
|
12 |
+
super(CustomCnnModel,self).__init__()
|
13 |
+
self.input_dim = input_dim
|
14 |
+
self.num_classes = num_classes
|
15 |
+
|
16 |
+
self.conv_layers = nn.Sequential(
|
17 |
+
nn.Conv2d(in_channels=3,out_channels=32,kernel_size=3,stride=1,padding=1), # 122x128x3 --> 3x3x3x32 --> wxhx32
|
18 |
+
nn.BatchNorm2d(num_features=32),
|
19 |
+
nn.ReLU(),
|
20 |
+
nn.MaxPool2d(kernel_size=2,stride=2),
|
21 |
+
|
22 |
+
nn.Conv2d(in_channels=32,out_channels=64,kernel_size=3,stride=1,padding=1),
|
23 |
+
nn.BatchNorm2d(num_features=64),
|
24 |
+
nn.ReLU(),
|
25 |
+
nn.MaxPool2d(kernel_size=2,stride=2),
|
26 |
+
|
27 |
+
nn.Conv2d(in_channels=64,out_channels=128,kernel_size=3,stride=1,padding=1),
|
28 |
+
nn.BatchNorm2d(num_features=128),
|
29 |
+
nn.ReLU(),
|
30 |
+
nn.MaxPool2d(kernel_size=2,stride=2),
|
31 |
+
|
32 |
+
nn.Conv2d(in_channels=128,out_channels=256,kernel_size=3,stride=1,padding=1),
|
33 |
+
nn.BatchNorm2d(num_features=256),
|
34 |
+
nn.ReLU(),
|
35 |
+
nn.MaxPool2d(kernel_size=2,stride=2),
|
36 |
+
)
|
37 |
+
|
38 |
+
self._to_linear = None
|
39 |
+
self._get_conv_output(self.input_dim)
|
40 |
+
|
41 |
+
|
42 |
+
self.fc_layers = nn.Sequential(
|
43 |
+
nn.Linear(self._to_linear,512),
|
44 |
+
nn.ReLU(),
|
45 |
+
nn.Linear(512,128),
|
46 |
+
nn.ReLU(),
|
47 |
+
nn.Linear(128,self.num_classes)
|
48 |
+
)
|
49 |
+
|
50 |
+
|
51 |
+
def _get_conv_output(self,input_dim=128):
|
52 |
+
with torch.no_grad():
|
53 |
+
dummy_input = torch.zeros(1,3,input_dim,input_dim)
|
54 |
+
output = self.conv_layers(dummy_input)
|
55 |
+
self._to_linear = output.view(1,-1).size(1)
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
|
60 |
+
def forward(self,x):
|
61 |
+
x = self.conv_layers(x)
|
62 |
+
x = x.view(x.size(0),-1)
|
63 |
+
x = self.fc_layers(x)
|
64 |
+
return x
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
class ImageClassifier:
|
69 |
+
def __init__(self,model_path,class_name=None):
|
70 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
71 |
+
self.model = CustomCnnModel(input_dim=128,num_classes=3).to(self.device)
|
72 |
+
self.model.load_state_dict(torch.load(model_path,map_location=self.device))
|
73 |
+
self.model.eval()
|
74 |
+
if class_name is None:
|
75 |
+
self.class_name = {0: 'Cat', 1: 'Dog', 2: 'person'}
|
76 |
+
else:
|
77 |
+
self.class_name = class_name
|
78 |
+
|
79 |
+
self.transform = transforms.Compose([
|
80 |
+
transforms.Resize((128,128)),
|
81 |
+
transforms.ToTensor(),
|
82 |
+
transforms.Normalize(mean=[0.5,0.5,0.5],std=[0.5,0.5,0.5]),
|
83 |
+
]
|
84 |
+
)
|
85 |
+
|
86 |
+
def predict(self,image_path):
|
87 |
+
image = Image.open(image_path).convert('RGB')
|
88 |
+
image_tensor = self.transform(image).unsqueeze(0).to(self.device)
|
89 |
+
|
90 |
+
with torch.no_grad():
|
91 |
+
output = self.model(image_tensor)
|
92 |
+
_,predicted = torch.max(output,1)
|
93 |
+
|
94 |
+
label = self.class_name[predicted.item()]
|
95 |
+
|
96 |
+
img = cv.imread(image_path)
|
97 |
+
cv.putText(img,label,(10,30),cv.FONT_HERSHEY_SIMPLEX,1,(255,0,0),2)
|
98 |
+
output_path = 'labeled-image.jpg'
|
99 |
+
cv.imwrite(output_path,img)
|
100 |
+
cwd = os.getcwd()
|
101 |
+
output_path = os.path.join(cwd,output_path)
|
102 |
+
|
103 |
+
return label,output_path
|
model/cnn-trained-model.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d428f26d1cb36ee748e2ef67df1290a405cc51e31254ae68d4288acb5b7add2d
|
3 |
+
size 35393367
|
requirments.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
torch
|
3 |
+
torchvision
|
4 |
+
opencv-python
|
5 |
+
pillow
|