-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel_train.py
More file actions
312 lines (218 loc) · 8.8 KB
/
model_train.py
File metadata and controls
312 lines (218 loc) · 8.8 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
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
from lib import *
from model import SiameseModel
from dataset import SignatureDataset
# import model_preprocess
train_master_index = []
test_master_index = []
iter_list = [] # Saves Iterations at which the model has been evaluated
train_loss_list = [] # Saves Train Loss
train_acc_list = [] # Saves Train Accuracy
test_loss_list = [] # Saves Test Loss
test_acc_list = [] # Saves Test Accuracy
def_triplet_loss_margin = 1
def_learning_rate = 0.001
def_batch_size = 100
def_n_iters = 760
def_inspect_size = 15
############################### FUNCTIONS ###############################
def get_encodings(matrix):
'''
Accepts a Tensor and returns its encoding.
'''
if torch.cuda.is_available():
matrix = Variable(matrix.cuda())
else:
matrix = Variable(matrix)
matrix = matrix.float()
matrix_enc = model(matrix)
return matrix_enc
def return_diff(anchors_enc, positives_enc, negatives_enc):
'''
Accepts the encodings of three tensors.
Returns d(E1,E2) and d(E1,E3) where d(A,B) is the
Frobenius norm of the vector A-B.
Returns the result as a pair of numpy arrays.
'''
assert(anchors_enc.shape == positives_enc.shape)
assert(anchors_enc.shape == negatives_enc.shape)
num = anchors_enc.shape[0]
pos_diff_vec = []
neg_diff_vec = []
anchors_enc = anchors_enc.cpu().detach().numpy()
positives_enc = positives_enc.cpu().detach().numpy()
negatives_enc = negatives_enc.cpu().detach().numpy()
for i in range(num):
pos_diff = np.linalg.norm(anchors_enc[i] - positives_enc[i])
neg_diff = np.linalg.norm(anchors_enc[i] - negatives_enc[i])
pos_diff_vec.append(pos_diff)
neg_diff_vec.append(neg_diff)
return np.array(pos_diff_vec), np.array(neg_diff_vec)
def get_time(time_begin, time_end):
'''
Formats the time elapsed during training.
Returns hours, minutes, and seconds.
'''
FMT = '%H:%M:%S'
td = (datetime.strptime(time_end[11:19], FMT) - datetime.strptime(time_begin[11:19], FMT)).seconds
hr = (td//3600)
min = (td - 3600*hr)//60
sec = (td - 3600*hr - 60*min)
return hr, min, sec
############################### MAIN ###############################
if __name__ == "__main__":
# Parsing arguments.
parser = argparse.ArgumentParser()
parser.add_argument(
"--n-iters",
"-n",
default=def_n_iters,
type=int,
help="Number of iteration to train the model."
)
parser.add_argument(
"--inspect-size",
"-i",
default=def_inspect_size,
type=int,
help="Time period after which the model is evaluated against the test set."
)
parser.add_argument(
"--learning-rate",
"-r",
default=def_learning_rate,
type=float,
help="Simply the learning rate."
)
parser.add_argument(
"--batch-size",
"-b",
default=def_batch_size,
type=int,
help="Simply the batch size."
)
parser.add_argument(
"--triplet-loss-margin",
"-t",
default=def_triplet_loss_margin,
type=int,
help="Margin for the triplet loss."
)
args = parser.parse_args()
# __________________________________________________________________________________
# Loading index lists
fileObject = open('master_indices.pkl','rb')
listf = pickle.load(fileObject)
train_master_index = listf[0]
test_master_index = listf[1]
# Instantiating the dataset class
train_dataset = SignatureDataset(train_master_index, test_master_index, is_train = True)
test_dataset = SignatureDataset(train_master_index, test_master_index, is_train = False)
# Making dataset iterable
train_loader = torch.utils.data.DataLoader(
dataset=train_dataset,
batch_size=args.batch_size,
# shuffle=True
)
test_loader = torch.utils.data.DataLoader(
dataset=test_dataset,
batch_size=args.batch_size,
# shuffle=True
)
# Instantiating the model class
model = SiameseModel()
if torch.cuda.is_available():
model.cuda()
# Instantiating the loss and optimizer class
triplet_loss = nn.TripletMarginLoss(margin=args.triplet_loss_margin)
optimizer = torch.optim.SGD(model.parameters(), lr=args.learning_rate)
# __________________________________________________________________________________
# Initializations
iterr = 0
iter_list.clear()
train_loss_list.clear()
train_acc_list.clear()
test_loss_list.clear()
test_acc_list.clear()
num_epochs = int(args.n_iters / (len(train_dataset) / args.batch_size))
print("Number of Iterations :", args.n_iters)
print("Number of Epochs :", num_epochs)
print("Number of Sample-Points :", int(args.n_iters/args.inspect_size))
print("------------------------------------------------")
# __________________________________________________________________________________
### TRAINING THE MODEL ###
time_begin = time.asctime() # Time when training started
init_iters = iterr
for epoch in range(num_epochs):
for i, (anchors, positives, negatives) in enumerate(train_loader):
# Get encodings by forward propogation
anchors_enc = get_encodings(anchors)
positives_enc = get_encodings(positives)
negatives_enc = get_encodings(negatives)
# Clearing the previous gradients
optimizer.zero_grad()
# Calculating the Train loss
loss = triplet_loss(anchors_enc, positives_enc, negatives_enc)
# Backward propogation
loss.backward()
# Optimizing the parameters
optimizer.step()
iterr += 1
print("Iter {:.0f} Done.\t Loss : {:.5f}".format(iterr - init_iters, loss.item()))
# -----------------------------------------------------------------------------------
### Inspecting the performance of the model ###
if (iterr == 0 or iterr % args.inspect_size == 0):
iter_list.append(iterr)
print("Iteration : {:.0f}/{:.0f} [{:2.0f}%] ".format(iterr - init_iters, args.n_iters, 100*(iterr - init_iters)/args.n_iters))
print('---------------------------')
# -----------------------------------------------------------------------------------
### Calculating train accuracy and loss ###
# NOTE : Using encoding obtained in current training iteration.
# Append train loss
train_loss_list.append(loss.item())
# Use encoding to obtain vector difference
pos_diff, neg_diff = return_diff(anchors_enc, positives_enc, negatives_enc)
# Append train accuracy
num_sample = anchors.shape[0]
tot_correct = np.sum(pos_diff < neg_diff)
train_acc = tot_correct/num_sample * 100
train_acc_list.append(train_acc)
print('[Train]\t Loss: {:.5f} | Acc: {:2.0f}%'.format(loss.item(), train_acc))
# -----------------------------------------------------------------------------------
### Calculating test accuracy and loss ###
# Use 100 samples for inspection from test set
anchors, positives, negatives = next(iter(test_loader))
# Get encodings by forward propogation
anchors_enc = get_encodings(anchors)
positives_enc = get_encodings(positives)
negatives_enc = get_encodings(negatives)
# Append test loss
loss = triplet_loss(anchors_enc, positives_enc, negatives_enc)
test_loss_list.append(loss.item())
# Use encoding to obtain vector difference
pos_diff, neg_diff = return_diff(anchors_enc, positives_enc, negatives_enc)
# Append test accuracy
num_sample = anchors.shape[0]
tot_correct = np.sum(pos_diff < neg_diff)
test_acc = tot_correct/num_sample * 100
test_acc_list.append(test_acc)
print('[Test ]\t Loss: {:.5f} | Acc: {:2.0f}%'.format(loss.item(), test_acc))
# -----------------------------------------------------------------------------------
print('=========================================================')
print("\nTraining Done.")
# Time when training ended
time_end = time.asctime()
# __________________________________________________________________________________
# Training output.
hr, min, sec = get_time(time_begin, time_end)
print("Total Iterations : {:.0f}".format(iterr))
print("Total Epochs : {:.0f}".format(iterr*100/60000))
print("Total Sample-Points : {:.0f}".format(iterr/args.inspect_size))
print("-------------------------------")
print("Loss - Train : {:.2f}".format(np.mean(train_loss_list[-10:])))
print("Loss - Test : {:.2f}".format(np.mean(test_loss_list[-10:])))
print("Acc - Train : {:.2f}".format(np.mean(train_acc_list[-10:])))
print("Acc - Test : {:.2f}".format(np.mean(test_acc_list[-10:])))
print("-------------------------------")
print("Start Time : {}".format(time_begin[11:19]))
print("End Time : {}".format(time_end[11:19]))
print("Total Train-time : {:2.0f}:{:2.0f}:{:2.0f}".format(hr,min,sec))