-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrainer_tamir.py
More file actions
132 lines (110 loc) · 5.23 KB
/
Copy pathtrainer_tamir.py
File metadata and controls
132 lines (110 loc) · 5.23 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
import csv
import copy
import time
from tqdm import tqdm
import torch
import numpy as np
import os
from sklearn.cluster import KMeans
from sklearn.metrics import accuracy_score
def return_nearest_cluster(point, clusters):
dists = np.abs(clusters - point)
return clusters[np.argmin(dists)].item()
def quantization(mask, num_classes):
out = np.zeros(shape=mask.shape, dtype=float)
flat_mask = mask.reshape(1, -1).T
clusters = KMeans(n_clusters=num_classes, random_state=0, max_iter=500).fit(flat_mask).cluster_centers_
for i in range(mask.shape[0]):
out[i] = return_nearest_cluster(mask[i], clusters)
return out
def indexed_cluster_map(mask, clusters):
out = np.zeros(shape=mask.shape, dtype=np.uint8)
length = mask.shape[0]
for i in range(length):
dists = np.abs(clusters - mask[i])
out[i] = np.argmin(dists)
return out
def accuracy_per_sample(quantized_output, true_mask, num_classes):
flat_mask = true_mask.reshape(1, -1).T
kmeans = KMeans(n_clusters=num_classes, random_state=0, max_iter=500).fit(flat_mask).cluster_centers_
clusters = sorted([kmeans[i].item() for i in range(len(kmeans))])
index_mask = indexed_cluster_map(true_mask, clusters)
index_output = indexed_cluster_map(quantized_output, clusters)
return accuracy_score(index_mask.flatten(), index_output.flatten())
def train_model(model, criterion, dataloaders, optimizer, metrics, bpath, num_epochs=3, using_unet=True):
since = time.time()
best_model_wts = copy.deepcopy(model.state_dict())
best_loss = 1e10
# Use gpu if available
device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu")
model.to(device)
# Initialize the log file for training and testing loss and metrics
fieldnames = ['epoch', 'Train_loss', 'Test_loss'] + \
[f'Train_{m}' for m in metrics.keys()] + \
[f'Test_{m}' for m in metrics.keys()]
with open(os.path.join(bpath, 'log.csv'), 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for epoch in range(1, num_epochs + 1):
print('Epoch {}/{}'.format(epoch, num_epochs))
print('-' * 10)
# Each epoch has a training and validation phase
# Initialize batch summary
batchsummary = {a: [0] for a in fieldnames}
for phase in ['Train', 'Test']:
if phase == 'Train':
model.train() # Set model to training mode
else:
model.eval() # Set model to evaluate mode
# Iterate over data.
for sample in tqdm(iter(dataloaders[phase])):
inputs = sample['image'].to(device).float()
masks = sample['mask'].to(device).float()
# zero the parameter gradients
optimizer.zero_grad()
# track history if only in train
with torch.set_grad_enabled(phase == 'Train'):
outputs = model(inputs)
if using_unet:
outputs = {'out': outputs}
loss = criterion(outputs['out'], masks)
y_pred = outputs[
'out'].data.cpu().numpy().ravel() # Move to cpu, convert to numpy and flatten to a long vector
y_true = masks.data.cpu().numpy().ravel() # Move to cpu, convert to numpy and flatten to a long vector
for name, metric in metrics.items():
if name == 'f1_score':
# Use a classification threshold of 0.1
batchsummary[f'{phase}_{name}'].append(
metric(y_true > 0, y_pred > 0.1))
elif name == 'accuracy':
batchsummary[f'{phase}_{name}'].append(
accuracy_per_sample(quantization(y_pred, 3), y_true, 3))
else:
batchsummary[f'{phase}_{name}'].append(
metric(y_true.astype('uint8'), y_pred))
# backward + optimize only if in training phase
if phase == 'Train':
loss.backward()
optimizer.step()
batchsummary['epoch'] = epoch
epoch_loss = loss
batchsummary[f'{phase}_loss'] = epoch_loss.item()
print('{} Loss: {:.4f}'.format(
phase, loss))
for field in fieldnames[3:]:
batchsummary[field] = np.mean(batchsummary[field])
print(batchsummary)
with open(os.path.join(bpath, 'log.csv'), 'a', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow(batchsummary)
# deep copy the model
if phase == 'Test' and loss < best_loss:
best_loss = loss
best_model_wts = copy.deepcopy(model.state_dict())
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))
print('Lowest Loss: {:4f}'.format(best_loss))
# load best model weights
model.load_state_dict(best_model_wts)
return model