-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining_collapsed_traits.py
164 lines (130 loc) · 6.38 KB
/
training_collapsed_traits.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
# training on chalearn with cropped image faces
import chainer
import numpy as np
# from deepimpression2.model import Siamese
from deepimpression2.model_15 import Siamese
import deepimpression2.constants as C
from chainer.functions import sigmoid_cross_entropy, mean_squared_error, softmax_cross_entropy
from chainer.optimizers import Adam
import h5py as h5
import deepimpression2.paths as P
import deepimpression2.chalearn20.data_utils as D
import time
from chainer.backends.cuda import to_gpu, to_cpu
import deepimpression2.util as U
import os
import cupy as cp
from chainer.functions import expand_dims
model = Siamese()
# optimizer = Adam(alpha=0.0002, beta1=0.5, beta2=0.999, eps=10e-8, weight_decay_rate=0.0001)
optimizer = Adam(alpha=0.0002, beta1=0.5, beta2=0.999, eps=10e-8)
optimizer.setup(model)
alpha = 1
if C.ON_GPU:
model = model.to_gpu(device=C.DEVICE)
print('Initializing')
print('model initialized with %d parameters' % model.count_params())
train_labels = h5.File(P.CHALEARN_TRAIN_LABELS_20, 'r')
val_labels = h5.File(P.CHALEARN_VAL_LABELS_20, 'r')
train_loss = []
confusion_matrix_train = np.zeros((C.EPOCHS, 4), dtype=float)
# confusion_matrix_trait_train = np.zeros((C.EPOCHS, 5, 4), dtype=int)
batch_statistics_train = np.zeros((2, C.EPOCHS))
val_loss = []
confusion_matrix_val = np.zeros((C.EPOCHS, 4), dtype=float)
# confusion_matrix_trait_val = np.zeros((C.EPOCHS, 5, 4), dtype=int)
batch_statistics_val = np.zeros((2, C.EPOCHS))
train_uid_keys_map = h5.File(P.TRAIN_UID_KEYS_MAPPING, 'r')
val_uid_keys_map = h5.File(P.VAL_UID_KEYS_MAPPING, 'r')
training_steps = len(train_labels) // C.TRAIN_BATCH_SIZE
val_steps = len(val_labels) // C.VAL_BATCH_SIZE
id_frames = h5.File(P.NUM_FRAMES, 'r')
print('Enter training loop with validation')
for e in range(C.EPOCHS): # C.EPOCHS
loss_tmp = []
cm_tmp = np.zeros((training_steps, 4), dtype=int)
# cm_trait_tmp = np.zeros((training_steps, 5, 4), dtype=int)
bs_tmp = np.zeros((2, training_steps), dtype=int)
ts = time.time()
for s in range(training_steps): # training_steps
labels, left_data, right_data = D.load_data('train', train_uid_keys_map, train_labels,
id_frames, trait_mode='collapse')
num_left, num_right = D.label_statistics(labels, trait_mode='collapse', xe='sigmoid')
bs_tmp[0][s] = num_left
bs_tmp[1][s] = num_right
if C.ON_GPU:
left_data = to_gpu(left_data, device=C.DEVICE)
right_data = to_gpu(right_data, device=C.DEVICE)
labels = to_gpu(labels, device=C.DEVICE)
# training
with cp.cuda.Device(C.DEVICE):
with chainer.using_config('train', True):
model.cleargrads()
prediction = expand_dims(model(left_data, right_data), axis=-1)
# sof XE
loss = softmax_cross_entropy(prediction, labels)
# sig XE
# loss = sigmoid_cross_entropy(prediction, labels)
# sig XE + MSE
# _1 = sigmoid_cross_entropy(prediction, labels)
# _2 = mean_squared_error(prediction, cp.asarray(labels, dtype=cp.float32))
# print('loss: %s %s' % (str(_1.data)[0:5], str(_2.data)[0:5]))
# loss = _1 + alpha * _2
loss.backward()
optimizer.update()
loss_tmp.append(float(loss.data))
cm_tmp[s] = U.make_confusion_matrix(to_cpu(prediction.data), to_cpu(labels), trait_mode='collapse', xe='softmax')
batch_statistics_train[0][e] = np.mean(bs_tmp[0], axis=0)
batch_statistics_train[1][e] = np.mean(bs_tmp[1], axis=0)
# confusion_matrix_trait_train[e] = np.mean(cm_trait_tmp, axis=0)
confusion_matrix_train[e] = np.mean(cm_tmp, axis=0)
loss_tmp_mean = np.mean(loss_tmp, axis=0)
train_loss.append(loss_tmp_mean)
print('E %d. train loss: ' % e, loss_tmp_mean,
' [tl, fl, tr, fr]: ', np.mean(cm_tmp, axis=0),
' left labels: ', batch_statistics_train[0][e],
' right labels: ', batch_statistics_train[1][e],
' time: ', time.time() - ts)
U.record_loss('train', loss_tmp_mean, confusion_matrix_train[e], np.mean(bs_tmp, axis=1))
# # validation
loss_tmp = []
cm_tmp = np.zeros((val_steps, 4), dtype=int)
# cm_trait_tmp = np.zeros((val_steps, 5, 4), dtype=int)
bs_tmp = np.zeros((2, val_steps), dtype=int)
ts = time.time()
for vs in range(val_steps): # val_steps
labels, left_data, right_data = D.load_data('val', val_uid_keys_map, val_labels,
id_frames, trait_mode='collapse')
num_left, num_right = D.label_statistics(labels, trait_mode='collapse', xe='sigmoid')
bs_tmp[0][vs] = num_left
bs_tmp[1][vs] = num_right
if C.ON_GPU:
left_data = to_gpu(left_data, device=C.DEVICE)
right_data = to_gpu(right_data, device=C.DEVICE)
labels = to_gpu(labels, device=C.DEVICE)
# validation
with cp.cuda.Device(C.DEVICE):
with chainer.using_config('train', False):
model.cleargrads()
prediction = expand_dims(model(left_data, right_data), axis=-1)
loss = softmax_cross_entropy(prediction, labels)
# loss = sigmoid_cross_entropy(prediction, labels) + \
# alpha * mean_squared_error(prediction, cp.asarray(labels, dtype=cp.float32))
loss_tmp.append(float(loss.data))
cm_tmp[vs] = U.make_confusion_matrix(to_cpu(prediction.data), to_cpu(labels), trait_mode='collapse', xe='softmax')
batch_statistics_val[0][e] = np.mean(bs_tmp[0], axis=0)
batch_statistics_val[1][e] = np.mean(bs_tmp[1], axis=0)
# confusion_matrix_trait_val[e] = np.mean(cm_trait_tmp, axis=0)
confusion_matrix_val[e] = np.mean(cm_tmp, axis=0)
loss_tmp_mean = np.mean(loss_tmp, axis=0)
val_loss.append(loss_tmp_mean)
print('E %d. val loss: ' % e, loss_tmp_mean,
' [tl, fl, tr, fr]: ', np.mean(cm_tmp, axis=0),
' left labels: ', batch_statistics_val[0][e],
' right labels: ', batch_statistics_val[1][e],
' time: ', time.time() - ts)
U.record_loss('val', loss_tmp_mean, confusion_matrix_train[e], np.mean(bs_tmp, axis=1))
# save model
if ((e + 1) % 10) == 0:
name = os.path.join(P.MODELS, 'epoch_%d_44' % e)
chainer.serializers.save_npz(name, model)