forked from iceberg-project/Seals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_sealnet.py
244 lines (192 loc) · 10.5 KB
/
train_sealnet.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
238
239
240
241
242
243
244
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
import numpy as np
from torchvision import datasets, transforms
from torch.autograd import Variable
import os
import argparse
from tensorboardX import SummaryWriter
import time
from utils.model_library import *
from PIL import ImageFile
import warnings
parser = argparse.ArgumentParser(description='trains a CNN to find seals in satellite imagery')
parser.add_argument('--training_dir', type=str, help='base directory to recursively search for images in')
parser.add_argument('--model_architecture', type=str, help='model architecture, must be a member of models '
'dictionary')
parser.add_argument('--hyperparameter_set', type=str, help='combination of hyperparameters used, must be a member of '
'hyperparameters dictionary')
parser.add_argument('--cv_weights', nargs='?', type=str, default='NO', help='weights for weighted-cross validation, '
'must be a member of cv_weights dictionary')
parser.add_argument('--output_name', type=str, help='name of output file from training, this name will also be used in '
'subsequent steps of the pipeline')
parser.add_argument('--pipeline', type=str, help='name of the detection pipeline where the model will be saved')
parser.add_argument('--dest_folder', type=str, default='saved_models', help='folder where the model will be saved')
args = parser.parse_args()
# check for invalid inputs
if args.model_architecture not in model_archs:
raise Exception("Invalid architecture -- see supported architectures:\n\n {}".format(model_archs.keys()))
if args.training_dir not in training_sets:
raise Exception("Training set is not defined in ./utils/model_library.py")
if args.hyperparameter_set not in hyperparameters:
raise Exception("Hyperparameter combination is not defined in ./utils/model_library.py")
if args.pipeline not in model_defs:
raise Exception('Pipeline is not defined in ./utils/model_library.py')
if args.cv_weights not in cv_weights:
raise Exception("Cross-validation are not defined in ./utils/model_library.py")
# image transforms seem to cause truncated images, so we need this
ImageFile.LOAD_TRUNCATED_IMAGES = True
# we get an RGB warning, but the loader properly converts to RGB -after- this
warnings.filterwarnings('ignore', module='PIL')
# Data augmentation and normalization for training
# Just normalization for validation
arch_input_size = model_archs[args.model_architecture]['input_size']
data_transforms = {
'training': transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.RandomRotation(180, expand=True),
transforms.CenterCrop(arch_input_size * 1.5),
transforms.RandomResizedCrop(size=arch_input_size, scale=(0.8, 1), ratio=(1, 1)),
transforms.ColorJitter(brightness=np.random.choice([0, 1]) * 0.05,
contrast=np.random.choice([0, 1]) * 0.05),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
'validation': transforms.Compose([
transforms.CenterCrop(arch_input_size),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}
# load datasets
data_dir = "./training_sets/{}".format(args.training_dir)
image_datasets = {x: dataloaders[args.pipeline](os.path.join(data_dir, x), data_transforms[x])
for x in ['training', 'validation']}
# store number of classes
num_classes = len(image_datasets['training'].classes)
# Force minibatches to have an equal representation amongst classes during training with a weighted sampler
def make_weights_for_balanced_classes(images, nclasses):
count = [0] * nclasses
for item in images:
count[item[1]] += 1
weight_per_class = [0.] * nclasses
N = float(sum(count))
for i in range(nclasses):
weight_per_class[i] = N / float(count[i])
weight = [0] * len(images)
for idx, val in enumerate(images):
weight[idx] = weight_per_class[val[1]]
return weight
# For unbalanced dataset we create a weighted sampler
weights = make_weights_for_balanced_classes(image_datasets['training'].imgs, num_classes)
weights = torch.DoubleTensor(weights)
sampler = torch.utils.data.sampler.WeightedRandomSampler(weights, len(weights))
# change batch size ot match number of GPU's being used?
dataloaders = {"training": torch.utils.data.DataLoader(image_datasets["training"],
batch_size=
hyperparameters[args.hyperparameter_set]['batch_size_train'],
sampler=sampler, num_workers=
hyperparameters[args.hyperparameter_set]['num_workers_train']),
"validation": torch.utils.data.DataLoader(image_datasets["validation"],
batch_size=
hyperparameters[args.hyperparameter_set]['batch_size_val'],
num_workers=
hyperparameters[args.hyperparameter_set]['num_workers_val'])
}
dataset_sizes = {x: len(image_datasets[x]) for x in ['training', 'validation']}
class_names = image_datasets['training'].classes
use_gpu = torch.cuda.is_available()
def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
since = time.time()
# create summary writer for tensorboardX
writer = SummaryWriter()
# keep track of training iterations
global_step = 0
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch + 1, num_epochs))
print('-' * 10)
# Each epoch has a training and validation phase
for phase in ['training', 'validation']:
if phase == 'training':
scheduler.step()
model.train(True) # Set model to training mode
else:
model.train(False) # Set model to evaluate mode
running_loss = 0.0
running_corrects = 0
# Iterate over data.
for data in dataloaders[phase]:
# get the inputs
inputs, targets = data
# wrap them in Variable
if use_gpu:
inputs = Variable(inputs.cuda())
targets = Variable(targets.cuda())
else:
inputs, targets = Variable(inputs), Variable(targets)
# zero the parameter gradients
optimizer.zero_grad()
# forward
outputs = model(inputs)
_, preds = torch.max(outputs.data, 1)
loss = criterion(outputs, targets)
# backward + optimize only if in training phase
if phase == 'training':
loss.backward()
optimizer.step()
global_step += 1
# statistics
running_loss += loss.item() * inputs.size(1)
running_corrects += torch.sum(preds == targets.data).item()
epoch_loss = running_loss / dataset_sizes[phase]
epoch_acc = running_corrects / dataset_sizes[phase]
if phase == 'validation':
writer.add_scalar('validation_loss', epoch_loss, global_step=global_step)
writer.add_scalar('validation_accuracy', epoch_acc, global_step=global_step)
else:
writer.add_scalar('training_loss', epoch_loss, global_step=global_step)
writer.add_scalar('training_accuracy', epoch_acc, global_step=global_step)
writer.add_scalar('learning_rate', optimizer.param_groups[-1]['lr'], global_step=global_step)
print('{} Loss: {:.4f} {} Acc: {:.4f}'.format(
phase, epoch_loss, phase, epoch_acc))
if phase == 'validation':
time_elapsed = time.time() - since
print('training time: {}h {:.0f}m {:.0f}s\n'.format(time_elapsed // 3600, (time_elapsed % 3600) // 60,
time_elapsed % 60))
time_elapsed = time.time() - since
print('Training complete in {}h {:.0f}m {:.0f}s'.format(
time_elapsed // 3600, (time_elapsed % 3600) // 60, time_elapsed % 60))
# save the model, keeping haulout and single seal models in separate folders
torch.save(model.state_dict(), './{}/{}/{}/{}.tar'.format(args.dest_folder, args.pipeline, args.output_name,
args.output_name))
return model
def main():
# loading the pretrained model and adding new classes to it
model_ft = model_defs[args.pipeline][args.model_architecture](num_classes)
# get weight
cv_weight = cv_weights[args.cv_weights](num_classes)
# define criterion
criterion = loss_functions[args.pipeline](cv_weight)
if use_gpu:
# i think we can set parallel GPU usage here. will test
# http://pytorch.org/docs/master/nn.html
# http://pytorch.org/docs/master/nn.html#dataparallel-layers-multi-gpu-distributed
# The batch size should be larger than the number of GPUs used.
# It should also be an integer multiple of the number of GPUs so that
# each chunk is the same size (so that each GPU processes the same number of samples).
# model_ft = nn.DataParallel(model_ft).cuda()
model_ft = model_ft.cuda()
criterion = criterion.cuda()
# Observe that all parameters are being optimized
optimizer_ft = optim.Adam(model_ft.parameters(), lr=hyperparameters[args.hyperparameter_set]['learning_rate'])
# Decay LR by a factor of 0.1 every 7 epochs
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=hyperparameters[args.hyperparameter_set]['step_size']
, gamma=hyperparameters[args.hyperparameter_set]['gamma'])
# start training
train_model(model_ft, criterion, optimizer_ft, exp_lr_scheduler,
num_epochs=hyperparameters[args.hyperparameter_set]['epochs'])
if __name__ == '__main__':
main()