Update README.md
Browse files
README.md
CHANGED
|
@@ -1,112 +1,139 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: bsd-3-clause
|
| 3 |
-
library_name: torch
|
| 4 |
-
pipeline_tag: "image-classification"
|
| 5 |
-
datasets: DanielCruz09/natural_disasters
|
| 6 |
-
---
|
| 7 |
-
|
| 8 |
-
# Natural Disaster Image Classifier
|
| 9 |
-
|
| 10 |
-
A PyTorch CNN that can classify images into the following categories:
|
| 11 |
-
- Fire
|
| 12 |
-
- Flood
|
| 13 |
-
- Earthquake
|
| 14 |
-
- Non-Damage
|
| 15 |
-
|
| 16 |
-
This model is based on PyTorch's ResNet50 architecture, which is licensed under the BSD-3-Clause License. The original ResNet architecture was introduced in *Deep Residual Learning for Image Recognition* (He et. al., 2015).
|
| 17 |
-
|
| 18 |
-
# Model Architecture
|
| 19 |
-
|
| 20 |
-
This model is built on top of **ResNet50**, a deep convolutional neural network. ResNet50 uses stacked residual blocks to enable the training of very deep networks by reducing the effects of vanishing gradients (which are very apparent in several modern architectures) via identity skip connections.
|
| 21 |
-
|
| 22 |
-
For this project:
|
| 23 |
-
- The base architecture comes from **TorchVision's ResNet50** implementation.
|
| 24 |
-
- The final fully connected layer was replaced with a classifier tailored to disaster relief image categories.
|
| 25 |
-
- All convolutional layers and residual blocks are identical to the original architecture.
|
| 26 |
-
- Pretrained ImageNet weights were used for initialization to accelerate convergence and improve generalization.
|
| 27 |
-
|
| 28 |
-
This architecture provides a robust balance of accuracy and efficiency for real-world image classification tasks.
|
| 29 |
-
|
| 30 |
-
# Training Details
|
| 31 |
-
|
| 32 |
-
The model was trained using a custom dataset of natural disaster images, organized into multiple classes as stated above. The training pipeline includes:
|
| 33 |
-
|
| 34 |
-
## Data Preprocessing
|
| 35 |
-
- Images were resized to 224 x 224.
|
| 36 |
-
- Normalization followed ImageNet mean & std.
|
| 37 |
-
|
| 38 |
-
## Training Procedure
|
| 39 |
-
- Framework: **PyTorch**
|
| 40 |
-
- Optimizer: **SGD**
|
| 41 |
-
- Learning Rate: `1e-2`
|
| 42 |
-
- Loss function: **Cross-Entropy Loss**
|
| 43 |
-
- Batch size: 32
|
| 44 |
-
|
| 45 |
-
# Evaluation
|
| 46 |
-
|
| 47 |
-
Model performance metrics computed for this model were:
|
| 48 |
-
- Accuracy
|
| 49 |
-
- Precision, Recall, F1 (all using a weighted average due to multiclass classification)
|
| 50 |
-
|
| 51 |
-
# How to Use
|
| 52 |
-
|
| 53 |
-
## Install the library
|
| 54 |
-
|
| 55 |
-
```python
|
| 56 |
-
pip install huggingface_hub
|
| 57 |
-
```
|
| 58 |
-
|
| 59 |
-
## Download the model
|
| 60 |
-
|
| 61 |
-
```python
|
| 62 |
-
from huggingface_hub import hf_hub_download
|
| 63 |
-
|
| 64 |
-
model_path = hf_hub_download(
|
| 65 |
-
repo_id="DanielCruz09/disaster-image-classifier",
|
| 66 |
-
filename="model_weights.pth"
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
print("Model downloaded to: ", model_path)
|
| 70 |
-
```
|
| 71 |
-
|
| 72 |
-
## Load the model into PyTorch
|
| 73 |
-
|
| 74 |
-
```python
|
| 75 |
-
import torch
|
| 76 |
-
from resnet50 import ResNet50
|
| 77 |
-
|
| 78 |
-
model = ResNet50(num_classes=4)
|
| 79 |
-
state_dict = torch.load(model_path, map_location="cpu")
|
| 80 |
-
model.model.load_state_dict(state_dict)
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
```
|
| 110 |
-
|
| 111 |
-
#
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: bsd-3-clause
|
| 3 |
+
library_name: torch
|
| 4 |
+
pipeline_tag: "image-classification"
|
| 5 |
+
datasets: DanielCruz09/natural_disasters
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
# Natural Disaster Image Classifier
|
| 9 |
+
|
| 10 |
+
A PyTorch CNN that can classify images into the following categories:
|
| 11 |
+
- Fire
|
| 12 |
+
- Flood
|
| 13 |
+
- Earthquake
|
| 14 |
+
- Non-Damage
|
| 15 |
+
|
| 16 |
+
This model is based on PyTorch's ResNet50 architecture, which is licensed under the BSD-3-Clause License. The original ResNet architecture was introduced in *Deep Residual Learning for Image Recognition* (He et. al., 2015).
|
| 17 |
+
|
| 18 |
+
# Model Architecture
|
| 19 |
+
|
| 20 |
+
This model is built on top of **ResNet50**, a deep convolutional neural network. ResNet50 uses stacked residual blocks to enable the training of very deep networks by reducing the effects of vanishing gradients (which are very apparent in several modern architectures) via identity skip connections.
|
| 21 |
+
|
| 22 |
+
For this project:
|
| 23 |
+
- The base architecture comes from **TorchVision's ResNet50** implementation.
|
| 24 |
+
- The final fully connected layer was replaced with a classifier tailored to disaster relief image categories.
|
| 25 |
+
- All convolutional layers and residual blocks are identical to the original architecture.
|
| 26 |
+
- Pretrained ImageNet weights were used for initialization to accelerate convergence and improve generalization.
|
| 27 |
+
|
| 28 |
+
This architecture provides a robust balance of accuracy and efficiency for real-world image classification tasks.
|
| 29 |
+
|
| 30 |
+
# Training Details
|
| 31 |
+
|
| 32 |
+
The model was trained using a custom dataset of natural disaster images, organized into multiple classes as stated above. The training pipeline includes:
|
| 33 |
+
|
| 34 |
+
## Data Preprocessing
|
| 35 |
+
- Images were resized to 224 x 224.
|
| 36 |
+
- Normalization followed ImageNet mean & std.
|
| 37 |
+
|
| 38 |
+
## Training Procedure
|
| 39 |
+
- Framework: **PyTorch**
|
| 40 |
+
- Optimizer: **SGD**
|
| 41 |
+
- Learning Rate: `1e-2`
|
| 42 |
+
- Loss function: **Cross-Entropy Loss**
|
| 43 |
+
- Batch size: 32
|
| 44 |
+
|
| 45 |
+
# Evaluation
|
| 46 |
+
|
| 47 |
+
Model performance metrics computed for this model were:
|
| 48 |
+
- Accuracy
|
| 49 |
+
- Precision, Recall, F1 (all using a weighted average due to multiclass classification)
|
| 50 |
+
|
| 51 |
+
# How to Use
|
| 52 |
+
|
| 53 |
+
## Install the library
|
| 54 |
+
|
| 55 |
+
```python
|
| 56 |
+
pip install huggingface_hub
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
## Download the model
|
| 60 |
+
|
| 61 |
+
```python
|
| 62 |
+
from huggingface_hub import hf_hub_download
|
| 63 |
+
|
| 64 |
+
model_path = hf_hub_download(
|
| 65 |
+
repo_id="DanielCruz09/disaster-image-classifier",
|
| 66 |
+
filename="model_weights.pth"
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
print("Model downloaded to: ", model_path)
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
## Load the model into PyTorch
|
| 73 |
+
|
| 74 |
+
```python
|
| 75 |
+
import torch
|
| 76 |
+
from resnet50 import ResNet50
|
| 77 |
+
|
| 78 |
+
model = ResNet50(num_classes=4)
|
| 79 |
+
state_dict = torch.load(model_path, map_location="cpu")
|
| 80 |
+
model.model.load_state_dict(state_dict)
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
## Evaluate the model
|
| 84 |
+
|
| 85 |
+
```python
|
| 86 |
+
test_path = "data/processed/Test/"
|
| 87 |
+
renamed = {
|
| 88 |
+
"Fire": "Fire_Disaster",
|
| 89 |
+
"Earthquake": "Land_Disaster",
|
| 90 |
+
"Normal": "Non_Damage",
|
| 91 |
+
"Flood": "Water_Disaster"
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
def rename_directories(old_name, new_name):
|
| 95 |
+
try:
|
| 96 |
+
current_dir = Path(old_name)
|
| 97 |
+
current_dir.rename(new_name)
|
| 98 |
+
except FileNotFoundError:
|
| 99 |
+
return
|
| 100 |
+
|
| 101 |
+
for name in renamed.keys():
|
| 102 |
+
old_path = os.path.join(test_path, name)
|
| 103 |
+
new_path = os.path.join(test_path, renamed[name])
|
| 104 |
+
rename_directories(old_path, new_path)
|
| 105 |
+
|
| 106 |
+
test_dataset = NaturalDisasterDataset(root=test_path)
|
| 107 |
+
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=True)
|
| 108 |
+
model.eval(test_loader=test_loader, write_path="results/resnet50_results.csv")
|
| 109 |
+
```
|
| 110 |
+
|
| 111 |
+
## Run inference
|
| 112 |
+
|
| 113 |
+
```python
|
| 114 |
+
from torchvision import transforms
|
| 115 |
+
from PIL import Image
|
| 116 |
+
|
| 117 |
+
transform = transforms.Compose([
|
| 118 |
+
transforms.Resize((224, 224)),
|
| 119 |
+
transforms.ToTensor(),
|
| 120 |
+
transforms.Normalize(
|
| 121 |
+
# same as ImageNet
|
| 122 |
+
mean=[0.485, 0.456, 0.406],
|
| 123 |
+
std=[0.229, 0.224, 0.225]
|
| 124 |
+
)
|
| 125 |
+
])
|
| 126 |
+
|
| 127 |
+
img = Image.open("example.jpg").convert("RGB")
|
| 128 |
+
image_tensor = preprocess(img).unsqueeze(0)
|
| 129 |
+
|
| 130 |
+
with torch.no_grad():
|
| 131 |
+
outputs = model(image_tensor)
|
| 132 |
+
probs = torch.softmax(outputs, dims=1)
|
| 133 |
+
|
| 134 |
+
predicted_class = probs.argmax().item()
|
| 135 |
+
print("Predicted class: ", predicted_class)
|
| 136 |
+
```
|
| 137 |
+
|
| 138 |
+
# Reference
|
| 139 |
+
He, K., Zhang, X., Ren, S., & Sun, J. (2015). *Deep Residual Learning for Image Recognition.* CVPR. https://arxiv.org/abs/1512.03385
|