Skip to content

Commit

Permalink
fix bugs with training
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketFlash committed Feb 18, 2020
1 parent 7b6b20b commit 1113f03
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 170 deletions.
40 changes: 19 additions & 21 deletions configs/bengali.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ MODEL:
encodings_len: 256
mode : 'triplet'
distance_type : 'l1'
backbone : 'resnet18'
backbone_name : 'efficientnet-b0'
backbone_weights : 'imagenet'
freeze_backbone : False
embeddings_normalization: True
Expand All @@ -12,18 +12,18 @@ DATALOADER:
dataset_path : '/home/rauf/datasets/bengali/pngs/train/'
csv_file : '/home/rauf/datasets/bengali/train_new.csv'
image_id_column : 'image_id'
label_column : 'label'
label_column : 'grapheme_root'
validate : True
val_ratio : 0.2

GENERATOR:
negatives_selection_mode : 'semihard'
mining_n_classes: 5
mining_n_samples: 3
k_classes: 3
k_samples: 5
margin: 0.5
batch_size : 8
n_batches : 200
augmentation_type : 'default'
batch_size : 10
n_batches : 10
augmentations : 'default'

TRAIN:
# optimizer parameters
Expand All @@ -38,23 +38,21 @@ TRAIN:
# plot training history
plot_history : True

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

batch_size : 8
val_steps : 200
steps_per_epoch : 500
n_epochs : 20
# batch_size : 8
# val_steps : 200
# steps_per_epoch : 10
# n_epochs : 1

SAVE_PATHS:
work_dir : 'work_dirs/road_signs_resnet18/'
encodings_path : 'encodings/'
model_save_name : 'best_model_resnet18.h5'
encodings_save_name: 'encodings_resnet18.pkl'
project_name : 'bengali_efficientnet'
work_dir : 'work_dirs/'

ENCODINGS:
# encodings parameters
Expand Down
3 changes: 2 additions & 1 deletion embedding_net/backbones.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def get_backbone(input_shape,
backbone_name='simple',
embeddings_normalization=True,
backbone_weights='imagenet',
freeze_backbone=False):
freeze_backbone=False,
**kwargs):
if backbone_name == 'simple':
input_image = Input(input_shape)
x = Conv2D(64, (10, 10), activation='relu',
Expand Down
22 changes: 11 additions & 11 deletions embedding_net/datagenerators.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ def __init__(self, embedding_model,
k_classes=5,
k_samples=5,
margin=0.5,
negative_selection_mode='semihard'):
negatives_selection_mode='semihard'):
super().__init__(class_files_paths=class_files_paths,
clas_names=class_names,
class_names=class_names,
input_shape=input_shape,
batch_size=batch_size,
n_batches=n_batches,
Expand All @@ -141,10 +141,10 @@ def __init__(self, embedding_model,
'hardest': self.hardest_negative,
'random_hard': self.random_hard_negative}
self.embedding_model = embedding_model
self.k_classes=k_classes,
self.k_samples=k_samples,
self.margin=margin
self.negative_selection_fn = modes[negative_selection_mode]
self.k_classes = k_classes
self.k_samples = k_samples
self.margin = margin
self.negative_selection_fn = modes[negatives_selection_mode]

def hardest_negative(self, loss_values, margin=0.5):
hard_negative = np.argmax(loss_values)
Expand All @@ -160,7 +160,6 @@ def semihard_negative(self, loss_values, margin=0.5):
return np.random.choice(semihard_negatives) if len(semihard_negatives) > 0 else None

def get_batch_triplets_mining(self):

selected_classes_idxs = np.random.choice(self.n_classes, size=self.k_classes, replace=False)
selected_classes = [self.class_names[cl] for cl in selected_classes_idxs]
selected_classes_n_elements = [self.n_samples[cl] for cl in selected_classes]
Expand Down Expand Up @@ -228,9 +227,10 @@ def __init__(self, class_files_paths,
input_shape=None,
batch_size = 32,
n_batches = 10,
augmentations=None):
augmentations=None,
**kwargs):
super().__init__(class_files_paths=class_files_paths,
clas_names=class_names,
class_names=class_names,
input_shape=input_shape,
batch_size=batch_size,
n_batches=n_batches,
Expand Down Expand Up @@ -284,7 +284,7 @@ def __init__(self, class_files_paths,
augmentations=None):

super().__init__(class_files_paths=class_files_paths,
clas_names=class_names,
class_names=class_names,
input_shape=input_shape,
batch_size=batch_size,
n_batches=n_batches,
Expand Down Expand Up @@ -346,7 +346,7 @@ def __init__(self, class_files_paths,
augmentations=None):

super().__init__(class_files_paths=class_files_paths,
clas_names=class_names,
class_names=class_names,
input_shape=input_shape,
batch_size=batch_size,
n_batches=n_batches,
Expand Down
43 changes: 21 additions & 22 deletions embedding_net/losses_and_accuracies.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import keras.backend as K
import keras
import tensorflow.keras.backend as K


def contrastive_loss(y_true, y_pred):
Expand Down Expand Up @@ -51,30 +50,30 @@ def accuracy(y_true, y_pred):
return K.mean(K.equal(y_true, K.cast(y_pred < 0.5, y_true.dtype)))


class tSNECallback(keras.callbacks.Callback):
# class tSNECallback(keras.callbacks.Callback):

def __init__(self, save_file_name='tSNE.gif'):
super(tSNECallback, self).__init__()
self.save_file_name = save_file_name
# def __init__(self, save_file_name='tSNE.gif'):
# super(tSNECallback, self).__init__()
# self.save_file_name = save_file_name

def on_train_begin(self, logs={}):
self.aucs = []
self.losses = []
# def on_train_begin(self, logs={}):
# self.aucs = []
# self.losses = []

def on_train_end(self, logs={}):
return
# def on_train_end(self, logs={}):
# return

def on_epoch_begin(self, epoch, logs={}):
return
# def on_epoch_begin(self, epoch, logs={}):
# return

def on_epoch_end(self, epoch, logs={}):
self.losses.append(logs.get('loss'))
y_pred = self.model.predict(self.model.validation_data[0])
self.aucs.append(roc_auc_score(self.model.validation_data[1], y_pred))
return
# def on_epoch_end(self, epoch, logs={}):
# self.losses.append(logs.get('loss'))
# y_pred = self.model.predict(self.model.validation_data[0])
# self.aucs.append(roc_auc_score(self.model.validation_data[1], y_pred))
# return

def on_batch_begin(self, batch, logs={}):
return
# def on_batch_begin(self, batch, logs={}):
# return

def on_batch_end(self, batch, logs={}):
return
# def on_batch_end(self, batch, logs={}):
# return
Loading

0 comments on commit 1113f03

Please sign in to comment.