-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcnn_conv_pool.py
75 lines (52 loc) · 2.63 KB
/
cnn_conv_pool.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
import numpy as np
import scipy.signal
def sigmoid(val):
return 1/(1+np.exp(-val))
def cnnConvolve(kernel,filters,images,weight,bias):
"""
cnnConvolve Returns the convolution of the features given by W and b with the given images
Parameters:
kernel - filter (feature) dimension
filters - number of feature maps
images - large images to convolve with, matrix in the form images(r, c,channel, image number)
weight, bias - weight, bias for features from the sparse autoencoder
weight is of shape (numFilters,filterDim*filterDim)
bias is of shape (numFilters,1)
Returns:
convolvedFeatures - matrix of convolved features in the form convolvedFeatures(imageRow, imageCol, featureNum, imageNum)
"""
num_images = images.shape[3]
image_size = images.shape[1]
num_channels = images.shape[2]
convDim = image_size - kernel + 1
convolvedFeatures = np.zeros(shape=(convDim,convDim,filters,num_images))
for imagenum in range(num_images):
for filterNum in range(filters):
convolvedImage = np.zeros(shape=(convDim,convDim))
for channel in range(num_channels):
feature_mat = weight[filterNum,(kernel*kernel)*channel:(kernel*kernel)*(channel+1)].reshape(kernel,kernel)
feature = np.flipud(np.fliplr(feature_mat))
img = images[:,:,channel,imagenum]
convolvedImage = convolvedImage + scipy.signal.convolve2d(img,feature,mode='valid')
convolvedImage = sigmoid(convolvedImage + bias[filterNum])
convolvedFeatures[:,:,filterNum,imagenum] = convolvedImage
return convolvedFeatures
def cnnPool(pool_kernel,convolvedFeatures):
"""
cnnPool Pools the given convolved features
Parameters:
poolDim - dimension of pooling region
convolvedFeatures - convolved features to pool (as given by cnnConvolve) convolvedFeatures(imageRow, imageCol, featureNum, imageNum)
Returns:
pooledFeatures - matrix of pooled features in the form pooledFeatures(poolRow, poolCol, featureNum, imageNum)
"""
num_images = convolvedFeatures.shape[3]
num_channels = convolvedFeatures.shape[2]
convolvedDim = convolvedFeatures.shape[0]
pool_size = convolvedDim/pool_kernel
pooledFeatures = np.zeros(shape=(pool_size,pool_size,num_channels,num_images))
for row in range(pool_size):
for col in range(pool_size):
pool = convolvedFeatures[row*pool_kernel:(row+1)*pool_kernel,col*pool_kernel:(col+1)*pool_kernel,:,:]
pooledFeatures[row,col,:,:] = np.mean(np.mean(pool,0),0)
return pooledFeatures