Skip to content

Commit

Permalink
fixed augmentations, uploaded data
Browse files Browse the repository at this point in the history
  • Loading branch information
KGallyamov committed Aug 7, 2024
1 parent f967acd commit b85ec6c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
13 changes: 5 additions & 8 deletions config/datasets/anomaly_detection_images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions innofw/core/datasets/anomalies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit b85ec6c

Please sign in to comment.