[ad8447]: / (1) PyTorch_HistoNet / functions / train_model_val.py

Download this file

225 lines (181 with data), 7.7 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
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
import torch
import time
import copy
import numpy as np
import os
import torch.nn as nn
from util import pause
from util import getClassCount
from util import normImageCustom
from util import imshow
from util import visImage
from util import print_pers
# multi label accuracy
def acc_fun_geno(preds, labelsTens):
labels_all = labelsTens.data.int()
runningsum = 0
preds_cpu = preds.cpu()
labels_all_cpu = labels_all.cpu()
for i, pred_sample in enumerate(preds_cpu):
labelv = labels_all_cpu[i]
numerator = torch.sum(np.bitwise_and(pred_sample, labelv))
denominator = torch.sum(np.bitwise_or(pred_sample, labelv))
runningsum += (numerator.double()/denominator.double())
return runningsum
# training with validation
def train_model_val(model, classVec,
optimizer, scheduler,
num_epochs, dataset_sizes, dataloader_train, dataloader_val,
batch_sizeP, num_classes, modelName,
dirResults, iteration, fileResultNameFull, log, cuda):
# check if final already exists
fileNameSaveFinal = 'modelsave_{0}_final.pt'.format(iteration+1)
if os.path.isfile(os.path.join(dirResults, fileNameSaveFinal)):
# display
if log:
print_pers('\tModel loaded', fileResultNameFull)
model.load_state_dict(torch.load(os.path.join(dirResults, fileNameSaveFinal)))
return model
#init time
since = time.time()
# init best model
best_model_wts = copy.deepcopy(model.state_dict())
best_acc = 0.0
# compute num batches
numBatches = {}
numBatches['train'] = np.round(dataset_sizes['train'] / batch_sizeP)
numBatches['val'] = np.round(dataset_sizes['val'] / batch_sizeP)
# class weights computed on train and val together
datasetSizeAll = dataset_sizes['train'] + dataset_sizes['val']
classCountAll = getClassCount(classVec['train']) + getClassCount(classVec['val'])
# if no count, put 1
numSub = 10
for listc, tt in enumerate(classCountAll):
if tt < numSub:
classCountAll[listc] = numSub
weightsBCE = torch.FloatTensor(datasetSizeAll / classCountAll)
if cuda:
weightsBCE = weightsBCE.to('cuda')
#print(classCountAll)
#print(weightsBCE)
# check if partial results
entries = os.listdir(dirResults)
max_epoch = -1
for entry in entries:
if entry.endswith(".pt"):
entry2 = os.path.splitext(entry)
temp = entry2[0].split('_')
# onlyy for this iteration
if int(temp[1]) != (iteration+1):
continue
entry3 = temp[-1]
saved_epoch = int(entry3)
if saved_epoch > max_epoch:
max_epoch = saved_epoch
if max_epoch > -1:
fileNameSave = 'modelsave_{0}_epoch_{1}.pt'.format(iteration+1, max_epoch)
model.load_state_dict(torch.load(os.path.join(dirResults, fileNameSave)))
# loop on epochs
for epoch in range(num_epochs):
# continue from saved epoch
if epoch <= max_epoch:
continue
# display
if log:
print_pers('\tEpoch {}/{}'.format(epoch+1, num_epochs), fileResultNameFull)
# Each epoch has a training and validation phase
for phase in ['train', 'val']:
if phase == 'train':
model.train() # Set model to training mode
else:
model.eval() # Set model to evaluate mode
# init losses and corrects
running_loss = 0.0
running_corrects = 0.0
running_corrects2 = 0.0
# choose dataloader
if phase == 'train':
dataloaders_chosen = dataloader_train
if phase == 'val':
dataloaders_chosen = dataloader_val
# Iterate over data.
for batch_num, (inputs, dummyTargets, filename, label) in enumerate(dataloaders_chosen):
# get size of current batch
sizeCurrentBatch = dummyTargets.size(0)
##################
#if batch_num > 10:
#break
##################
# cuda
if cuda:
inputs = inputs.to('cuda')
label = label.to('cuda')
# display
if batch_num % 100 == 0:
print_pers("\t\tBatch n. {0} / {1}".format(batch_num, int(numBatches[phase])), fileResultNameFull)
# indexes
#indStart = batch_num * batch_sizeP
#indEnd = indStart + sizeCurrentBatch
# zero the parameter gradients
optimizer.zero_grad()
# forward
# track history if only in train
with torch.set_grad_enabled(phase == 'train'):
outputs = model(inputs)
if cuda:
outputs = outputs.to('cuda')
m = nn.Sigmoid()
preds = (m(outputs) > 0.5).int()
#print(label)
#print(label.size())
#print(outputs)
#print(outputs.size())
#criterion = torch.nn.BCEWithLogitsLoss(pos_weight=weightsBCE)
criterion = torch.nn.MultiLabelSoftMarginLoss(weight=weightsBCE)
loss = criterion(outputs.float(), label.float())
#print(loss)
#pause()
# backward + optimize only if in training phase
if phase == 'train':
loss.backward()
optimizer.step()
# statistics
with torch.no_grad():
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == label.data.int())
running_corrects2 += acc_fun_geno(preds, label)
# update schedulers
if phase == 'train':
for schedulerSingle in scheduler:
schedulerSingle.step()
# compute epochs losses
with torch.no_grad():
epoch_loss = running_loss / dataset_sizes[phase]
epoch_acc = running_corrects.double() / (dataset_sizes[phase] * num_classes)
epoch_acc2 = running_corrects2.double() / dataset_sizes[phase]
# display
if log:
print_pers('\t\t{} Loss: {:.4f} Acc (1-HL): {:.4f}; (MultiL): {:.4f}'.format(phase, epoch_loss, epoch_acc, epoch_acc2),
fileResultNameFull)
# if greater val accuracy, deep copy the model
if phase == 'val' and epoch_acc > best_acc:
best_acc = epoch_acc
best_model_wts = copy.deepcopy(model.state_dict())
# save model at epoch
if epoch % 20 == 0:
fileNameSave = 'modelsave_{0}_epoch_{1}.pt'.format(iteration+1, epoch)
torch.save(model.state_dict(), os.path.join(dirResults, fileNameSave))
# del
del inputs, dummyTargets, label
torch.cuda.empty_cache()
# time
time_elapsed = time.time() - since
print('\tTraining complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
print('\tBest val Acc: {:4f}'.format(best_acc))
# load best model weights
model.load_state_dict(best_model_wts)
# save final
torch.save(model.state_dict(), os.path.join(dirResults, fileNameSaveFinal))
# del
torch.cuda.empty_cache()
return model