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

Update torch and lightning package versions #1949

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
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ core = [
"opencv-python>=4.5.3.56",
"pandas>=1.1.0",
"timm>=0.5.4,<=0.9.16",
"lightning>2,<2.2.0",
"torch>=2,<2.2.0",
"lightning>=2.2",
"torch>=2",
"torchmetrics>=1.3.2",
"open-clip-torch>=2.23.0",
]
Expand Down
1 change: 0 additions & 1 deletion src/anomalib/models/image/cflow/lightning_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ def training_step(self, batch: dict[str, str | torch.Tensor], *args, **kwargs) -
del args, kwargs # These variables are not used.

opt = self.optimizers()
self.model.encoder.eval()

images: torch.Tensor = batch["image"]
activation = self.model.encoder(images)
Expand Down
7 changes: 5 additions & 2 deletions src/anomalib/models/image/cflow/torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Copyright (C) 2022-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0


from collections.abc import Sequence

import einops
Expand Down Expand Up @@ -58,7 +57,11 @@ def __init__(
self.dec_arch = decoder
self.pool_layers = layers

self.encoder = TimmFeatureExtractor(backbone=self.backbone, layers=self.pool_layers, pre_trained=pre_trained)
self.encoder = TimmFeatureExtractor(
backbone=self.backbone,
layers=self.pool_layers,
pre_trained=pre_trained,
).eval()
self.pool_dims = self.encoder.out_dims
self.decoders = nn.ModuleList(
[
Expand Down
3 changes: 1 addition & 2 deletions src/anomalib/models/image/csflow/lightning_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# Copyright (C) 2022-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0


import logging
from typing import Any

Expand Down Expand Up @@ -68,6 +67,7 @@ def _setup(self) -> None:
clamp=self.clamp,
num_channels=self.num_channels,
)
self.model.feature_extractor.eval()

def training_step(self, batch: dict[str, str | torch.Tensor], *args, **kwargs) -> STEP_OUTPUT:
"""Perform the training step of CS-Flow.
Expand All @@ -82,7 +82,6 @@ def training_step(self, batch: dict[str, str | torch.Tensor], *args, **kwargs) -
"""
del args, kwargs # These variables are not used.

self.model.feature_extractor.eval()
z_dist, jacobians = self.model(batch["image"])
loss = self.loss(z_dist, jacobians)
self.log("train_loss", loss.item(), on_epoch=True, prog_bar=True, logger=True)
Expand Down
3 changes: 1 addition & 2 deletions src/anomalib/models/image/csflow/torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# Copyright (C) 2022-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0


from math import exp

import numpy as np
Expand Down Expand Up @@ -562,7 +561,7 @@ def __init__(
self.input_dims = (num_channels, *input_size)
self.clamp = clamp
self.cross_conv_hidden_channels = cross_conv_hidden_channels
self.feature_extractor = MultiScaleFeatureExtractor(n_scales=3, input_size=input_size)
self.feature_extractor = MultiScaleFeatureExtractor(n_scales=3, input_size=input_size).eval()
self.graph = CrossScaleFlow(
input_dims=self.input_dims,
n_coupling_blocks=n_coupling_blocks,
Expand Down
4 changes: 1 addition & 3 deletions src/anomalib/models/image/fastflow/lightning_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# Copyright (C) 2022-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0


from typing import Any

import torch
Expand Down Expand Up @@ -52,9 +51,8 @@ def __init__(
self.conv3x3_only = conv3x3_only
self.hidden_ratio = hidden_ratio

self.loss = FastflowLoss()

self.model: FastflowModel
self.loss = FastflowLoss()

def _setup(self) -> None:
if self.input_size is None:
Expand Down
5 changes: 1 addition & 4 deletions src/anomalib/models/image/padim/lightning_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# Copyright (C) 2022-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0


import logging

import torch
Expand Down Expand Up @@ -52,7 +51,7 @@ def __init__(
pre_trained=pre_trained,
layers=layers,
n_features=n_features,
).eval()
)

self.stats: list[torch.Tensor] = []
self.embeddings: list[torch.Tensor] = []
Expand All @@ -75,9 +74,7 @@ def training_step(self, batch: dict[str, str | torch.Tensor], *args, **kwargs) -
"""
del args, kwargs # These variables are not used.

self.model.feature_extractor.eval()
embedding = self.model(batch["image"])

self.embeddings.append(embedding.cpu())

def fit(self) -> None:
Expand Down
9 changes: 6 additions & 3 deletions src/anomalib/models/image/padim/torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Copyright (C) 2022-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0


from random import sample
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -67,8 +66,8 @@ class PadimModel(nn.Module):

def __init__(
self,
layers: list[str],
backbone: str = "resnet18",
layers: list[str] = ["layer1", "layer2", "layer3"], # noqa: B006
pre_trained: bool = True,
n_features: int | None = None,
) -> None:
Expand All @@ -77,7 +76,11 @@ def __init__(

self.backbone = backbone
self.layers = layers
self.feature_extractor = TimmFeatureExtractor(backbone=self.backbone, layers=layers, pre_trained=pre_trained)
self.feature_extractor = TimmFeatureExtractor(
backbone=self.backbone,
layers=layers,
pre_trained=pre_trained,
).eval()
self.n_features_original = sum(self.feature_extractor.out_dims)
self.n_features = n_features or _N_FEATURES_DEFAULTS.get(self.backbone)
if self.n_features is None:
Expand Down
2 changes: 0 additions & 2 deletions src/anomalib/models/image/patchcore/lightning_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ def training_step(self, batch: dict[str, str | torch.Tensor], *args, **kwargs) -
"""
del args, kwargs # These variables are not used.

self.model.feature_extractor.eval()
embedding = self.model(batch["image"])

self.embeddings.append(embedding)

def fit(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/anomalib/models/image/patchcore/torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(
backbone=self.backbone,
pre_trained=pre_trained,
layers=self.layers,
)
).eval()
self.feature_pooler = torch.nn.AvgPool2d(3, 1, 1)
self.anomaly_map_generator = AnomalyMapGenerator()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# Copyright (C) 2022-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0


from collections.abc import Sequence
from typing import Any

Expand Down Expand Up @@ -50,9 +49,8 @@ def __init__(
self.layers = layers
self.anomaly_map_mode = anomaly_map_mode

self.loss = ReverseDistillationLoss()

self.model: ReverseDistillationModel
self.loss = ReverseDistillationLoss()

def _setup(self) -> None:
if self.input_size is None:
Expand Down
2 changes: 0 additions & 2 deletions src/anomalib/models/image/stfpm/lightning_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# Copyright (C) 2022-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0


from collections.abc import Sequence
from typing import Any

Expand Down Expand Up @@ -61,7 +60,6 @@ def training_step(self, batch: dict[str, str | torch.Tensor], *args, **kwargs) -
"""
del args, kwargs # These variables are not used.

self.model.teacher_model.eval()
teacher_features, student_features = self.model.forward(batch["image"])
loss = self.loss(teacher_features, student_features)
self.log("train_loss", loss.item(), on_epoch=True, prog_bar=True, logger=True)
Expand Down
3 changes: 1 addition & 2 deletions src/anomalib/models/image/stfpm/torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Copyright (C) 2022-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0


from collections.abc import Sequence
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -36,7 +35,7 @@ def __init__(
self.tiler: Tiler | None = None

self.backbone = backbone
self.teacher_model = TimmFeatureExtractor(backbone=self.backbone, pre_trained=True, layers=layers)
self.teacher_model = TimmFeatureExtractor(backbone=self.backbone, pre_trained=True, layers=layers).eval()
self.student_model = TimmFeatureExtractor(
backbone=self.backbone,
pre_trained=False,
Expand Down
12 changes: 5 additions & 7 deletions src/anomalib/models/image/uflow/feature_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ def get_feature_extractor(backbone: str, input_size: tuple[int, int] = (256, 256
msg = f"Feature extractor must be one of {AVAILABLE_EXTRACTORS}."
raise ValueError(msg)

feature_extractor: nn.Module
if backbone in ["resnet18", "wide_resnet50_2"]:
return FeatureExtractor(backbone, input_size, layers=("layer1", "layer2", "layer3"))
feature_extractor = FeatureExtractor(backbone, input_size, layers=("layer1", "layer2", "layer3")).eval()
if backbone == "mcait":
return MCaitFeatureExtractor()
msg = (
"`backbone` must be one of `[mcait, resnet18, wide_resnet50_2]`. These are the only feature extractors tested. "
"It does not mean that other feature extractors will not work."
)
raise ValueError(msg)
feature_extractor = MCaitFeatureExtractor().eval()

return feature_extractor


class FeatureExtractor(TimmFeatureExtractor):
Expand Down
1 change: 0 additions & 1 deletion src/anomalib/models/image/uflow/torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Copyright (C) 2023-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0


import torch
from FrEIA import framework as ff
from FrEIA import modules as fm
Expand Down
9 changes: 3 additions & 6 deletions tests/integration/model/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# Copyright (C) 2023-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0


from pathlib import Path

import pytest
Expand Down Expand Up @@ -144,12 +143,10 @@ def test_export(
dataset_path (Path): Root to dataset from fixture.
project_path (Path): Path to temporary project folder from fixture.
"""
if model_name == "reverse_distillation":
# TODO(ashwinvaidya17): Restore this test after fixing reverse distillation
if model_name in ("reverse_distillation", "rkde"):
# TODO(ashwinvaidya17): Restore this test after fixing the issue
# https://github.com/openvinotoolkit/anomalib/issues/1513
pytest.skip("Reverse distillation fails to convert to ONNX")
elif model_name == "rkde" and export_type == ExportType.OPENVINO:
pytest.skip("RKDE fails to convert to OpenVINO")
pytest.skip(f"{model_name} fails to convert to ONNX and OpenVINO")

model, dataset, engine = self._get_objects(
model_name=model_name,
Expand Down
Loading