-
Notifications
You must be signed in to change notification settings - Fork 22
/
train.py
234 lines (191 loc) · 7.45 KB
/
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
import numpy as np
import torch
import torchvision
from torchvision import datasets, models, transforms
import torch.utils.data as data
from torch.utils.tensorboard import SummaryWriter
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
from nets import *
import time, os, copy, argparse
import multiprocessing
from torchsummary import summary
from matplotlib import pyplot as plt
# Construct argument parser
ap = argparse.ArgumentParser()
ap.add_argument("--mode", required=True, help="Training mode: finetue/transfer/scratch")
args= vars(ap.parse_args())
# Set training mode
train_mode=args["mode"]
# Set the train and validation directory paths
train_directory = 'imds_small/train'
valid_directory = 'imds_small/val'
# Set the model save path
PATH="model.pth"
# Batch size
bs = 64
# Number of epochs
num_epochs = 10
# Number of classes
num_classes = 11
# Number of workers
num_cpu = multiprocessing.cpu_count()
# Applying transforms to the data
image_transforms = {
'train': transforms.Compose([
transforms.RandomResizedCrop(size=256, scale=(0.8, 1.0)),
transforms.RandomRotation(degrees=15),
transforms.RandomHorizontalFlip(),
transforms.CenterCrop(size=224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])
]),
'valid': transforms.Compose([
transforms.Resize(size=256),
transforms.CenterCrop(size=224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])
])
}
# Load data from folders
dataset = {
'train': datasets.ImageFolder(root=train_directory, transform=image_transforms['train']),
'valid': datasets.ImageFolder(root=valid_directory, transform=image_transforms['valid'])
}
# Size of train and validation data
dataset_sizes = {
'train':len(dataset['train']),
'valid':len(dataset['valid'])
}
# Create iterators for data loading
dataloaders = {
'train':data.DataLoader(dataset['train'], batch_size=bs, shuffle=True,
num_workers=num_cpu, pin_memory=True, drop_last=True),
'valid':data.DataLoader(dataset['valid'], batch_size=bs, shuffle=True,
num_workers=num_cpu, pin_memory=True, drop_last=True)
}
# Class names or target labels
class_names = dataset['train'].classes
print("Classes:", class_names)
# Print the train and validation data sizes
print("Training-set size:",dataset_sizes['train'],
"\nValidation-set size:", dataset_sizes['valid'])
# Set default device as gpu, if available
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if train_mode=='finetune':
# Load a pretrained model - Resnet18
print("\nLoading resnet18 for finetuning ...\n")
model_ft = models.resnet18(pretrained=True)
# Modify fc layers to match num_classes
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs,num_classes )
elif train_mode=='scratch':
# Load a custom model - VGG11
print("\nLoading VGG11 for training from scratch ...\n")
model_ft = MyVGG11(in_ch=3,num_classes=11)
# Set number of epochs to a higher value
num_epochs=100
elif train_mode=='transfer':
# Load a pretrained model - MobilenetV2
print("\nLoading mobilenetv2 as feature extractor ...\n")
model_ft = models.mobilenet_v2(pretrained=True)
# Freeze all the required layers (i.e except last conv block and fc layers)
for params in list(model_ft.parameters())[0:-5]:
params.requires_grad = False
# Modify fc layers to match num_classes
num_ftrs=model_ft.classifier[-1].in_features
model_ft.classifier=nn.Sequential(
nn.Dropout(p=0.2, inplace=False),
nn.Linear(in_features=num_ftrs, out_features=num_classes, bias=True)
)
# Transfer the model to GPU
model_ft = model_ft.to(device)
# Print model summary
print('Model Summary:-\n')
for num, (name, param) in enumerate(model_ft.named_parameters()):
print(num, name, param.requires_grad )
summary(model_ft, input_size=(3, 224, 224))
print(model_ft)
# Loss function
criterion = nn.CrossEntropyLoss()
# Optimizer
optimizer_ft = optim.SGD(model_ft.parameters(), lr=0.001, momentum=0.9)
# Learning rate decay
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)
# Model training routine
print("\nTraining:-\n")
def train_model(model, criterion, optimizer, scheduler, num_epochs=30):
since = time.time()
best_model_wts = copy.deepcopy(model.state_dict())
best_acc = 0.0
# Tensorboard summary
writer = SummaryWriter()
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print('-' * 10)
# Each epoch has a training and validation phase
for phase in ['train', 'valid']:
if phase == 'train':
model.train() # Set model to training mode
else:
model.eval() # Set model to evaluate mode
running_loss = 0.0
running_corrects = 0
# Iterate over data.
for inputs, labels in dataloaders[phase]:
inputs = inputs.to(device, non_blocking=True)
labels = labels.to(device, non_blocking=True)
# zero the parameter gradients
optimizer.zero_grad()
# forward
# track history if only in train
with torch.set_grad_enabled(phase == 'train'):
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
loss = criterion(outputs, labels)
# backward + optimize only if in training phase
if phase == 'train':
loss.backward()
optimizer.step()
# statistics
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
if phase == 'train':
scheduler.step()
epoch_loss = running_loss / dataset_sizes[phase]
epoch_acc = running_corrects.double() / dataset_sizes[phase]
print('{} Loss: {:.4f} Acc: {:.4f}'.format(
phase, epoch_loss, epoch_acc))
# Record training loss and accuracy for each phase
if phase == 'train':
writer.add_scalar('Train/Loss', epoch_loss, epoch)
writer.add_scalar('Train/Accuracy', epoch_acc, epoch)
writer.flush()
else:
writer.add_scalar('Valid/Loss', epoch_loss, epoch)
writer.add_scalar('Valid/Accuracy', epoch_acc, epoch)
writer.flush()
# deep copy the model
if phase == 'valid' and epoch_acc > best_acc:
best_acc = epoch_acc
best_model_wts = copy.deepcopy(model.state_dict())
print()
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))
print('Best val Acc: {:4f}'.format(best_acc))
# load best model weights
model.load_state_dict(best_model_wts)
return model
# Train the model
model_ft = train_model(model_ft, criterion, optimizer_ft, exp_lr_scheduler,
num_epochs=num_epochs)
# Save the entire model
print("\nSaving the model...")
torch.save(model_ft, PATH)
'''
Sample run: python train.py --mode=finetue
'''