Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HPO] Enabling ote_anomalib report score for hpopt #1099

Merged
merged 6 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion external/anomaly/ote_anomalib/callbacks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@

from .inference import AnomalyInferenceCallback
from .progress import ProgressCallback
from .score_report import ScoreReportingCallback

__all__ = ["AnomalyInferenceCallback", "ProgressCallback"]
__all__ = ["AnomalyInferenceCallback", "ProgressCallback", "ScoreReportingCallback"]
43 changes: 43 additions & 0 deletions external/anomaly/ote_anomalib/callbacks/score_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Score reporting callback"""

# Copyright (C) 2020 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.

from typing import Optional

from ote_sdk.entities.train_parameters import TrainParameters
from pytorch_lightning import Callback


class ScoreReportingCallback(Callback):
"""
Callback for reporting score.
"""

def __init__(self, parameters: Optional[TrainParameters] = None) -> None:
if parameters is not None:
self.score_reporting_callback = parameters.update_progress
else:
self.score_reporting_callback = None

def on_validation_epoch_end(self, trainer, pl_module):
"""
If score exists in trainer.logged_metrics, report the score.
"""
if self.score_reporting_callback is not None:
score = None
metric = getattr(self.score_reporting_callback, 'metric', None)
if metric in trainer.logged_metrics:
score = float(trainer.logged_metrics[metric])
self.score_reporting_callback(progress=0, score=score)
8 changes: 6 additions & 2 deletions external/anomaly/ote_anomalib/train_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from anomalib.utils.callbacks import MinMaxNormalizationCallback
from ote_anomalib import AnomalyInferenceTask
from ote_anomalib.callbacks import ProgressCallback
from ote_anomalib.callbacks import ProgressCallback, ScoreReportingCallback
from ote_anomalib.data import OTEAnomalyDataModule
from ote_anomalib.logging import get_logger
from ote_sdk.entities.datasets import DatasetEntity
Expand Down Expand Up @@ -50,7 +50,11 @@ def train(
logger.info("Training Configs '%s'", config)

datamodule = OTEAnomalyDataModule(config=config, dataset=dataset, task_type=self.task_type)
callbacks = [ProgressCallback(parameters=train_parameters), MinMaxNormalizationCallback()]
callbacks = [
ProgressCallback(parameters=train_parameters),
MinMaxNormalizationCallback(),
ScoreReportingCallback(parameters=train_parameters)
]

self.trainer = Trainer(**config.trainer, logger=False, callbacks=callbacks)
self.trainer.fit(model=self.model, datamodule=datamodule)
Expand Down