-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
288 lines (227 loc) · 9.09 KB
/
Copy pathcache.py
File metadata and controls
288 lines (227 loc) · 9.09 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import os
import torch
import torch.nn as nn
import torch.optim as optim
import torch.optim.lr_scheduler as lr_scheduler
import torch.utils.data as data
import torchvision.transforms as transforms
from PIL import Image
import transforms as ext_transforms
from models.enet import ENet
from train import Train
from test import Test
from metric.iou import IoU
from args import get_arguments
from data.utils import enet_weighing, median_freq_balancing
import utils
# Get the arguments
args = get_arguments()
# Set the device to CPU
device = torch.device('cpu')
def load_dataset(dataset):
print("\nLoading dataset...\n")
print("Selected dataset:", args.dataset)
print("Dataset directory:", args.dataset_dir)
print("Save directory:", args.save_dir)
# Define image transformations
image_transform = transforms.Compose([
transforms.Resize((args.height, args.width)),
transforms.ToTensor()
])
# Define label transformations
label_transform = transforms.Compose([
transforms.Resize((args.height, args.width), Image.NEAREST),
ext_transforms.PILToLongTensor()
])
# Load the training set
train_set = dataset(
args.dataset_dir,
transform=image_transform,
label_transform=label_transform
)
train_loader = data.DataLoader(
train_set,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.workers
)
# Load the validation set
val_set = dataset(
args.dataset_dir,
mode='val',
transform=image_transform,
label_transform=label_transform
)
val_loader = data.DataLoader(
val_set,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.workers
)
# Load the test set
test_set = dataset(
args.dataset_dir,
mode='test',
transform=image_transform,
label_transform=label_transform
)
test_loader = data.DataLoader(
test_set,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.workers
)
# Get class encoding
class_encoding = train_set.color_encoding
# Remove 'road_marking' class if using CamVid dataset
if args.dataset.lower() == 'camvid':
del class_encoding['road_marking']
# Get the number of classes
num_classes = len(class_encoding)
# Print dataset information
print("Number of classes to predict:", num_classes)
print("Train dataset size:", len(train_set))
print("Validation dataset size:", len(val_set))
# Display a batch of samples if requested
if args.mode.lower() == 'test':
images, labels = next(iter(test_loader))
else:
images, labels = next(iter(train_loader))
print("Image size:", images.size())
print("Label size:", labels.size())
print("Class-color encoding:", class_encoding)
if args.imshow_batch:
print("Close the figure window to continue...")
label_to_rgb = transforms.Compose([
ext_transforms.LongTensorToRGBPIL(class_encoding),
transforms.ToTensor()
])
color_labels = utils.batch_transform(labels, label_to_rgb)
utils.imshow_batch(images, color_labels)
# Compute class weights
print("\nWeighing technique:", args.weighing)
print("Computing class weights...")
print("(this can take a while depending on the dataset size)")
class_weights = None
if args.weighing.lower() == 'enet':
class_weights = enet_weighing(train_loader, num_classes)
elif args.weighing.lower() == 'mfb':
class_weights = median_freq_balancing(train_loader, num_classes)
if class_weights is not None:
class_weights = torch.from_numpy(class_weights).float().to(device)
if args.ignore_unlabeled:
ignore_index = list(class_encoding).index('unlabeled')
class_weights[ignore_index] = 0
print("Class weights:", class_weights)
return (train_loader, val_loader, test_loader), class_weights, class_encoding
def train(train_loader, val_loader, class_weights, class_encoding):
print("\nTraining...\n")
num_classes = len(class_encoding)
# Initialize ENet model
model = ENet(num_classes).to(device)
print(model)
# Define loss function
criterion = nn.CrossEntropyLoss(weight=class_weights)
# Define optimizer
optimizer = optim.Adam(
model.parameters(),
lr=args.learning_rate,
weight_decay=args.weight_decay
)
# Define learning rate scheduler
lr_updater = lr_scheduler.StepLR(optimizer, args.lr_decay_epochs, args.lr_decay)
# Define evaluation metric
if args.ignore_unlabeled:
ignore_index = list(class_encoding).index('unlabeled')
else:
ignore_index = None
metric = IoU(num_classes, ignore_index=ignore_index)
# Optionally resume from checkpoint
if args.resume:
model, optimizer, start_epoch, best_miou = utils.load_checkpoint(
model, optimizer, args.save_dir, args.name)
print("Resuming from model: Start epoch = {0} | Best mean IoU = {1:.4f}".format(start_epoch, best_miou))
else:
start_epoch = 0
best_miou = 0
# Start training
train = Train(model, train_loader, optimizer, criterion, metric, device)
val = Test(model, val_loader, criterion, metric, device)
for epoch in range(start_epoch, args.epochs):
print(">>>> [Epoch: {0:d}] Training".format(epoch))
epoch_loss, (iou, miou) = train.run_epoch(args.print_step)
lr_updater.step()
print(">>>> [Epoch: {0:d}] Avg. loss: {1:.4f} | Mean IoU: {2:.4f}".format(epoch, epoch_loss, miou))
if (epoch + 1) % 10 == 0 or epoch + 1 == args.epochs:
print(">>>> [Epoch: {0:d}] Validation".format(epoch))
loss, (iou, miou) = val.run_epoch(args.print_step)
print(">>>> [Epoch: {0:d}] Avg. loss: {1:.4f} | Mean IoU: {2:.4f}".format(epoch, loss, miou))
if epoch + 1 == args.epochs or miou > best_miou:
for key, class_iou in zip(class_encoding.keys(), iou):
print("{0}: {1:.4f}".format(key, class_iou))
if miou > best_miou:
print("\nBest model thus far. Saving...\n")
best_miou = miou
utils.save_checkpoint(model, optimizer, epoch + 1, best_miou, args)
return model
def test(model, test_loader, class_weights, class_encoding):
print("\nTesting...\n")
num_classes = len(class_encoding)
# Define loss function
criterion = nn.CrossEntropyLoss(weight=class_weights)
# Define evaluation metric
if args.ignore_unlabeled:
ignore_index = list(class_encoding).index('unlabeled')
else:
ignore_index = None
metric = IoU(num_classes, ignore_index=ignore_index)
# Test the model
test = Test(model, test_loader, criterion, metric, device)
print(">>>> Running test dataset")
loss, (iou, miou) = test.run_epoch(args.print_step)
class_iou = dict(zip(class_encoding.keys(), iou))
print(">>>> Avg. loss: {0:.4f} | Mean IoU: {1:.4f}".format(loss, miou))
for key, class_iou in zip(class_encoding.keys(), iou):
print("{0}: {1:.4f}".format(key, class_iou))
if args.imshow_batch:
print("A batch of predictions from the test set...")
images, _ = next(iter(test_loader))
predict(model, images, class_encoding)
def predict(model, images, class_encoding):
images = images.to(device)
model.eval()
with torch.no_grad():
predictions = model(images)
_, predictions = torch.max(predictions.data, 1)
label_to_rgb = transforms.Compose([
ext_transforms.LongTensorToRGBPIL(class_encoding),
transforms.ToTensor()
])
color_predictions = utils.batch_transform(predictions.cpu(), label_to_rgb)
utils.imshow_batch(images.data.cpu(), color_predictions)
# Run the script if executed directly
if __name__ == '__main__':
# Verify the dataset directory exists
assert os.path.isdir(args.dataset_dir), f"The directory \"{args.dataset_dir}\" doesn't exist."
# Verify the save directory exists
assert os.path.isdir(args.save_dir), f"The directory \"{args.save_dir}\" doesn't exist."
# Import the selected dataset module
if args.dataset.lower() == 'camvid':
from data import CamVid as dataset
elif args.dataset.lower() == 'cityscapes':
from data import Cityscapes as dataset
else:
raise RuntimeError(f"\"{args.dataset}\" is not a supported dataset.")
loaders, class_weights, class_encoding = load_dataset(dataset)
train_loader, val_loader, test_loader = loaders
if args.mode.lower() in {'train', 'full'}:
model = train(train_loader, val_loader, class_weights, class_encoding)
if args.mode.lower() in {'test', 'full'}:
if args.mode.lower() == 'test':
num_classes = len(class_encoding)
model = ENet(num_classes).to(device)
optimizer = optim.Adam(model.parameters())
model = utils.load_checkpoint(model, optimizer, args.save_dir, args.name)[0]
if args.mode.lower() == 'test':
print(model)
test(model, test_loader, class_weights, class_encoding)