-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_dsm_msr.py
224 lines (181 loc) · 6.67 KB
/
model_dsm_msr.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# -*- coding: utf-8 -*-
"""DMI_MSR.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1wa0-XtJpB5E6CA_p0abh8Y7Dqz9U_Vnd
"""
from google.colab import drive
drive.mount('/content/drive')
import numpy as np
import matplotlib.pyplot as plt
import os
from random import shuffle
from tqdm import tqdm
import cv2
def get_data(DATADIR):
CATEGORIES = ["a01", "a02", "a03", "a04", "a05", "a06", "a07", "a08", "a09", "a10", "a11", "a12", "a13", "a14", "a15", "a16", "a17", "a18", "a19", "a20"]
for img in os.listdir(DATADIR):
img_array = cv2.imread(os.path.join(DATADIR, img), cv2.IMREAD_COLOR)
plt.imshow(img_array)
plt.show()
break
IMG_SIZE = 112
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
plt.imshow(new_array, cmap = 'gray')
plt.show()
# a01_s01_e01
def action_label_img(img):
action_label = img.split('_')[-4]
if action_label[1] == '0':
i = int(action_label[2:]) - 1
label = [0] * 20
label[i] = 1
return label
else:
i = int(action_label[1:]) - 1
label = [0] * 20
label[i] = 1
return label
training_data = []
def create_training_data():
for img in tqdm(os.listdir(DATADIR)):
try:
img_array = cv2.imread(os.path.join(DATADIR, img), cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
#category = img.split('_')[-4]
#class_num = CATEGORIES.index(category)
class_num = action_label_img(img)
training_data.append([new_array, class_num])
except Exception as e:
pass
shuffle(training_data)
# np.save('train_data.npy', training_data)
create_training_data()
X = []
Y = []
for features, label in training_data:
X.append(features)
Y.append(label)
X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
Y = np.array(Y)
return X,Y
X_train,y_train = get_data("/content/drive/My Drive/Minor Project/DMI_MSR/subject_odd")
print(X_train.shape,y_train.shape)
X_test,y_test = get_data("/content/drive/My Drive/Minor Project/DMI_MSR/subject_even")
print(X_test.shape,y_test.shape)
import keras
from keras.callbacks import ModelCheckpoint, EarlyStopping
import tensorflow as tf
from tensorflow.keras.models import Sequential
from sklearn.model_selection import KFold
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
from sklearn.model_selection import train_test_split
from keras.regularizers import l1, l2, l1_l2
model = Sequential()
model.add(Conv2D(32, (7, 7), input_shape=X_train.shape[1:], activation = 'relu',kernel_regularizer = l1(0.0001)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (5, 5),activation = 'relu',kernel_regularizer = l1(0.001)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, (3, 3),activation = 'relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, (3, 3),activation = 'relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(512, (2, 2),activation = 'relu', kernel_regularizer = l2(0.1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dropout(0.3))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(20))
model.add(Activation('softmax'))
opt = keras.optimizers.Adam(learning_rate=0.0001)
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
earlystopper = EarlyStopping(monitor='val_loss', min_delta=0, patience=15, verbose=1, mode='auto', baseline=None, restore_best_weights=False)
# checkpoint = ModelCheckpoint(filepath='/checkpoint/checkpoint-{epoch:02d}-{val_loss:.2f}-{val_accuracy}.hdf5')
filepath='/content/checkpoint/checkpoint-{val_accuracy:.4f}-{val_loss:.2f}-{epoch:02d}.hdf5'
checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
# clear checkpoint folder
folder = '/content/checkpoint/'
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
history = model.fit(X_train, y_train, epochs=2000, batch_size=64, validation_data=(X_test, y_test), verbose=1,callbacks=[checkpoint])
# get model with best val accuracy
import glob
list_of_files = glob.glob('/content/checkpoint/*')
latest_file = max(list_of_files, key=os.path.getctime)
print(latest_file)
print("Model Evaluate...")
new_model = tf.keras.models.load_model(latest_file)
loss, accuracy = new_model.evaluate(X_test,y_test)
# loss, accuracy = model.evaluate(X_test,y_test)
print("loss: ",loss)
print("Tesing Accuracy: ", accuracy)
plt.plot(history.history['accuracy'][:1000])
plt.plot(history.history['val_accuracy'][:1000])
plt.title('model accuracy')
plt.ylabel('acuuracy')
plt.xlabel('epoch')
plt.legend(['train','test'], loc='upper left')
plt.show()
plt.plot(history.history['loss'][:1000])
plt.plot(history.history['val_loss'][:1000])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train','test'], loc='upper right')
plt.show()
import pandas as pd
history.history['val_loss']
hist_df = pd.DataFrame(history.history)
# save to json:
#hist_json_file = 'history.json'
#with open(hist_json_file, mode='w') as f:
# hist_df.to_json(f)
# or save to csv:
hist_csv_file = 'history.csv'
with open(hist_csv_file, mode='w') as f:
hist_df.to_csv(f)
dot_img_file = 'model_1.png'
tf.keras.utils.plot_model(model, to_file=dot_img_file, show_shapes=True)
y_pred = model.predict(X_test)
matrix = tf.math.confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))
labels = ['High-arm-wave',
'Horizontal-arm-wave',
'Hammer',
'Hand-catch',
'Forward-punch',
'High-throw',
'Draw-x',
'Draw-tick',
'Draw-circle',
'Hand-clap',
'Two-hand-wave',
'Side-boxing',
'Bend',
'Forward-kick',
'Side-kick',
'Jogging',
'Tennis-swing',
'Tennis-serve',
'Golf-swing',
'Pick-up-and-throw']
def plot(cm):
plt.imshow(cm,interpolation = 'nearest', cmap = plt.cm.Blues)
plt.title("Confusion Matrix for MSRAction3D Dataset")
plt.colorbar(fraction=0.0455)
tick_marks = np.arange(len(labels))
plt.xticks(tick_marks, labels, rotation = 90)
plt.yticks(tick_marks, labels)
matrix_norm = np.asarray(matrix)
matrix_norm = matrix_norm.astype('float') / matrix_norm.sum(axis = 1)
plt.figure(figsize=(10,10))
plot(matrix_norm)
plt.savefig('cm.png')