-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCNN.py
66 lines (37 loc) · 1.58 KB
/
CNN.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
import numpy as np
import pandas as pd
from IPython.core.display import display,Image
class initialize:
def __init__(self, filter_n):
self.num_filters = num_filters
self.filters = np.random.randn(num_filters, 3, 3) / 9
def iteration(self, sample):
# generates all possible 3*3 image regions using valid padding
dim1, dim2 = image.shape
for i in range(dim1 - 2):
for j in range(dim2 - 2):
image_reg = image[i:(i + 3), j:(j + 3)]
yield image_reg, i, j
def ForwardPropagation(self, t_data):
self.last_input = input
dim1, dim2 = input.shape
res = np.zeros((dim1 - 2, dim2 - 2, self.num_filters))
for image_reg, i, j in self.iterate_regions(input):
res[i, j] = np.sum(image_reg * self.filters, axis=(1, 2))
return res
def BackwardPropagtion(self, d_l_d_out, learn_rate):
d_l_d_filters = np.zeros(self.filters.shape)
for im_region, i, j in self.iterate_regions(self.last_input):
for f in range(self.num_filters):
d_l_d_filters[f] += d_l_d_out[i, j, f] * im_region
# update filters
self.filters -= learn_rate * d_l_d_filters
return None
def maxPool(self, sample):
dim1, dim2, _ = sample.shape
resize1 = dim1 // 2
resize2 = dim2 // 2
for i in range(resize1):
for j in range(resize2):
image_reg = sample[(i * 2):(i * 2 + 2), (j * 2):(j * 2 + 2)]
yield image_reg, i, j