Skip to content

Commit

Permalink
add negatives mining strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketFlash committed Sep 5, 2019
1 parent 01a9b12 commit b8f6106
Show file tree
Hide file tree
Showing 14 changed files with 449 additions and 4,425 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,4 @@ encodings/
weights/
plots/
sub.csv
core
1 change: 1 addition & 0 deletions configs/road_signs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ optimizer : 'radam'
learning_rate : 0.0001
project_name : 'road_signs/'
freeze_backbone : True
embeddings_normalization: True

#paths
dataset_path : '/home/rauf/datasets/road_signs/road_signs_separated/'
Expand Down
1 change: 1 addition & 0 deletions configs/road_signs_mobilenetv2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ optimizer : 'radam'
learning_rate : 0.0001
project_name : 'road_signs/'
freeze_backbone : True
embeddings_normalization: True

#paths
dataset_path : '/home/rauf/datasets/road_signs/road_signs_separated/'
Expand Down
1 change: 1 addition & 0 deletions configs/road_signs_resnet18.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ optimizer : 'radam'
learning_rate : 0.0001
project_name : 'road_signs/'
freeze_backbone : True
embeddings_normalization: True

#paths
dataset_path : '/home/rauf/datasets/road_signs/road_signs_separated/'
Expand Down
1 change: 1 addition & 0 deletions configs/road_signs_resnet50v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ optimizer : 'radam'
learning_rate : 0.0001
project_name : 'road_signs/'
freeze_backbone : True
embeddings_normalization: True

#paths
dataset_path : '/home/rauf/datasets/road_signs/road_signs_separated/'
Expand Down
1 change: 1 addition & 0 deletions configs/road_signs_resnet50v2_merged_dataset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ optimizer : 'radam'
learning_rate : 0.0001
project_name : 'road_signs/'
freeze_backbone : True
embeddings_normalization: True

#paths
dataset_path : '/home/rauf/datasets/road_signs_merged/'
Expand Down
1 change: 1 addition & 0 deletions configs/road_signs_resnext50.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ optimizer : 'radam'
learning_rate : 0.0001
project_name : 'road_signs/'
freeze_backbone : True
embeddings_normalization: True

#paths
dataset_path : '/home/rauf/datasets/road_signs/road_signs_separated/'
Expand Down
3 changes: 2 additions & 1 deletion configs/road_signs_resnext50_merged_dataset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ backbone_weights : 'imagenet'
optimizer : 'radam'
learning_rate : 0.0001
project_name : 'road_signs/'
freeze_backbone : False
freeze_backbone : True
embeddings_normalization: True

#paths
dataset_path : '/home/rauf/datasets/road_signs_merged/'
Expand Down
1 change: 1 addition & 0 deletions configs/road_signs_simple2_merged_dataset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ optimizer : 'radam'
learning_rate : 0.0001
project_name : 'road_signs/'
freeze_backbone : True
embeddings_normalization: True

#paths
dataset_path : '/home/rauf/datasets/road_signs_merged/'
Expand Down
153 changes: 84 additions & 69 deletions siamese_net/backbones.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,79 +3,94 @@
from classification_models import Classifiers
from keras.models import Model
from keras.regularizers import l2
import keras.backend as K

