Keras 模型加载

完整代码

from operator import le

import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense,Dropout,Convolution2D,MaxPooling2D,Flatten
from keras.optimizers import SGD,Adam
from  keras.regularizers import l2
from keras.models import load_model
# https://s3.amazonaws.com/img-datasets/mnist.npz
# 1、加载数据
(x_train,y_train),(x_test,y_test) = mnist.load_data("./data")

# 2、数据预处理
# 将 (60000,28,28) 转为 (60000,28,28,1);其中 -1 表示自动推断 ; 除以255.0进行归一化
x_train = x_train.reshape(-1,28,28,1)/255.0  # (60000, 784)
x_test = x_test.reshape(-1,28,28,1)/255.0
# 转为one-hot 编码
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes=10)

# 3、加载模型
model = load_model(r'model.h5')

# 4、评估模型
loss, accuracy = model.evaluate(x_test,y_test)
print('\ntest loss={},accuracy={}'.format(loss,accuracy))

loss, accuracy = model.evaluate(x_train,y_train)
print('\ntrain loss={},accuracy={}'.format(loss,accuracy))
# 5、保存模型
model.save('model.h5')

 

posted @ 2022-09-06 16:05  小白啊小白,Fighting  阅读(36)  评论(0编辑  收藏  举报