-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConvolutions.py
334 lines (291 loc) · 14.7 KB
/
Convolutions.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import sys
from Dense import Dense
from Flatten import Flatten
from Softmax import Softmax
if sys.platform == 'darwin':
print('Setting KMP_DUPLICATE_LIB_OK')
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
import numpy as np
from scipy.signal import convolve2d
from tensorflow import keras
from utility_functions import relu, averager, extract_averager_value, np_random_normal, \
batch_generator
from Layer import Layer, ActivationFunction
from Model import Model
class Convolution2D(Layer):
def __init__(self, weights=None, shape=None, activation=relu, **kwargs):
if weights is None:
assert shape is not None, 'Both weights and shape cannot be None'
self.filter_size, _, self.num_channels, self.num_filters = shape
weights = np_random_normal(0, 1 / np.sqrt(
self.filter_size * self.filter_size * self.num_channels),
size=(self.filter_size,
self.filter_size,
self.num_channels,
self.num_filters))
super().__init__(weights, **kwargs)
self.filter_size, _, self.num_channels, self.num_filters = self.weights.shape
self.activation = activation
# With all true 1423.0891828536987
self.use_fancy_indexing_for_feed_forward = False
self.use_fancy_indexing_for_back_prop = False
self.use_fancy_indexing_for_weight_update = False
def feed_forward(self, X_batch, **kwargs):
if self.first_feed_forward: # First run
self.first_feed_forward = False
self.batch_size = len(X_batch)
self.image_size = X_batch.shape[1]
# The following is used as argument to out of ufuncs
self.input_conv = np.zeros((self.batch_size, self.image_size,
self.image_size,
self.num_filters))
# creating the zero padding structure once is efficient
self.image_size_embedding_size = self.image_size + self.filter_size - 1
self.input_zero_padded = np.zeros((self.batch_size,
self.image_size_embedding_size,
self.image_size_embedding_size,
self.num_channels))
z = np.arange(0, self.image_size)
zs = np.stack([z + i for i in range(self.weights.shape[0])], 1)
self.batch_index = np.arange(self.batch_size)[:, None, None, None, None, None, None]
self.channel_index = np.arange(self.num_channels)[None, None, None, None, None, :, None]
self.filter_index = np.arange(self.num_filters)[None, None, None, None, None, None, :]
self.rows = zs[None, :, None, :, None, None, None]
self.cols = zs[None, None, :, None, :, None, None]
self.tmp = np.zeros(shape=(self.batch_size,
self.image_size,
self.image_size,
self.filter_size,
self.filter_size,
self.num_channels,
self.num_filters
)
)
self.input = X_batch
# Convolution
if self.use_fancy_indexing_for_feed_forward:
self.input_zero_padded[:,
self.filter_size // 2:-self.filter_size // 2 + 1,
self.filter_size // 2:-self.filter_size // 2 + 1] \
= self.input
# TODO: better to loose the last index from all the fancy indices
# and do a tensordot
# Also compare with reshape and a np.matmul
np.multiply(self.input_zero_padded[self.batch_index,
self.rows,
self.cols,
self.channel_index],
self.weights[None, None, None, :, :, :, :],
out=self.tmp
)
self.tmp.sum(axis=(3, 4, 5), out=self.input_conv)
else:
self.input_conv[:] = 0
for batch_id in range(self.batch_size):
for next_layer_channel_id in range(self.weights.shape[3]):
for current_layer_channel_id in range(self.weights.shape[2]):
# Sum over current layer after convolving
# Summation is done succesively in place on the ouput array
np.sum(
[self.input_conv[batch_id, :, :, next_layer_channel_id],
convolve2d(
self.input[batch_id, :, :,
current_layer_channel_id],
self.weights[::-1, ::-1,
current_layer_channel_id,
next_layer_channel_id
],
'same'
)
],
axis=0,
out=self.input_conv[batch_id, :, :,
next_layer_channel_id]
)
# self.output = self.activation(self.input_conv)
# self.output_d = self.activation(self.input_conv, der=True)
self.output = self.input_conv
return self.output
def back_prop(self, loss_d_output):
if self.first_back_prop:
self.first_back_prop = False
# The following three are used with out parameter of ufuncs
self.loss_d_output_times_output_d = np.zeros_like(
self.output)
self.loss_derivative_input = np.zeros_like(self.input)
self.loss_derivative_input2 = np.zeros_like(self.input)
self.loss_derivative_weights = np.zeros_like(self.weights)
self.loss_d_output_times_output_d_zero_padded = np.zeros((self.batch_size,
self.image_size_embedding_size,
self.image_size_embedding_size,
self.num_filters))
# np.multiply(loss_d_output, self.output_d,
# out=self.loss_d_output_times_output_d)
self.loss_d_output_times_output_d = loss_d_output
# correction for weights
if self.trainable:
self.input_zero_padded[:,
self.filter_size // 2:-self.filter_size // 2 + 1,
self.filter_size // 2:-self.filter_size // 2 + 1] \
= self.input
if self.use_fancy_indexing_for_weight_update:
(self.input_zero_padded[
self.batch_index, self.rows, self.cols, self.channel_index] *
self.loss_d_output_times_output_d[:, :, :, None, None, None, :]) \
.sum(axis=(0, 1, 2), out=self.loss_derivative_weights)
else:
for alpha in range(self.weights.shape[0]):
for beta in range(self.weights.shape[1]):
x = self.loss_d_output_times_output_d[:, :, :, None, :] \
* self.input_zero_padded[:,
alpha:self.input_zero_padded.shape[1] - (
self.filter_size - 1 - alpha),
beta:self.input_zero_padded.shape[2] - (self.filter_size - 1 - beta),
:,
None]
np.sum(x, axis=(0, 1, 2), out=self.loss_derivative_weights[alpha, beta])
if not self.first_layer:
self.loss_d_output_times_output_d_zero_padded[:,
self.filter_size // 2:-self.filter_size // 2 + 1,
self.filter_size // 2:-self.filter_size // 2 + 1] = \
self.loss_d_output_times_output_d
if self.use_fancy_indexing_for_back_prop:
np.multiply(self.loss_d_output_times_output_d_zero_padded[self.batch_index,
self.rows,
self.cols,
self.filter_index],
self.weights[None, None, None, ::-1, ::-1, :, :],
out=self.tmp
)
self.tmp.sum(axis=(3, 4, 6), out=self.loss_derivative_input)
else:
self.loss_derivative_input[:] = 0
if not self.first_layer:
for batch_id in range(loss_d_output.shape[0]):
for prev_layer_channel_id in range(self.weights.shape[2]):
for channel_id in range(self.weights.shape[3]):
np.sum(
[self.loss_derivative_input[batch_id, :, :,
prev_layer_channel_id],
convolve2d(
self.loss_d_output_times_output_d[batch_id, :, :,
channel_id],
self.weights[:, :, prev_layer_channel_id, channel_id],
'same'
)
],
axis=0,
out=self.loss_derivative_input[batch_id, :, :,
prev_layer_channel_id]
)
return self.loss_derivative_input
if __name__ == '__main__':
DATASET = 'CIFAR10'
if DATASET == 'CIFAR10':
print('Using CIFAR10')
(X_train_full, y_train_full), (
X_test, y_test) = keras.datasets.cifar10.load_data()
elif DATASET == 'CIFAR100':
print('Using CIFAR100')
(X_train_full, y_train_full), (
X_test, y_test) = keras.datasets.cifar100.load_data()
else:
print('Using Fashion_MNIST')
(X_train_full, y_train_full), (
X_test, y_test) = keras.datasets.fashion_mnist.load_data()
X_train, X_valid = X_train_full[:-5000], X_train_full[-5000:]
y_train, y_valid = y_train_full[:-5000], y_train_full[-5000:]
X_mean = X_train.mean(axis=0, keepdims=True)
X_std = X_train.std(axis=0, keepdims=True) + 1e-7
X_train = (X_train - X_mean) / X_std
X_valid = (X_valid - X_mean) / X_std
X_test = (X_test - X_mean) / X_std
if len(X_train_full.shape) == 3:
X_train = X_train[..., np.newaxis]
X_valid = X_valid[..., np.newaxis]
X_test = X_test[..., np.newaxis]
y_train = y_train.flatten()
y_valid = y_valid.flatten()
y_test = y_test.flatten()
input_num_channels = X_train.shape[3]
input_image_size = X_train.shape[1]
num_categories = len(set(list(y_train)))
batch_size = 32
num_steps = len(y_train) // batch_size
import time
for num_filters in (5,):
np.random.seed(42)
t0 = time.time()
print('Training with num_filters ', num_filters)
W1 = np_random_normal(0, 1 / np.sqrt(3 * 3 * input_num_channels),
size=(3, 3, input_num_channels, num_filters))
W11 = np_random_normal(0, 1 / np.sqrt(3 * 3 * num_filters),
size=(3, 3, num_filters, num_filters))
W2 = np_random_normal(0, 1 / np.sqrt(
num_filters * input_image_size * input_image_size),
size=(num_filters * input_image_size * input_image_size,
num_categories))
m = Model()
learning_rate = 0.001
m.layers = [
Convolution2D(weights=W1, name='Conv1', trainable=True,
activation=relu,
learning_rate=learning_rate,
first_layer=True
),
ActivationFunction(relu),
Flatten(),
Dense(output_dimension=10),
Softmax()
]
for epoch in range(1):
t_start_epoch = time.time()
# Train
train_loss = averager()
train_accuracy = averager()
for i, (X_batch, y_batch) in enumerate(
batch_generator(X_train, y_train, batch_size, num_steps)):
time_step = time.time()
if (i + 1) % 10 == 0:
delta_time = time_step - t_start_epoch
eta = (num_steps / (i + 1) - 1) * delta_time
sys.stdout.write(
'Epoch: {} Step {}/{} Time Spent {:.2f}s Estimated Time {:.2f}s\r'.format(
epoch + 1, i + 1,
num_steps, delta_time, eta))
loss, accuracy = m.feed_forward_and_back_prop(X_batch, y_batch)
train_loss.send(loss)
train_accuracy.send(accuracy)
# Validate
loss_averager_valid = averager()
accuracy_averager_valid = averager()
for X_valid_batch, y_valid_batch in batch_generator(X_valid, y_valid,
batch_size,
len(
y_valid) //
batch_size):
m.feed_forward(X_valid_batch, )
loss, accuracy = m.loss(y_valid_batch)
loss_averager_valid.send(loss.mean())
accuracy_averager_valid.send(accuracy.mean())
# report
train_loss, train_accuracy, valid_loss, valid_accuracy = map(
extract_averager_value, [
train_loss,
train_accuracy,
loss_averager_valid,
accuracy_averager_valid]
)
msg = 'Epoch {}: train loss {:.2f}, train acc {:.2f}, valid loss {' \
':.2f}, valid acc {:.2f}, time taken {:.2f}s'.format(
epoch + 1,
train_loss,
train_accuracy,
valid_loss,
valid_accuracy,
time.time() - t_start_epoch
)
print(msg)
t1 = time.time()
print('Total time', t1 - t0)