mirror of
https://github.com/aladdinpersson/Machine-Learning-Collection.git
synced 2026-04-10 12:33:44 +00:00
Initial commit
This commit is contained in:
13
ML/TensorFlow/CNN_architectures/VGGNet/README.md
Normal file
13
ML/TensorFlow/CNN_architectures/VGGNet/README.md
Normal file
@@ -0,0 +1,13 @@
|
||||
[Original Paper - Very Deep Convolutional Networks for Large-Scale Image Recognition (2014)](https://arxiv.org/abs/1409.1556)
|
||||
[Related Video](https://www.youtube.com/watch?v=ACmuBbuXn20)
|
||||
|
||||
Some questions I had when I was reading the paper
|
||||
- [What does 1x1 convolution mean in a neural network?](https://stats.stackexchange.com/questions/194142/what-does-1x1-convolution-mean-in-a-neural-network)
|
||||
- [A guide to receptive field arithmetic for Convolutional Neural Networks](https://medium.com/mlreview/a-guide-to-receptive-field-arithmetic-for-convolutional-neural-networks-e0f514068807)
|
||||
|
||||
Some other useful links
|
||||
- [VGGNet summary](https://medium.com/coinmonks/paper-review-of-vggnet-1st-runner-up-of-ilsvlc-2014-image-classification-d02355543a11)
|
||||
- [VGGNet in Keras](https://towardsdatascience.com/step-by-step-vgg16-implementation-in-keras-for-beginners-a833c686ae6c)
|
||||
- [VGGNet with Batch Normalization](https://gist.github.com/jjangsangy/38d644606130f05b806a4261c493a820)
|
||||
|
||||
This code is inspired by [VGGNet implement from scratch in PyTorch by aladdinpersson](https://github.com/aladdinpersson/Machine-Learning-Collection/blob/master/ML/Pytorch/CNN_architectures/pytorch_vgg_implementation.py).
|
||||
21
ML/TensorFlow/CNN_architectures/VGGNet/test.py
Normal file
21
ML/TensorFlow/CNN_architectures/VGGNet/test.py
Normal file
@@ -0,0 +1,21 @@
|
||||
# disable tensorflow debugging messages
|
||||
import os
|
||||
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
||||
|
||||
from vggnet import VGGNet
|
||||
|
||||
# Integer value represents output channel after performing the convolution layer
|
||||
# 'M' represents the max pooling layer
|
||||
# After convolution blocks; flatten the output and use 4096x4096x1000 Linear Layers
|
||||
# with soft-max at the end
|
||||
VGG_types = {
|
||||
'VGG11': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
|
||||
'VGG13': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
|
||||
'VGG16': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
|
||||
'VGG19': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
|
||||
}
|
||||
|
||||
if __name__ == "__main__":
|
||||
# test VGGNet16
|
||||
model = VGGNet(name = "VGGNet16", architecture = VGG_types["VGG16"], input_shape=(224, 224, 3), classes = 1000)
|
||||
model.summary()
|
||||
126
ML/TensorFlow/CNN_architectures/VGGNet/vggnet.py
Normal file
126
ML/TensorFlow/CNN_architectures/VGGNet/vggnet.py
Normal file
@@ -0,0 +1,126 @@
|
||||
# Tensorflow v.2.3.1
|
||||
|
||||
"""
|
||||
Programmed by the-robot <https://github.com/the-robot>
|
||||
"""
|
||||
|
||||
from tensorflow.keras.layers import (
|
||||
Activation,
|
||||
BatchNormalization,
|
||||
Conv2D,
|
||||
Dense,
|
||||
Dropout,
|
||||
Flatten,
|
||||
Input,
|
||||
MaxPooling2D,
|
||||
)
|
||||
from tensorflow.keras import Model
|
||||
import tensorflow as tf
|
||||
import typing
|
||||
|
||||
tf.config.run_functions_eagerly(True)
|
||||
|
||||
@tf.function
|
||||
def VGGNet(
|
||||
name: str,
|
||||
architecture: typing.List[ typing.Union[int, str] ],
|
||||
input_shape: typing.Tuple[int],
|
||||
classes: int = 1000
|
||||
) -> Model:
|
||||
"""
|
||||
Implementation of the VGGNet architecture.
|
||||
|
||||
Arguments:
|
||||
name -- name of the architecture
|
||||
architecture -- number of output channel per convolution layers in VGGNet
|
||||
input_shape -- shape of the images of the dataset
|
||||
classes -- integer, number of classes
|
||||
|
||||
Returns:
|
||||
model -- a Model() instance in Keras
|
||||
"""
|
||||
|
||||
# convert input shape into tensor
|
||||
X_input = Input(input_shape)
|
||||
|
||||
# make convolution layers
|
||||
X = make_conv_layer(X_input, architecture)
|
||||
|
||||
# flatten the output and make fully connected layers
|
||||
X = Flatten()(X)
|
||||
X = make_dense_layer(X, 4096)
|
||||
X = make_dense_layer(X, 4096)
|
||||
|
||||
# classification layer
|
||||
X = Dense(units = classes, activation = "softmax")(X)
|
||||
|
||||
model = Model(inputs = X_input, outputs = X, name = name)
|
||||
return model
|
||||
|
||||
def make_conv_layer(
|
||||
X: tf.Tensor,
|
||||
architecture: typing.List[ typing.Union[int, str] ],
|
||||
activation: str = 'relu'
|
||||
) -> tf.Tensor:
|
||||
"""
|
||||
Method to create convolution layers for VGGNet.
|
||||
In VGGNet
|
||||
- Kernal is always 3x3 for conv-layer with padding 1 and stride 1.
|
||||
- 2x2 kernel for max pooling with stride of 2.
|
||||
|
||||
Arguments:
|
||||
X -- input tensor
|
||||
architecture -- number of output channel per convolution layers in VGGNet
|
||||
activation -- type of activation method
|
||||
|
||||
Returns:
|
||||
X -- output tensor
|
||||
"""
|
||||
|
||||
for output in architecture:
|
||||
|
||||
# convolution layer
|
||||
if type(output) == int:
|
||||
out_channels = output
|
||||
|
||||
X = Conv2D(
|
||||
filters = out_channels,
|
||||
kernel_size = (3, 3),
|
||||
strides = (1, 1),
|
||||
padding = "same"
|
||||
)(X)
|
||||
X = BatchNormalization()(X)
|
||||
X = Activation(activation)(X)
|
||||
|
||||
# relu activation is added (by default activation) so that all the
|
||||
# negative values are not passed to the next layer
|
||||
|
||||
# max-pooling layer
|
||||
else:
|
||||
X = MaxPooling2D(
|
||||
pool_size = (2, 2),
|
||||
strides = (2, 2)
|
||||
)(X)
|
||||
|
||||
return X
|
||||
|
||||
def make_dense_layer(X: tf.Tensor, output_units: int, dropout = 0.5, activation = 'relu') -> tf.Tensor:
|
||||
"""
|
||||
Method to create dense layer for VGGNet.
|
||||
|
||||
Arguments:
|
||||
X -- input tensor
|
||||
output_units -- output tensor size
|
||||
dropout -- dropout value for regularization
|
||||
activation -- type of activation method
|
||||
|
||||
Returns:
|
||||
X -- input tensor
|
||||
"""
|
||||
|
||||
X = Dense(units = output_units)(X)
|
||||
X = BatchNormalization()(X)
|
||||
X = Activation(activation)(X)
|
||||
X = Dropout(dropout)(X)
|
||||
|
||||
return X
|
||||
Reference in New Issue
Block a user