摘要
记录一下这道深度学习入门的wp
思路
隐藏层的输出为512,输出层的输出10,导入模型得到报错,神经网络的输入必须为(28,28)
,再用 tf.latest_checkpoint
和 tf.load_weights
导入模型文件,最后输出概率最大的预测结果组成 flag
,由于预测图片在压缩中有精度误差,和真实结果不同,所幸赛方给出了 hint4
,修改第4位为 2。
code
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
| 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/{}.webp'.format(i))
|