|
a |
|
b/test_lstm.py |
|
|
1 |
# -*- coding: utf-8 -*- |
|
|
2 |
"""Test_LSTM.ipynb |
|
|
3 |
|
|
|
4 |
** |
|
|
5 |
* This file is part of Hybrid CNN-LSTM for COVID-19 Severity Score Prediction paper. |
|
|
6 |
* |
|
|
7 |
* Written by Ankan Ghosh Dastider and Farhan Sadik. |
|
|
8 |
* |
|
|
9 |
* Copyright (c) by the authors under Apache-2.0 License. Some rights reserved, see LICENSE. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
""" |
|
|
13 |
|
|
|
14 |
model = load_model('') #Link CNN model weight directory |
|
|
15 |
model_lstm = load_model('') #Link LSTM model weight directory |
|
|
16 |
|
|
|
17 |
data_dir_test = '' #Link test video |
|
|
18 |
test_dir = os.path.join(data_dir_test) |
|
|
19 |
|
|
|
20 |
test_data = [] |
|
|
21 |
for file in sorted(os.listdir(test_dir)): |
|
|
22 |
# print(file) |
|
|
23 |
test_data.append(['{}'.format(file)]) |
|
|
24 |
|
|
|
25 |
test_on = pd.DataFrame(test_data, columns=['File']) |
|
|
26 |
test_on.head() |
|
|
27 |
|
|
|
28 |
IMAGE_SIZE = 128 |
|
|
29 |
NUM_FRAMES = test_on.shape[0] |
|
|
30 |
|
|
|
31 |
def read_image_test(filepath): |
|
|
32 |
return cv2.imread(os.path.join(data_dir_test, filepath)) # Loading a color image is the default flag |
|
|
33 |
# Resize image to target size |
|
|
34 |
def resize_image(newimage, image_size): |
|
|
35 |
return cv2.resize(newimage.copy(), image_size, interpolation=cv2.INTER_AREA) |
|
|
36 |
|
|
|
37 |
X_Test = np.zeros((NUM_FRAMES, IMAGE_SIZE, IMAGE_SIZE, 3)) |
|
|
38 |
Y_Test = np.zeros((NUM_FRAMES, 1)) |
|
|
39 |
|
|
|
40 |
for i, file in tqdm(enumerate(test_on['File'].values)): |
|
|
41 |
newimage = read_image_test(file) |
|
|
42 |
if newimage is not None: |
|
|
43 |
X_Test[i] = resize_image(newimage, (IMAGE_SIZE, IMAGE_SIZE)) |
|
|
44 |
match = re.search('Score(\d)',file) |
|
|
45 |
score = int(match.group(1)) |
|
|
46 |
Y_Test[i] = score |
|
|
47 |
#print(file) |
|
|
48 |
#print(score) |
|
|
49 |
|
|
|
50 |
Y_Test = to_categorical(Y_Test, num_classes=4) |
|
|
51 |
# print(Y_Test) |
|
|
52 |
# Normalize the data |
|
|
53 |
X_Test = X_Test / 255. |
|
|
54 |
print('X_Test Shape: {}'.format(X_Test.shape)) |
|
|
55 |
print('Y_Test Shape: {}'.format(Y_Test.shape)) |
|
|
56 |
|
|
|
57 |
output = np.zeros((1, NUM_FRAMES, 64)) |
|
|
58 |
|
|
|
59 |
specific_layer_output = K.function([model.layers[0].input], [model.get_layer('dropout_35').output]) |
|
|
60 |
layer_output = specific_layer_output([X_Test])[0] |
|
|
61 |
#print(layer_output.shape) |
|
|
62 |
#print(layer_output) |
|
|
63 |
output[0] = layer_output |
|
|
64 |
|
|
|
65 |
print('Output from CNN Shape: {}'.format(output.shape)) |
|
|
66 |
#custom3 = model.predict(X_Test) |
|
|
67 |
#print(custom3) |
|
|
68 |
|
|
|
69 |
score_types = ['Score 0', 'Score 1', 'Score 2', 'Score 3'] |
|
|
70 |
Y_pred_test = model_lstm.predict(output) |
|
|
71 |
Y_pred_test = np.reshape(Y_pred_test, (Y_pred_test.shape[1], Y_pred_test.shape[2])) |
|
|
72 |
Y_pred_test = np.argmax(Y_pred_test, axis=1) |
|
|
73 |
|
|
|
74 |
Y_true_test = np.argmax(Y_Test, axis=1) |
|
|
75 |
|
|
|
76 |
#print(Y_pred_lstm.shape) |
|
|
77 |
#print(Y_Val_LSTM.shape) |
|
|
78 |
|
|
|
79 |
cm = confusion_matrix(Y_true_test, Y_pred_test) |
|
|
80 |
plt.figure(figsize=(12, 12)) |
|
|
81 |
ax = sns.heatmap(cm, cmap=plt.cm.Greens, annot=True, square=True, xticklabels=score_types, yticklabels=score_types) |
|
|
82 |
ax.set_ylabel('Actual', fontsize=40) |
|
|
83 |
ax.set_xlabel('Predicted', fontsize=40) |
|
|
84 |
|
|
|
85 |
final_loss, final_accuracy = model.evaluate(X_Test, Y_Test) |
|
|
86 |
print('Final Loss: {}, Final Accuracy: {}'.format(final_loss, final_accuracy)) |
|
|
87 |
|