-
Notifications
You must be signed in to change notification settings - Fork 1
/
lenet.py
55 lines (45 loc) · 1.92 KB
/
lenet.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
# AIM: To create LeNet class.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Conv2D, Dense
from tensorflow.keras.layers import Flatten, MaxPooling2D, Rescaling
from tensorflow.keras import backend as K
class LeNet:
'''
Class to construct LeNet model.
LeNet Architecture:
INPUT => CONV => RELU => POOL => CONV => RELU => POOL =>
FC => RELU => FC => SOFTMAX
'''
@staticmethod
def build(width, height, depth, classes):
'''
Static method to build the LeNet model architecture.
Parameters:
width (int): Width of the input image.
height (int): Height of the input image.
depth (int): Depth of the input image.
classes (int): Number of output classes to learn to predict.
Returns:
model: Constructed lenet network architecture.
'''
model = Sequential()
inputShape = (height, width, depth) # initialize the model along with the input shape to be "channels last"
if K.image_data_format() == "channels_first": # if we are using "channels first", update the input shape
inputShape = (depth, height, width)
# normalizing the images
model.add(Rescaling(1./255, input_shape=inputShape))
# define the model layers
model.add(Conv2D(20, (5, 5), padding="same", input_shape=inputShape))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(50, (5, 5), padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(500))
model.add(Activation("relu"))
model.add(Dense(classes))
model.add(Activation("softmax"))
# model name
model._name = 'LeNet'
return model