隐藏层的输出为512,输出层的输出10,,导入模型得到报错,神经网络的输入必须为(28,28)
,再用 tf.latest_checkpoint
和 tf.load_weights
导入模型文件,最后输出概率最大的预测结果组成 flag
,由于预测图片在压缩中有精度误差,和真实结果不同,所幸赛方给出了 hint4
,修改第4位为 2。
import tensorflow as tf
from tensorflow.keras import models,layers
from PIL import Image
import numpy as np
class model(object):
def __init__(self):
model = models.Sequential()
model.add(layers.Flatten(input_shape = (28,28,1)))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.summary()
self.model = model
class Predict(object):
def __init__(self):
latest = tf.train.latest_checkpoint('./weights')
self.cnn = model()
self.cnn.model.load_weights(latest)
def predict(self, image_path):
img = Image.open(image_path).convert('I')
img = img.resize((28,28))
img = np.reshape(img, (28, 28, 1)) / 255.
x = np.array([img])
y = np.argmax(self.cnn.model.predict(x))
print(image_path)
print(y)
if __name__ == "__main__":
p = Predict()
for i in range(0, 14):
p.predict('./flag/{}.jpg'.format(i))
``
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)