Skip to content

Commit

Permalink
remove main function in each model
Browse files Browse the repository at this point in the history
  • Loading branch information
xuhongzuo committed Dec 1, 2022
1 parent 3504f03 commit c3a1867
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 312 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ deepod.egg-info/
dist/
build/
.idea
#data/
deepod/models/_local_test_.py
data/
**/__pycache__
5 changes: 1 addition & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ Python Deep Outlier/Anomaly Detection (DeepOD)
**DeepOD** is an open-source python framework for deep learning-based anomaly detection on multivariate data. DeepOD provides unified low-code implementation of different detection models based on PyTorch.


DeepOD includes nine popular deep outlier detection / anomaly detection algorithms (in unsupervised/weakly-supervised paradigm) for now. More baseline algorithms will be included later.
DeepOD includes ten popular deep outlier detection / anomaly detection algorithms (in unsupervised/weakly-supervised paradigm) for now. More baseline algorithms will be included later.



Installation
~~~~~~~~~~~~~~
The DeepOD framework can be installed via:

install a stable version (we have six models in this version, please clone this repository for more models)


.. code-block:: bash
Expand Down Expand Up @@ -64,7 +62,6 @@ Supported Models
DevNet, KDD, 2019, weakly-supervised, Deep Anomaly Detection with Deviation Networks
PReNet, ArXiv, 2020, weakly-supervised, Deep Weakly-supervised Anomaly Detection
Deep SAD, ICLR, 2020, weakly-supervised, Deep Semi-Supervised Anomaly Detection
~~~~~~~~~~~~~~


Usages
Expand Down
32 changes: 32 additions & 0 deletions deepod/models/_local_test_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from deepod.models import *
from sklearn.metrics import roc_auc_score
import numpy as np

if __name__ == '__main__':
import pandas as pd
import numpy as np

file = '../../data/38_thyroid.npz'
data = np.load(file, allow_pickle=True)
x, y = data['X'], data['y']
y = np.array(y, dtype=int)

anom_id = np.where(y==1)[0]
known_anom_id = np.random.choice(anom_id, 30)
y_semi = np.zeros_like(y)
y_semi[known_anom_id] = 1

clf = DevNet(device='cpu')
clf.fit(x, y_semi)

scores = clf.decision_function(x)

from sklearn.metrics import roc_auc_score
auc = roc_auc_score(y_score=scores, y_true=y)
print(auc)

clf = DeepSVDD(device='cpu')
clf.fit(x, y_semi)
scores = clf.decision_function(x)
auc = roc_auc_score(y_score=scores, y_true=y)
print(auc)
24 changes: 0 additions & 24 deletions deepod/models/anogan.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,27 +209,3 @@ def training_forward(self, batch_x, net, criterion):
def inference_forward(self, batch_x, net, criterion):
# implement in _inference
pass

if __name__ == '__main__':
import numpy as np

file = '../../data/38_thyroid.npz'
data = np.load(file, allow_pickle=True)
x, y = data['X'], data['y']
y = np.array(y, dtype=int)

anom_id = np.where(y==1)[0]
known_anom_id = np.random.choice(anom_id, 30)
y_semi = np.zeros_like(y)
y_semi[known_anom_id] = 1

clf = AnoGAN(device='cuda', prt_steps=1, epochs=100)
clf.fit(x, y_semi)

scores = clf.decision_function(x)

from sklearn.metrics import roc_auc_score

auc = roc_auc_score(y_score=scores, y_true=y)

print(auc)
26 changes: 0 additions & 26 deletions deepod/models/devnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,29 +172,3 @@ def forward(self, y_true, y_pred):
return loss

return loss


if __name__ == '__main__':
import pandas as pd
import numpy as np

file = '../../data/38_thyroid.npz'
data = np.load(file, allow_pickle=True)
x, y = data['X'], data['y']
y = np.array(y, dtype=int)

anom_id = np.where(y==1)[0]
known_anom_id = np.random.choice(anom_id, 30)
y_semi = np.zeros_like(y)
y_semi[known_anom_id] = 1

clf = DevNet(device='cpu')
clf.fit(x, y_semi)

