diff --git a/config/datasets/anomaly_detection_images.yaml b/config/datasets/anomaly_detection_images.yaml index 9c594e9d..8496966c 100644 --- a/config/datasets/anomaly_detection_images.yaml +++ b/config/datasets/anomaly_detection_images.yaml @@ -7,22 +7,19 @@ The MVTec anomaly detection dataset (MVTec AD) https://www.mvtec.com/company/research/datasets/mvtec-ad DOI: 10.1007/s11263-020-01400-4 DOI: 10.1109/CVPR.2019.00982 -" -https://www: - mvtec: - com/company/research/datasets/mvtec-ad: +https://www:mvtec:com/company/research/datasets/mvtec-ad" markup_info: 'Train images do not contain anomalies' date_time: 20.07.2024 _target_: innofw.core.datamodules.lightning_datamodules.anomaly_detection_images.ImageAnomaliesLightningDataModule train: - source: ./data/MVTEC/train + source: https://api.blackhole.ai.innopolis.university/public-datasets/anomaly_detection_mvtec/train.zip target: ./data/MVTEC/train test: - source: ./data/MVTEC/test + source: https://api.blackhole.ai.innopolis.university/public-datasets/anomaly_detection_mvtec/test.zip target: ./data/MVTEC/test infer: - source: ./data/MVTEC/infer - target: ./data/MVTEC/infer + source: https://api.blackhole.ai.innopolis.university/public-datasets/anomaly_detection_mvtec/test.zip + target: ./data/MVTEC/test diff --git a/config/experiments/anomaly-detection/KG_210724_ba083ak_anomaly_detection_images.yaml b/config/experiments/anomaly-detection/KG_210724_ba083ak_anomaly_detection_images.yaml index 609a0ce1..fe736f3c 100644 --- a/config/experiments/anomaly-detection/KG_210724_ba083ak_anomaly_detection_images.yaml +++ b/config/experiments/anomaly-detection/KG_210724_ba083ak_anomaly_detection_images.yaml @@ -14,4 +14,11 @@ task: "anomaly-detection-images" random_seed: 0 epochs: 3 batch_size: 8 -accelerator: cpu # gpu +accelerator: gpu + +wandb: + enable: True + project: anomaly_detect_mvtec + entity: "k-galliamov" + group: none + job_type: training diff --git a/innofw/core/datamodules/lightning_datamodules/anomaly_detection_images.py b/innofw/core/datamodules/lightning_datamodules/anomaly_detection_images.py index 3be594a1..29fb3464 100644 --- a/innofw/core/datamodules/lightning_datamodules/anomaly_detection_images.py +++ b/innofw/core/datamodules/lightning_datamodules/anomaly_detection_images.py @@ -44,7 +44,7 @@ def __init__( test, infer=None, batch_size: int = 2, - val_size: float = 0.2, + val_size: float = 0.5, num_workers: int = 1, augmentations=None, stage=None, @@ -58,8 +58,10 @@ def __init__( self.val_size = val_size def setup_train_test_val(self, **kwargs): - self.train_dataset = AnomaliesDataset(self.train_source, self.aug, add_labels=False) - self.test_dataset = AnomaliesDataset(self.test_source, add_labels=True) + self.train_dataset = AnomaliesDataset(self.train_source, self.get_aug(self.aug, 'train'), + add_labels=False) + self.test_dataset = AnomaliesDataset(self.test_source, self.get_aug(self.aug, 'test'), + add_labels=True) # divide into train, val, test - val is a part of test since train does not have anomalies n = len(self.test_dataset) @@ -101,7 +103,7 @@ def predict_dataloader(self): return test_dataloader def setup_infer(self): - self.predict_dataset = AnomaliesDataset(self.predict_source) + self.predict_dataset = AnomaliesDataset(self.predict_source, self.aug) def save_preds(self, preds, stage: Stages, dst_path: pathlib.Path): dst_path = pathlib.Path(dst_path) diff --git a/innofw/core/datasets/anomalies.py b/innofw/core/datasets/anomalies.py index 5946437f..861eb2be 100644 --- a/innofw/core/datasets/anomalies.py +++ b/innofw/core/datasets/anomalies.py @@ -26,11 +26,11 @@ class AnomaliesDataset(Dataset): """ def __init__(self, data_path, augmentations, add_labels=False): - self.images = list(Path(data_path + '/images').iterdir()) + self.images = list(Path(str(data_path) + '/images').iterdir()) self.add_labels = add_labels self.augmentations = augmentations if self.add_labels: - self.labels = list(Path(data_path + '/labels').iterdir()) + self.labels = list(Path(str(data_path) + '/labels').iterdir()) def __len__(self): return len(self.images) @@ -39,11 +39,12 @@ def __getitem__(self, idx): imagePath = self.images[idx] image = cv2.imread(str(imagePath)) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) - + image = torch.from_numpy(image).float() + image = torch.div(image, 255) if not self.add_labels: - return image + return self.augmentations(image) if self.augmentations is not None else image mask = cv2.imread(str(self.labels[idx]), 0) - image, mask = self.augmentations(image, mask) - mask = mask[None, :] + if self.augmentations is not None: + image, mask = self.augmentations(image=image, mask=mask) return image, mask diff --git a/innofw/core/models/torch/lightning_modules/anomaly_detection_images.py b/innofw/core/models/torch/lightning_modules/anomaly_detection_images.py index 65655aa0..3d071c7c 100644 --- a/innofw/core/models/torch/lightning_modules/anomaly_detection_images.py +++ b/innofw/core/models/torch/lightning_modules/anomaly_detection_images.py @@ -72,7 +72,7 @@ def training_step(self, x, batch_idx): loss = self.loss_fn(x, x_rec) metrics = self.compute_metrics('train', x_rec, x) self.log_metrics('train', metrics) - self.log("loss", loss, on_step=False, on_epoch=True) + self.log("train_loss", loss, on_step=False, on_epoch=True) return {"loss": loss} def validation_step(self, batch, batch_idx): @@ -82,7 +82,7 @@ def validation_step(self, batch, batch_idx): mask = self.compute_anomaly_mask(x) metrics = self.compute_metrics('val', mask, y) self.log_metrics('val', metrics) - self.log("loss", loss, on_step=False, on_epoch=True) + self.log("val_loss", loss, on_step=False, on_epoch=True) return {"loss": loss} def test_step(self, x, batch_idx): @@ -91,7 +91,7 @@ def test_step(self, x, batch_idx): mask = self.compute_anomaly_mask(x) metrics = self.compute_metrics('val', mask, y) self.log_metrics('val', metrics) - self.log("loss", loss, on_step=False, on_epoch=True) + self.log("test_loss", loss, on_step=False, on_epoch=True) return {"loss": loss} def predict_step(self, x, batch_idx, **kwargs):