Update README.md
Browse files
README.md
CHANGED
@@ -9,5 +9,189 @@ app_file: app.py
|
|
9 |
pinned: false
|
10 |
license: mit
|
11 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
9 |
pinned: false
|
10 |
license: mit
|
11 |
---
|
12 |
+
# Model Name
|
13 |
+
|
14 |
+
panda_cat_dog_classification
|
15 |
+
|
16 |
+
### Model Description
|
17 |
+
This model classifies animals among pandas, cats and dogs. It was trained using custom CNN model.
|
18 |
+
|
19 |
+
|
20 |
+
- **Developed by:** Neelima Monjusha Preeti
|
21 |
+
- **Model type:** custom CNN model
|
22 |
+
- **Language(s):** Python
|
23 |
+
- **License:** MIT
|
24 |
+
- **Contact:** [email protected]
|
25 |
+
|
26 |
+
### Data Preprocessing
|
27 |
+
The image dataset is preprocessed with the following portion:
|
28 |
+
|
29 |
+
```bash
|
30 |
+
transform = transforms.Compose([
|
31 |
+
transforms.Resize((224,224)),
|
32 |
+
transforms.ToTensor(),
|
33 |
+
transforms.Normalize((0.485,0.456,0.406),(0.229,0.224,0.225))
|
34 |
+
])
|
35 |
+
```
|
36 |
+
|
37 |
+
transforms.Resize((224,224)) resizes the input image to (224, 224) pixels.
|
38 |
+
transforms.ToTensor() converts the input image into a PyTorch tensor. Neural networks typically operate on tensors, so this transformation converts the image into a format suitable for further processing.
|
39 |
+
transforms.Normalize(()) normalizes the tensor image with mean and standard deviation. The values provided are mean and standard deviation values for each channel in the tensor.
|
40 |
+
|
41 |
+
### Model Architecture
|
42 |
+
|
43 |
+
The model was trained with custom CNN() model. this CNN architecture consists of two convolutional layers followed by two fully connected layers, and it is designed for a classification task with three classes.
|
44 |
+
|
45 |
+
```bash
|
46 |
+
class CNN(nn.Module):
|
47 |
+
def __init__(self):
|
48 |
+
super(CNN, self).__init__()
|
49 |
+
self.conv1 = nn.Conv2d(3, 6, 5)
|
50 |
+
self.conv2 = nn.Conv2d(6, 16, 5)
|
51 |
+
self.pool = nn.MaxPool2d(2, 2)
|
52 |
+
self.fc1 = nn.Linear(16 * 53 * 53, 120)
|
53 |
+
self.fc2 = nn.Linear(120, 84)
|
54 |
+
self.fc3 = nn.Linear(84, 3)
|
55 |
+
|
56 |
+
def forward(self, x):
|
57 |
+
x = self.conv1(x)
|
58 |
+
x = self.pool(x)
|
59 |
+
x = self.conv2(x)
|
60 |
+
x = self.pool(x)
|
61 |
+
x = x.view(-1, 16 * 53 * 53)
|
62 |
+
x = self.fc1(x)
|
63 |
+
x = self.fc2(x)
|
64 |
+
x = self.fc3(x)
|
65 |
+
return x
|
66 |
+
|
67 |
+
```
|
68 |
+
Then used batch_size = 8 and CrossEntropyLoss() for loss function. Then used Adam optimizer with a learning rate 0.001 for optimization process.
|
69 |
+
|
70 |
+
```bash
|
71 |
+
loss_function = nn.CrossEntropyLoss()
|
72 |
+
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
73 |
+
```
|
74 |
+
### Training Loop
|
75 |
+
|
76 |
+
Loading the data then breaking it into mini batches. Then forward pass and loss function calculation. After that backward propagation and optimization.
|
77 |
+
Backward Propagation and Optimization:
|
78 |
+
|
79 |
+
```bash
|
80 |
+
optimizer.zero_grad()
|
81 |
+
loss.backward()
|
82 |
+
optimizer.step()
|
83 |
+
```
|
84 |
+
### Test data
|
85 |
+
|
86 |
+
Test data loaded and calculate the accuracy.
|
87 |
+
|
88 |
+
The accuracy was 53.333333333333336% .
|
89 |
+
|
90 |
+
### Result Analysis
|
91 |
+
The packages needed for creating the huggingface interface is loaded with:
|
92 |
+
|
93 |
+
```bash
|
94 |
+
import gradio as gr
|
95 |
+
import torch
|
96 |
+
from torchvision import transforms
|
97 |
+
```
|
98 |
+
|
99 |
+
The model was saved with the following:
|
100 |
+
|
101 |
+
```bash
|
102 |
+
model_scripted = torch.jit.script(model)
|
103 |
+
model_scripted.save('./models/cat_dog_cnn.pt')
|
104 |
+
```
|
105 |
+
## HuggingFace Result analysis
|
106 |
+
First the custom model cat_dog_cnn.pt is loaded. Then the output function is specified. As this is a Image Classification model.
|
107 |
+
|
108 |
+
```bash
|
109 |
+
|---app_data
|
110 |
+
| |---cat.jpg
|
111 |
+
| |---dog.jpg
|
112 |
+
| |---panda.jpg
|
113 |
+
|
|
114 |
+
```
|
115 |
+
|
116 |
+
Example images are loaded.
|
117 |
+
The classes for prediction are - CLASSES = ["Cat", "Dog", "Panda"].
|
118 |
+
The output function for prediction is
|
119 |
+
|
120 |
+
```bash
|
121 |
+
def classify_image(inp):
|
122 |
+
inp = transform(inp).unsqueeze(0)
|
123 |
+
out = model(inp)
|
124 |
+
return CLASSES[out.argmax().item()]
|
125 |
+
```
|
126 |
+
|
127 |
+
This will return the classes of the input image.
|
128 |
+
# Interface Creation
|
129 |
+
|
130 |
+
For creating huggingface interface this following portion is added:
|
131 |
+
|
132 |
+
```bash
|
133 |
+
iface = gr.Interface(fn=classify_image,
|
134 |
+
inputs=gr.Image(type="pil", label="Input Image"),
|
135 |
+
outputs="text",
|
136 |
+
examples=[
|
137 |
+
|
138 |
+
"./app_data/cat.jpg",
|
139 |
+
"./app_data/dog.jpg",
|
140 |
+
"./app_data/panda.jpg",
|
141 |
+
|
142 |
+
|
143 |
+
])
|
144 |
+
```
|
145 |
+
This portion is going to create an interface for taking the image input. Then example images and output is defined to be the classes from cat, dog and panda.
|
146 |
+
Now with the following the interface of the app is loaded.
|
147 |
+
|
148 |
+
```bash
|
149 |
+
iface.launch()
|
150 |
+
```
|
151 |
+
|
152 |
+
|
153 |
+
### Project Structure
|
154 |
+
```bash
|
155 |
+
|
|
156 |
+
|---app_data
|
157 |
+
| |---images(used for examples)
|
158 |
+
|
|
159 |
+
|---models
|
160 |
+
| |---cat_dog_cnn.pt
|
161 |
+
|
|
162 |
+
|---train(image dataset for training)
|
163 |
+
|
|
164 |
+
|---test(image dataset for testing)
|
165 |
+
|
|
166 |
+
|---Readme.md(about project)
|
167 |
+
|
|
168 |
+
|---app.py(the interface for project)
|
169 |
+
|
|
170 |
+
|---requirements.txt(libraries needed for project)
|
171 |
+
|
|
172 |
+
|---main.ipynb(project code)
|
173 |
+
```
|
174 |
+
|
175 |
+
### How to Run
|
176 |
+
|
177 |
+
```bash
|
178 |
+
|
179 |
+
git clone https://huggingface.co/spaces/neelimapreeti297/panda_cat_dog_classification/tree/main
|
180 |
+
|
181 |
+
cd panda_cat_dog_classification
|
182 |
+
|
183 |
+
pip install -r requirements.txt
|
184 |
+
|
185 |
+
python app.py
|
186 |
+
```
|
187 |
+
|
188 |
+
|
189 |
+
### License
|
190 |
+
This project is licensed under the MIT License.
|
191 |
+
|
192 |
+
### Contributor
|
193 |
+
Neelima Monjusha Preeti - [email protected]
|
194 |
+
|
195 |
+
link: https://huggingface.co/spaces/neelimapreeti297/panda_cat_dog_classification
|
196 |
|
197 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|