Semi-supervised anomaly detection using autoencoders

Semi-supervised anomaly detection using autoencoders

  • Preface
  • Prerequisites
  • Related introduction
  • Semi-supervised Anomaly Detection using AutoEncoders
    • Project structure
    • Training model
    • test model
  • refer to



Foreword

  • Due to my limited level, errors and omissions will inevitably occur. Please criticize and correct me.
  • For more exciting content, you can click to enter the YOLO series of columns and natural language processing
    View the column or my personal homepage
  • Face disguise detection based on DETR
  • YOLOv7 trains its own data set (mask detection)
  • YOLOv8 trains its own data set (football detection)
  • YOLOv5: TensorRT accelerates YOLOv5 model inference
  • YOLOv5: IoU, GIoU, DIoU, CIoU, EIoU
  • Playing with Jetson Nano (5): TensorRT accelerates YOLOv5 target detection
  • YOLOv5: Add SE, CBAM, CoordAtt, ECA attention mechanism
  • YOLOv5: Interpretation of yolov5s.yaml configuration file and adding small target detection layer
  • Python converts COCO format instance segmentation data set to YOLO format instance segmentation data set
  • YOLOv5: Use version 7.0 to train your own instance segmentation model (instance segmentation of vehicles, pedestrians, road signs, lane lines, etc.)
  • Use Kaggle GPU resources to experience the Stable Diffusion open source project for free

Prerequisite

  • Familiar with Python

Related introduction

  • Python is a cross-platform computer programming language. It is a high-level scripting language that combines interpretation, compilation, interactivity and object-oriented. It was originally designed for writing automated scripts (shells). As the version is constantly updated and new language features are added, it is increasingly used for the development of independent, large-scale projects.
  • PyTorch is a deep learning framework that encapsulates many network and deep learning related tools for our convenience, instead of having to write them one by one. It is divided into CPU and GPU versions. Other frameworks include TensorFlow, Caffe, etc. PyTorch was launched by Facebook Artificial Intelligence Research (FAIR) based on Torch. It is a Python-based sustainable computing package that provides two advanced functions: 1. Tensor computing with powerful GPU acceleration (such as NumPy); 2. , Automatic differentiation mechanism when building deep neural networks.

Semi-supervised Anomaly Detection using AutoEncoders

  • Anomaly detection refers to the task of discovering abnormal instances from normal data. In some applications, these outliers or abnormal instances are more interesting than normal instances. Especially in the case of industrial optical inspection and infrastructure asset management, finding these defects (abnormal areas) is extremely important.
  • Traditionally, and even today, this process was performed by hand. Humans rely on the saliency of defects compared to normal textures to detect defects. However, manual inspection is slow, tedious, subjective, and susceptible to human bias. Therefore, automation of defect detection is desirable. But for defect detection, the lack of availability of a large number of anomaly instances and labeled data is a problem.
  • In this paper “Semi-supervised Anomaly Detection using AutoEncoders”, a convolutional autoencoder architecture is proposed for anomaly detection, trained only on defect-free (normal) instances. For the test image, the residual mask obtained by subtracting the original image from the autoencoder output is thresholded to obtain the defect segmentation mask. The method was tested on two datasets and achieved an impressive average F1 score of 0.885. Even though no defect images were used during training, the network learned to detect the actual shape of the defect.
  • Paper address: https://arxiv.org/abs/2001.03674
  • Official source code address: https://github.com/msminhas93/anomaly-detection-using-autoencoders
  • If you are interested, you can check the paper and official source code address.

Autoencoder network architecture implemented in Python, the code is as follows.

import torch.nn as nn
import torch.nn.functional as F
import torch


