-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnomalyDetectTestMultiModel.py
139 lines (107 loc) · 4.38 KB
/
AnomalyDetectTestMultiModel.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
#python -i AnomalyDetectTestMultiModel.py -R "TrainedModels/*" -D "DataCache/Pattern_1000000.0_10_100_10_[3,10]_2_(5,10)_[1,5]_0.05_(5,15).h5"
import sys,os,argparse
import glob
# Configuration of this job
parser = argparse.ArgumentParser()
# Start by creating a new config file and changing the line below
parser.add_argument('-C', '--config',default="AnomalyDetectTestConfig.py")
parser.add_argument('-L', '--LoadModel',default=False)
parser.add_argument('-D', '--LoadData',default=False)
parser.add_argument('--gpu', dest='gpuid', default="")
parser.add_argument('--N_Inject', dest='N_Analyze', default="10")
parser.add_argument('--cpu', action="store_true")
parser.add_argument('--NoAnalysis', action="store_true")
parser.add_argument('--Test', action="store_true")
parser.add_argument('-s',"--hyperparamset", default="0")
parser.add_argument('--generator', action="store_true")
parser.add_argument('-R', '--LoadDirectory',default=False)
# Configure based on commandline flags... this really needs to be cleaned up
args = parser.parse_args()
Analyze = not args.NoAnalysis
TestMode = not args.Test
UseGPU = not args.cpu
gpuid = args.gpuid
if args.hyperparamset:
HyperParamSet = int(args.hyperparamset)
ConfigFile = args.config
useGenerator = args.generator
Train=False
LoadModel=args.LoadModel
LoadData=args.LoadData
LoadDirectory=args.LoadDirectory
ModelDirs=glob.glob(LoadDirectory)
N_Analyze=int(args.N_Analyze)
# Configuration from PBS:
if "PBS_ARRAYID" in os.environ:
HyperParamSet = int(os.environ["PBS_ARRAYID"])
if "PBS_QUEUE" in os.environ:
if "cpu" in os.environ["PBS_QUEUE"]:
UseGPU=False
if "gpu" in os.environ["PBS_QUEUE"]:
UseGPU=True
gpuid=int(os.environ["PBS_QUEUE"][3:4])
if UseGPU:
print "Using GPU",gpuid
os.environ['THEANO_FLAGS'] = "mode=FAST_RUN,device=gpu%s,floatX=float32,force_device=True" % (gpuid)
else:
print "Using CPU."
from keras.callbacks import EarlyStopping
# Process the ConfigFile
execfile(ConfigFile)
# Now put config in the current scope. Must find a prettier way.
if "Config" in dir():
for a in Config:
exec(a+"="+str(Config[a]))
# Load the Data
from RandomData import *
# Load the original sample
(Train_X, Test_X) = GeneratePatternSample(filename=LoadData,FractionTest=0.1,MaxLoad=N_Analyze/0.1)
N_Examples=min(N_Analyze,Test_X.shape[0])
N_Samples=Test_X.shape[1]
N_Inputs=Test_X.shape[2]
# Create New Patterns to Inject
(Inject_X, Inject_Test_X) = GeneratePatternSample(N_Examples,N_Inputs,N_Samples,FractionTest=0,
N_Patterns=N_Patterns,
PatternSamples=PatternSamples,
NoiseSigma=0., # No additional Noise
A_range=A_range,
f_range=f_range,
s_range=s_range,
L_range=L_range,
cache=False)
# Inject the new patterns
Injected_X=Test_X[:N_Examples]+Inject_X
# Normalize the Data... seems to be critical!
Norm=np.max(Train_X) # Use the same normalization as the training sample
Train_X=Train_X/Norm
Test_X=Test_X/Norm
Inject_X=Inject_X/Norm
Injected_X=Injected_X/Norm
# Build/Load the Models
from ModelWrapper import ModelWrapper
for ModelDir in ModelDirs:
try:
print "Loading Model From:",ModelDir
LoadModel=ModelDir
if LoadModel[-1]=="/":
LoadModel=LoadModel[:-1]
Name=os.path.basename(LoadModel)
MyModel=ModelWrapper(Name)
MyModel.InDir=LoadModel
MyModel.Load()
# Print out the Model Summary
MyModel.Model.summary()
# Compile The Model
print "Compiling Model."
MyModel.Compile()
# Analysis
import AutoEncoderAnalysis
AutoEncoderAnalysis.AnalyzeInjection([ Test_X[0:N_Analyze],
Inject_X[0:N_Analyze],
Injected_X[0:N_Analyze]],
MyModel,
basename="Injection",
directory=MyModel.OutDir+"/Analysis")
print "Output to:",MyModel.OutDir
except:
print "Failed."