Skip to content

Commit 03a5309

Browse files
authored
remove setup methods from base module (#2474)
* remove setup methods from base module * update setup in winclip
1 parent a532cb5 commit 03a5309

File tree

2 files changed

+7
-30
lines changed

2 files changed

+7
-30
lines changed

src/anomalib/models/components/base/anomalib_module.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import lightning.pytorch as pl
5151
import torch
5252
from lightning.pytorch import Callback
53-
from lightning.pytorch.trainer.states import TrainerFn
5453
from lightning.pytorch.utilities.types import STEP_OUTPUT
5554
from torch import nn
5655
from torchvision.transforms.v2 import Compose, Normalize, Resize
@@ -143,7 +142,6 @@ def __init__(
143142
self.visualizer = self._resolve_visualizer(visualizer)
144143

145144
self._input_size: tuple[int, int] | None = None
146-
self._is_setup = False
147145

148146
@property
149147
def name(self) -> str:
@@ -154,33 +152,6 @@ def name(self) -> str:
154152
"""
155153
return self.__class__.__name__
156154

157-
def setup(self, stage: str | None = None) -> None:
158-
"""Set up the model if not already done.
159-
160-
This method ensures the model is built by calling ``_setup()`` if needed.
161-
162-
Args:
163-
stage (str | None, optional): Current stage of training.
164-
Defaults to ``None``.
165-
"""
166-
if getattr(self, "model", None) is None or not self._is_setup:
167-
self._setup()
168-
if isinstance(stage, TrainerFn):
169-
# only set the flag if the stage is a TrainerFn, which means the
170-
# setup has been called from a trainer
171-
self._is_setup = True
172-
173-
def _setup(self) -> None:
174-
"""Set up the model architecture.
175-
176-
This method should be overridden by subclasses to build their model
177-
architecture. It is called by ``setup()`` when the model needs to be
178-
initialized.
179-
180-
This is useful when the model cannot be fully initialized in ``__init__``
181-
because it requires data-dependent parameters.
182-
"""
183-
184155
def configure_callbacks(self) -> Sequence[Callback] | Callback:
185156
"""Configure callbacks for the model.
186157

src/anomalib/models/image/winclip/lightning_model.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ def __init__(
125125
self.class_name = class_name
126126
self.k_shot = k_shot
127127
self.few_shot_source = Path(few_shot_source) if few_shot_source else None
128+
self.is_setup = False
128129

129-
def _setup(self) -> None:
130+
def setup(self, stage: str) -> None:
130131
"""Setup WinCLIP model.
131132
132133
This method:
@@ -137,6 +138,10 @@ def _setup(self) -> None:
137138
Note:
138139
This hook is called before the model is moved to the target device.
139140
"""
141+
del stage
142+
if self.is_setup:
143+
return
144+
140145
# get class name
141146
self.class_name = self._get_class_name()
142147
ref_images = None
@@ -158,6 +163,7 @@ def _setup(self) -> None:
158163

159164
# call setup to initialize the model
160165
self.model.setup(self.class_name, ref_images)
166+
self.is_setup = True
161167

162168
def _get_class_name(self) -> str:
163169
"""Get the class name used in the prompt ensemble.

0 commit comments

Comments
 (0)