Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,100 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: mit
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
datasets:
|
| 4 |
+
- jxie/stl10
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
# Image Classifier
|
| 8 |
+
|
| 9 |
+
This repository contains a pre-trained PyTorch model, designed for classifying images into 10 categories: airplane, bird, car, cat, deer, dog, horse, monkey, ship, and truck. The model uses a Convolutional Neural Network (CNN) architecture and can classify images based on the categories below.
|
| 10 |
+
|
| 11 |
+
## Model Overview
|
| 12 |
+
|
| 13 |
+
The model is a simple CNN classifier with two convolutional blocks followed by a fully connected layer. It was trained on an image dataset and can classify images into the following categories:
|
| 14 |
+
|
| 15 |
+
- **0**: Airplane
|
| 16 |
+
- **1**: Bird
|
| 17 |
+
- **2**: Car
|
| 18 |
+
- **3**: Cat
|
| 19 |
+
- **4**: Deer
|
| 20 |
+
- **5**: Dog
|
| 21 |
+
- **6**: Horse
|
| 22 |
+
- **7**: Monkey
|
| 23 |
+
- **8**: Ship
|
| 24 |
+
- **9**: Truck
|
| 25 |
+
|
| 26 |
+
## Model Architecture
|
| 27 |
+
|
| 28 |
+
The model consists of the following layers:
|
| 29 |
+
1. **Conv Block 1**: Two convolutional layers with ReLU activations followed by max pooling.
|
| 30 |
+
2. **Conv Block 2**: Two more convolutional layers with ReLU activations and max pooling.
|
| 31 |
+
3. **Fully Connected Classifier**: A linear layer that maps the features to 10 output categories.
|
| 32 |
+
|
| 33 |
+
Here’s the architecture of the model:
|
| 34 |
+
```python
|
| 35 |
+
class CNNV0(nn.Module):
|
| 36 |
+
def __init__(self, input_shape: int, hidden_units: int, output_shape: int):
|
| 37 |
+
super().__init__()
|
| 38 |
+
self.conv_block_1 = nn.Sequential(
|
| 39 |
+
nn.Conv2d(in_channels=input_shape, out_channels=hidden_units, kernel_size=3, stride=1, padding=1),
|
| 40 |
+
nn.ReLU(),
|
| 41 |
+
nn.Conv2d(in_channels=hidden_units, out_channels=hidden_units, kernel_size=3, stride=1, padding=1),
|
| 42 |
+
nn.ReLU(),
|
| 43 |
+
nn.MaxPool2d(kernel_size=2)
|
| 44 |
+
)
|
| 45 |
+
self.conv_block_2 = nn.Sequential(
|
| 46 |
+
nn.Conv2d(in_channels=hidden_units, out_channels=hidden_units, kernel_size=3, stride=1, padding=1),
|
| 47 |
+
nn.ReLU(),
|
| 48 |
+
nn.Conv2d(in_channels=hidden_units, out_channels=hidden_units, kernel_size=3, stride=1, padding=1),
|
| 49 |
+
nn.ReLU(),
|
| 50 |
+
nn.MaxPool2d(kernel_size=2)
|
| 51 |
+
)
|
| 52 |
+
self.classifier = nn.Sequential(
|
| 53 |
+
nn.Flatten(),
|
| 54 |
+
nn.Linear(in_features=hidden_units*576, out_features=output_shape)
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
def forward(self, x):
|
| 58 |
+
x = self.conv_block_1(x)
|
| 59 |
+
x = self.conv_block_2(x)
|
| 60 |
+
x = self.classifier(x)
|
| 61 |
+
return x
|
| 62 |
+
|
| 63 |
+
## Requirements
|
| 64 |
+
|
| 65 |
+
- **Python** 3.7 or higher
|
| 66 |
+
- **PyTorch** 1.8 or higher
|
| 67 |
+
- **torchvision** (for loading and preprocessing images)
|
| 68 |
+
|
| 69 |
+
## Usage
|
| 70 |
+
|
| 71 |
+
1. Clone this repository and install dependencies:
|
| 72 |
+
```bash
|
| 73 |
+
git clone <repository-url>
|
| 74 |
+
cd <repository-folder>
|
| 75 |
+
pip install torch torchvision
|
| 76 |
+
|
| 77 |
+
2. Load and use the model in your Python script:
|
| 78 |
+
```python
|
| 79 |
+
import torch
|
| 80 |
+
from torchvision import transforms
|
| 81 |
+
from PIL import Image
|
| 82 |
+
|
| 83 |
+
# Load the model
|
| 84 |
+
model = torch.load('model_0.pth')
|
| 85 |
+
model.eval() # Set to evaluation mode
|
| 86 |
+
|
| 87 |
+
# Load and preprocess the image
|
| 88 |
+
transform = transforms.Compose([
|
| 89 |
+
transforms.Resize((224, 224)),
|
| 90 |
+
transforms.ToTensor(),
|
| 91 |
+
])
|
| 92 |
+
img = Image.open('path_to_image.jpg')
|
| 93 |
+
img = transform(img).view(1, 3, 224, 224) # Reshape to (1, 3, 224, 224) for batch processing
|
| 94 |
+
|
| 95 |
+
# Predict
|
| 96 |
+
with torch.no_grad():
|
| 97 |
+
output = model(img)
|
| 98 |
+
_, predicted = torch.max(output, 1)
|
| 99 |
+
print("Predicted Aircraft Type:", predicted.item())
|
| 100 |
+
|