-
Notifications
You must be signed in to change notification settings - Fork 2
/
pre_train.py
258 lines (181 loc) · 7.92 KB
/
pre_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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# -*- coding: utf-8 -*-
from sklearn.metrics import confusion_matrix
from plot_confusion_matrix import plot_confusion_matrix
import numpy as np
import torch
import matplotlib.pyplot as plt
plt.ioff()
from log import log
from network import save_fig
import preprocessing as pp
import network
def get_accuracy(prediction,label):
_, idx_C = label.max(1)
_, idx_P = prediction.max(1)
cases = list(label.size())[0]
correct = list(torch.where(idx_C==idx_P)[0].size())[0]
return correct/cases
def get_accuracy_binary(prediction,label):
cases = list(label.size())[0]
correct = list(torch.where(prediction.round()==label)[0].size())[0]
return correct/cases
def train_C(params):
# -------------------
# Parameters
# -------------------
log(str(params),name=params['log_name'])
# # Clear remaining model
# network.clear(params['name']+'_R'+str(params['start_run']))
# -------------------
# CUDA
# -------------------
cuda = True if torch.cuda.is_available() else False
C_Loss = torch.nn.BCELoss()
if cuda:
C_Loss.cuda()
floatTensor = torch.cuda.FloatTensor
log("CUDA Training.",name=params['log_name'])
else:
floatTensor = torch.FloatTensor
log("CPU Training.",name=params['log_name'])
# -------------------
# Data scaling
# -------------------
'''
XTL ... Training data labelled
XTU ... Training data unlabelled
XL ... Labelled data
XU ... Unlabelled data
XV ... Validation data
'''
dset_L = params['dset_L']
dset_V = params['dset_V']
XTL, YTL = pp.get_data(params,dset_L)
XV, YV = pp.get_data(params,dset_V)
XTL = pp.scale_minmax(XTL)
XV = pp.scale_minmax(XV)
if params['ratio_V'] < 1.0:
XV, YV = pp.select_random(XV,YV,params['ratio_L'])
log("Selected %s of validation samples."%( format(params['ratio_V'],'0.2f') ),name=params['log_name'])
XV, YV = pp.get_tensor(XV, YV)
# -------------------
# Load accuracy
# -------------------
mat_accuracy_C = network.load_R_Acc(params)
# -------------------
# Start Training
# -------------------
YF = None
PF = None
for run in range(params['runs']):
# -------------------
# Training Data
# -------------------
XL, YL = XTL, YTL
if params['ratio_L'] < 1.0:
XL, YL = pp.select_random(XL,YL,params['ratio_L'])
log("Selected %s of labelled samples."%( format(params['ratio_L'],'0.2f') ),name=params['log_name'])
count_L = YL.shape[0]
log("Number of labelled samples = %d."%( count_L ),name=params['log_name'])
dataloader = pp.get_dataloader(params, XL, YL)
C = network.load_Ref(run,params)
# -------------------
# Optimizers
# -------------------
optimizer_C = torch.optim.Adam(C.parameters(), lr=params['CLR'], betas=(params['CB1'], params['CB2']))
# -------------------
# Training
# -------------------
if run >= params['start_run']:
if params['oversampling']:
XL, YL = pp.over_sampling(params, XL, YL)
log("Oversampling: created %d new labelled samples."%( XL.shape[0]-count_L ),name=params['log_name'])
for epoch in range(params['epochs']):
# Jump to start epoch
if run == params['start_run']:
if epoch < params['start_epoch']:
continue
running_loss_C = 0.0
for i, data in enumerate(dataloader, 1):
loss_C = []
# -------------------
# Train the classifier on real samples
# -------------------
X1, Y1 = data
optimizer_C.zero_grad()
P1 = C(X1)
loss = C_Loss(P1, Y1)
loss_C.append(loss)
loss.backward()
optimizer_C.step()
# -------------------
# Calculate overall loss
# -------------------
running_loss_C += np.mean([loss.item() for loss in loss_C])
# -------------------
# Post Epoch
# -------------------
logString = "[Run %d/%d] [Epoch %d/%d] [C loss: %f]"%(run+1, params['runs'], epoch+1, params['epochs'], running_loss_C/(i))
log(logString,save=False,name=params['log_name'])
if (epoch+1)%params['save_step'] == 0:
# log("~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~|",save=False,name=params['log_name'])
idx = run,int(epoch/params['save_step'])+1
# Predict labels
PV = C(XV)
acc_C_real = get_accuracy(PV, YV)
mat_accuracy_C[idx] = acc_C_real
logString = "[Run %d/%d] [Epoch %d/%d] [C acc: %f ]"%(run+1, params['runs'], epoch+1, params['epochs'], acc_C_real)
log(logString,save=True,name=params['log_name'])
network.save_Ref(params['name'],run,C)
network.save_R_Acc(params, mat_accuracy_C)
params['start_epoch'] = epoch+1
network.save_Parameter(params)
# log("~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~|",save=False,name=params['log_name'])
# End of Training Run
params['start_run'] = run+1
params['start_epoch'] = 0
network.save_Parameter(params)
# -------------------
# Post Run
# -------------------
# Classify Validation data
PC = C(XV).detach()
if YF == None:
YF = YV
PF = PC
else:
YF = torch.cat((YF, YV), 0)
PF = torch.cat((PF, PC), 0)
# -------------------
# Post Training
# -------------------
timeline = np.arange(0,params['epochs']+1,params['save_step'])
# -------------------
# Plot Accuracy
# -------------------
acc_C = np.mean(mat_accuracy_C,axis=0)
fig, ax = plt.subplots()
legend = []
cmap = plt.get_cmap('gnuplot')
indices = np.linspace(0, cmap.N, 7)
colors = [cmap(int(i)) for i in indices]
ax.plot(timeline,acc_C,c=colors[0],linestyle='solid')
legend.append("Accuracy $A_C$")
ax.set_xlim(0.0,params['epochs'])
ax.set_ylim(0.0,1.0)
ax.legend(legend)
ax.set_xlabel('Epoch')
ax.set_ylabel('Accuracy')
ax.grid()
save_fig(params,'eval',fig)
# -------------------
# Generate Confusion Matrix
# -------------------
YF = pp.one_hot_to_labels(params,YF)
PF = pp.one_hot_to_labels(params,PF)
con_mat = confusion_matrix(YF, PF, labels=None, sample_weight=None, normalize='true')
plot_confusion_matrix(con_mat,params,name='C',title='Confusion matrix')
# -------------------
# Log Results
# -------------------
log(" - "+params['name']+": [C acc: %f]"%(acc_C[-1]),name='results')