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

Set transformations from the config file #990

Merged
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
17 changes: 16 additions & 1 deletion src/anomalib/data/utils/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import albumentations as A
from albumentations.pytorch import ToTensorV2
from omegaconf import DictConfig

from anomalib.data.utils.image import get_image_height_and_width

Expand Down Expand Up @@ -82,8 +83,22 @@ def get_transforms(
transforms: A.Compose

if config is not None:
if isinstance(config, DictConfig):
logger.info("Loading transforms from config File")
transforms_list = []
for key, value in config.items():
if hasattr(A, key):
transform = getattr(A, key)(**value)
logger.info(f"Transform {transform} added!")
transforms_list.append(transform)
else:
raise ValueError(f"Transformation {key} is not part of albumentations")

transforms_list.append(ToTensorV2())
transforms = A.Compose(transforms_list, additional_targets={"image": "image", "depth_image": "image"})

# load transforms from config file
if isinstance(config, str):
elif isinstance(config, str):
logger.info("Reading transforms from Albumentations config file: %s.", config)
transforms = A.load(filepath=config, data_format="yaml")
elif isinstance(config, A.Compose):
Expand Down
7 changes: 7 additions & 0 deletions src/anomalib/utils/hpo/runners.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the changes to this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you perform HPO sweeps on colab, the GPU memory isn't freed after each run and accumulates over time. To solve this, the model needs to be deleted and torch cache has to be cleared:
https://stackoverflow.com/a/57569472/11951277

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

from __future__ import annotations

import gc

import pytorch_lightning as pl
import torch
import wandb
from comet_ml import Optimizer
from omegaconf import DictConfig, ListConfig, OmegaConf
Expand Down Expand Up @@ -77,6 +80,10 @@ def sweep(self) -> None:
trainer = pl.Trainer(**config.trainer, logger=wandb_logger, callbacks=callbacks)
trainer.fit(model, datamodule=datamodule)

del model
gc.collect()
torch.cuda.empty_cache()


class CometSweep:
"""comet sweep.
Expand Down