mirror of
https://github.com/aladdinpersson/Machine-Learning-Collection.git
synced 2026-02-20 13:50:41 +00:00
Initial commit
This commit is contained in:
5
ML/TensorFlow/CNN_architectures/LeNet5/README.md
Normal file
5
ML/TensorFlow/CNN_architectures/LeNet5/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
[Original Paper - GradientBased Learning Applied to Document Recognition (1998)](http://vision.stanford.edu/cs598_spring07/papers/Lecun98.pdf)
|
||||
[Related Video](https://www.youtube.com/watch?v=fcOW-Zyb5Bo)
|
||||
|
||||
Some other useful links
|
||||
- [Understanding and Implementing LeNet-5 CNN Architecture](https://towardsdatascience.com/understanding-and-implementing-lenet-5-cnn-architecture-deep-learning-a2d531ebc342)
|
||||
78
ML/TensorFlow/CNN_architectures/LeNet5/lenet5.py
Normal file
78
ML/TensorFlow/CNN_architectures/LeNet5/lenet5.py
Normal file
@@ -0,0 +1,78 @@
|
||||
# Tensorflow v.2.3.1
|
||||
|
||||
"""
|
||||
Programmed by the-robot <https://github.com/the-robot>
|
||||
"""
|
||||
|
||||
from tensorflow.keras.layers import (
|
||||
AveragePooling2D,
|
||||
Conv2D,
|
||||
Dense,
|
||||
Flatten,
|
||||
Input,
|
||||
)
|
||||
from tensorflow.keras import Model
|
||||
import tensorflow as tf
|
||||
import typing
|
||||
|
||||
tf.config.run_functions_eagerly(True)
|
||||
|
||||
@tf.function
|
||||
def LeNet5(input_shape: typing.Tuple[int], classes: int = 1000) -> Model:
|
||||
"""
|
||||
Implementation of the classic LeNet architecture.
|
||||
|
||||
Arguments:
|
||||
input_shape -- shape of the images of the dataset
|
||||
classes -- integer, number of classes
|
||||
|
||||
Returns:
|
||||
model -- a Model() instance in Keras
|
||||
|
||||
Note:
|
||||
because I want to keep it original, I used tanh activation instead of ReLU activation.
|
||||
however based on newer papers, the rectified linear unit (ReLU) performed much faster than
|
||||
tanh activation.
|
||||
"""
|
||||
|
||||
# convert input shape into tensor
|
||||
X_input = Input(input_shape)
|
||||
|
||||
# layer 1
|
||||
X = Conv2D(
|
||||
filters = 6,
|
||||
kernel_size = (5, 5),
|
||||
strides = (1, 1),
|
||||
activation = "tanh",
|
||||
padding = "valid",
|
||||
)(X_input)
|
||||
X = AveragePooling2D(pool_size = (2, 2), strides = (2, 2), padding = "valid")(X)
|
||||
|
||||
# layer 2
|
||||
X = Conv2D(
|
||||
filters = 16,
|
||||
kernel_size = (5, 5),
|
||||
strides = (1, 1),
|
||||
activation = "tanh",
|
||||
padding = "valid",
|
||||
)(X)
|
||||
X = AveragePooling2D(pool_size = (2, 2), strides = (2, 2), padding = "valid")(X)
|
||||
|
||||
# layer 3
|
||||
X = Conv2D(
|
||||
filters = 120,
|
||||
kernel_size = (5, 5),
|
||||
strides = (1, 1),
|
||||
activation = "tanh",
|
||||
padding = "valid",
|
||||
)(X)
|
||||
|
||||
# layer 4
|
||||
X = Flatten()(X)
|
||||
X = Dense(units = 84, activation = "tanh")(X)
|
||||
|
||||
# layer 5 (classification layer)
|
||||
X = Dense(units = classes, activation = "softmax")(X)
|
||||
|
||||
model = Model(inputs = X_input, outputs = X, name = "LeNet5")
|
||||
return model
|
||||
9
ML/TensorFlow/CNN_architectures/LeNet5/test.py
Normal file
9
ML/TensorFlow/CNN_architectures/LeNet5/test.py
Normal file
@@ -0,0 +1,9 @@
|
||||
# disable tensorflow debugging messages
|
||||
import os
|
||||
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
||||
|
||||
from lenet5 import LeNet5
|
||||
|
||||
if __name__ == "__main__":
|
||||
model = LeNet5(input_shape = (32, 32, 1), classes = 10)
|
||||
model.summary()
|
||||
Reference in New Issue
Block a user