-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfusion.py
149 lines (85 loc) · 2.82 KB
/
confusion.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
# This file just generates a confusion matrix for the normal model. Just helpful to see how the model performs across
# different stats for false positive and false negatives
import numpy as np
from keras.models import Sequential, Model
from keras.layers import Dense, Conv2D, Conv3D, Flatten, Dropout, MaxPooling2D, MaxPooling3D, Activation
from keras import optimizers
from keras.preprocessing.image import array_to_img, img_to_array, load_img
import os
import csv
import cv2
from keras.callbacks import ModelCheckpoint
import pickle as pk
from sklearn.metrics import confusion_matrix
width = 100
height = 100
frames_input = 11
np.set_printoptions(precision=4, suppress=True)
# np.random.seed(1730)
counter = 0
X = []
y = []
for (dirpath, dirname, filenames) in os.walk("data-normal-v1"):
if len(filenames) > 1:
for filename in filenames:
# print(dirpath)
#train or validation
typ = dirpath.split("\\")[-2]
#attempt or normal
clas = dirpath.split("\\")[-1]
if filename.endswith(".npy") and typ == "train":
frame = np.load(dirpath + "\\" + filename)
X.append(frame)
if clas == "attempt":
y.append([1,0])
else:
y.append([0,1])
X.append(np.fliplr(frame))
if clas == "attempt":
y.append([1,0])
else:
y.append([0,1])
counter += 2
X = np.asarray(X)
X = X /255.0
print(X.shape)
X.reshape(counter, width, height, frames_input, 1)
X = np.expand_dims(X, axis=4)
print(X.shape)
y = np.asarray(y)
#this shuffles up the data, necessary bc when we do validation_split, it just picks consecutive data items
assert(len(X) == len(y))
p = np.random.permutation(len(X))
X, y = X[p], y[p]
data = [X, y]
# data = np.array(data)
# np.save("video-class-data-x.npy", X)
# np.save("video-class-data-y.npy", y)
model = Sequential()
model.add(Conv3D(8, (2,2,2), input_shape=(frames_input, width, height, 1)))
model.add(Activation('relu'))
# model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))
model.add(Conv3D(4, (4,4,4)))
model.add(Activation('relu'))
# model.add(MaxPooling3D(pool_size=(2, 2, 2)))
# model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(32))
model.add(Activation('relu'))
model.add(Dense(16))
model.add(Activation('relu'))
model.add(Dense(2))
model.add(Activation('softmax'))
model.load_weights("weights/normal-0.84.hdf5")
# sgd = optimizers.SGD(lr=100)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# checkpoint = ModelCheckpoint("weights-{epoch:02d}-{val_acc:.2f}.hdf5", monitor='val_acc', verbose=1, save_best_only=True, mode='max')
# callbacks_list = [checkpoint]
y_hat = model.predict(X)
y_hat = np.argmax(y_hat, axis=1)
y = np.argmax(y, axis = 1)
stats = confusion_matrix(y, y_hat)
print(stats)
print(stats.ravel())
error = model.evaluate(x= X, y = y, batch_size=5)
print(error)