-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor refactor and addiing callback, metric and loss to experiment
- Loading branch information
Showing
7 changed files
with
232 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
/*************************************************************************** | ||
segmentation_models_trainer | ||
------------------- | ||
begin : 2020-09-21 | ||
git sha : $Format:%H$ | ||
copyright : (C) 2020 by Philipe Borba - Cartographic Engineer @ Brazilian Army | ||
email : philipeborba at gmail dot com | ||
***************************************************************************/ | ||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
**** | ||
""" | ||
import tensorflow as tf | ||
import segmentation_models as sm | ||
from dataclasses import dataclass | ||
from dataclasses_jsonschema import JsonSchemaMixin | ||
|
||
@dataclass | ||
class Loss(JsonSchemaMixin): | ||
class_name: str | ||
config: dict | ||
framework: str | ||
|
||
def __post_init__(self): | ||
if self.framework == 'sm': | ||
self.loss_obj = self.get_sm_loss(self.class_name) | ||
elif self.framework == 'tf.keras': | ||
identifier = { | ||
"class_name" : self.class_name, | ||
"config" : self.config | ||
} | ||
self.loss_obj = tf.keras.losses.get(identifier) | ||
else: | ||
raise ValueError("Loss not implemented") | ||
|
||
|
||
def get_sm_loss(self, name): | ||
if self.class_name == 'jaccard_loss': | ||
return sm.losses.JaccardLoss(**self.config) | ||
elif self.class_name == 'dice_loss': | ||
return sm.losses.DiceLoss(**self.config) | ||
elif self.class_name == 'binary_focal_loss': | ||
return sm.losses.BinaryFocalLoss(**self.config) | ||
elif self.class_name == 'categorical_focal_loss': | ||
return sm.losses.CategoricalFocalLoss(**self.config) | ||
elif self.class_name == 'binary_crossentropy': | ||
return sm.losses.BinaryCELoss(**self.config) | ||
elif self.class_name == 'categorical_crossentropy': | ||
return sm.losses.CategoricalCELoss(**self.config) | ||
elif self.class_name == 'bce_dice_loss': | ||
return sm.losses.BinaryCELoss(**self.config) + sm.losses.DiceLoss(**self.config) | ||
elif self.class_name == 'bce_jaccard_loss': | ||
return sm.losses.BinaryCELoss(**self.config) + sm.losses.JaccardLoss(**self.config) | ||
elif self.class_name == 'cce_dice_loss': | ||
return sm.losses.CategoricalCELoss(**self.config) + sm.losses.DiceLoss(**self.config) | ||
elif self.class_name == 'cce_jaccard_loss': | ||
return sm.losses.CategoricalCELoss(**self.config) + sm.losses.JaccardLoss(**self.config) | ||
elif self.class_name == 'binary_focal_dice_loss': | ||
return sm.losses.BinaryFocalLoss(**self.config) + sm.losses.DiceLoss(**self.config) | ||
elif self.class_name == 'binary_focal_jaccard_loss': | ||
return sm.losses.BinaryFocalLoss(**self.config) + sm.losses.JaccardLoss(**self.config) | ||
elif self.class_name == 'categorical_focal_dice_loss': | ||
return sm.losses.CategoricalFocalLoss(**self.config) + sm.losses.DiceLoss(**self.config) | ||
elif self.class_name == 'categorical_focal_jaccard_loss': | ||
return sm.losses.CategoricalFocalLoss(**self.config) + sm.losses.JaccardLoss(**self.config) | ||
else: | ||
raise ValueError("SM Loss not implemented") | ||
|
||
if __name__ == "__main__": | ||
import json | ||
x = Loss( | ||
class_name='bce_dice_loss', | ||
config={}, | ||
framework='sm' | ||
) | ||
print(x.to_json()) | ||
x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
/*************************************************************************** | ||
segmentation_models_trainer | ||
------------------- | ||
begin : 2020-09-21 | ||
git sha : $Format:%H$ | ||
copyright : (C) 2020 by Philipe Borba - Cartographic Engineer @ Brazilian Army | ||
email : philipeborba at gmail dot com | ||
***************************************************************************/ | ||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
**** | ||
""" | ||
import tensorflow as tf | ||
import segmentation_models as sm | ||
from typing import Any, List | ||
from dataclasses import dataclass | ||
from dataclasses_jsonschema import JsonSchemaMixin | ||
|
||
@dataclass | ||
class Metric(JsonSchemaMixin): | ||
class_name: str | ||
config: dict | ||
framework: str | ||
|
||
def __post_init__(self): | ||
if self.framework == 'sm': | ||
self.metric_obj = self.get_sm_metric(self.class_name) | ||
elif self.framework == 'tf.keras': | ||
identifier = { | ||
"class_name" : self.class_name, | ||
"config" : self.config | ||
} | ||
self.metric_obj = tf.keras.metrics.get(identifier) | ||
else: | ||
raise ValueError("Metric not implemented") | ||
|
||
def get_sm_metric(self, name): | ||
if self.class_name == 'iou_score': | ||
return sm.metrics.iou_score | ||
elif self.class_name == 'precision': | ||
return sm.metrics.precision | ||
elif self.class_name == 'recall': | ||
return sm.metrics.recall | ||
elif self.class_name == 'f1_score': | ||
return sm.metrics.f1_score | ||
elif self.class_name == 'f2_score': | ||
return sm.metrics.f2_score | ||
else: | ||
raise ValueError("SM metric not implemented") | ||
|
||
@dataclass | ||
class MetricList(JsonSchemaMixin): | ||
items: List[Metric] | ||
|
||
def get_tf_objects(self): | ||
return [ | ||
i.metric_obj for i in items | ||
] | ||
|
||
if __name__ == "__main__": | ||
import json | ||
metric_list = [ | ||
Metric( | ||
class_name=i, | ||
config={}, | ||
framework='sm' | ||
) for i in ['iou_score', 'precision', 'recall', 'f1_score', 'f2_score'] | ||
] + [ | ||
Metric( | ||
class_name=i, | ||
config={} if i != 'MeanIoU' else {'num_classes':2}, | ||
framework='tf.keras' | ||
) for i in ['LogCoshError', 'KLDivergence', 'MeanIoU'] | ||
] | ||
x=MetricList(metric_list) | ||
print(x.to_json()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.