-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
100 lines (79 loc) · 3.05 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
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
# -*- coding: utf-8 -*-
# @Time : 2021/6/29 7:08 下午
# @Author : Zijie Huang
# @FileName: mnist_cnn.py
# @Email : [email protected]
# @Software: PyCharm
import tensorflow as tf
tf.compat.v1.enable_eager_execution
import struct
import numpy as np
batch_size = 256
num_classes = 10
epochs=18
# input image dimensions
img_rows, img_cols = 28, 28
def mnist_load_img(img_path):
with open(img_path, "rb") as fp:
# >是以大端模式读取,i是整型模式,读取前四位的标志位,
# unpack()函数:是将4个字节联合后再解析成一个数,(读取后指针自动后移)
msb = struct.unpack('>i', fp.read(4))[0]
# 标志位为2051,后存图像数据;标志位为2049,后存图像标签
if msb == 2051:
# 读取样本个数60000,存入cnt
cnt = struct.unpack('>i', fp.read(4))[0]
# rows:行数28;cols:列数28
rows = struct.unpack('>i', fp.read(4))[0]
cols = struct.unpack('>i', fp.read(4))[0]
imgs = np.empty((cnt, rows, cols), dtype="int")
for i in range(0, cnt):
for j in range(0, rows):
for k in range(0, cols):
# 16进制转10进制
pxl = int(hex(fp.read(1)[0]), 16)
imgs[i][j][k] = pxl
return imgs
else:
return np.empty(1)
# 读MNIST数据集的图片标签
def mnist_load_label(label_path):
with open(label_path, "rb") as fp:
msb = struct.unpack('>i', fp.read(4))[0];
if msb == 2049:
cnt = struct.unpack('>i', fp.read(4))[0];
labels = np.empty(cnt, dtype="int");
for i in range(0, cnt):
label = int(hex(fp.read(1)[0]), 16);
labels[i] = label;
return labels;
else:
return np.empty(1);
x_test = mnist_load_img("./data/wm/256/t10k-images-idx3-ubyte")
y_test = mnist_load_label("./data/wm/256/t10k-labels-idx1-ubyte")
'''x_test = mnist_load_img("./data/mnist/t10k-images-idx3-ubyte")
y_test = mnist_load_label("./data/mnist/t10k-labels-idx1-ubyte")'''
if tf.keras.backend.image_data_format() == 'channels_first':
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_test = x_test.astype('float32')
x_test /= 255
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_test = tf.keras.utils.to_categorical(y_test, num_classes)
# 载入模型
model = tf.keras.models.load_model('./model/mnist.h5')
file_path='./out/27/chaos/2/*.png'
'''for f in gb.glob(file_path):
print(f)
img=cv2.imread(f)
mg_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
img=mg_gray.reshape(-1,28,28,1)
p=model.predict(img,batch_size=1)
print(p)'''
#model.predict(img)
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])