-
Notifications
You must be signed in to change notification settings - Fork 1
/
TS_imaging.py
151 lines (122 loc) · 7.36 KB
/
TS_imaging.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
140
141
142
143
144
145
146
147
148
149
150
151
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import optimizers
from tensorflow.keras.layers import Conv2D, Dense, Flatten, Activation, MaxPooling2D, Input, Conv1D, GlobalAveragePooling1D, GlobalAveragePooling2D, BatchNormalization, Layer, Add, MaxPool2D
import pandas as pd
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report, confusion_matrix
from pyts.image import RecurrencePlot,GramianAngularField
#Defined VGG11 architecture
def VGG11(X_train, num_classes, lr = 0.0001):
model = keras.models.Sequential([
keras.layers.Conv2D(filters=64, kernel_size=(3,3), padding="same", activation='relu', input_shape=(X_train.shape[1: ])),
keras.layers.MaxPool2D(pool_size=(2,2), strides=(2,2)),
keras.layers.Conv2D(filters=128, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"),
keras.layers.MaxPool2D(pool_size=(2,2), strides=(2,2)),
keras.layers.Conv2D(filters=256, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"),
keras.layers.Conv2D(filters=256, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"),
keras.layers.MaxPool2D(pool_size=(2,2), strides=(2,2)),
keras.layers.Conv2D(filters=512, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"),
keras.layers.Conv2D(filters=512, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"),
keras.layers.MaxPool2D(pool_size=(2,2), strides=(2,2)),
keras.layers.Conv2D(filters=512, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"),
keras.layers.Conv2D(filters=512, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"),
keras.layers.MaxPool2D(pool_size=(2,2), strides=(2,2)),
keras.layers.Flatten(),
keras.layers.Dense(4096, activation='relu'),
#keras.layers.Dense(4096, activation='relu'),
keras.layers.Dense(num_classes, activation='softmax')
])
adam = optimizers.Adam(lr = lr)
model.compile(loss = 'categorical_crossentropy', optimizer = adam, metrics = ['accuracy'])
return model
#Defined alexnet architecture
def alexnet(X_train, num_classes, lr = 0.0001):
model = keras.models.Sequential([
keras.layers.Conv2D(filters=96, kernel_size=(11,11), strides=(4,4), activation='relu', input_shape=(X_train.shape[1: ])),
keras.layers.BatchNormalization(),
keras.layers.MaxPool2D(pool_size=(3,3), strides=(2,2)),
keras.layers.Conv2D(filters=256, kernel_size=(5,5), strides=(1,1), activation='relu', padding="same"),
keras.layers.BatchNormalization(),
keras.layers.MaxPool2D(pool_size=(3,3), strides=(2,2)),
keras.layers.Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"),
keras.layers.BatchNormalization(),
keras.layers.Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"),
keras.layers.BatchNormalization(),
keras.layers.Conv2D(filters=256, kernel_size=(3,3), strides=(1,1), activation='relu', padding="same"),
keras.layers.BatchNormalization(),
keras.layers.MaxPool2D(pool_size=(3,3), strides=(2,2)),
keras.layers.Flatten(),
keras.layers.Dense(4096, activation='relu'),
keras.layers.Dropout(0.5),
keras.layers.Dense(4096, activation='relu'),
keras.layers.Dropout(0.5),
keras.layers.Dense(num_classes, activation='softmax')
])
adam = optimizers.Adam(lr = lr)
model.compile(loss = 'categorical_crossentropy', optimizer = adam, metrics = ['accuracy'])
return model
def basic_cnn_functional(X_train, num_classes, lr = 0.0001):
input1 = Input(shape = X_train.shape[1: ])
cnn = Conv2D(filters = 128, kernel_size = (3,3), strides = (1,1), activation='relu', padding = 'same')(input1)
cnn1 = Conv2D(filters = 64, kernel_size = (7,7), strides = (2,2), activation='relu', padding = 'same')(cnn)
cnn2 = Conv2D(filters = 32, kernel_size = (7,7), strides = (1,1), activation='relu', padding = 'same')(cnn1)
cnn3 = Conv2D(filters = 16, kernel_size = (7,7), strides = (1,1), padding = 'same')(cnn2)
act = Activation('relu')(cnn3)
maxP = MaxPooling2D(pool_size = (2,2))(act)
# prior layer should be flattend to be connected to dense layers
Flatt = Flatten()(maxP)
# dense layer with 64 neurons
dense = Dense(64, activation = 'relu')(Flatt)
# final layer with neurons to classify the instances
output = Dense(num_classes, activation = 'softmax')(dense)
adam = optimizers.Adam(lr = lr)
model = keras.models.Model(inputs=input1, outputs=output)
model.compile(loss = 'categorical_crossentropy', optimizer = adam, metrics = ['accuracy'])
return model
def CNN_GAF():
#load raw anomaly TS data
df = pd.read_pickle("dataset_TS_bin_class.pkl")
X = df.loc[:,df.columns[:-2]].to_numpy()
#load binary labels "y" and multiclass labels Y_anom
y = df['anomaly'].to_numpy()
Y_anom = df["type"].to_numpy(dtype=np.uint8)
num_classes = np.unique(Y_anom).shape[0]
#----------Uncomment this block if you want to test recurrence plot transfomration and comment the GAF block both use pyts library (https://pyts.readthedocs.io/en/stable/index.html)----------------------
#RP = RecurrencePlot()
# X_rp = RP.fit_transform(X)
# X_rp = np.float32(X_rp)
# X_rp = X_rp.reshape(X_rp.shape[0], X_rp.shape[1], X_rp.shape[2], 1)
#---------------------------------------------------------------------
#--------------------------------GAF BLOCK-------------------------------------
gasf = GramianAngularField(image_size=300, method='difference') #'difference' 'summation' (choose depending if you want gramian summation or difference field)
X_gasf = gasf.fit_transform(X)
X_gasf = np.float32(X_gasf)
X_gasf = X_gasf.reshape(X_gasf.shape[0], X_gasf.shape[1], X_gasf.shape[2], 1)
#---------------------------------------------------------------------
y_bin = tf.keras.utils.to_categorical(y, num_classes=2)
y_multi = tf.keras.utils.to_categorical(Y_anom, num_classes=5)
#---------------------Train Test Split
X_train, X_test, y_train, y_test = train_test_split(X_gasf, y_multi, test_size=0.33) #change X_gasf for X_rp if you want to test RP transformation
#____________________MODEL SELECTION AND TRAINING___________________________
model = basic_cnn_functional(X_train, num_classes=5, lr = 0.0001) #set number of classes based on the problem and set learning rate if needed (binary, multiclass)
#There are more samples for class 0 than other, so we asign weights to compensate for that
class_weight = {0: 0.1,
1: 1.,
2: 1.,
3: 1.,
4: 1.}
history = model.fit(X_train, y_train, batch_size = 16, validation_split = 0.2, epochs = 10, verbose = 2, class_weight=class_weight)
#----------------model evaluation-----------------------------------------
results = model.evaluate(X_test, y_test, verbose = 2)
print('Test accuracy: ', results[1])
Y_pred = model.predict(X_test, verbose = 2)
y_pred = np.argmax(Y_pred, axis=1)
Y_test = np.argmax(y_test, axis=-1)
print(confusion_matrix(Y_test, y_pred))
target_names = ["noanomaly", "norecovery","recovery", "spikes", "slow"]
print(classification_report(Y_test, y_pred, target_names=target_names))
if __name__ == "__main__":
CNN_GAF()