Skip to content

Commit

Permalink
some modification
Browse files Browse the repository at this point in the history
  • Loading branch information
JVD9kh96 committed Jun 22, 2021
1 parent 299fd66 commit e91514e
Show file tree
Hide file tree
Showing 7 changed files with 583 additions and 0 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
127 changes: 127 additions & 0 deletions 4-Object_Detection/YOLOV3/core/backbone.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,130 @@ def darknet53(input_data):
return route_1, route_2, input_data


def cspdarknet53(input_data):

input_data = common.convolutional(input_data, (3, 3, 3, 32), activate_type="mish")
input_data = common.convolutional(input_data, (3, 3, 32, 64), downsample=True, activate_type="mish")

route = input_data
route = common.convolutional(route, (1, 1, 64, 64), activate_type="mish")
input_data = common.convolutional(input_data, (1, 1, 64, 64), activate_type="mish")
for i in range(1):
input_data = common.residual_block(input_data, 64, 32, 64, activate_type="mish")
input_data = common.convolutional(input_data, (1, 1, 64, 64), activate_type="mish")

input_data = tf.concat([input_data, route], axis=-1)
input_data = common.convolutional(input_data, (1, 1, 128, 64), activate_type="mish")
input_data = common.convolutional(input_data, (3, 3, 64, 128), downsample=True, activate_type="mish")
route = input_data
route = common.convolutional(route, (1, 1, 128, 64), activate_type="mish")
input_data = common.convolutional(input_data, (1, 1, 128, 64), activate_type="mish")
for i in range(2):
input_data = common.residual_block(input_data, 64, 64, 64, activate_type="mish")
input_data = common.convolutional(input_data, (1, 1, 64, 64), activate_type="mish")
input_data = tf.concat([input_data, route], axis=-1)

input_data = common.convolutional(input_data, (1, 1, 128, 128), activate_type="mish")
input_data = common.convolutional(input_data, (3, 3, 128, 256), downsample=True, activate_type="mish")
route = input_data
route = common.convolutional(route, (1, 1, 256, 128), activate_type="mish")
input_data = common.convolutional(input_data, (1, 1, 256, 128), activate_type="mish")
for i in range(8):
input_data = common.residual_block(input_data, 128, 128, 128, activate_type="mish")
input_data = common.convolutional(input_data, (1, 1, 128, 128), activate_type="mish")
input_data = tf.concat([input_data, route], axis=-1)

input_data = common.convolutional(input_data, (1, 1, 256, 256), activate_type="mish")
route_1 = input_data
input_data = common.convolutional(input_data, (3, 3, 256, 512), downsample=True, activate_type="mish")
route = input_data
route = common.convolutional(route, (1, 1, 512, 256), activate_type="mish")
input_data = common.convolutional(input_data, (1, 1, 512, 256), activate_type="mish")
for i in range(8):
input_data = common.residual_block(input_data, 256, 256, 256, activate_type="mish")
input_data = common.convolutional(input_data, (1, 1, 256, 256), activate_type="mish")
input_data = tf.concat([input_data, route], axis=-1)

input_data = common.convolutional(input_data, (1, 1, 512, 512), activate_type="mish")
route_2 = input_data
input_data = common.convolutional(input_data, (3, 3, 512, 1024), downsample=True, activate_type="mish")
route = input_data
route = common.convolutional(route, (1, 1, 1024, 512), activate_type="mish")
input_data = common.convolutional(input_data, (1, 1, 1024, 512), activate_type="mish")
for i in range(4):
input_data = common.residual_block(input_data, 512, 512, 512, activate_type="mish")
input_data = common.convolutional(input_data, (1, 1, 512, 512), activate_type="mish")
input_data = tf.concat([input_data, route], axis=-1)

input_data = common.convolutional(input_data, (1, 1, 1024, 1024), activate_type="mish")
input_data = common.convolutional(input_data, (1, 1, 1024, 512))
input_data = common.convolutional(input_data, (3, 3, 512, 1024))
input_data = common.convolutional(input_data, (1, 1, 1024, 512))

input_data = tf.concat([tf.nn.max_pool(input_data, ksize=13, padding='SAME', strides=1), tf.nn.max_pool(input_data, ksize=9, padding='SAME', strides=1)
, tf.nn.max_pool(input_data, ksize=5, padding='SAME', strides=1), input_data], axis=-1)
input_data = common.convolutional(input_data, (1, 1, 2048, 512))
input_data = common.convolutional(input_data, (3, 3, 512, 1024))
input_data = common.convolutional(input_data, (1, 1, 1024, 512))

return route_1, route_2, input_data

def cspdarknet53_tiny(input_data):
input_data = common.convolutional(input_data, (3, 3, 3, 32), downsample=True)
input_data = common.convolutional(input_data, (3, 3, 32, 64), downsample=True)
input_data = common.convolutional(input_data, (3, 3, 64, 64))

route = input_data
input_data = common.route_group(input_data, 2, 1)
input_data = common.convolutional(input_data, (3, 3, 32, 32))
route_1 = input_data
input_data = common.convolutional(input_data, (3, 3, 32, 32))
input_data = tf.concat([input_data, route_1], axis=-1)
input_data = common.convolutional(input_data, (1, 1, 32, 64))
input_data = tf.concat([route, input_data], axis=-1)
input_data = tf.keras.layers.MaxPool2D(2, 2, 'same')(input_data)

input_data = common.convolutional(input_data, (3, 3, 64, 128))
route = input_data
input_data = common.route_group(input_data, 2, 1)
input_data = common.convolutional(input_data, (3, 3, 64, 64))
route_1 = input_data
input_data = common.convolutional(input_data, (3, 3, 64, 64))
input_data = tf.concat([input_data, route_1], axis=-1)
input_data = common.convolutional(input_data, (1, 1, 64, 128))
input_data = tf.concat([route, input_data], axis=-1)
input_data = tf.keras.layers.MaxPool2D(2, 2, 'same')(input_data)

