Skip to content

Instantly share code, notes, and snippets.

@weiji14
Last active November 28, 2017 19:34
Show Gist options
  • Save weiji14/24770458cac614e3c4bab1cc5793c4cf to your computer and use it in GitHub Desktop.
Save weiji14/24770458cac614e3c4bab1cc5793c4cf to your computer and use it in GitHub Desktop.
Attempt at a keras implementation of TinyYoloModel
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 416, 416, 3) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 416, 416, 16) 432
_________________________________________________________________
batch_normalization_1 (Batch (None, 416, 416, 16) 64
_________________________________________________________________
leaky_re_lu_1 (LeakyReLU) (None, 416, 416, 16) 0
_________________________________________________________________
max_pooling_2d_1 (MaxPooling (None, 208, 208, 16) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 208, 208, 32) 4608
_________________________________________________________________
batch_normalization_2 (Batch (None, 208, 208, 32) 128
_________________________________________________________________
leaky_re_lu_2 (LeakyReLU) (None, 208, 208, 32) 0
_________________________________________________________________
max_pooling_2d_2 (MaxPooling (None, 104, 104, 32) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 104, 104, 64) 18432
_________________________________________________________________
batch_normalization_3 (Batch (None, 104, 104, 64) 256
_________________________________________________________________
leaky_re_lu_3 (LeakyReLU) (None, 104, 104, 64) 0
_________________________________________________________________
max_pooling_2d_3 (MaxPooling (None, 52, 52, 64) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 52, 52, 128) 73728
_________________________________________________________________
batch_normalization_4 (Batch (None, 52, 52, 128) 512
_________________________________________________________________
leaky_re_lu_4 (LeakyReLU) (None, 52, 52, 128) 0
_________________________________________________________________
max_pooling_2d_4 (MaxPooling (None, 26, 26, 128) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 26, 26, 256) 294912
_________________________________________________________________
batch_normalization_5 (Batch (None, 26, 26, 256) 1024
_________________________________________________________________
leaky_re_lu_5 (LeakyReLU) (None, 26, 26, 256) 0
_________________________________________________________________
max_pooling_2d_5 (MaxPooling (None, 13, 13, 256) 0
_________________________________________________________________
conv2d_6 (Conv2D) (None, 13, 13, 512) 1179648
_________________________________________________________________
batch_normalization_6 (Batch (None, 13, 13, 512) 2048
_________________________________________________________________
leaky_re_lu_6 (LeakyReLU) (None, 13, 13, 512) 0
_________________________________________________________________
max_pooling_2d_6 (MaxPooling (None, 13, 13, 512) 0
_________________________________________________________________
conv2d_7 (Conv2D) (None, 13, 13, 1024) 4718592
_________________________________________________________________
batch_normalization_7 (Batch (None, 13, 13, 1024) 4096
_________________________________________________________________
leaky_re_lu_7 (LeakyReLU) (None, 13, 13, 1024) 0
_________________________________________________________________
conv2d_8 (Conv2D) (None, 13, 13, 512) 4718592
_________________________________________________________________
batch_normalization_8 (Batch (None, 13, 13, 512) 2048
_________________________________________________________________
leaky_re_lu_8 (LeakyReLU) (None, 13, 13, 512) 0
_________________________________________________________________
conv2d_9 (Conv2D) (None, 13, 13, 6) 3072
_________________________________________________________________
flatten_1 (Flatten) (None, 1014) 0
_________________________________________________________________
fc (Dense) (None, 5) 5075
=================================================================
Total params: 11,027,267
Trainable params: 11,022,179
Non-trainable params: 5,088
_____________________________
#%% YOLO in Keras from scratch?
np.random.seed(123)
from keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D
from keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D
from keras.layers.advanced_activations import LeakyReLU
from keras.models import load_model, Model
from keras.preprocessing.image import ImageDataGenerator
def TinyYoloModel(input_shape=(416,416,3)):
"""
Implementation of the Tiny YOLOv2 Model in Keras.
Arguments:
input_shape -- shape of the images of the dataset
Returns:
model -- a Model() instance in Keras
"""
# Define the input placeholder as a tensor with shape input_shape. Think of this as your input image!
X_input = Input(shape=input_shape)
#https://github.com/xslittlegrass/CarND-Vehicle-Detection/blob/master/vehicle%20detection.ipynb
# Layer 1-8
for i in range(1,9):
if i==1:
X = X_input
# 2D Convolution
if i != 8:
X = Conv2D(2**(3+i), kernel_size=(3,3), strides=(1,1), padding='same', name='conv2d_'+str(i), use_bias=False)(X)
elif i == 8:
X = Conv2D(512, kernel_size=(3,3), strides=(1,1), padding='same', name='conv2d_'+str(i), use_bias=False)(X)
# Batch Normalization
X = BatchNormalization(name='batch_normalization_'+str(i))(X)
# Leaky ReLU
X = LeakyReLU(alpha=0.1, name='leaky_re_lu_'+str(i))(X)
# MaxPooling2D
if i <= 5:
X = MaxPooling2D(pool_size=(2,2), name='max_pooling_2d_'+str(i))(X)
if i == 6:
X = MaxPooling2D(pool_size=(1,1), name='max_pooling_2d_'+str(i))(X)
# Layer 9
'''
Parameters:
bbox_count -- number of bounding boxes you want each YOLO grid to output per grid cell (e.g. 5 in YOLOv2)
bbox_info -- Confidence score for a bbox, and bbox dimension's x-position, y-position, width, and height (i.e. fixed at 5)
classes -- number of classes you want to predict (e.g. building, tree, road, etc)
So if predicting on:
1 bounding box per grid, 1 class, filters = 1*(5+1) = 6
5 bounding boxes per grid, 20 classes, filters = 5*(5+20) = 125 (e.g. PASCAL VOC )
filters = (no. of bounding boxes you want model to predict) * (no. of classes + 5)
'''
bbox_info = 5 #fixed at 5!!
bbox_count, classes = (1, 1)
last_filters = bbox_count * (bbox_info + classes)
X = Conv2D(filters=last_filters, kernel_size=(1,1), strides=(1,1), padding='same', name='conv2d_9', use_bias=False)(X)
# FLATTEN X (means convert it to a vector) + FULLYCONNECTED
X = Flatten()(X)
X = Dense(5, activation='relu', name='fc')(X)
# Create model. This creates your Keras model instance, you'll use this instance to train/test the model.
model = Model(inputs = X_input, outputs = X, name='TinyYoloModel')
return model
tinyYoloModel = TinyYoloModel()
tinyYoloModel.summary()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment