-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_torchvision.py
390 lines (335 loc) · 13.6 KB
/
train_torchvision.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import csv
from typing import Dict, List
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.models as models
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
from torch.utils.data import Dataset
from tqdm import tqdm
import json
import argparse
from pathlib import Path
from PIL import Image
def create_argparser() -> argparse.Namespace:
parser = argparse.ArgumentParser()
# directory with relevant folders
parser.add_argument("-project_directory", type=Path)
# number of epochs before training is stopped if patience hasn't stopped it early
parser.add_argument("-num_epochs", type=int)
# number of classes within the dataset
parser.add_argument("-num_classes", type=int)
# proportion of the gradient that is used to update parameters
parser.add_argument("-learning_rate", type=float)
# training will stop when this number of epochs have past
# and the validation loss has not improved
parser.add_argument("-patience", type=int)
# number of images to take feed into the model before
# measuring gradient and updating parameters
parser.add_argument("-batch_size", type=int)
# name of the model being trained e.g. model.pth.tar
parser.add_argument("-model_save_name", type=str)
# shape of the input image -> channels first for PyTorch
parser.add_argument("-img_shape", type=list, nargs="+")
# architecture to use
parser.add_argument("-architecture", type=str)
# if set to true, this argument uses the GPU
# to set as true use --use_GPU in CLI
parser.add_argument("-use_GPU", action="store_true")
return parser.parse_args()
def read_csv(csv_path: Path) -> List:
with open(csv_path) as opened_csv:
reader = csv.reader(opened_csv)
return [x for x in reader]
def define_device(use_GPU: bool) -> torch.device:
"""
defines device to manage allocation of tensors
"""
if use_GPU:
return torch.device("cuda")
else:
return torch.device("cpu")
def define_model(
architecture: str,
num_classes: int,
device: torch.device
) -> models:
"""
defines different torchvision models and replaces the classifier layer with an
appropriatiely sized one for the number of classes in the dataset
"""
if architecture == "resnet18":
model = models.resnet18()
num_ftrs = model.fc.in_features
model.fc = nn.Linear(in_features=num_ftrs, out_features=num_classes)
return model.to(device)
elif architecture == "vgg":
model = models.vgg11_bn()
num_ftrs = model.classifier[6].in_features
model.classifier[6] = nn.Linear(num_ftrs, num_classes)
return model.to(device)
elif architecture == "densenet":
model = models.densenet121()
num_ftrs = model.classifier.in_features
model.classifier = nn.Linear(num_ftrs, num_classes)
return model.to(device)
def define_optimizer(model: models.efficientnet.EfficientNet, learning_rate: float) -> optim.Adam:
"""
returns the algorithm that updates the parameters
"""
return optim.Adam(model.parameters(), lr=learning_rate)
def define_criterion() -> nn.CrossEntropyLoss:
"""
returns the algorithm to measure loss
"""
return nn.CrossEntropyLoss()
def load_model(weight_path: Path, model: models.efficientnet.EfficientNet) -> models.efficientnet:
"""
loads all parameters of a model
"""
checkpoint = torch.load(weight_path)
model.load_state_dict(checkpoint['state_dict'])
return model
class ImageClassificationDataset(Dataset):
"""
dataset reads filenames and classes
from csv and returns an opened image and the class
"""
def __init__(self, csv_file: Path, img_dir: Path, transform=None, resize=None) -> None:
self.dataset_tuples = read_csv(csv_path=csv_file) # (file name, class)
self.img_dir = img_dir
self.transform = transform
self.resize = resize
def __len__(self) -> None:
return len(self.dataset_tuples)
def __getitem__(self, index: int) -> tuple[torch.tensor, torch.tensor]:
y_label = torch.tensor(int(self.dataset_tuples[index][1]))
img_path = self.img_dir.joinpath(self.dataset_tuples[index][0])
image = Image.open(img_path).convert("RGB")
# convert [["2", "2", "4"], ["2", "2", "4"]]
# to (224, 224)
resize_formatted_tuple = (strings_to_int(x) for x in self.resize)
if self.resize:
image = image.resize(size=resize_formatted_tuple)
if self.transform:
image = self.transform(image)
return image, y_label
def strings_to_int(string_list: list) -> int:
empty_string = ""
return int(empty_string.join(string_list))
def save_checkpoint(state: Dict, filepath: Path) -> None:
"""
saves the model state dictionary to a .pth.tar tile
"""
print("saving...")
torch.save(state, filepath)
def create_datasets(
img_size: tuple,
batch_size: int,
train_file_path: Path,
img_dir: Path,
val_file_path: Path
) -> tuple[
ImageClassificationDataset,
torch.utils.data.DataLoader,
ImageClassificationDataset,
torch.utils.data.DataLoader,
List,
List
]:
"""
creates the datasets and returns dataloaders
"""
train_dataset = ImageClassificationDataset(
csv_file=train_file_path,
img_dir=img_dir,
transform=transforms.Compose([
transforms.ToTensor(),
]),
resize=img_size[1:]
)
val_dataset = ImageClassificationDataset(
csv_file=val_file_path,
img_dir=img_dir,
transform=transforms.Compose([
transforms.ToTensor(),
]),
resize=img_size[1:]
)
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
val_loader = DataLoader(dataset=val_dataset, batch_size=batch_size, shuffle=False)
return train_dataset, train_loader, val_dataset, val_loader
@torch.no_grad()
def validate_model(
model: models.efficientnet.EfficientNet,
val_loader: torch.utils.data.DataLoader,
device: torch.device,
criterion: nn.CrossEntropyLoss,
val_losses: List,
num_correct_val: int
) -> tuple[models.efficientnet_b0, List, int]:
"""
evaluates model performance on the validation set
"""
model.eval() # turn dropout and batch norm off
for val_data, val_targets in tqdm(val_loader, desc='Validation'):
val_data = val_data.to(device=device)
val_targets = val_targets.to(device=device)
val_scores = model(val_data)
_, val_predictions = val_scores.max(1)
num_correct_val += (val_predictions == val_targets).sum().detach()
val_loss = criterion(val_scores, val_targets)
val_losses.append(val_loss.item())
model.train() # turn regularization back on
return model, val_losses, num_correct_val
def train(
num_epochs: int,
patience: int,
model_save_name: str,
train_dataset: ImageClassificationDataset,
train_loader: torch.utils.data.DataLoader,
val_dataset: ImageClassificationDataset,
val_loader: torch.utils.data.DataLoader,
model: models.efficientnet,
optimizer: optim.Adam,
device: torch.device,
criterion: nn.CrossEntropyLoss,
model_save_dir: Path
) -> tuple[List, List, List, List]:
"""
trains a model until the validation
loss fails to decrease after a specified number of epochs [patience]
"""
patience_counter = 0
best_checkpoint = None
plot_losses = []
plot_val_losses = []
min_val_loss = 0
plot_accuracy = []
plot_val_accuracy = []
for epoch in range(num_epochs):
if patience_counter == patience:
break # early stopping
losses = []
val_losses = []
num_correct_train = 0
num_correct_val = 0
checkpoint = {'state_dict': model.state_dict(), 'optimizer': optimizer.state_dict()}
for data, targets in tqdm(train_loader, desc='Train'):
data = data.to(device=device)
targets = targets.to(device=device)
scores = model(data)
with torch.no_grad(): # counting correct predictions
_, predictions = scores.max(1)
num_correct_train += (predictions == targets).sum().detach()
loss = criterion(scores, targets) # calculate loss
losses.append(loss.item())
optimizer.zero_grad() # clear gradient information
loss.backward() # calculate gradient
optimizer.step()
model, val_losses, num_correct_val = validate_model(
model=model,
val_loader=val_loader,
device=device,
criterion=criterion,
val_losses=val_losses,
num_correct_val=num_correct_val
)
if epoch == 0:
min_val_loss = (sum(val_losses) / len(val_losses))
best_checkpoint = checkpoint
save_checkpoint(state=best_checkpoint, filepath=model_save_dir.joinpath(model_save_name))
patience_counter += 1
else:
new_loss = (sum(val_losses) / len(val_losses))
if new_loss < min_val_loss:
best_checkpoint = checkpoint
save_checkpoint(state=best_checkpoint, filepath=model_save_dir.joinpath(model_save_name))
min_val_loss = (sum(val_losses) / len(val_losses))
patience_counter = 0
else:
patience_counter += 1
# print loss, accuracy, val loss, and val accuracy after end of epoch
print(f'Epoch: {epoch + 1} Loss: {sum(losses) / len(losses)}\
Accuracy: {num_correct_train.cpu().numpy() / len(train_dataset)}\
Validation Loss: {sum(val_losses) / len(val_losses)}\
Validation Accuracy: {num_correct_val.cpu().numpy() / len(val_dataset)}', flush=True)
plot_losses.append([epoch + 1, (sum(losses) / len(losses))])
plot_val_losses.append([epoch + 1, (sum(val_losses) / len(val_losses))])
plot_accuracy.append([epoch + 1, (num_correct_train.cpu().numpy() / len(train_dataset))])
plot_val_accuracy.append([epoch + 1, (num_correct_val.cpu().numpy() / len(val_dataset))])
return plot_losses, plot_val_losses, plot_accuracy, plot_val_accuracy
def save_results(
batch_size: int,
model_save_name: str,
train_tile_csv: str,
val_tile_csv: str,
patience: int,
plot_losses: List,
plot_val_losses: List,
plot_accuracy: List,
plot_val_accuracy: List,
result_dir: Path,
optimizer: optim.Adam
) -> None:
"""
saves the results of training and hyperparameters into json files
"""
# save hyperparameter configurations
hyperparameters = {'batch size': batch_size,
'model save name': model_save_name,
'optimizer': optimizer.defaults,
'train dataset': train_tile_csv,
'validation dataset': val_tile_csv,
'patience': patience}
json_losses = {'loss values': plot_losses,
'accuracy values': plot_accuracy,
'val loss values': plot_val_losses,
'val accuracy values': plot_val_accuracy}
with open(f'{result_dir.joinpath(model_save_name[:-8])}_hyperparameters.json', 'w') as outfile:
json.dump(hyperparameters, outfile)
with open(f'{result_dir.joinpath(model_save_name[:-8])}_loss_values.json', 'w') as outfile:
json.dump(json_losses, outfile)
def main():
args = create_argparser()
train_dataset, train_loader, val_dataset, val_loader = create_datasets(
batch_size=args.batch_size,
img_size=args.img_shape,
train_file_path=args.project_directory.joinpath('csv').joinpath("train.csv"),
img_dir=args.project_directory.joinpath('bin'),
val_file_path=args.project_directory.joinpath("csv").joinpath("val.csv")
)
device = define_device(use_GPU=args.use_GPU)
model = define_model(architecture=args.architecture, num_classes=args.num_classes, device=device)
optimizer = define_optimizer(model=model, learning_rate=args.learning_rate)
criterion = define_criterion()
plot_losses, plot_val_losses, plot_accuracy, plot_val_accuracy = train(
num_epochs=args.num_epochs,
patience=args.patience,
model_save_name=args.model_save_name,
train_dataset=train_dataset,
train_loader=train_loader,
val_dataset=val_dataset,
val_loader=val_loader,
model=model,
optimizer=optimizer,
device=device,
criterion=criterion,
model_save_dir=args.project_directory.joinpath("models")
)
save_results(
batch_size=args.batch_size,
model_save_name=args.model_save_name,
train_tile_csv="train.csv",
val_tile_csv="val.csv",
patience=args.patience,
plot_losses=plot_losses,
plot_val_losses=plot_val_losses,
plot_accuracy=plot_accuracy,
plot_val_accuracy=plot_val_accuracy,
result_dir=args.project_directory.joinpath("results"),
optimizer=optimizer
)
if __name__ == "__main__":
main()