-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal_test.py
154 lines (133 loc) · 4.38 KB
/
final_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
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
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 16 09:52:29 2019
@author: wen
"""
import numpy as np
import cv2
from skimage.transform import resize
from keras.models import load_model
import pandas as pd
from sklearn.metrics import accuracy_score
from collections import Counter
#载入测试数据
path = './test_image_cnngru/'
test = pd.read_csv("test_cnngru.csv")
#保存预测结果
image = []
b = []
array = []
c2d = []
c3d = []
count = 1
#混合预测测试集,1行为采用2dcnn
for image_name in test[test.Image_name1.notnull()].Image_name1:
#导入数据
#img = Image.open(path + 'frame%d.jpg' % i).convert('L')
img = cv2.imread(path + image_name, 0)
equ = cv2.equalizeHist(img) #使图片的像素趋近于高斯分布
img = cv2.GaussianBlur(equ, (5, 5), 0)
#ret, binary = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
a = resize(img, preserve_range = True, output_shape = (112, 112, 1)).astype(int)
c = resize(img, preserve_range = True, output_shape = (224, 224, 1)).astype(int)
img = a / 255 #数据归一化
image = c / 255
b.append(img)
image = np.expand_dims(image, 0)
array.append(image)
if count% 5 == 0:
#增加一个维度
b = np.expand_dims(b, 0)
c3d.append(b)
c2d.append(array)
b = []
array = []
count += 1
# np.array(c2d).shape
#2dcnn预测
#载入模型
model = load_model('model_cnn_short_threshold.h5')
#测试模型
result1 = []
for i in c2d:
temp = []
for j in i:
pred = model.predict(j)
temp.append(np.argmax(pred))
# Counter(temp).most_common(1):返回出现频率最高的一个数
temp = Counter(temp).most_common(1)[0][0]
result1.append(temp)
#3dcnn预测
#载入模型
model = load_model('model_3dcnn.h5')
#测试模型
result = []
for i in c3d:
pred = model.predict(i)
result.append(np.argmax(pred))
#对比,如果2dCNN为1的而3dCNN对应位置不为1的,则相应位置将会变为1
for i in range(len(result)):
if result1[i] == 1 and result[i] != 1:
result[i] = 1
if result1[i] == 2 and result[i] != 2:
result[i] = 2
label = test[test.Class1.notnull()].Class1
#输出测试集的准确率
accuracy = accuracy_score(np.array(label), np.array(result))
print("The accuracy of the model in test is:", accuracy)
#混合预测整个测试集,1行为采用2dcnn
for image_name in test.Image_name:
#导入数据
#img = Image.open(path + 'frame%d.jpg' % i).convert('L')
img = cv2.imread(path + image_name, 0)
equ = cv2.equalizeHist(img) #使图片的像素趋近于高斯分布
img = cv2.GaussianBlur(equ, (5, 5), 0)
#ret, binary = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
a = resize(img, preserve_range = True, output_shape = (112, 112, 1)).astype(int)
c = resize(img, preserve_range = True, output_shape = (224, 224, 1)).astype(int)
img = a / 255 #数据归一化
image = c / 255
b.append(img)
image = np.expand_dims(image, 0)
array.append(image)
if count% 5 == 0:
#增加一个维度
b = np.expand_dims(b, 0)
c3d.append(b)
c2d.append(array)
b = []
array = []
count += 1
# np.array(c2d).shape
#2dcnn预测
#载入模型
model = load_model('model_cnn_short_threshold.h5')
#测试模型
result1 = []
for i in c2d:
temp = []
for j in i:
pred = model.predict(j)
temp.append(np.argmax(pred))
# Counter(temp).most_common(1):返回出现频率最高的一个数
temp = Counter(temp).most_common(1)[0][0]
result1.append(temp)
#3dcnn预测
#载入模型
model = load_model('model_3dcnn.h5')
#测试模型
result = []
for i in c3d:
pred = model.predict(i)
result.append(np.argmax(pred))
#对比,如果2dCNN为1的而3dCNN对应位置不为1的,则相应位置将会变为1
for i in range(len(result)):
if result1[i] == 1 and result[i] != 1:
result[i] = 1
if result1[i] == 2 and result[i] != 2:
result[i] = 2
label = test[test.Class.notnull()].Class
#输出测试集的准确率
accuracy = accuracy_score(np.array(label), np.array(result))
print("The accuracy of the model in test is:", accuracy)
# The accuracy of the model in test is: 0.9210526315789473