Skip to content

Commit

Permalink
add new generator
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketFlash committed Feb 14, 2020
1 parent 25fe9f6 commit 2c1f026
Show file tree
Hide file tree
Showing 9 changed files with 1,001 additions and 68 deletions.
56 changes: 56 additions & 0 deletions configs/bengali.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
MODEL:
input_shape : [128, 128, 3]
encodings_len: 256
mode : 'triplet'
distance_type : 'l1'
backbone : 'resnet18'
backbone_weights : 'imagenet'
freeze_backbone : False
embeddings_normalization: True

TRAIN:
margin: 0.5
batch_size : 8
augmentation_type : 'default'

# optimizer parameters
optimizer : 'radam'
learning_rate : 0.0001
decay_factor : 0.99
step_size : 1

# embeddings learning training parameters
n_epochs : 1000
n_batches : 200
val_batch_size : 8
val_steps : 200
negatives_selection_mode : 'semihard'
mining_n_classes: 5
mining_n_samples: 3

# softmax pretraining parameters
softmax_pretraining : True
softmax_batch_size : 8
softmax_val_steps : 200
softmax_steps_per_epoch : 500
softmax_epochs : 20

# plot training history
plot_history : True

PATHS:
work_dir : 'work_dirs/road_signs_resnet18/'
dataset_path : '/home/rauf/datasets/bengali/pngs/train/'
csv_file : '/home/rauf/datasets/bengali/train.csv'
image_id_column : 'image_id'
label_column : 'label'
encodings_path : 'encodings/'
model_save_name : 'best_model_resnet18.h5'
encodings_save_name: 'encodings_resnet18.pkl'

ENCODINGS:
# encodings parameters
save_encodings : True
centers_only: False
max_num_samples_of_each_class : 30
knn_k : 1
90 changes: 48 additions & 42 deletions configs/road_signs_resnet18.yml
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
input_shape : [48, 48, 3]
encodings_len: 256
margin: 0.5
mode : 'triplet'
distance_type : 'l1'
backbone : 'resnet18'
backbone_weights : 'imagenet'
project_name : 'road_signs/'
freeze_backbone : False
augmentation_type : 'default'
embeddings_normalization: True
MODEL:
encodings_len: 256
mode : 'triplet'
distance_type : 'l1'
backbone : 'resnet18'
backbone_weights : 'imagenet'
freeze_backbone : False
embeddings_normalization: True

# optimizer parameters
optimizer : 'radam'
learning_rate : 0.0001
decay_factor : 0.99
step_size : 1
TRAIN:
input_shape : [48, 48, 3]
margin: 0.5
augmentation_type : 'default'

# embeddings learning training parameters
n_epochs : 1000
n_steps_per_epoch : 200
val_batch_size : 8
val_steps : 200
negatives_selection_mode : 'semihard'
mining_n_classes: 5
mining_n_samples: 3
# optimizer parameters
optimizer : 'radam'
learning_rate : 0.0001
decay_factor : 0.99
step_size : 1

# softmax pretraining parameters
softmax_pretraining : True
softmax_batch_size : 8
softmax_val_steps : 200
softmax_steps_per_epoch : 500
softmax_epochs : 20
# embeddings learning training parameters
n_epochs : 1000
n_steps_per_epoch : 200
val_batch_size : 8
val_steps : 200
negatives_selection_mode : 'semihard'
mining_n_classes: 5
mining_n_samples: 3

#paths
work_dir : 'work_dirs/road_signs_resnet18/'
dataset_path : '/home/rauf/datasets/road_signs/road_signs_separated/'
plot_history : True
encodings_path : 'encodings/'
model_save_name : 'best_model_resnet18.h5'
encodings_save_name: 'encodings_resnet18.pkl'
# softmax pretraining parameters
softmax_pretraining : True
softmax_batch_size : 8
softmax_val_steps : 200
softmax_steps_per_epoch : 500
softmax_epochs : 20

# encodings parameters
save_encodings : True
centers_only: False
max_num_samples_of_each_class : 30
knn_k : 1
# plot training history
plot_history : True

PATHS:
work_dir : 'work_dirs/road_signs_resnet18/'
dataset_path : '/home/rauf/datasets/road_signs/road_signs_separated/'
csv_file : '/home/rauf/datasets/road_signs/road_signs_separated/'
encodings_path : 'encodings/'
model_save_name : 'best_model_resnet18.h5'
encodings_save_name: 'encodings_resnet18.pkl'

ENCODINGS:
# encodings parameters
save_encodings : True
centers_only: False
max_num_samples_of_each_class : 30
knn_k : 1
43 changes: 30 additions & 13 deletions embedding_net/backbones.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from keras.layers import Dense, Input, Lambda, Dropout, Flatten
from keras.layers import Conv2D, MaxPool2D, BatchNormalization, concatenate
from keras.models import Model
from keras.regularizers import l2
import keras.backend as K
from tensorflow.keras.layers import Dense, Input, Lambda, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPool2D, BatchNormalization, concatenate
from tensorflow.keras.models import Model
from tensorflow.keras.regularizers import l2
import tensorflow.keras.backend as K


def get_backbone(input_shape,
encodings_len=4096,
backbone_type='simple',
backbone_name='simple',
embeddings_normalization=True,
backbone_weights='imagenet',
freeze_backbone=False):
if backbone_type == 'simple':
if backbone_name == 'simple':
input_image = Input(input_shape)
x = Conv2D(64, (10, 10), activation='relu',
kernel_regularizer=l2(2e-4))(input_image)
Expand All @@ -34,7 +34,7 @@ def get_backbone(input_shape,
x, axis=1), name='l2_norm')(encoded_output)
base_model = Model(
inputs=[input_image], outputs=[encoded_output])
elif backbone_type == 'simple2':
elif backbone_name == 'simple2':
input_image = Input(input_shape)
x = Conv2D(32, kernel_size=3, activation='relu',
kernel_regularizer=l2(2e-4))(input_image)
Expand Down Expand Up @@ -76,11 +76,28 @@ def get_backbone(input_shape,
base_model = Model(
inputs=[input_image], outputs=[encoded_output])
else:
from classification_models.keras import Classifiers
classifier, preprocess_input = Classifiers.get(backbone_type)
backbone_model = classifier(input_shape=input_shape,
weights=backbone_weights,
include_top=False)
if backbone_name.startswith('efficientnet'):
import efficientnet.tfkeras as efn
efficientnet_models = {
'efficientnet-b0': efn.EfficientNetB0,
'efficientnet-b1': efn.EfficientNetB1,
'efficientnet-b2': efn.EfficientNetB2,
'efficientnet-b3': efn.EfficientNetB3,
'efficientnet-b4': efn.EfficientNetB4,
'efficientnet-b5': efn.EfficientNetB5,
'efficientnet-b6': efn.EfficientNetB6,
'efficientnet-b7': efn.EfficientNetB7,
}
Efficientnet_model = efficientnet_models[backbone_name]
backbone_model = Efficientnet_model(input_shape=input_shape,
weights=backbone_weights,
include_top=False)
else:
from classification_models.tfkeras import Classifiers
classifier, preprocess_input = Classifiers.get(backbone_name)
backbone_model = classifier(input_shape=input_shape,
weights=backbone_weights,
include_top=False)

if freeze_backbone:
for layer in backbone_model.layers[:-2]:
Expand Down
Loading

0 comments on commit 2c1f026

Please sign in to comment.