Reducing parameters with pooling

Image Modeling with Keras

Ariel Rokem

Senior Data Scientist, University of Washington

model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_12 (Conv2D)           (None, 28, 28, 5)         50        
_________________________________________________________________
conv2d_13 (Conv2D)           (None, 28, 28, 15)        690       
_________________________________________________________________
flatten_6 (Flatten)          (None, 11760)             0         
_________________________________________________________________
dense_9 (Dense)              (None, 3)                 35283     
=================================================================
Total params: 36,023
Trainable params: 36,023
Non-trainable params: 0
_________________________________________________________________

Image Modeling with Keras

Image Modeling with Keras

Image Modeling with Keras

Image Modeling with Keras

Image Modeling with Keras

Image Modeling with Keras

Image Modeling with Keras

Image Modeling with Keras

Implementing max pooling

result = np.zeros((im.shape[0]//2, im.shape[1]//2))

result[0, 0] = np.max(im[0:2, 0:2])
result[0, 1] = np.max(im[0:2, 2:4])
result[0, 2] = np.max(im[0:2, 4:6])

...

result[1, 0] = np.max(im[2:4, 0:2])

result[1, 1] = np.max(im[2:4, 2:4])

...

Image Modeling with Keras

Implementing max pooling

for ii in range(result.shape[0]):
    for jj in range(result.shape[1]):
        result[ii, jj] = np.max(im[ii*2:ii*2+2, jj*2:jj*2+2])
Image Modeling with Keras

Max pooling in Keras

from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, MaxPool2D

model = Sequential() model.add(Conv2D(5, kernel_size=3, activation='relu', input_shape=(img_rows, img_cols, 1)))
model.add(MaxPool2D(2))
model.add(Conv2D(15, kernel_size=3, activation='relu', input_shape=(img_rows, img_cols, 1)))
model.add(MaxPool2D(2))
model.add(Flatten()) model.add(Dense(3, activation='softmax'))
Image Modeling with Keras
model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 26, 26, 5)         50        
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 13, 13, 5)         0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 11, 11, 15)        690       
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 5, 5, 15)          0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 375)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 3)                 1128      
=================================================================
Total params: 1,868
Trainable params: 1,868
Non-trainable params: 0
_________________________________________________________________
Image Modeling with Keras

Let's practice!

Image Modeling with Keras

Preparing Video For Download...