Overview of AnoGAN
AnoGAN (Anomaly GAN) is a method that utilizes Generative Adversarial Network (GAN) for anomaly detection and will be particularly applicable to anomaly detection in medical imaging and quality inspection in the manufacturing industry.
AnoGAN is an anomaly detection method that learns only normal data and uses it to find anomalous data. Based on conventional GAN (Goodfellow et al., 2014), the Generator (G) and Discriminator (D) are trained to build a generative model that captures the characteristics of normal data The principle of operation is as follows.
The principle of operation is as follows
- Training GAN: Train GAN using only normal data; Generator \(G\) learns the distribution of normal data; Discriminator \(D\) learns to distinguish between real and generated data; Generator \(G\) learns the distribution of normal data; Discriminator (D) learns to distinguish between real and generated data; Generator \(G\) learns to distinguish between real and generated data.
- Anomaly score computation:
- For each input image \(x\), the corresponding latent variable \(z*\) is obtained by optimization (Latent Space Optimization).
- Using the optimized \(z*\), generate an image from Generator \(G(z*)\).
- The difference between the input image \(x\) and the generated image \(G(z*)\) is evaluated as an anomaly score.
- The higher the anomaly score, the more likely it is to be anomalous.
The anomaly score consists of the following two elements
- Reconstruction error: \(||x-G(z*)||\) pixel-level difference between the input image and the reconstructed image.
- Error in the Discriminator’s feature space: \(||D(x)-D(G(z*))||\)Difference in the Discriminator’s internal feature map.
AnoGAN problems and improvement methods are as follows
- Problems
- Optimization to find the optimal \(z*\) is time consuming (slow inference).
- May not properly capture complete anomaly data.
- Improved method: f-AnoGAN (Fast AnoGAN)
- Reduce inference time by training Encoder\(E\) in advance and obtaining \(z*\) directly.
- After training GAN, Encoder\(E\) is additionally trained to estimate the latent vector directly from the input image.
The idea of AnoGAN has been one of the key methods that have led to many recent developments in the field of anomaly detection x deep learning.
Implementation Example
A simple example implementation of AnoGAN (based on PyTorch) is shown below. This code is an example of a simple AnoGAN implementation that uses the MNIST dataset for anomaly detection.
1. install the necessary libraries
pip install torch torchvision matplotlib numpy tqdm
2. implementation of AnoGAN
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import numpy as np
import matplotlib.pyplot as plt
# Device Settings
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# hyperparameter
latent_dim = 100
image_size = 28*28
batch_size = 64
epochs = 50
lr = 0.0002
# Dataset (Normal data: only “0” in MNIST is used)
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
mnist = torchvision.datasets.MNIST(root='./data', train=True, transform=transform, download=True)
normal_data = [data for data in mnist if data[1] == 0] # Extract only the number “0
train_loader = torch.utils.data.DataLoader(normal_data, batch_size=batch_size, shuffle=True)
# **(1) Model definition for GAN**.
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.model = nn.Sequential(
nn.Linear(latent_dim, 128),
nn.ReLU(),
nn.Linear(128, 256),
nn.ReLU(),
nn.Linear(256, 512),
nn.ReLU(),
nn.Linear(512, image_size),
nn.Tanh()
)
def forward(self, z):
return self.model(z).view(-1, 1, 28, 28)
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.model = nn.Sequential(
nn.Linear(image_size, 512),
nn.LeakyReLU(0.2),
nn.Linear(512, 256),
nn.LeakyReLU(0.2),
nn.Linear(256, 128),
nn.LeakyReLU(0.2),
nn.Linear(128, 1),
nn.Sigmoid()
)
def forward(self, x):
return self.model(x.view(-1, image_size))
# **(2) Model initialization**.
G = Generator().to(device)
D = Discriminator().to(device)
# **(3) Loss function and optimization**.
criterion = nn.BCELoss()
optimizer_G = optim.Adam(G.parameters(), lr=lr, betas=(0.5, 0.999))
optimizer_D = optim.Adam(D.parameters(), lr=lr, betas=(0.5, 0.999))
# **(4) Learning GAN**.
for epoch in range(epochs):
for i, (images, _) in enumerate(train_loader):
images = images.view(-1, image_size).to(device)
batch_size = images.size(0)
# Label
real_labels = torch.ones(batch_size, 1).to(device)
fake_labels = torch.zeros(batch_size, 1).to(device)
# **Discriminator Learning**.
z = torch.randn(batch_size, latent_dim).to(device)
fake_images = G(z).detach()
loss_D = criterion(D(images), real_labels) + criterion(D(fake_images), fake_labels)
optimizer_D.zero_grad()
loss_D.backward()
optimizer_D.step()
# **Generator Learning**.
z = torch.randn(batch_size, latent_dim).to(device)
fake_images = G(z)
loss_G = criterion(D(fake_images), real_labels) # The purpose of the Generator is to generate a realistic image
optimizer_G.zero_grad()
loss_G.backward()
optimizer_G.step()
print(f"Epoch [{epoch+1}/{epochs}] Loss_D: {loss_D.item():.4f} Loss_G: {loss_G.item():.4f}")
print("Training Finished!")
# **(5) Calculation of anomaly score**.
def anomaly_score(x, G, z_dim=100, steps=500, lr=0.1):
z = torch.randn(1, z_dim, requires_grad=True, device=device)
optimizer = torch.optim.Adam([z], lr=lr)
for _ in range(steps):
fake_x = G(z)
loss = torch.mean((fake_x - x) ** 2)
optimizer.zero_grad()
loss.backward()
optimizer.step()
return loss.item()
# **(6) Detection of anomalous data**.
test_data = torchvision.datasets.MNIST(root='./data', train=False, transform=transform, download=True)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=1, shuffle=True)
G.eval()
anomaly_scores = []
for i, (img, label) in enumerate(test_loader):
img = img.to(device)
score = anomaly_score(img, G)
anomaly_scores.append((score, label.item()))
if i >= 100:
break
# **(7) Visualization of results**.
scores, labels = zip(*anomaly_scores)
plt.hist(scores, bins=30, alpha=0.7, label="Anomaly Score")
plt.xlabel("Anomaly Score")
plt.ylabel("Frequency")
plt.legend()
plt.show()
Explanation
- Data preparation: Using the MNIST dataset, train only “0” as normal data. After training, evaluate all data except “0” as abnormal data.
- Training of GAN: Generator and Discriminator are trained to learn features of normal data.
- Abnormality score calculation: Optimize the latent vector z*) for the image \(x\) and compare it with the generated image. The reconstruction error is used as the anomaly score.
- Anomaly detection: Anomaly score is low for normal data and high for anomalous data (non-“0”).
Improvement points include the following
- Speed-up by f-AnoGAN: Latent space optimization is replaced by Encoder to improve inference speed.
- CNN-based Generator / Discriminator: By using a convolutional neural network (CNN) instead of the MLP described above, image features are captured more appropriately.
Application Examples
AnoGAN (Anomaly GAN) has been applied in a wide range of fields, including medical imaging, anomaly detection in manufacturing, cyber security, and financial transaction monitoring. Specific examples of applications are described below.
1. medical image diagnosis
- Application fields: MRI/CT scans, X-rays, fundus images, etc.
- Objective: Early detection of diseases (detection of tumors and abnormal areas)
- Application example: Detection of abnormalities in brain MRI
- Data: Only normal brain MRI images are learned
- Process:
- Learning from normal brain images with GAN
- Input abnormal brain images and GAN reconstructs them
- The area with the highest reconstruction error is judged as the abnormal area
- Result:
- Normal areas are reconstructed with low error
- Tumors and lesions cannot be reconstructed by GAN and the error is high
- Advantages
- Unsupervised learning, so no need to label abnormal data
- Reduces burden on physicians by assisting diagnosis
- Contributes to early detection (detects even small abnormalities)
- Reference: f-AnoGAN (fast version of AnoGAN) is used for medical diagnosis.
- Paper: “Unsupervised Anomaly Detection with Generative Adversarial Networks to Guide Marker Discovery” (Schlegl et al., 2017)
2. anomaly detection in the manufacturing industry
- Application fields: quality inspection of automobiles, semiconductors, food, clothing, etc.
- Objective: automation of visual inspection (detection of flaws, defects, and abnormal patterns)
- Application example: Semiconductor wafer defect detection
- Data: Learning only normal wafer images
- Process:
- Learns normal patterns and builds a generative model
- Compare with actual wafer images
- Identify abnormal areas
- Result:
- Automatic detection of minute scratches and contaminations that are difficult for the human eye to notice
- Automatically detects minute scratches and contaminants that are difficult to notice with the human eye
- Improved inspection accuracy, cost reduction, and work efficiency
- Advantages
- Capable of real-time anomaly detection
- Automates quality assurance (reduces manual inspections)
- Accelerates visual inspection (even for complex products)
- Reference: Siemens, Sony, and others use GANs for anomaly detection on production lines
3. cyber security
- Application fields: network anomaly detection, malware detection, unauthorized access monitoring
- Objective: Classify normal and abnormal traffic
- Application example: Detection of DDoS attacks
- Data: Learning normal communication logs
- Processes:
- Learn normal network patterns
- Evaluate new traffic data with GAN
- If anomaly score is high, determine possible attack
- Result:
- Detection of zero-day attacks (unknown attacks)
- Visualize anomalous accesses and suspicious activity by bots
- Advantages
- Real-time monitoring possible (immediate detection of network traffic anomalies)
- Can be combined with existing IDS (Intrusion Detection System)
- Reference: Google and Cloudflare apply GAN as part of their anomaly detection model
4. anomaly detection in financial transactions
- Application fields: Detection of fraudulent transactions (credit card fraud, insider trading)
- Objective: Discriminate between normal transaction patterns and anomalous transactions
- Application example: Detection of fraudulent use of credit cards
- Data: Learning from normal transaction history
- Process:
- Learn normal transaction patterns
- Calculate anomaly scores for new transaction data
- Flag high-scoring transactions as possible fraud
- Results
- More accurate than traditional rule-based detection
- Detects anomalous patterns in real time
- Advantage
- More accurate than traditional methods (even for newer fraud methods)
- Reduces false positives
- Allows real-time monitoring
- Reference: PayPal and VISA are researching fraud detection using GAN
5. anomaly detection in smart cities
- Application fields: Surveillance camera anomaly detection, traffic accident prediction
- Objective: Safety monitoring of cities, detection of anomalous behavior
- Application example: anomaly detection of security cameras
- Data: Learning from normal pedestrian and vehicle videos
- Process: Learning normal motion
- Process
- Learns normal movement
- Detects suspicious behavior (falls, fights, sudden movements) as abnormal
- Results:
- Contribution to the prevention of incidents and accidents
- Improved efficiency of surveillance operations (less manpower)
- Advanrage
- Early detection of accidents and crimes
- Improved urban safety through behavior analysis
- Reference: The City of London, U.K., has introduced AI-based security camera monitoring.
reference book
Reference books related to AnoGAN and anomaly detection are listed below.
1. basics of deep learning and anomaly detection
“
Explains basic concepts of anomaly detection, statistical methods, and anomaly detection techniques based on machine learning.
Useful as prerequisite knowledge for GAN-based anomaly detection methods.
Basics of GANs (adversarial generative networks)
GANs in Action: Deep learning with Generative Adversarial Networks
2019
Author(s): Jakub Langr, Vladimir Bok
Explains the basic concepts and applications of GANs.
It provides knowledge of DCGANs, WGANs, etc., which are the foundation of AnoGANs.
“
GANs Applied to Anomaly Detection
Anomaly detection methods such as AnoGAN, f-AnoGAN, and VAE-GAN are explained in detail.
Also introduces specific examples of anomaly detection in the medical and manufacturing industries.
Deep Learning for Anomaly Detection: A Survey
2022
Authors: Raghavendra Chalapathy, Sanjay Chawla
[isbn: 978-3031019516]
A comprehensive description of anomaly detection techniques using deep learning.
Describes methods for applying GANs, autoencoders, VAEs, etc. to anomaly detection.
Anomaly Detection with Machine Learning.
In addition to GAN, anomaly detection methods using LSTM and Transformer are also explained.
Learn real industrial applications (finance, fraud detection, medicine, etc.).
コメント