-
Notifications
You must be signed in to change notification settings - Fork 0
/
anomaly_detection.py
153 lines (123 loc) · 4.04 KB
/
anomaly_detection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision import datasets
import torch.optim as optim
from torchvision.transforms import transforms
from torchvision.utils import save_image
import numpy as np
import matplotlib.pyplot as plt
lr = 0.001
batch_size = 100
epochs = 10
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
'''
Step 1:
'''
# MNIST dataset
dataset = datasets.MNIST(root='./mnist_data/',
train=True,
transform=transforms.ToTensor(),
download=True)
train_dataset, validation_dataset = torch.utils.data.random_split(dataset, [50000, 10000])
test_dataset = datasets.MNIST(root='./mnist_data/',
train=False,
transform=transforms.ToTensor())
# KMNIST dataset, only need test dataset
anomaly_dataset = datasets.KMNIST(root='./kmnist_data/',
train=False,
transform=transforms.ToTensor(),
download=True)
# print(len(train_dataset)) # 50000
# print(len(validation_dataset)) # 10000
# print(len(test_dataset)) # 10000
# print(len(anomaly_dataset)) # 10000
'''
Step 2: AutoEncoder
'''
# Define Encoder
class Encoder(nn.Module):
def __init__(self):
super(Encoder, self).__init__()
self.fc1 = nn.Linear(784, 256)
self.fc2 = nn.Linear(256, 128)
self.fc3 = nn.Linear(128, 32)
def forward(self, x):
x = x.view(x.size(0), -1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
z = F.relu(self.fc3(x))
return z
# Define Decoder
class Decoder(nn.Module):
def __init__(self):
super(Decoder, self).__init__()
self.fc1 = nn.Linear(32, 128)
self.fc2 = nn.Linear(128, 256)
self.fc3 = nn.Linear(256, 784)
def forward(self, z):
z = F.relu(self.fc1(z))
z = F.relu(self.fc2(z))
x = F.sigmoid(self.fc3(z)) # to make output's pixels are 0~1
x = x.view(x.size(0), 1, 28, 28)
return x
'''
Step 3: Instantiate model & define loss and optimizer
'''
enc = Encoder().to(device)
dec = Decoder().to(device)
loss_function = nn.MSELoss()
optimizer = optim.Adam(list(enc.parameters()) + list(dec.parameters()), lr=lr)
'''
Step 4: Training
'''
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
train_loss_list = []
import time
start = time.time()
for epoch in range(epochs) :
print("{}th epoch starting.".format(epoch))
enc.train()
dec.train()
for batch, (images, _) in enumerate(train_loader) :
images = images.to(device)
z = enc(images)
reconstructed_images = dec(z)
optimizer.zero_grad()
train_loss = loss_function(images, reconstructed_images)
train_loss.backward()
train_loss_list.append(train_loss.item())
optimizer.step()
print(f"[Epoch {epoch:3d}] Processing batch #{batch:3d} reconstruction loss: {train_loss.item():.6f}", end='\r')
end = time.time()
print("Time ellapsed in training is: {}".format(end - start))
# plotting train loss
plt.plot(range(1,len(train_loss_list)+1), train_loss_list, 'r', label='Training loss')
plt.title('Training loss')
plt.xlabel('Iterations')
plt.ylabel('Loss')
plt.legend()
plt.savefig('loss.png')
enc.eval()
dec.eval()
'''
Step 5: Calculate standard deviation by using validation set
'''
validation_loader = torch.utils.data.DataLoader(dataset=validation_dataset, batch_size=batch_size)
for images, _ in validation_loader:
pass
threshold = mean + 3 * std
print("threshold: ", threshold)
'''
Step 6: Anomaly detection (mnist)
'''
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size)
for images, _ in test_loader:
pass
'''
Step 7: Anomaly detection (kmnist)
'''
anomaly_loader = torch.utils.data.DataLoader(dataset=anomaly_dataset, batch_size=batch_size)
for images, _ in anomaly_loader:
pass