scores = clf.decision_function(x)

from sklearn.metrics import roc_auc_score

auc = roc_auc_score(y_score=scores, y_true=y)

print(auc)
25 changes: 0 additions & 25 deletions deepod/models/dsad.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,28 +187,3 @@ def forward(self, rep, semi_targets=None, reduction=None):
return torch.sum(loss)
elif reduction == 'none':
return loss


if __name__ == '__main__':
import numpy as np

file = '../../data/38_thyroid.npz'
data = np.load(file, allow_pickle=True)
x, y = data['X'], data['y']
y = np.array(y, dtype=int)

anom_id = np.where(y == 1)[0]
known_anom_id = np.random.choice(anom_id, 30)
y_semi = np.zeros_like(y)
y_semi[known_anom_id] = 1

clf = DeepSAD(device='cpu')
clf.fit(x, y_semi)

scores = clf.decision_function(x)

from sklearn.metrics import roc_auc_score

auc = roc_auc_score(y_score=scores, y_true=y)

print(auc)
25 changes: 0 additions & 25 deletions deepod/models/dsvdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,28 +166,3 @@ def forward(self, rep, reduction=None):
return torch.sum(loss)
elif reduction == 'none':
return loss


if __name__ == '__main__':
import numpy as np

file = '../../data/38_thyroid.npz'
data = np.load(file, allow_pickle=True)
x, y = data['X'], data['y']
y = np.array(y, dtype=int)

anom_id = np.where(y==1)[0]
known_anom_id = np.random.choice(anom_id, 30)
y_semi = np.zeros_like(y)
y_semi[known_anom_id] = 1

clf = DeepSVDD(device='cpu')
clf.fit(x, y_semi)

scores = clf.decision_function(x)

from sklearn.metrics import roc_auc_score

auc = roc_auc_score(y_score=scores, y_true=y)

print(auc)
28 changes: 0 additions & 28 deletions deepod/models/goad.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,31 +192,3 @@ def weights_init(m):
torch.nn.init.eye_(m.weight)
elif classname.find('Emb') != -1:
torch.nn.init.normal(m.weight, mean=0, std=0.01)




if __name__ == '__main__':
import pandas as pd
import numpy as np

file = '../../data/38_thyroid.npz'
data = np.load(file, allow_pickle=True)
x, y = data['X'], data['y']
y = np.array(y, dtype=int)

anom_id = np.where(y==1)[0]
known_anom_id = np.random.choice(anom_id, 30)
y_semi = np.zeros_like(y)
y_semi[known_anom_id] = 1

clf = GOAD(device='cpu', epochs=1)
clf.fit(x, y_semi)

scores = clf.decision_function(x)

from sklearn.metrics import roc_auc_score

auc = roc_auc_score(y_score=scores, y_true=y)

print(auc)
44 changes: 0 additions & 44 deletions deepod/models/icl.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,47 +276,3 @@ def positive_matrix_builder(self, data):
complement_matrix = data[:, np.arange(self.all_idx.shape[0])[:, None], self.all_idx_complement]

return matrix, complement_matrix


if __name__ == '__main__':
import numpy as np
import pandas as pd

file = '/home/xuhz/dataset/1-tabular/fmnist.inlier_var.3percent.5dup/fmnist_anom7_nnormal9_id9_1.csv'
df = pd.read_csv(file)
df.replace([np.inf, -np.inf], np.nan, inplace=True)
df.fillna(method='ffill', inplace=True)
x = df.values[:, :-1]
y = np.array(df.values[:, -1], dtype=int)

norm_idx = np.where(y == 0)[0]
anom_idx = np.where(y == 1)[0]
split = int(0.5 * len(norm_idx))
train_norm_idx, test_norm_idx = norm_idx[:split], norm_idx[split:]

x_train = x[train_norm_idx]
y_train = y[train_norm_idx]

x_test = x[np.hstack([test_norm_idx, anom_idx])]
y_test = y[np.hstack([test_norm_idx, anom_idx])]
x_train = x_train / 255
x_test = x_test / 255

