Skip to content

SSD #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

SSD #61

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions 4-Object_Detection/SSD/core/backbone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#! /usr/bin/env python
# coding=utf-8
#================================================================
# Copyright (C) 2019 * Ltd. All rights reserved.
#
# Editor : VIM
# File name : backbone.py
# Author : YunYang1994
# Created date: 2019-07-11 23:37:51
# Description :
#
#================================================================

import tensorflow as tf

def vgg16(input_data):

#======================================VGG16_start===================================================
# conv1
conv = tf.keras.layers.Conv2D(64, 3, activation='relu', padding='same')(input_data) #conv1_1
conv = tf.keras.layers.Conv2D(64, 3, activation='relu', padding='same')(conv) #conv1_2
conv = tf.keras.layers.MaxPooling2D(2, strides=2, padding='same')(conv) #pool1

# conv2
conv = tf.keras.layers.Conv2D(128, 3, activation='relu', padding='same')(conv)#conv2_1
conv = tf.keras.layers.Conv2D(128, 3, activation='relu', padding='same')(conv)#conv2_2
conv = tf.keras.layers.MaxPooling2D(2, strides=2, padding='same')(conv)#pool2

# conv3
conv = tf.keras.layers.Conv2D(256, 3, activation='relu', padding='same')(conv)#conv3_1
conv = tf.keras.layers.Conv2D(256, 3, activation='relu', padding='same')(conv)#conv3_2
conv = tf.keras.layers.Conv2D(256, 3, activation='relu', padding='same')(conv)#conv3_3
conv = tf.keras.layers.MaxPooling2D(2, strides=2, padding='same')(conv)#pool3

# conv4
conv = tf.keras.layers.Conv2D(512, 3, activation='relu', padding='same')(conv)
conv = tf.keras.layers.Conv2D(512, 3, activation='relu', padding='same')(conv)
conv = tf.keras.layers.Conv2D(512, 3, activation='relu', padding='same')(conv)
conv4 = conv
conv = tf.keras.layers.MaxPooling2D(2, strides=2, padding='same')(conv)

# conv5
conv = tf.keras.layers.Conv2D(512, 3, activation='relu', padding='same')(conv)
conv = tf.keras.layers.Conv2D(512, 3, activation='relu', padding='same')(conv)
conv = tf.keras.layers.Conv2D(512, 3, activation='relu', padding='same')(conv)
conv = tf.keras.layers.MaxPooling2D(3, strides=1, padding='same')(conv)

return conv4, conv


58 changes: 58 additions & 0 deletions 4-Object_Detection/SSD/core/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#! /usr/bin/env python
# coding=utf-8
#================================================================
# Copyright (C) 2019 * Ltd. All rights reserved.
#
# Editor : VIM
# File name : common.py
# Author : YunYang1994
# Created date: 2019-07-11 23:12:53
# Description :
#
#================================================================

import tensorflow as tf

class BatchNormalization(tf.keras.layers.BatchNormalization):
"""
"Frozen state" and "inference mode" are two separate concepts.
`layer.trainable = False` is to freeze the layer, so the layer will use
stored moving `var` and `mean` in the "inference mode", and both `gama`
and `beta` will not be updated !
"""
def call(self, x, training=False):
if not training:
training = tf.constant(False)
training = tf.logical_and(training, self.trainable)
return super().call(x, training)

def convolutional(input_layer, filters_shape, downsample=False, activate=True, bn=True):
if downsample:
input_layer = tf.keras.layers.ZeroPadding2D(((1, 0), (1, 0)))(input_layer)
padding = 'valid'
strides = 2
else:
strides = 1
padding = 'same'

conv = tf.keras.layers.Conv2D(filters=filters_shape[-1], kernel_size = filters_shape[0], strides=strides, padding=padding,
use_bias=not bn, kernel_regularizer=tf.keras.regularizers.l2(0.0005),
kernel_initializer=tf.random_normal_initializer(stddev=0.01),
bias_initializer=tf.constant_initializer(0.))(input_layer)

if bn: conv = BatchNormalization()(conv)
if activate == True: conv = tf.nn.leaky_relu(conv, alpha=0.1)

return conv

def residual_block(input_layer, input_channel, filter_num1, filter_num2):
short_cut = input_layer
conv = convolutional(input_layer, filters_shape=(1, 1, input_channel, filter_num1))
conv = convolutional(conv , filters_shape=(3, 3, filter_num1, filter_num2))

residual_output = short_cut + conv
return residual_output

def upsample(input_layer):
return tf.image.resize(input_layer, (input_layer.shape[1] * 2, input_layer.shape[2] * 2), method='nearest')

58 changes: 58 additions & 0 deletions 4-Object_Detection/SSD/core/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#! /usr/bin/env python
# coding=utf-8
#================================================================
# Copyright (C) 2019 * Ltd. All rights reserved.
#
# Editor : VIM
# File name : config.py
# Author : YunYang1994
# Created date: 2019-02-28 13:06:54
# Description :
#
#================================================================

from easydict import EasyDict as edict


__C = edict()
# Consumers can get config by: from config import cfg

cfg = __C

# YOLO options
__C.YOLO = edict()

# Set the class name
__C.YOLO.CLASSES = "./data/classes/coco.names"
__C.YOLO.ANCHORS = "./data/anchors/basline_anchors.txt"
__C.YOLO.STRIDES = [8, 16, 32]
__C.YOLO.ANCHOR_PER_SCALE = 3
__C.YOLO.IOU_LOSS_THRESH = 0.5

# Train options
__C.TRAIN = edict()

__C.TRAIN.ANNOT_PATH = "./data/dataset/yymnist_train.txt"
__C.TRAIN.BATCH_SIZE = 4
# __C.TRAIN.INPUT_SIZE = [320, 352, 384, 416, 448, 480, 512, 544, 576, 608]
__C.TRAIN.INPUT_SIZE = [416]
__C.TRAIN.DATA_AUG = True
__C.TRAIN.LR_INIT = 1e-3
__C.TRAIN.LR_END = 1e-6
__C.TRAIN.WARMUP_EPOCHS = 2
__C.TRAIN.EPOCHS = 30



# TEST options
__C.TEST = edict()

__C.TEST.ANNOT_PATH = "./data/dataset/yymnist_test.txt"
__C.TEST.BATCH_SIZE = 2
__C.TEST.INPUT_SIZE = 544
__C.TEST.DATA_AUG = False
__C.TEST.DECTECTED_IMAGE_PATH = "./data/detection/"
__C.TEST.SCORE_THRESHOLD = 0.3
__C.TEST.IOU_THRESHOLD = 0.45


Loading