def get_backbone(input_shape,encodings_len=4096,backbone_type='simple',backbone_weights='imagenet',freeze_backbone=False):
if backbone_type == 'simple':
input_image = Input(input_shape)
x = Conv2D(64, (10, 10), activation='relu',
kernel_regularizer=l2(2e-4))(input_image)
x = MaxPool2D()(x)
x = Conv2D(128, (7, 7), activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = MaxPool2D()(x)
x = Conv2D(128, (4, 4), activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = MaxPool2D()(x)
x = Conv2D(256, (4, 4), activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = Flatten()(x)
encoded_output = Dense(encodings_len, activation='sigmoid',
kernel_regularizer=l2(1e-3))(x)
base_model = Model(
inputs=[input_image], outputs=[encoded_output])
elif backbone_type == 'simple2':
input_image = Input(input_shape)
x = Conv2D(32, kernel_size=3, activation='relu',
kernel_regularizer=l2(2e-4))(input_image)
x = BatchNormalization()(x)
x = Conv2D(32, kernel_size=3, activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = BatchNormalization()(x)
x = Conv2D(32, kernel_size=5, strides=2, padding='same', activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = BatchNormalization()(x)
x = Dropout(0.4)(x)

x = Conv2D(64, kernel_size=3, activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = BatchNormalization()(x)
x = Conv2D(64, kernel_size=3, activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = BatchNormalization()(x)
x = Conv2D(64, kernel_size=5, strides=2, padding='same', activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = BatchNormalization()(x)
x = Dropout(0.4)(x)
def get_backbone(input_shape,
encodings_len=4096,
backbone_type='simple',
embeddings_normalization=True,
backbone_weights='imagenet',
freeze_backbone=False):
if backbone_type == 'simple':
input_image = Input(input_shape)
x = Conv2D(64, (10, 10), activation='relu',
kernel_regularizer=l2(2e-4))(input_image)
x = MaxPool2D()(x)
x = Conv2D(128, (7, 7), activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = MaxPool2D()(x)
x = Conv2D(128, (4, 4), activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = MaxPool2D()(x)
x = Conv2D(256, (4, 4), activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = Flatten()(x)
encoded_output = Dense(encodings_len, activation='relu',
kernel_regularizer=l2(1e-3))(x)
if embeddings_normalization:
encoded_output = Lambda(lambda x: K.l2_normalize(
x, axis=1), name='l2_norm')(encoded_output)
base_model = Model(
inputs=[input_image], outputs=[encoded_output])
elif backbone_type == 'simple2':
input_image = Input(input_shape)
x = Conv2D(32, kernel_size=3, activation='relu',
kernel_regularizer=l2(2e-4))(input_image)
x = BatchNormalization()(x)
x = Conv2D(32, kernel_size=3, activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = BatchNormalization()(x)
x = Conv2D(32, kernel_size=5, strides=2, padding='same', activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = BatchNormalization()(x)
x = Dropout(0.4)(x)

x = Conv2D(128, kernel_size=4, activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = BatchNormalization()(x)
x = Flatten()(x)
x = Dense(512, activation="relu")(x)
x = Dropout(0.5)(x)
encoded_output = Dense(encodings_len, activation='sigmoid',
kernel_regularizer=l2(1e-3))(x)
base_model = Model(
inputs=[input_image], outputs=[encoded_output])
else:
classifier, preprocess_input = Classifiers.get(backbone_type)
backbone_model = classifier(input_shape=input_shape,
weights=backbone_weights,
include_top=False)
x = Conv2D(64, kernel_size=3, activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = BatchNormalization()(x)
x = Conv2D(64, kernel_size=3, activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = BatchNormalization()(x)
x = Conv2D(64, kernel_size=5, strides=2, padding='same', activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = BatchNormalization()(x)
x = Dropout(0.4)(x)

if freeze_backbone:
for layer in backbone_model.layers[:-2]:
layer.trainable = False
x = Conv2D(128, kernel_size=4, activation='relu',
kernel_regularizer=l2(2e-4))(x)
x = BatchNormalization()(x)
x = Flatten()(x)
x = Dense(512, activation="relu")(x)
x = Dropout(0.5)(x)
encoded_output = Dense(encodings_len, activation='relu',
kernel_regularizer=l2(1e-3))(x)
if embeddings_normalization:
encoded_output = Lambda(lambda x: K.l2_normalize(
x, axis=1), name='l2_norm')(encoded_output)
base_model = Model(
inputs=[input_image], outputs=[encoded_output])
else:
classifier, preprocess_input = Classifiers.get(backbone_type)
backbone_model = classifier(input_shape=input_shape,
weights=backbone_weights,
include_top=False)

after_backbone = backbone_model.output
x = Flatten()(after_backbone)
# x = Dense(512, activation="relu")(x)
# x = Dropout(0.5)(x)
# x = Dense(512, activation="relu")(x)
# x = Dropout(0.5)(x)
encoded_output = Dense(encodings_len, activation="relu")(x)
if freeze_backbone:
for layer in backbone_model.layers[:-2]:
layer.trainable = False

base_model = Model(
inputs=[backbone_model.input], outputs=[encoded_output])
after_backbone = backbone_model.output
x = Flatten()(after_backbone)
# x = Dense(512, activation="relu")(x)
# x = Dropout(0.5)(x)
# x = Dense(512, activation="relu")(x)
# x = Dropout(0.5)(x)
encoded_output = Dense(encodings_len, activation="relu")(x)
if embeddings_normalization:
encoded_output = Lambda(lambda x: K.l2_normalize(
x, axis=1), name='l2_norm')(encoded_output)
base_model = Model(
inputs=[backbone_model.input], outputs=[encoded_output])

return base_model
return base_model
Loading

0 comments on commit b8f6106

Please sign in to comment.