rverschuren / STPM-Augmented-for-industrial-anomaly-detection

Student-Teacher Feature Pyramid Matching for Unsupervised Anomaly Detection implemented with a UNet backbone.
Apache License 2.0
1 stars 0 forks source link

Find appropriate Data Augmentation Script and Apply it to the dataset (subset of dataset) #2

Open AntonDahl opened 1 year ago

AntonDahl commented 1 year ago

Find some scripts for dataAug here: https://github.com/AgaMiko/data-augmentation-review/blob/master/README.md#Computer-vision

rverschuren commented 1 year ago

The interest of performing data augmentation for this project is double:

One way of measuring the second point is to apply anomaly detection on another type of textile and see how it can perform.

It is important to choose the right augmentation operations to avoid creating anomalies by augmenting the data.

Some ideas to implement

If you want to run the model and output the augmentation to observe what it is doing:

Run the following command !python train_resnet.py --phase augment --category carpet --num_epochs 3

rverschuren commented 1 year ago

Using this library : https://github.com/kornia/kornia

And this example https://pytorch-lightning.readthedocs.io/en/stable/notebooks/lightning_examples/augmentation_kornia.html

rverschuren commented 1 year ago

Default

Code:

        self.data_transforms = transforms.Compose([
                        transforms.Resize((args.load_size, args.load_size), transforms.InterpolationMode.LANCZOS),
                        transforms.ToTensor(),
                        transforms.CenterCrop(args.input_size),
                        transforms.Normalize(mean=mean_train,
                                            std=std_train)])

Image: image

Default mode but without normalization and centercrop.

Code:

        self.data_transforms = transforms.Compose([
                        transforms.Resize((args.load_size, args.load_size), transforms.InterpolationMode.LANCZOS),
                        transforms.ToTensor()    ])

Image: image

rverschuren commented 1 year ago

If you want to add some augmentations.

  1. Import it by adding the class in the import line from kornia.augmentation import RandomBoxBlur, ColorJitter, Normalize [OTHER.....]

  2. Add in the sequential composition of augmentations:

    class DataAugmentation(nn.Module):
    """Module to perform data augmentation using Kornia on torch tensors."""
    
    def __init__(self, apply_color_jitter: bool = False) -> None:
        super().__init__()
        self._apply_color_jitter = apply_color_jitter
    
        self.transforms = nn.Sequential(
            RandomBoxBlur(kernel_size=(3, 3), border_type='reflect', p=0.75),
            Normalize(mean=mean_train, std=std_train)
            [ADD HERE]
        )
  3. Run to observe output files: !python train_resnet.py --phase augment --category carpet --num_epochs 3

  4. open the 2 generate files.

    "img_augment.png" "img_before_augment.png"

rverschuren commented 1 year ago

I tried to add the RandomRotation, but the problem is that we lose information on the image and a part of the image is set to black pixels. Which could be detected as an anomaly. By using the RandomAffine(degrees=45.0, scale=(1,2), padding_mode=2, p=.75), we can set the scale value and the padding_mode which allows us to keep the texture of the textile everywhere on the image.

Se the difference of RandomRotation and RandomAffine on the newt two images.

Screenshot from 2022-12-03 15-54-22

img_augment (3)

rverschuren commented 1 year ago

Now let's compare when playing with colors, contrast, brightness, ...

Using the kornia.augmentation.ColorJiggle class.

To better understand the difference, I did not normalize the data before saving the image. The two next images are

Without normalization jiggle01 With normalization img_augment (5)

In the commit I set it to 0.1

rverschuren commented 1 year ago

Adding some blur to obtain something similar to leather.

        self.transforms = nn.Sequential(
            RandomBoxBlur(kernel_size=(2,2), border_type='reflect', p=0.2),
            RandomBoxBlur(kernel_size=(3,3), border_type='reflect', p=0.2),
            RandomBoxBlur(kernel_size=(5,5), border_type='reflect', p=0.2),
            RandomBoxBlur(kernel_size=(7,7), border_type='reflect', p=0.2),
            RandomBoxBlur(kernel_size=(5,5), border_type='reflect', p=0.2),
            RandomBoxBlur(kernel_size=(15,15), border_type='reflect', p=0.1),
            RandomAffine(degrees=45.0, scale=(1,2), padding_mode=2, p=.75),
            ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.),
            #Normalize(mean=mean_train, std=std_train)
        )

img_augment (6)

img_augment (7)