Tensorflow-gpu保存keras模型为Savedmodel格式并载入进行推理
一、保存模型
mynetwork为keras模型对象,通过mynetwork =tf.keras.models.Sequential
(...)类似方法建立;
tf.saved_model.save(mynetwork, 'saved_model')##保存模型,说明文件夹即可
二、载入保存的模型并进行推理
import tensorflow as tf import numpy as np gpus = tf.config.experimental.list_physical_devices('GPU')##获取可用GPU for gpu in (gpus): tf.config.experimental.set_memory_growth(gpu, True)##设置显存使用方式 (x_train, y_train),(x_test, y_test) = tf.keras.datasets.mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 ##数据预处理归一化 x_train = x_train[..., tf.newaxis] ##增加一个channle维数 x_test = x_test[..., tf.newaxis] x_train = x_train.astype(np.float32)##原始数据类型为float64,因为调用了cudnn加速,需要转换为float32 x_test = x_test.astype(np.float32) train_set = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000).batch(32) test_set = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32) infer = tf.saved_model.load("saved_model")##读取模型 train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')##准确率定义 test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy') for x, y in train_set: pre_train = infer.call(x) acc_train = train_accuracy(y, pre_train) for x, y in test_set: pre_test = infer.call(x) acc_test = test_accuracy(y, pre_test) print(acc_train)##输出准确率张量 print(acc_test)
三、预测结果