-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlv3_optimize.py
211 lines (172 loc) · 8.18 KB
/
dlv3_optimize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import os
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import csv
from torch.utils.data import DataLoader
from torchvision import transforms, models
from torchvision.models import resnet50, ResNet50_Weights
from torchmetrics.classification import JaccardIndex
from data.datasets import SharedTransformFloodDataset
# Configuration
input_size = (1024, 768)
h, w = input_size
batch_size = 1
epochs = 20
learning_rate = 0.0001
model_name = "DeepLabV3"
num_classes = 10
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Data transformations
img_transforms = transforms.Compose([
transforms.ToTensor()
])
label_transforms = transforms.Compose([
torch.from_numpy
])
# Metrics paths
train_metrics_path = f'running_metrics/training_metrics_{model_name}.csv'
test_metrics_path = f'running_metrics/test_metrics_{model_name}.csv'
# Initialize metrics files
with open(train_metrics_path, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Epoch', 'Batch', 'Loss', 'mIoU'])
with open(test_metrics_path, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Epoch', 'Batch', 'Loss', 'mIoU'])
# Paths to FloodNet dataset
train_image_dir = "ShrunkenFloodNet/FloodNet-Supervised_v1.0/train/train-org-img"
train_mask_dir = "ShrunkenFloodNet/FloodNet-Supervised_v1.0/train/train-label-img"
val_image_dir = "ShrunkenFloodNet/FloodNet-Supervised_v1.0/val/val-org-img"
val_mask_dir = "ShrunkenFloodNet/FloodNet-Supervised_v1.0/val/val-label-img"
# Datasets and DataLoaders
train_dataset = SharedTransformFloodDataset(train_image_dir, train_mask_dir, h, w,
transform=img_transforms, target_transform=label_transforms)
val_dataset = SharedTransformFloodDataset(val_image_dir, val_mask_dir, h, w,
transform=img_transforms, target_transform=label_transforms)
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False)
class ASPP(nn.Module):
def __init__(self, in_channels, out_channels):
super(ASPP, self).__init__()
self.conv_1x1_1 = nn.Conv2d(in_channels, out_channels, kernel_size=1)
self.bn_1 = nn.BatchNorm2d(out_channels)
self.conv_3x3_1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=6, dilation=6)
self.bn_2 = nn.BatchNorm2d(out_channels)
self.conv_3x3_2 = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=12, dilation=12)
self.bn_3 = nn.BatchNorm2d(out_channels)
self.conv_3x3_3 = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=18, dilation=18)
self.bn_4 = nn.BatchNorm2d(out_channels)
self.global_avg_pool = nn.AdaptiveAvgPool2d((1, 1))
self.conv_1x1_2 = nn.Conv2d(in_channels, out_channels, kernel_size=1)
self.bn_5 = nn.BatchNorm2d(out_channels)
self.conv_1x1_3 = nn.Conv2d(out_channels * 5, out_channels, kernel_size=1)
self.bn_6 = nn.BatchNorm2d(out_channels)
def forward(self, x):
x1 = F.relu(self.bn_1(self.conv_1x1_1(x)))
x2 = F.relu(self.bn_2(self.conv_3x3_1(x)))
x3 = F.relu(self.bn_3(self.conv_3x3_2(x)))
x4 = F.relu(self.bn_4(self.conv_3x3_3(x)))
x5 = self.global_avg_pool(x)
x5 = self.conv_1x1_2(x5)
x5 = F.relu(x5)
x5 = F.interpolate(x5, size=x4.size()[2:], mode='bilinear', align_corners=True)
x = torch.cat([x1, x2, x3, x4, x5], dim=1)
x = F.relu(self.bn_6(self.conv_1x1_3(x)))
return x
# Define the Decoder part for DeepLabV3+
class DeepLabV3PlusDecoder(nn.Module):
def __init__(self, low_level_in_channels, low_level_out_channels, out_channels):
super(DeepLabV3PlusDecoder, self).__init__()
self.low_level_conv = nn.Conv2d(low_level_in_channels, low_level_out_channels, kernel_size=1, bias=False)
self.low_level_bn = nn.BatchNorm2d(low_level_out_channels)
self.final_conv = nn.Sequential(
nn.Conv2d(low_level_out_channels + out_channels, out_channels, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU()
)
def forward(self, x, low_level_feat):
low_level_feat = self.low_level_bn(self.low_level_conv(low_level_feat))
x = F.interpolate(x, size=low_level_feat.shape[2:], mode='bilinear', align_corners=True)
x = torch.cat([x, low_level_feat], dim=1)
return self.final_conv(x)
# Combine ASPP and Decoder into DeepLabV3+
class DeepLabV3Plus(nn.Module):
def __init__(self, num_classes=num_classes, backbone='resnet50'):
super(DeepLabV3Plus, self).__init__()
# Load ResNet50 backbone
self.backbone = models.resnet50(weights=ResNet50_Weights.DEFAULT)
# Extract layers for feature extraction
self.low_level_features = nn.Sequential(*list(self.backbone.children())[:4]) # First few layers (conv1, bn1, relu, maxpool)
self.high_level_features = nn.Sequential(*list(self.backbone.children())[4:-2])
# ASPP
self.aspp = ASPP(in_channels=2048, out_channels=256)
# Decoder
self.decoder = DeepLabV3PlusDecoder(low_level_in_channels=64, low_level_out_channels=48, out_channels=256)
# Final classification layer
self.classifier = nn.Conv2d(256, num_classes, kernel_size=1)
def forward(self, x):
# Extract low-level features
low_level_feat = self.low_level_features(x)
# Extract high-level features
x = self.high_level_features(low_level_feat)
# Apply ASPP on high-level features
x = self.aspp(x)
# Decode features using low-level and ASPP output
x = self.decoder(x, low_level_feat)
# Final classification layer
x = self.classifier(x)
# Upsample to match the input image size
return F.interpolate(x, size=(h, w), mode='bilinear', align_corners=True)
# Instantiate the model
model = DeepLabV3Plus(num_classes=num_classes).to(device)
# Loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.AdamW(model.parameters(), lr=learning_rate)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', patience=3, factor=0.5)
jaccard_metric = JaccardIndex(num_classes=num_classes, task="multiclass").to(device)
# Training and validation loops
for epoch in range(epochs):
print(f"Epoch: {epoch + 1}")
# Training
model.train()
train_loss = 0.0
for batch_idx, (images, masks) in enumerate(train_loader):
images, masks = images.to(device), masks.to(device).long()
outputs = model(images)
loss = criterion(outputs, masks)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Save metrics
preds = torch.argmax(outputs, dim=1)
mIoU = jaccard_metric(preds, masks)
with open(train_metrics_path, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([epoch + 1, batch_idx + 1, loss.item(), mIoU.item()])
# Validation
model.eval()
val_loss = 0.0
total_iou = 0.0
with torch.no_grad():
for batch_idx, (images, masks) in enumerate(val_loader):
images, masks = images.to(device), masks.to(device).long()
outputs = model(images)
loss = criterion(outputs, masks)
val_loss += loss.item()
# Save IoU metrics
preds = torch.argmax(outputs, dim=1)
mIoU = jaccard_metric(preds, masks)
total_iou += mIoU.item()
with open(test_metrics_path, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([epoch + 1, batch_idx + 1, loss.item(), mIoU.item()])
# Adjust learning rate
scheduler.step(val_loss)
print(f"Validation Loss: {val_loss:.4f}, Mean IoU: {total_iou / len(val_loader):.4f}")
print("Training complete.")