-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRNAConvolutionMaxPool.py
More file actions
67 lines (55 loc) · 2.39 KB
/
RNAConvolutionMaxPool.py
File metadata and controls
67 lines (55 loc) · 2.39 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
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 27 12:15:25 2020
@author: Robert
"""
def convolution(image, filt, bias, s=1):
'''
Confolves `filt` over `image` using stride `s`
'''
(n_f, n_c_f, f, _) = filt.shape # filter dimensions
n_c, in_dim, _ = image.shape # image dimensions
out_dim = int((in_dim - f)/s)+1 # calculate output dimensions
# ensure that the filter dimensions match the dimensions of the input image
assert n_c == n_c_f, "Dimensions of filter must match dimensions of input image"
out = np.zeros((n_f,out_dim,out_dim)) # create the matrix to hold the values of the convolution operation
# convolve each filter over the image
for curr_f in range(n_f):
curr_y = out_y = 0
# move filter vertically across the image
while curr_y + f <= in_dim:
curr_x = out_x = 0
# move filter horizontally across the image
while curr_x + f <= in_dim:
# perform the convolution operation and add the bias
out[curr_f, out_y, out_x] = np.sum(filt[curr_f] * image[:,curr_y:curr_y+f, curr_x:curr_x+f]) + bias[curr_f]
curr_x += s
out_x += 1
curr_y += s
out_y += 1
return out
def maxpool(image, f=2, s=2):
```
Downsample input `image` using a kernel size of `f` and a stride of `s`
```
n_c, h_prev, w_prev = image.shape
# calculate output dimensions after the maxpooling operation.
h = int((h_prev - f)/s)+1
w = int((w_prev - f)/s)+1
# create a matrix to hold the values of the maxpooling operation.
downsampled = np.zeros((n_c, h, w))
# slide the window over every part of the image using stride s. Take the maximum value at each step.
for i in range(n_c):
curr_y = out_y = 0
# slide the max pooling window vertically across the image
while curr_y + f <= h_prev:
curr_x = out_x = 0
# slide the max pooling window horizontally across the image
while curr_x + f <= w_prev:
# choose the maximum value within the window at each step and store it to the output matrix
downsampled[i, out_y, out_x] = np.max(image[i, curr_y:curr_y+f, curr_x:curr_x+f])
curr_x += s
out_x += 1
curr_y += s
out_y += 1
return downsampled