Update README.md
Browse files
README.md
CHANGED
|
@@ -13,17 +13,18 @@ library_name: keras
|
|
| 13 |
panda_cat_dog_classification
|
| 14 |
|
| 15 |
### Model Description
|
| 16 |
-
This model classifies animals among pandas, cats and dogs. It was trained using
|
| 17 |
|
| 18 |
|
| 19 |
- **Developed by:** Neelima Monjusha Preeti
|
| 20 |
-
- **Model type:**
|
| 21 |
- **Language(s):** Python
|
| 22 |
- **License:** MIT
|
| 23 |
- **Contact:** [email protected]
|
| 24 |
|
| 25 |
### Data Preprocessing
|
| 26 |
The image dataset is preprocessed with the following portion:
|
|
|
|
| 27 |
```bash
|
| 28 |
transform = transforms.Compose([
|
| 29 |
transforms.Resize((224,224)),
|
|
@@ -65,10 +66,87 @@ class CNN(nn.Module):
|
|
| 65 |
```
|
| 66 |
Then used batch_size = 8 and CrossEntropyLoss() for loss function. Then used Adam optimizer with a learning rate 0.001 for optimization process.
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
|
|
|
|
|
|
| 70 |
|
|
|
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
|
| 74 |
### Project Structure
|
|
|
|
| 13 |
panda_cat_dog_classification
|
| 14 |
|
| 15 |
### Model Description
|
| 16 |
+
This model classifies animals among pandas, cats and dogs. It was trained using custom CNN model.
|
| 17 |
|
| 18 |
|
| 19 |
- **Developed by:** Neelima Monjusha Preeti
|
| 20 |
+
- **Model type:** custom CNN model
|
| 21 |
- **Language(s):** Python
|
| 22 |
- **License:** MIT
|
| 23 |
- **Contact:** [email protected]
|
| 24 |
|
| 25 |
### Data Preprocessing
|
| 26 |
The image dataset is preprocessed with the following portion:
|
| 27 |
+
|
| 28 |
```bash
|
| 29 |
transform = transforms.Compose([
|
| 30 |
transforms.Resize((224,224)),
|
|
|
|
| 66 |
```
|
| 67 |
Then used batch_size = 8 and CrossEntropyLoss() for loss function. Then used Adam optimizer with a learning rate 0.001 for optimization process.
|
| 68 |
|
| 69 |
+
```bash
|
| 70 |
+
loss_function = nn.CrossEntropyLoss()
|
| 71 |
+
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
| 72 |
+
```
|
| 73 |
+
### Training Loop
|
| 74 |
+
|
| 75 |
+
Loading the data then breaking it into mini batches. Then forward pass and loss function calculation. After that backward propagation and optimization.
|
| 76 |
+
Backward Propagation and Optimization:
|
| 77 |
+
|
| 78 |
+
```bash
|
| 79 |
+
optimizer.zero_grad()
|
| 80 |
+
loss.backward()
|
| 81 |
+
optimizer.step()
|
| 82 |
+
```
|
| 83 |
+
### Test data
|
| 84 |
+
|
| 85 |
+
Test data loaded and calculate the accuracy.
|
| 86 |
+
|
| 87 |
+
The accuracy was 53.333333333333336% .
|
| 88 |
+
|
| 89 |
+
### Result Analysis
|
| 90 |
+
The packages needed for creating the huggingface interface is loaded with:
|
| 91 |
+
|
| 92 |
+
```bash
|
| 93 |
+
import gradio as gr
|
| 94 |
+
import torch
|
| 95 |
+
from torchvision import transforms
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
The model was saved with the following:
|
| 99 |
|
| 100 |
+
```bash
|
| 101 |
+
model_scripted = torch.jit.script(model)
|
| 102 |
+
model_scripted.save('./models/cat_dog_cnn.pt')
|
| 103 |
+
```
|
| 104 |
+
## HuggingFace Result analysis
|
| 105 |
+
First the custom model cat_dog_cnn.pt is loaded. Then the output function is specified. As this is a Image Classification model.
|
| 106 |
+
|
| 107 |
+
```bash
|
| 108 |
+
|---app_data
|
| 109 |
+
| |---cat.jpg
|
| 110 |
+
| |---dog.jpg
|
| 111 |
+
| |---panda.jpg
|
| 112 |
+
|
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
Example images are loaded.
|
| 116 |
+
The classes for prediction are - CLASSES = ["Cat", "Dog", "Panda"].
|
| 117 |
+
The output function for prediction is
|
| 118 |
+
|
| 119 |
+
```bash
|
| 120 |
+
def classify_image(inp):
|
| 121 |
+
inp = transform(inp).unsqueeze(0)
|
| 122 |
+
out = model(inp)
|
| 123 |
+
return CLASSES[out.argmax().item()]
|
| 124 |
+
```
|
| 125 |
|
| 126 |
+
This will return the classes of the input image.
|
| 127 |
+
# Interface Creation
|
| 128 |
|
| 129 |
+
For creating huggingface interface this following portion is added:
|
| 130 |
|
| 131 |
+
```bash
|
| 132 |
+
iface = gr.Interface(fn=classify_image,
|
| 133 |
+
inputs=gr.Image(type="pil", label="Input Image"),
|
| 134 |
+
outputs="text",
|
| 135 |
+
examples=[
|
| 136 |
+
|
| 137 |
+
"./app_data/cat.jpg",
|
| 138 |
+
"./app_data/dog.jpg",
|
| 139 |
+
"./app_data/panda.jpg",
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
])
|
| 143 |
+
```
|
| 144 |
+
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.
|
| 145 |
+
Now with the following the interface of the app is loaded.
|
| 146 |
+
|
| 147 |
+
```bash
|
| 148 |
+
iface.launch()
|
| 149 |
+
```
|
| 150 |
|
| 151 |
|
| 152 |
### Project Structure
|