File size: 2,008 Bytes
ec24a39 |
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 |
# Implement lenet-5 model
import numpy as np
import tensorflow.keras as tfk
import tensorflow.keras.layers as tfl
def lenet_model(input_shape):
'''
Implement Functional Keras API to train model. The model has 5 layers
-----------------------------------------------------------------------------------
1. conv layer:
k_size: (5, 5)
stride: (1, 1)
n_filters: 6
padding: 'same'
activation: relu
2. avg pooling:
pool_size: (2, 2)
3. conv layer:
k_size: (5, 5)
stride: (1, 1)
n_filters: 16
padding: 'valid'
activation: relu
4. avg pooling
pool_size: (2, 2)
strides: (2, 2)
5. conv layer:
k_size: (5, 5)
stride: (1, 1)
n_filters: 120
padding: 'valid'
activation: relu
5. flatten
6. classifier
activation: softmax
classes: 10
-----------------------------------------------------------------------------------------------------------------------
'''
input_img = tfk.Input(shape = input_shape)
z1 = tfl.Conv2D(kernel_size = (5, 5), strides = (1, 1), filters = 6, padding = 'same', activation = 'tanh')(input_img)
p1 = tfl.AveragePooling2D(pool_size = (2, 2))(z1)
z2 = tfl.Conv2D(kernel_size = (5, 5), strides = (1, 1), filters = 16, padding = 'valid', activation = 'tanh')(p1)
p2 = tfl.AveragePooling2D(pool_size = (2, 2), strides = (2, 2))(z2)
z3 = tfl.Conv2D(kernel_size = (5, 5), strides = (1, 1), filters = 120, padding = 'valid', activation = 'tanh')(p2)
flat = tfl.Flatten()(z3)
tanh = tfl.Dense(units = 84, activation = 'tanh')(flat)
output = tfl.Dense(units = 10, activation = 'softmax')(tanh)
model = tfk.Model(inputs = input_img, outputs = output)
return model |