Skip to content

Commit

Permalink
Merge pull request #19 from Decathlon/hyperparameter-multilabel
Browse files Browse the repository at this point in the history
Hyperparameter multilabel
  • Loading branch information
yangobeil authored May 24, 2022
2 parents 19b565d + 9364b98 commit f1422de
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
16 changes: 9 additions & 7 deletions decavision/model_training/tfrecords_image_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def __init__(self,
self.tfrecords_folder = tfrecords_folder
self.use_TPU, self.use_GPU = utils.check_PU()
self.multilabel = multilabel
self.metric = 'accuracy'
if multilabel:
self.metric = "f1_score"
else:
self.metric = 'accuracy'
if self.use_TPU and batch_size % 8:
print(
'Batch size {} is not multiple of 8, required for TPU'.format(batch_size))
Expand Down Expand Up @@ -312,12 +315,12 @@ def _create_model(self, activation, hidden_size, dropout, l2_lambda):
predictions = tf.keras.layers.Activation(
'sigmoid', name='preds')(x) # Output activation
loss = 'binary_crossentropy'
metrics = [self.metric, training_utils.f1_score]
metrics = ["accuracy", training_utils.f1_score]
else:
predictions = tf.keras.layers.Activation(
'softmax', name='preds')(x) # Output activation
loss = 'categorical_crossentropy'
metrics = [self.metric]
metrics = ["accuracy"]

return tf.keras.Model(inputs=base_model.input, outputs=predictions, name=self.transfer_model), base_model_last_block, loss, metrics

Expand All @@ -330,7 +333,7 @@ def fit(self, save_model=None, export_model=None, patience=0,
to the pretrained model, with potentially an extra combination of Dense, Dropout and Batchnorm.
Only added layers are trained, unless there is some fine tuning, in which case a second round of
training is done with the last block of the pretrained model unfrozen. Training can be stopped if
no sufficient improvement in accuracy.
no sufficient improvement in accuracy or f1-score (in case of multilabel classification).
If one of the Efficientnet Bs is used, the model includes a layer that normalizes the pixels. This processing
step is not included in the other models so it has to be done on the data separately.
Expand Down Expand Up @@ -466,6 +469,7 @@ def hyperparameter_optimization(self, num_iterations=20, n_random_starts=10, pat
logging.StreamHandler()
]
)
logging.getLogger('googleapiclient.discovery_cache').setLevel(logging.ERROR)
# declare the hyperparameters search space
dim_epochs = skopt.space.Integer(low=1, high=6, name='epochs')
dim_hidden_size = skopt.space.Integer(
Expand Down Expand Up @@ -563,10 +567,8 @@ def fitness(epochs, hidden_size, learning_rate, learning_rate_fine_tuning, dropo

if save_results:
with open('hyperparameters_dimensions.pickle', 'wb') as f:
dill.dump(dimensions, f)
with open('hyperparameters_search.pickle', 'wb') as f:
dill.dump(search_result.x, f)
print("Hyperparameter search saved!")
print("Hyperparameter results saved!")

# build results dictionary
results_dict = {dimensions[i].name: search_result.x[i]
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setuptools.setup(
name="decavision",
version="1.4.0",
version="1.4.1",
author="Décathlon Canada",
author_email="[email protected]",
description="A package to easily train powerful image classification models using colab's free TPUs.",
Expand All @@ -16,6 +16,9 @@
in Google colab notebooks. You can find the full documentation
[here](https://decavision-doc.herokuapp.com/)
## Version 1.4.1
Fix hyperparameter optimization for multilabel
## Version 1.4.0
Added multilabel classification
Expand Down

0 comments on commit f1422de

Please sign in to comment.