forked from acheshkov/eyeglasses_classifier_lightweight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
169 lines (145 loc) · 6.91 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
import torch
import torchvision
import numpy as np
import argparse
from sklearn.metrics import f1_score
from torchvision import transforms, utils
from torch.utils.data import Subset, DataLoader, Dataset
from typing import Tuple, List, Iterable
from image_folder import ImageFolderWithPaths
from avg_meter import AverageMeter
from stop_criteria import StopCriteria
TRAIN_SAMPLES_FRACTION = .8 # fraction of train samples to total number of samples
NORMALIZITAION_FOR_PRETRAINED = transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
def parse_args():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description='Script to train'
)
parser.add_argument('--epochs', type=int, help='Epochs number', default=1)
parser.add_argument('--batch-size', type=int, help='Batch size', default=10)
parser.add_argument('--images-path', type=str, help='Images directory location', required=True)
parser.add_argument('--lr', type=float, help='Learning rate', default=1e-5)
args = parser.parse_args()
return args
def _mk_k_folds_indicies(arr: List[int], k: int) -> Iterable[Tuple[List[int], List[int]]]:
''' split list of integers up to "k" pairs that will form the base of k-fold partitions of dataset'''
def array_diff(a1, a2):
return list(filter(lambda v: len(list(filter(lambda x: x == v, a2))) == 0, a1))
splited = np.array_split(arr, k)
return [(array_diff(arr, s.tolist()), s.tolist()) for s in splited]
def mk_k_folds(ds: Dataset, k: int, batch_size: int) -> Iterable[Tuple[DataLoader, DataLoader]]:
''' make k-folds. returns Iterator (Train data, Validation data)'''
indices = list(range(0, len(ds)))
np.random.shuffle(indices)
splited = _mk_k_folds_indicies(indices, k)
mk_data_loader = lambda idxs: DataLoader(Subset(ds, idxs), batch_size, num_workers=0)
return [(mk_data_loader(train_idxs), mk_data_loader(val_idxs)) for (train_idxs, val_idxs) in splited]
def train_cycle(
data_loader: DataLoader,
model: torch.nn.Module,
optimizer: torch.optim.Optimizer,
device,
backprop=True) -> Tuple[float, float]:
''' regular single training/validation cycle '''
avg_meter = AverageMeter()
score = 0
targets = []
ys = []
for *batch, _ in data_loader:
x = batch[0].to(device)
target = batch[1].to(device).float()
y = model(x).squeeze(1)
y_sigm = torch.sigmoid(y).cpu()
targets = np.concatenate((targets, target.cpu().numpy()))
ys = np.concatenate((ys, torch.sign(torch.where(y_sigm > 0.5, y_sigm, torch.tensor(.0))).detach().numpy()))
if backprop: optimizer.zero_grad()
loss_fn = torch.nn.BCEWithLogitsLoss()
loss = loss_fn(y, target)
if backprop:
loss.backward()
optimizer.step()
avg_meter.update(loss.item(), len(batch))
score = f1_score(targets, ys)
return avg_meter.avg, score
if __name__ == '__main__':
args = parse_args()
print(args)
MAX_EPOCH = args.epochs
BATCH_SIZE = args.batch_size
LR = args.lr
np.random.seed(17)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print("Device", device)
# ============ Data preparation ==============
transform = transforms.Compose([
transforms.ToTensor(),
NORMALIZITAION_FOR_PRETRAINED
])
images = ImageFolderWithPaths(args.images_path, transform=transform)
folds = mk_k_folds(images, k=5, batch_size=BATCH_SIZE)
# ============ Train Cumbersome Model ===============
print('Resnet18 Regular Training ...')
fold_scores = []
for fold_n, (train_loader, val_loader) in enumerate(folds): # loop over folds
print(f'Start fold #{fold_n + 1} ...')
print(f'Train is {len(train_loader) * BATCH_SIZE} length')
print(f'Val is {len(val_loader) * BATCH_SIZE} length')
model = torchvision.models.resnet18(pretrained=True)
model.fc = torch.nn.Linear(512, 1)
model.num_classes = 1
model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=LR)
stop_criteria = StopCriteria()
for epoch in range(0, MAX_EPOCH):
model.train()
avg_loss, score = train_cycle(train_loader, model, optimizer, device)
print(epoch, 'TRAIN', round(avg_loss, 3), round(score, 3))
model.eval()
with torch.no_grad():
avg_loss, score = train_cycle(val_loader, model, optimizer, device, backprop=False)
print(epoch, 'VAL ', round(avg_loss, 3), round(score, 3))
if stop_criteria.check(round(avg_loss, 4), round(score, 4), model):
print("Stop training. Score hasn't improved.")
break
print("Best score is", round(stop_criteria.best_score, 3))
fold_scores.append(stop_criteria.best_score)
torch.save(stop_criteria.get_best_model_params(), './resnet_params')
print(f'E[score] = {round(np.mean(fold_scores), 3)}, Var[score] = {round(np.std(fold_scores), 3)}')
# ============ SqueezeNet Regular Training =================
print('SqueezeNet Regular Training ...')
fold_scores = []
for fold_n, (train_loader, val_loader) in enumerate(folds):
print(f'Start fold #{fold_n + 1} ...')
print(f'Train is {len(train_loader) * BATCH_SIZE} length')
print(f'Val is {len(val_loader) * BATCH_SIZE} length')
model = torchvision.models.squeezenet1_1(pretrained=True)
model.classifier = torch.nn.Sequential(
torch.nn.Dropout(p=0.5),
torch.nn.Conv2d(512, 1, kernel_size=(1, 1), stride=(1, 1)),
torch.nn.ReLU(),
torch.nn.AvgPool2d(kernel_size=13, stride=1, padding=0)
)
model.num_classes = 1
model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=LR)
stop_criteria = StopCriteria()
for epoch in range(0, MAX_EPOCH):
model.train()
avg_loss, score = train_cycle(train_loader, model, optimizer, device)
print(epoch, 'TRAIN', round(avg_loss, 3), round(score, 3))
model.eval()
with torch.no_grad():
avg_loss, score = train_cycle(val_loader, model, optimizer, device, backprop=False)
print(epoch, 'VAL ', round(avg_loss, 3), round(score, 3))
if stop_criteria.check(round(avg_loss, 4), round(score, 4), model):
print("Stop training. Score hasn't not improve.")
torch.save(stop_criteria.get_best_model_params(), './squeezenet_params')
break
print("Best score is", round(stop_criteria.best_score, 3))
fold_scores.append(stop_criteria.best_score)
torch.save(stop_criteria.get_best_model_params(), './squeezenet_params')
print(f'E[score] = {round(np.mean(fold_scores), 3)}, Var[score] = {round(np.std(fold_scores), 3)}')