-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrunCNN.py
135 lines (93 loc) · 4.09 KB
/
runCNN.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
import os
import pickle
import numpy as np
from tqdm import trange
from CNN import CNN, SGD, AdamGD
from Layers import dataloader, one_hot_encoding, resize_dataset, save_params_to_file, load_params_from_file, InputLayer
from ObjectiveFuncs import CrossEntropy
def unpickle(file):
with open(file, 'rb') as fo:
d = pickle.load(fo, encoding='bytes')
return d
def load_batch(filename):
data = unpickle(filename)
batch_x = [np.reshape(x, (3, 32, 32)) for x in data[b'data']]
return batch_x, np.array(data[b'labels'])
def preprocess():
train_x, train_y = [], []
for batch in range(1, 6):
batch_x, batch_y = load_batch(os.path.join('cifar-10-batches-py', f'data_batch_{batch}'))
train_x.append(batch_x)
train_y.append(batch_y)
train_x = np.concatenate(train_x)
train_y = np.concatenate(train_y)
test_x, test_y = load_batch(os.path.join('cifar-10-batches-py', 'test_batch'))
return train_x, train_y, test_x, test_y
def train(x_train, y_train, optimizer_choice):
x_train = resize_dataset(x_train)
x_train = InputLayer(x_train).forwardPropagate(x_train)
y_train = one_hot_encoding(y_train)
model = CNN()
cost = CrossEntropy(None)
eta = 0.005 if optimizer_choice != 1 else 0.0001
optimizer = SGD(eta=eta, params=model.get_params()) if optimizer_choice != 1 else AdamGD(eta=eta, rho1=0.9,
rho2=0.999, epsilon=1e-8,
params=model.get_params())
train_costs = []
print("----------------TRAINING-----------------\n")
num_epochs = 20
batch_size = 1
num_train_obs = len(x_train)
loading_bar = None
for epoch in range(num_epochs):
loading_bar = trange(num_train_obs // batch_size)
train_loss = 0
train_acc = 0
train_loader = dataloader(x_train, y_train, batch_size)
for i, (X_batch, y_batch) in zip(loading_bar, train_loader):
y_pred = model.forwardPropagate(X_batch)
cost.y = y_batch
loss = cost.eval(y_pred)
grads = model.backwardPropagate(y_pred, y_batch)
params = optimizer.update_params(grads)
model.set_params(params)
train_loss += loss * batch_size
train_acc += sum(np.argmax(y_batch, axis=1) == np.argmax(y_pred, axis=1))
loading_bar.set_description("[Train] Epoch {}".format(epoch + 1))
train_loss /= num_train_obs
train_costs.append(train_loss)
train_acc /= num_train_obs
info_train = "train-loss: {:0.6f} | train-acc: {:0.3f}"
print(info_train.format(train_loss, train_acc))
save_params_to_file(model, optimizer_choice)
print()
loading_bar.close()
def eval_efficiency(x_train, x_test, y_test, optimizer_choice):
x_test = InputLayer(x_train).forwardPropagate(x_test)
x_test = resize_dataset(x_test)
y_test = one_hot_encoding(y_test)
cost = CrossEntropy(None)
model = CNN()
model = load_params_from_file(model, optimizer_choice)
print("--------------------EVALUATION-------------------\n")
batch_size = 1
num_test_obs = len(x_test)
test_loss = 0
test_accuracy = 0
loading_bar = trange(num_test_obs // batch_size)
test_loader = dataloader(x_test, y_test, batch_size)
for i, (x_batch, y_batch) in zip(loading_bar, test_loader):
y_pred = model.forwardPropagate(x_batch)
cost.y = y_batch
loss = cost.eval(y_pred)
test_loss += loss * batch_size
test_accuracy += sum(np.argmax(y_batch, axis=1) == np.argmax(y_pred, axis=1))
loading_bar.set_description("Evaluation")
test_loss /= num_test_obs
test_accuracy /= num_test_obs
print("test-loss: {:0.6f} | test-acc: {:0.3f}".format(test_loss, test_accuracy))
if __name__ == '__main__':
choice = int(input("1 for ADAM, 0 for Reg:\n"))
train_x, train_y, test_x, test_y = preprocess()
train(train_x, train_y, choice)
eval_efficiency(train_x, test_x, test_y, choice)