Spaces:
Running
Running
File size: 12,990 Bytes
d03866e |
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
"""
This function is adapted from [M2N2] by [Dongmin Kim et al.]
Original source: [https://github.com/carrtesy/M2N2]
Reimplemented by: [EmorZz1G]
"""
from tqdm import tqdm
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
from .base import BaseDetector
from ..utils.torch_utility import EarlyStoppingTorch, get_gpu
from torch.utils.data import DataLoader
from ..utils.dataset import ReconstructDataset
from typing import Literal
# models
'''
Basic MLP implementation by:
Dongmin Kim (tommy.dm.kim@gmail.com)
'''
class Detrender(nn.Module):
def __init__(self, num_features: int, gamma=0.99):
"""
:param num_features: the number of features or channels
:param eps: a value added for numerical stability
"""
super(Detrender, self).__init__()
self.num_features = num_features
self.gamma = gamma
self.mean = nn.Parameter(torch.zeros(1, 1, self.num_features), requires_grad=False)
def forward(self, x, mode:str):
if mode == 'norm':
x = self._normalize(x)
elif mode == 'denorm':
x = self._denormalize(x)
else: raise NotImplementedError
return x
def _update_statistics(self, x):
dim2reduce = tuple(range(0, x.ndim-1))
mu = torch.mean(x, dim=dim2reduce, keepdim=True).detach()
self.mean.lerp_(mu, 1-self.gamma)
def _set_statistics(self, x:torch.Tensor):
self.mean = nn.Parameter(x, requires_grad=False)
def _normalize(self, x):
x = x - self.mean
return x
def _denormalize(self, x):
x = x + self.mean
return x
class MLP(nn.Module):
def __init__(self, seq_len, num_channels, latent_space_size, gamma, normalization="None"):
super().__init__()
self.L, self.C = seq_len, num_channels
self.encoder = Encoder(seq_len*num_channels, latent_space_size)
self.decoder = Decoder(seq_len*num_channels, latent_space_size)
self.normalization = normalization
if self.normalization == "Detrend":
self.use_normalizer = True
self.normalizer = Detrender(num_channels, gamma=gamma)
else:
self.use_normalizer = False
def forward(self, X):
B, L, C = X.shape
assert (L == self.L) and (C == self.C)
if self.use_normalizer:
X = self.normalizer(X, "norm")
z = self.encoder(X.reshape(B, L*C))
out = self.decoder(z).reshape(B, L, C)
if self.use_normalizer:
out = self.normalizer(out, "denorm")
return out
class Encoder(nn.Module):
def __init__(self, input_size, latent_space_size):
super().__init__()
self.linear1 = nn.Linear(input_size, input_size // 2)
self.relu1 = nn.ReLU()
self.linear2 = nn.Linear(input_size // 2, input_size // 4)
self.relu2 = nn.ReLU()
self.linear3 = nn.Linear(input_size // 4, latent_space_size)
self.relu3 = nn.ReLU()
def forward(self, x):
x = self.linear1(x)
x = self.relu1(x)
x = self.linear2(x)
x = self.relu2(x)
x = self.linear3(x)
x = self.relu3(x)
return x
class Decoder(nn.Module):
def __init__(self, input_size, latent_space_size):
super().__init__()
self.linear1 = nn.Linear(latent_space_size, input_size // 4)
self.relu1 = nn.ReLU()
self.linear2 = nn.Linear(input_size // 4, input_size // 2)
self.relu2 = nn.ReLU()
self.linear3 = nn.Linear(input_size // 2, input_size)
def forward(self, x):
x = self.linear1(x)
x = self.relu1(x)
x = self.linear2(x)
x = self.relu2(x)
out = self.linear3(x)
return out
class MLP_Trainer:
def __init__(
self, model, train_loader, valid_loader=None,
epochs=10, lr=1e-3, L2_reg=0, device='cuda'
):
self.model = model
self.train_loader = train_loader
self.valid_loader = valid_loader
self.device = device
self.epochs = epochs
self.optimizer = torch.optim.AdamW(
params=self.model.parameters(), lr=lr, weight_decay=L2_reg)
def train(self):
train_iterator = tqdm(
range(1, self.epochs + 1),
total=self.epochs,
desc="training epochs",
leave=True
)
if self.valid_loader is not None:
early_stop = EarlyStoppingTorch(patience=5)
for epoch in train_iterator:
train_stats = self.train_epoch()
if self.valid_loader is not None:
valid_loss = self.valid()
early_stop(valid_loss, self.model)
if early_stop.early_stop:
break
def train_epoch(self):
self.model.train()
train_summary = 0.0
for i, batch_data in enumerate(self.train_loader):
train_log = self._process_batch(batch_data)
train_summary += train_log["summary"]
train_summary /= len(self.train_loader)
return train_summary
def _process_batch(self, batch_data) -> dict:
X = batch_data[0].to(self.device)
B, L, C = X.shape
# recon
Xhat = self.model(X)
# optimize
self.optimizer.zero_grad()
loss = F.mse_loss(Xhat, X)
loss.backward()
self.optimizer.step()
out = {
"recon_loss": loss.item(),
"summary": loss.item(),
}
return out
@torch.no_grad()
def valid(self):
assert self.valid_loader is not None, 'cannot valid without any data'
self.model.eval()
for i, batch_data in enumerate(self.valid_loader):
X = batch_data[0].to(self.device)
Xhat = self.model(X)
loss = F.mse_loss(Xhat, X)
return loss.item()
class MLP_Tester:
def __init__(self, model, train_loader, test_loader, lr=1e-3, device='cuda'):
self.model = model
self.train_loader = train_loader
self.test_loader = test_loader
self.device = device
self.lr = lr
@torch.no_grad()
def offline(self, dataloader):
self.model.eval()
it = tqdm(
dataloader,
total=len(dataloader),
desc="offline inference",
leave=True
)
recon_errors = []
for i, batch_data in enumerate(it):
X = batch_data[0].to(self.device)
B, L, C = X.shape
Xhat = self.model(X)
recon_error = F.mse_loss(Xhat, X, reduction='none')
recon_error = recon_error.detach().cpu().numpy()
recon_errors.append(recon_error)
torch.cuda.empty_cache()
recon_errors = np.concatenate(recon_errors, axis=0) # (B, L, C)
anomaly_scores = recon_errors.mean(axis=2).reshape(-1) # (B, L) => (B*L,)
return anomaly_scores
def online(self, dataloader, init_thr, normalization="None"):
self.model.train()
it = tqdm(
dataloader,
total=len(dataloader),
desc="online inference",
leave=True
)
tau = init_thr
TT_optimizer = torch.optim.SGD(
[p for p in self.model.parameters()], lr=self.lr)
Xs, Xhats = [], []
preds = []
As, thrs = [], []
update_count = 0
for i, batch_data in enumerate(it):
X = batch_data[0].to(self.device)
B, L, C = X.shape
# Update of test-time statistics.
if normalization == "Detrend":
self.model.normalizer._update_statistics(X)
# inference
Xhat = self.model(X)
E = (Xhat-X)**2
A = E.mean(dim=2)
# A: (B, L, C) -> (B, L)
ytilde = (A >= tau).float()
pred = ytilde
# log model outputs
Xs.append(X)
Xhats.append(Xhat.clone().detach())
# generate anomaly scores for each time step
As.append(A.clone().detach())
preds.append(pred.clone().detach())
thrs.append(tau)
# learn new-normals
TT_optimizer.zero_grad()
mask = (ytilde == 0)
recon_loss = (A * mask).mean()
recon_loss.backward()
TT_optimizer.step()
update_count += torch.sum(mask).item()
# outputs
anoscs = torch.cat(As, axis=0).reshape(-1).detach().cpu().numpy()
print('total update count:', update_count)
return anoscs
class M2N2(BaseDetector):
def __init__(self,
win_size=12,
stride=12,
num_channels=1,
batch_size=64,
epochs=10,
latent_dim=128,
lr=1e-3,
ttlr=1e-3, # learning rate for online test-time adaptation
normalization="Detrend",
gamma=0.99,
th=0.95, # 95 percentile == 0.95 quantile
valid_size=0.2,
infer_mode='online'):
self.model_name = 'M2N2'
self.normalization = normalization
self.device = get_gpu(True)
self.model = MLP(
seq_len=win_size,
num_channels=num_channels,
latent_space_size=latent_dim,
gamma=gamma,
normalization=normalization,
).to(self.device)
self.th = th
self.lr = lr
self.ttlr = ttlr
self.epochs = epochs
self.batch_size = batch_size
self.win_size = win_size
self.stride = stride
self.valid_size = valid_size
self.infer_mode = infer_mode
def fit(self, data):
if self.valid_size is None:
self.train_loader = DataLoader(
dataset=ReconstructDataset(
data, window_size=self.win_size, stride=self.stride),
batch_size=self.batch_size,
shuffle=True
)
self.valid_loader = None
else:
data_train = data[:int((1-self.valid_size)*len(data))]
data_valid = data[int((1-self.valid_size)*len(data)):]
self.train_loader = DataLoader(
dataset=ReconstructDataset(
data_train, window_size=self.win_size, stride=self.stride),
batch_size=self.batch_size,
shuffle=True
)
self.valid_loader = DataLoader(
dataset=ReconstructDataset(
data_valid, window_size=self.win_size, stride=self.stride),
batch_size=self.batch_size,
shuffle=False,
)
self.trainer = MLP_Trainer(
model=self.model,
train_loader=self.train_loader,
valid_loader=self.valid_loader,
epochs=self.epochs,
lr=self.lr,
device=self.device
)
self.trainer.train()
self.tester = MLP_Tester(
model=self.model,
train_loader=self.train_loader,
test_loader=self.train_loader,
lr=self.ttlr,
device=self.device,
)
train_anoscs = self.tester.offline(self.train_loader)
self.tau = np.quantile(train_anoscs, self.th)
print('tau', self.tau)
def decision_function(self, data):
self.test_loader = DataLoader(
dataset=ReconstructDataset(
data, window_size=self.win_size, stride=self.stride),
batch_size=self.batch_size,
shuffle=False,
)
self.tester = MLP_Tester(
model=self.model,
train_loader=self.train_loader,
test_loader=self.test_loader,
lr=self.ttlr,
device=self.device,
)
if self.infer_mode == 'online':
anoscs = self.tester.online(
self.test_loader, self.tau,
normalization=self.normalization)
else:
anoscs = self.tester.offline(self.test_loader)
self.decision_scores_ = pad_by_edge_value(anoscs, len(data), mode='right')
return self.decision_scores_
def pad_by_edge_value(scores, target_len, mode: Literal['both', 'left', 'right']):
scores = np.array(scores).reshape(-1)
assert len(scores) <= target_len, f'the length of scores is more than target one'
print(f'origin length: {len(scores)}; target length: {target_len}')
current_len = scores.shape[0]
pad_total = max(target_len-current_len, 0)
if mode == 'left':
pad_before = pad_total
elif mode == 'right':
pad_before = 0
else:
pad_before = pad_total // 2 + 1
pad_after = pad_total - pad_before
padded_scores = np.pad(scores, (pad_before, pad_after), mode='edge')
return padded_scores |