class AnomalyAE(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 48, (11, 11), stride=(1, 1), padding=5)
        self.bn1 = nn.BatchNorm2d(48)

        self.conv2 = nn.Conv2d(48, 48, (9, 9), stride=(2, 2), padding=4)
        self.bn2 = nn.BatchNorm2d(48)

        self.conv3 = nn.Conv2d(48, 48, (7, 7), stride=(2, 2), padding=3)
        self.bn3 = nn.BatchNorm2d(48)

        self.conv4 = nn.Conv2d(48, 48, (5, 5), stride=(2, 2), padding=2)
        self.bn4 = nn.BatchNorm2d(48)

        self.conv5 = nn.Conv2d(48, 48, (3, 3), stride=(2, 2), padding=1)
        self.bn5 = nn.BatchNorm2d(48)

        self.conv_tr1 = nn.ConvTranspose2d(
            48, 48, (5, 5), stride=(2, 2), padding=2, output_padding=1)
        self.bn_tr1 = nn.BatchNorm2d(48)

        self.conv_tr2 = nn.ConvTranspose2d(
            96, 48, (7, 7), stride=(2, 2), padding=3, output_padding=1)
        self.bn_tr2 = nn.BatchNorm2d(48)

        self.conv_tr3 = nn.ConvTranspose2d(
            96, 48, (9, 9), stride=(2, 2), padding=4, output_padding=1)
        self.bn_tr3 = nn.BatchNorm2d(48)

        self.conv_tr4 = nn.ConvTranspose2d(
            96, 48, (11, 11), stride=(2, 2), padding=5, output_padding=1)
        self.bn_tr4 = nn.BatchNorm2d(48)

        self.conv_output = nn.Conv2d(96, 1, (1, 1), (1, 1))
        self.bn_output = nn.BatchNorm2d(1)

    def forward(self, x):
        slope=0.2
        # print(x.shape)
        x = F.leaky_relu((self.bn1(self.conv1(x))), slope)
        # print(x.shape)
        x1 = F.leaky_relu((self.bn2(self.conv2(x))), slope)
        # print(x1.shape)
        x2 = F.leaky_relu((self.bn3(self.conv3(x1))), slope)
        # print(x2.shape)
        x3 = F.leaky_relu((self.bn4(self.conv4(x2))), slope)
        # print(x3.shape)
        x4 = F.leaky_relu((self.bn5(self.conv5(x3))), slope)
        # print(x4.shape)

        x5 = F.leaky_relu(self.bn_tr1(self.conv_tr1(x4)), slope)
        # print(x5.shape)
        x6 = F.leaky_relu(self.bn_tr2(
            self.conv_tr2(torch.cat([x5, x3], 1))), slope)
        # print(x6.shape)
        x7 = F.leaky_relu(self.bn_tr3(
            self.conv_tr3(torch.cat([x6, x2], 1))), slope)
        # print(x7.shape)
        x8 = F.leaky_relu(self.bn_tr4(
            self.conv_tr4(torch.cat([x7, x1], 1))), slope)
        # print(x8.shape)

        output = F.leaky_relu(self.bn_output(
            self.conv_output(torch.cat([x8, x], 1))), slope)
        # print(output.shape)
        return output

if __name__ == "__main__":
    x = torch.rand([16,1,512,512])
    model = AnomalyAE()
    y = model(x)
    # print(x.shape, x.dtype)
    # print(y.shape, y.dtype)

Project structure


Training model

python main.py --train_dir ./data/Train --val_dir ./data/Test --train_batch_size 1 --val_batch_size 1 --save_graph --epochs 25

Test model

python test.py

Reference

[1] Manpreet Singh Minhas, John Zelek. Semi-supervised Anomaly Detection using AutoEncoders. 2022
[2] https://github.com/msminhas93/anomaly-detection-using-autoencoders
[3] https://arxiv.org/abs/2001.03674

  • Due to my limited level, errors and omissions will inevitably occur. Please criticize and correct me.
  • For more exciting content, you can click to enter the YOLO series of columns and natural language processing
    View the column or my personal homepage
  • Face disguise detection based on DETR
  • YOLOv7 trains its own data set (mask detection)
  • YOLOv8 trains its own data set (football detection)
  • YOLOv5: TensorRT accelerates YOLOv5 model inference
  • YOLOv5: IoU, GIoU, DIoU, CIoU, EIoU
  • Playing with Jetson Nano (5): TensorRT accelerates YOLOv5 target detection
  • YOLOv5: Add SE, CBAM, CoordAtt, ECA attention mechanism
  • YOLOv5: Interpretation of yolov5s.yaml configuration file and adding small target detection layer
  • Python converts COCO format instance segmentation data set to YOLO format instance segmentation data set
  • YOLOv5: Use version 7.0 to train your own instance segmentation model (instance segmentation of vehicles, pedestrians, road signs, lane lines, etc.)
  • Use Kaggle GPU resources to experience the Stable Diffusion open source project for free