-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
67 lines (45 loc) · 1.56 KB
/
test.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
#Date: 2021-7-30
#Programmer: HYUN WOOK KANG
from tensorflow.keras import Sequential
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.layers import LSTM, Activation, Dense, Dropout
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.utils import to_categorical
from keras.models import load_model
from sklearn.metrics import f1_score
import numpy as np
import os
import argparse
ap=argparse.ArgumentParser()
ap.add_argument('-w', '--weights', help='name of the saved weights')
args=ap.parse_args()
args=vars(args)
import pickle
with open('test_data.pkl', 'rb') as f:
test_data=pickle.load(f)
data=test_data[:,:-1]
labels=test_data[:,-1]
le = LabelEncoder()
le.fit(labels)
labels=le.transform(labels)
testX=data=data.reshape(data.shape+(1,))
"""Pass the train data to the model"""
# model.compile(loss='categorical_crossentropy', optimizer='adam')
saved_model_path=os.path.join('./model', args['weights'])
model = load_model(saved_model_path)
"""Load saved model weights"""
import os
print('====Performance====')
predictions=model.predict(testX, verbose=0)
corr=0
#https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html
y_true=[]
y_pred=[]
for i in range(len(predictions)):
idx=np.argmax(predictions[i])
y_pred.append(idx)
y_true.append(labels[i])
if(idx==labels[i]):
corr+=1
print('acc: {0:.4f}'.format(corr/len(predictions)))
print('f1 score: {0:.4f}'.format(f1_score(y_true, y_pred, average='weighted')))