# file = '../../data/38_thyroid.npz'
# data_ = np.load(file, allow_pickle=True)
# x, y = data_['X'], data_['y']
# y = np.array(y, dtype=int)
# x_train = x
# x_test = x
# y_test = y

clf = ICL(device='cuda', epochs=100, hidden_dims='100', act='LeakyReLU', verbose=2)
clf.fit(x_train)

scores = clf.decision_function(x_test)

from sklearn.metrics import roc_auc_score

auc = roc_auc_score(y_score=scores, y_true=y_test)

print(auc)
27 changes: 0 additions & 27 deletions deepod/models/neutral.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,30 +191,3 @@ def forward(self, z):
return loss

return loss



if __name__ == '__main__':
import pandas as pd
import numpy as np

file = '../../data/38_thyroid.npz'
data = np.load(file, allow_pickle=True)
x, y = data['X'], data['y']
y = np.array(y, dtype=int)

anom_id = np.where(y==1)[0]
known_anom_id = np.random.choice(anom_id, 30)
y_semi = np.zeros_like(y)
y_semi[known_anom_id] = 1

clf = NeuTraL(device='cpu')
clf.fit(x, y_semi)

scores = clf.decision_function(x)

from sklearn.metrics import roc_auc_score

auc = roc_auc_score(y_score=scores, y_true=y)

print(auc)
27 changes: 0 additions & 27 deletions deepod/models/prenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,30 +201,3 @@ def batch_generation(self):
batch_y[i] = 8

return batch_x1, batch_x2, batch_y


if __name__ == '__main__':
import pandas as pd
import numpy as np

# file = '../../data/38_thyroid.npz'
file = '/home/xuhz/dataset/1-tabular/ADBench-classical/27_PageBlocks.npz'
data = np.load(file, allow_pickle=True)
x, y = data['X'], data['y']
y = np.array(y, dtype=int)

anom_id = np.where(y == 1)[0]
k_anom_id = np.random.choice(anom_id, 30, replace=False)
y_semi = np.zeros_like(y)
y_semi[k_anom_id] = 1

clf = PReNet(device='cpu', batch_size=256, epochs=10)
clf.fit(x, y_semi)

scores = clf.decision_function(x)

from sklearn.metrics import roc_auc_score

auc = roc_auc_score(y_score=scores, y_true=y)

print(auc)
26 changes: 0 additions & 26 deletions deepod/models/rca.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,29 +249,3 @@ def forward(self, x1, x2):
z2 = self.enc2(x2)
x_hat2 = self.dec2(z2)
return z1, z2, x_hat1, x_hat2


if __name__ == '__main__':
import pandas as pd
import numpy as np

file = '../../data/38_thyroid.npz'
data = np.load(file, allow_pickle=True)
x, y = data['X'], data['y']
y = np.array(y, dtype=int)

anom_id = np.where(y==1)[0]
known_anom_id = np.random.choice(anom_id, 30)
y_semi = np.zeros_like(y)
y_semi[known_anom_id] = 1

clf = RCA(epochs=10, device='cpu')
clf.fit(x, y_semi)

scores = clf.decision_function(x)

from sklearn.metrics import roc_auc_score

auc = roc_auc_score(y_score=scores, y_true=y)

print(auc)
27 changes: 0 additions & 27 deletions deepod/models/rdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,30 +138,3 @@ def forward(self, rep, rep1, x, x1):
rdp_loss = F.mse_loss(d_target, d_pred, reduction='none')

return gap_loss + rdp_loss



if __name__ == '__main__':
import pandas as pd
import numpy as np

file = '../../data/38_thyroid.npz'
data = np.load(file, allow_pickle=True)
x, y = data['X'], data['y']
y = np.array(y, dtype=int)

anom_id = np.where(y==1)[0]
known_anom_id = np.random.choice(anom_id, 30)
y_semi = np.zeros_like(y)
y_semi[known_anom_id] = 1

clf = RDP(epochs=10, device='cpu')
clf.fit(x, y_semi)

scores = clf.decision_function(x)

from sklearn.metrics import roc_auc_score

auc = roc_auc_score(y_score=scores, y_true=y)

print(auc)
Loading

0 comments on commit c3a1867

Please sign in to comment.