-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpredict.py
79 lines (60 loc) · 2.32 KB
/
predict.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
from keras.applications import MobileNetV2, VGG16, InceptionResNetV2
from keras.optimizers import SGD
from keras.models import Model
from keras.layers import GlobalAveragePooling2D, Dense, Flatten, GlobalAveragePooling2D
from keras import backend as K
from keras.engine.network import Network
from keras.datasets import fashion_mnist
from sklearn.neighbors import LocalOutlierFactor
from sklearn import metrics
from sklearn.preprocessing import MinMaxScaler
from keras.models import load_model
import tensorflow as tf
from data import kyocera_data
from utils.evaluate import caculate_acc
import csv
import numpy as np
import matplotlib.pyplot as plt
input_shape = (224, 224, 3)
data_path = 'data'
X_train_s, X_ref, y_ref, X_test_s, X_test_b ,x_test_s_path, x_test_b_path = kyocera_data(data_path)
# mobile = MobileNetV2(include_top=True, input_shape=input_shape, alpha=alpha,
# depth_multiplier=1, weights='imagenet')
# mobile = InceptionResNetV2(include_top=False, input_shape= input_shape, weights='imagenet')
mobile = VGG16(include_top=True, input_shape=input_shape, weights='imagenet')
mobile.layers.pop()
model = Model(inputs=mobile.input,outputs=mobile.layers[-1].output)
# flat = GlobalAveragePooling2D()(mobile.layers[-1].output)
# model = Model(inputs=mobile.input,outputs=flat)
model.load_weights('model/model_t_smd_300.h5')
print(X_train_s.shape)
print(X_test_s.shape)
print(X_test_b.shape)
train = model.predict(X_train_s)
test_s = model.predict(X_test_s)
test_b = model.predict(X_test_b)
train = train.reshape((len(X_train_s),-1))
print('reshape train',train.shape)
test_s = test_s.reshape((len(X_test_s),-1))
print('reshape test normal',test_s.shape)
test_b = test_b.reshape((len(X_test_b),-1))
print('reshape test abnormal',test_b.shape)
print('fit model')
ms = MinMaxScaler()
train = ms.fit_transform(train)
test_s = ms.transform(test_s)
test_b = ms.transform(test_b)
# fit the model
clf = LocalOutlierFactor(n_neighbors=5)
y_pred = clf.fit(train)
Z1 = -clf._decision_function(test_s)
Z2 = -clf._decision_function(test_b)
#ROC
y_true = np.zeros(len(test_s)+len(test_b))
y_true[len(test_s):] = 1
path = x_test_s_path + x_test_b_path
# precision, recall, f1 = caculate_acc(y_true, np.hstack((Z1,Z2)),path)
fpr, tpr, _ = metrics.roc_curve(y_true, np.hstack((Z1, Z2)))
# AUC
auc = metrics.auc(fpr, tpr)
print('auc', auc)