-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlenet_functions.py
More file actions
42 lines (32 loc) · 1.52 KB
/
lenet_functions.py
File metadata and controls
42 lines (32 loc) · 1.52 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
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, AveragePooling2D, Flatten, Dense, Activation
def create_lenet(input_shape=(28, 28, 1), num_classes=10):
"""
Builds the LeNet architecture.
Args:
input_shape: tuple, shape of the input images (default: MNIST dimensions).
num_classes: int, number of classes for the final classification layer.
Returns:
A Keras Sequential model representing LeNet.
"""
model = Sequential()
# First Convolutional Layer
model.add(Conv2D(filters=6, kernel_size=(5, 5), strides=(1, 1),
padding="same", input_shape=input_shape, name="conv1"))
model.add(Activation("tanh")) # Tanh activation (Maybe consider different one)
model.add(AveragePooling2D(pool_size=(2, 2), strides=(2, 2), name="pool1"))
# Second Convolutional Layer
model.add(Conv2D(filters=16, kernel_size=(5, 5), strides=(1, 1),
padding="valid", name="conv2"))
model.add(Activation("tanh"))
model.add(AveragePooling2D(pool_size=(2, 2), strides=(2, 2), name="pool2"))
# Flatten layer
model.add(Flatten(name="flatten"))
# First Fully Connected Layer
model.add(Dense(units=120, activation="tanh", name="fc1"))
# Second Fully Connected Layer
model.add(Dense(units=84, activation="tanh", name="fc2"))
# Output Layer
model.add(Dense(units=num_classes, activation="softmax", name="output"))
return model