input_data = common.convolutional(input_data, (3, 3, 128, 256))
route = input_data
input_data = common.route_group(input_data, 2, 1)
input_data = common.convolutional(input_data, (3, 3, 128, 128))
route_1 = input_data
input_data = common.convolutional(input_data, (3, 3, 128, 128))
input_data = tf.concat([input_data, route_1], axis=-1)
input_data = common.convolutional(input_data, (1, 1, 128, 256))
route_1 = input_data
input_data = tf.concat([route, input_data], axis=-1)
input_data = tf.keras.layers.MaxPool2D(2, 2, 'same')(input_data)

input_data = common.convolutional(input_data, (3, 3, 512, 512))

return route_1, input_data

def darknet53_tiny(input_data):
input_data = common.convolutional(input_data, (3, 3, 3, 16))
input_data = tf.keras.layers.MaxPool2D(2, 2, 'same')(input_data)
input_data = common.convolutional(input_data, (3, 3, 16, 32))
input_data = tf.keras.layers.MaxPool2D(2, 2, 'same')(input_data)
input_data = common.convolutional(input_data, (3, 3, 32, 64))
input_data = tf.keras.layers.MaxPool2D(2, 2, 'same')(input_data)
input_data = common.convolutional(input_data, (3, 3, 64, 128))
input_data = tf.keras.layers.MaxPool2D(2, 2, 'same')(input_data)
input_data = common.convolutional(input_data, (3, 3, 128, 256))
route_1 = input_data
input_data = tf.keras.layers.MaxPool2D(2, 2, 'same')(input_data)
input_data = common.convolutional(input_data, (3, 3, 256, 512))
input_data = tf.keras.layers.MaxPool2D(2, 1, 'same')(input_data)
input_data = common.convolutional(input_data, (3, 3, 512, 1024))

return route_1, input_data
28 changes: 28 additions & 0 deletions 4-Object_Detection/YOLOV3/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#================================================================

import tensorflow as tf
from tensorflow.keras import layers
import tensorflow_addons as tfa
from tensorflow import keras
import numpy as np

class BatchNormalization(tf.keras.layers.BatchNormalization):
"""
Expand Down Expand Up @@ -56,3 +60,27 @@ def residual_block(input_layer, input_channel, filter_num1, filter_num2):
def upsample(input_layer):
return tf.image.resize(input_layer, (input_layer.shape[1] * 2, input_layer.shape[2] * 2), method='nearest')

def mlp(x, hidden_units, dropout_rate):
for units in hidden_units:
x = layers.Dense(units, activation=tf.nn.gelu)(x)
x = layers.Dropout(dropout_rate)(x)
return x

def transformer(input_layer, projection_dim, transformer_units, num_layers = 4, num_heads = 4):
encoded_patches = input_layer
for _ in range(num_layers):
# Layer normalization 1.
x1 = layers.LayerNormalization(epsilon=1e-6)(encoded_patches)
# Create a multi-head attention layer.
attention_output = layers.MultiHeadAttention(
num_heads=num_heads, key_dim=projection_dim, dropout=0.1
)(x1, x1)
# Skip connection 1.
x2 = layers.Add()([attention_output, encoded_patches])
# Layer normalization 2.
x3 = layers.LayerNormalization(epsilon=1e-6)(x2)
# MLP.
x3 = mlp(x3, hidden_units=transformer_units, dropout_rate=0.1)
# Skip connection 2.
encoded_patches = layers.Add()([x3, x2])
return encoded_patches
66 changes: 66 additions & 0 deletions 4-Object_Detection/YOLOV3/core/vit_backbone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import tensorflow_addons as tfa


def mlp(x, hidden_units, dropout_rate):
for units in hidden_units:
x = layers.Dense(units, activation=tf.nn.gelu)(x)
x = layers.Dropout(dropout_rate)(x)
return x

class Patches(layers.Layer):
def __init__(self, patch_size):
super(Patches, self).__init__()
self.patch_size = patch_size

def call(self, images):
batch_size = tf.shape(images)[0]
patches = tf.image.extract_patches(
images=images,
sizes=[1, self.patch_size, self.patch_size, 1],
strides=[1, self.patch_size, self.patch_size, 1],
rates=[1, 1, 1, 1],
padding="VALID",
)
patch_dims = patches.shape[-1]
patches = tf.reshape(patches, [batch_size, -1, patch_dims])
return patches

class Patches_sp(layers.Layer):
def __init__(self, patch_size):
super(Patches_sp, self).__init__()
self.patch_size = patch_size

def call(self, images):
batch_size = tf.shape(images)[0]
patches = tf.image.extract_patches(
images=images,
sizes=[1, self.patch_size[0], self.patch_size[1], 1],
strides=[1, self.patch_size[0], self.patch_size[1], 1],
rates=[1, 1, 1, 1],
padding="VALID",
)
patch_dims = patches.shape[-1]
patches = tf.reshape(patches, [batch_size, -1, patch_dims])
return patches



class PatchEncoder(layers.Layer):
def __init__(self, num_patches, projection_dim):
super(PatchEncoder, self).__init__()
self.num_patches = num_patches
self.projection = layers.Dense(units=projection_dim)
self.position_embedding = layers.Embedding(
input_dim=num_patches, output_dim=projection_dim
)

def call(self, patch):
positions = tf.range(start=0, limit=self.num_patches, delta=1)
encoded = self.projection(patch) + self.position_embedding(positions)
return encoded


Loading

0 comments on commit e91514e

Please sign in to comment.