1. save/load weights
特点:
最干净,最轻量级,只保存权重和偏置参数,不会保存整个网络的结构,所以在后期恢复模型之前,必须手动创建和之前模型一模一样的模型,以保证权重和偏置的维度和保存之前的相同。
保存:
# path可以为:path + filename|path + filename.ckpt|filename|filename.ckpt默认保存到当前路径
model.save_weights(path)
加载:
# 创建和原来相同模型
model = tf.keras.Sequential(
[tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(10)]
)
# 模型编译
model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss=tf.losses.CategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
# 加载文件
# path可以为:path + filename.ckpt|filename.ckpt默认保存到当前路径,必须给出完整的文件名
model.load_weights(path)
示例:
import tensorflow as tf
(x, y), (x_val, y_val) = tf.keras.datasets.mnist.load_data()
print(x.shape, y.shape)
train_db = tf.data.Dataset.from_tensor_slices((x, y))
test_db = tf.data.Dataset.from_tensor_slices((x_val, y_val))
def processing(x, y):
x = tf.cast(x, dtype=tf.float32)/255.
x = tf.reshape(x, [-1, 28*28])
x = tf.squeeze(x)
y = tf.cast(y, dtype=tf.int32)
y = tf.one_hot(y, depth=10)
return x, y
batcs = 128
train_db = train_db.map(processing).shuffle(10000).batch(batcs)
test_db = test_db.map(processing).batch(batcs)
sample = next(iter(train_db))
print(sample[0].shape, sample[1].shape)
network = tf.keras.Sequential(
[tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(10)]
)
# network.build(input_shape=[None, 28*28])
network.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss=tf.losses.CategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
network.fit(train_db, epochs=10, validation_data=test_db)
# 保存并删除原来的网络
network.save_weights('save_weight/save_weight.ckpt')
print("已经保存网络!")
del network
# 预测代码
model = tf.keras.Sequential(
[tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(10)]
)
model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss=tf.losses.CategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
model.load_weights('save_weight/save_weight.ckpt')
sample = next(iter(train_db))
xx = sample[0]
yy = sample[1]
yy = tf.argmax(yy, axis=1)
pred = model.predict(xx)
pred = tf.argmax(pred, axis=1)
print(pred == yy)
2. save/load entire model
特点:
这种方法效率低,简单粗暴,会将网络的结构,权重和优化器的状态等参数全部保存下来,后期恢复的时候就没必要创建新的网络了。
保存:
# path可以为:path|path + filename.h5 默认在当前路径新建文件夹
model.save(path)
加载:
# path可以为:path(需保存时也只有路径)|path + filename.h5
model = tf.keras.models.load_model(path)
示例:
import tensorflow as tf
(x, y), (x_val, y_val) = tf.keras.datasets.mnist.load_data()
print(x.shape, y.shape)
train_db = tf.data.Dataset.from_tensor_slices((x, y))
test_db = tf.data.Dataset.from_tensor_slices((x_val, y_val))
def processing(x, y):
x = tf.cast(x, dtype=tf.float32)/255.
x = tf.reshape(x, [-1, 28*28])
x = tf.squeeze(x)
y = tf.cast(y, dtype=tf.int32)
y = tf.one_hot(y, depth=10)
return x, y
batcs = 128
train_db = train_db.map(processing).shuffle(10000).batch(batcs)
test_db = test_db.map(processing).batch(batcs)
sample = next(iter(train_db))
print(sample[0].shape, sample[1].shape)
network = tf.keras.Sequential(
[tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(10)]
)
# network.build(input_shape=[None, 28*28])
network.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss=tf.losses.CategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
network.fit(train_db, epochs=1, validation_data=test_db)
# 保存并删除原来的网络
network.save(r'model/lzf_test.h5')
print("已经保存网络!")
del network
# 预测代码
model = tf.keras.models.load_model(r'model/lzf_test.h5')
sample = next(iter(train_db))
xx = sample[0]
yy = sample[1]
yy = tf.argmax(yy, axis=1)
pred = model.predict(xx)
pred = tf.argmax(pred, axis=1)
print(pred == yy)
3. save_model
特点:
这种保存环境主要用于工业环境的部署,可以被多种语言引用。
保存:
tf.saved_model.save(model, path)
加载:
imported = tf.saved_model.load(path)
func = imported.signatures["serving_default"] # 加载进来了一个函数,并不是网络
func(x)
示例:
import tensorflow as tf
(x, y), (x_val, y_val) = tf.keras.datasets.mnist.load_data()
print(x.shape, y.shape)
train_db = tf.data.Dataset.from_tensor_slices((x, y))
test_db = tf.data.Dataset.from_tensor_slices((x_val, y_val))
def processing(x, y):
x = tf.cast(x, dtype=tf.float32)/255.
x = tf.reshape(x, [-1, 28*28])
x = tf.squeeze(x)
y = tf.cast(y, dtype=tf.int32)
y = tf.one_hot(y, depth=10)
return x, y
batcs = 128
train_db = train_db.map(processing).shuffle(10000).batch(batcs)
test_db = test_db.map(processing).batch(batcs)
sample = next(iter(train_db))
print(sample[0].shape, sample[1].shape)
network = tf.keras.Sequential(
[tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(10)]
)
# network.build(input_shape=[None, 28*28])
network.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss=tf.losses.CategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
network.fit(train_db, epochs=1, validation_data=test_db)
# 保存并删除原来的网络
tf.saved_model.save(network, 'save_model')
print("已经保存网络!")
del network
# 预测代码
imported = tf.saved_model.load('save_model')
func = imported.signatures["serving_default"]
xx = tf.constant(x[0:10], dtype=tf.float32)
xx = tf.reshape(xx, [10, 28*28])
pred = func(xx)
print(pred, y[0:10])
输出了一些不明东西,目前还未搞清楚,希望大佬讲解一下