Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update #1

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@
[![PWC](https://img.shields.io/endpoint.svg?url=https://paperswithcode.com/badge/deep-isolation-forest-for-anomaly-detection/anomaly-detection-on-nb15-dos)](https://paperswithcode.com/sota/anomaly-detection-on-nb15-dos?p=deep-isolation-forest-for-anomaly-detection)


This repository is the source code of the paper "Deep Isolation Forest for Anomaly Detection" (see full paper at https://arxiv.org/abs/2206.06602 )
Please consider citing our paper if you find this repository useful.
This repository is the source code of the paper "**Deep Isolation Forest for Anomaly Detection**" published in TKDE (April 2023). (see full paper at https://arxiv.org/abs/2206.06602 or https://ieeexplore.ieee.org/document/10108034/ )


### How to use?

```
@article{xu2022deep,
title={Deep Isolation Forest for Anomaly Detection},
author={Xu, Hongzuo and Pang, Guansong and Wang, Yijie and Wang, Yongjun},
journal={arXiv preprint arXiv:2206.06602},
year={2022}
}
```

DIF provides easy APIs like the sklearn style.
We first instantiate the model class by giving the parameters
Expand All @@ -27,7 +21,32 @@ from algorithms.dif import DIF
model_configs = {'n_ensemble':50, 'n_estimators':6}
model = DIF(**model_configs)
model.fit(X_train)
score = model.predict(X_test)
score = model.decision_function(X_test)
```

:boom:**Note:**
- DIF is also included in our `DeepOD` python library. Please see https://github.com/xuhongzuo/DeepOD
- Please also see the Zhihu blog (in Chinese) https://zhuanlan.zhihu.com/p/625557221


### Citation

Please consider citing our paper if you find this repository useful.

H. Xu, G. Pang, Y. Wang and Y. Wang, "Deep Isolation Forest for Anomaly Detection," in IEEE Transactions on Knowledge and Data Engineering, doi: 10.1109/TKDE.2023.3270293.

```
@ARTICLE{xu2023deep,
author={Xu, Hongzuo and Pang, Guansong and Wang, Yijie and Wang, Yongjun},
journal={IEEE Transactions on Knowledge and Data Engineering},
title={Deep Isolation Forest for Anomaly Detection},
year={2023},
volume={},
number={},
pages={1-14},
doi={10.1109/TKDE.2023.3270293}}

```


---
5 changes: 3 additions & 2 deletions algorithms/dif.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ def __init__(self, network_name='mlp', network_class=None,
self.network_args['be_size'] = None if self.new_ensemble_method == False else self.n_ensemble
elif network_name == 'gin':
self.network_args['activation'] = activation
elif network_name == 'dilated_conv':
self.network_args['hidden_dim'] = hidden_dim
self.network_args['n_emb'] = rep_dim
if network_class is not None:
self.Net = network_class
print(f'network additional parameters: {network_args}')
Expand Down Expand Up @@ -336,8 +339,6 @@ def set_seed(seed):
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True

@staticmethod
def single_predict_abla(x_reduced, clf):
Expand Down
69 changes: 69 additions & 0 deletions algorithms/net_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def choose_net(network_name):
return LSTMNet
elif network_name == 'gin':
return GinEncoderGraph
elif network_name == 'dilated_conv':
return DilatedConvEncoder
else:
raise NotImplementedError("")

Expand Down Expand Up @@ -229,6 +231,73 @@ def forward(self, x):
return emb


class SamePadConv(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, dilation=1, groups=1):
super().__init__()
self.receptive_field = (kernel_size - 1) * dilation + 1
padding = self.receptive_field // 2
self.conv = torch.nn.Conv1d(
in_channels, out_channels, kernel_size,
padding=padding,
dilation=dilation,
groups=groups
)
self.remove = 1 if self.receptive_field % 2 == 0 else 0

def forward(self, x):
out = self.conv(x)
if self.remove > 0:
out = out[:, :, : -self.remove]
return out


class ConvBlock(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, dilation, final=False):
super().__init__()
self.conv1 = SamePadConv(in_channels, out_channels, kernel_size, dilation=dilation)
self.conv2 = SamePadConv(out_channels, out_channels, kernel_size, dilation=dilation)
self.projector = torch.nn.Conv1d(in_channels, out_channels, 1) if in_channels != out_channels or final else None

def forward(self, x):
residual = x if self.projector is None else self.projector(x)
x = F.gelu(x)
x = self.conv1(x)
x = F.gelu(x)
x = self.conv2(x)
return x + residual


class DilatedConvEncoder(torch.nn.Module):
def __init__(self, n_features, hidden_dim=20, n_emb=20, layers=1, kernel_size=3):
super().__init__()
self.input_fc = torch.nn.Linear(n_features, hidden_dim)
channels = [hidden_dim] * layers + [n_emb]
self.net = torch.nn.Sequential(*[
ConvBlock(
channels[i - 1] if i > 0 else hidden_dim,
channels[i],
kernel_size=kernel_size,
dilation=2 ** i,
final=(i == len(channels) - 1)
)
for i in range(len(channels))
])
self.repr_dropout = torch.nn.Dropout(p=0.1)

def forward(self, x):
x = self.input_fc(x)
x = x.transpose(1, 2) # B x Ch x T
x = self.net(x)
# x = self.repr_dropout(x)
x = x.transpose(1, 2)

x = F.max_pool1d(
x.transpose(1, 2),
kernel_size=x.size(1)
).transpose(1, 2).squeeze(1)
return x


class GinEncoderGraph(torch.nn.Module):
def __init__(self, n_features, n_hidden, n_emb, n_layers,
pooling='sum', activation='relu'):
Expand Down
Loading