-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlv3_train.py
237 lines (188 loc) · 8.73 KB
/
dlv3_train.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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, Dataset
from torchvision import transforms, models
from torchvision.models import resnet50, ResNet50_Weights
from torchvision.models.segmentation.deeplabv3 import DeepLabHead
from PIL import Image
from tqdm import tqdm
from torchmetrics.classification import JaccardIndex
from data.datasets import SharedTransformFloodDataset
# Configuration
input_size = (1024, 768)
h, w = input_size
batch_size = 1
epochs = 50
learning_rate = 0.0001
model_name = "DeepLabV3"
num_classes = 10
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Data transformations
transform = transforms.Compose([
transforms.Resize(input_size),
transforms.ToTensor()
])
#Create metrics file
# Define the file name and write the header
train_metrics_path = f'running_metrics/training_metrics_{model_name}.csv'
test_metrics_path = f'running_metrics/test_metrics_{model_name}.csv'
with open(train_metrics_path, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Epoch', 'Batch', 'Loss', 'mIoU']) # Header
with open(test_metrics_path, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Epoch', 'Batch', 'Loss', 'mIoU'])
img_transforms = transforms.Compose([
transforms.ToTensor()
])
label_transforms = transforms.Compose([
torch.from_numpy
])
# 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)
print(train_loader)
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)
# Set up the model and print the summary
model = DeepLabV3Plus(num_classes=num_classes)
model = model.to("cuda" if torch.cuda.is_available() else "cpu")
# Loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate, weight_decay=1e-4)
jaccard_metric = JaccardIndex(num_classes=num_classes, task="multiclass").to(device)
for epoch in range(epochs):
print(f"Epoch: {epoch + 1}")
# Training Loop
model.train()
train_loss = 0.0
for batch_idx, (images, masks) in enumerate(train_loader):
images, masks = images.to(device), masks.to(device)
masks = masks.long()
# Forward pass
outputs = model(images)
loss = criterion(outputs, masks)
# Backward pass and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss += loss.item() * images.size(0)
# Calculate mIoU
preds = torch.argmax(outputs, dim=1)
mIoU = jaccard_metric(preds, masks.int())
#Save Metrics
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 Loop with IoU calculation
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)
masks = masks.long()
# Generate predictions
outputs = model(images)
# Calculate Loss
loss = criterion(outputs, masks)
val_loss += loss.item() * images.size(0)
# Calculate mIoU
preds = torch.argmax(outputs, dim=1)
mIoU = jaccard_metric(preds, masks.int())
total_iou += mIoU
# Save IoU and Loss metrics to CSV
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()])
# Reset metrics and log average loss/IoU for the epoch
jaccard_metric.reset()
val_loss /= len(val_loader.dataset)
mean_iou = total_iou / len(val_loader)
print(f"Validation Loss: {val_loss:.4f}, Mean IoU: {mean_iou:.4f}")
print("Training complete.")