tensorflow 2.0 keras 高层接口 之 模型的加载与保存

7.3 模型的保存与加载

  • Outline
  • save/load weights
    • 完整代码
  • save/load model
    • 完整代码
  • saved_model

Outline

  1. save/load weights
  2. save/load entire model
  3. saved_model

save/load weights

  1. 保存权值
  2. 建立同样结构的模型
  3. 加载权值
  4. 使用模型
# save weights
network.save_weights('../save_weights/weights.ckpt')
# load weights
model = create_model()
network.load_weights('../save_weights/weights.ckpt')

loss, acc = model.evaluate(test_images, test_labels)
print('Restore model, accuracy : {:5.2f}%'.format(100*acc))

完整代码

import  tensorflow as tf
from    tensorflow.keras import datasets, layers, optimizers, Sequential, metrics
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'


def preprocess(x, y):
    """
    x is a simple image, not a batch
    """
    x = tf.cast(x, dtype=tf.float32) / 255.
    x = tf.reshape(x, [28*28])
    y = tf.cast(y, dtype=tf.int32)
    y = tf.one_hot(y, depth=10)
    return x,y


batchsz = 128
(x, y), (x_val, y_val) = datasets.mnist.load_data()
print('datasets:', x.shape, y.shape, x.min(), x.max())



db = tf.data.Dataset.from_tensor_slices((x,y))
db = db.map(preprocess).shuffle(60000).batch(batchsz)
ds_val = tf.data.Dataset.from_tensor_slices((x_val, y_val))
ds_val = ds_val.map(preprocess).batch(batchsz) 

sample = next(iter(db))
print(sample[0].shape, sample[1].shape)


network = Sequential([layers.Dense(256, activation='relu'),
                     layers.Dense(128, activation='relu'),
                     layers.Dense(64, activation='relu'),
                     layers.Dense(32, activation='relu'),
                     layers.Dense(10)])
network.build(input_shape=(None, 28*28))
network.summary()




network.compile(optimizer=optimizers.Adam(lr=0.01),
		loss=tf.losses.CategoricalCrossentropy(from_logits=True),
		metrics=['accuracy']
	)

network.fit(db, epochs=3, validation_data=ds_val, validation_freq=2)
 
network.evaluate(ds_val)

network.save_weights('../save_weights/weights.ckpt')
print('saved weights.')
del network

network = Sequential([layers.Dense(256, activation='relu'),
                     layers.Dense(128, activation='relu'),
                     layers.Dense(64, activation='relu'),
                     layers.Dense(32, activation='relu'),
                     layers.Dense(10)])
network.compile(optimizer=optimizers.Adam(lr=0.01),
		loss=tf.losses.CategoricalCrossentropy(from_logits=True),
		metrics=['accuracy']
	)
network.load_weights('../save_weights/weights.ckpt')
print('loaded weights!')
network.evaluate(ds_val)

save/load model

  1. 保存模型
  2. 加载模型
  3. 使用模型

注:

  • 相比保存与加载权值,不需要额外创建模型,模型网络参数被全部保存。
  • 但劣势是效率不高
network.save('../save_model/model.h5')
print('saved total model.')
del network

print('load model from file')
network = tf.keras.models.load_model('../save_model/model.h5')
network.evaluate(ds_val)

完整代码

import  tensorflow as tf
from    tensorflow.keras import datasets, layers, optimizers, Sequential, metrics
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

def preprocess(x, y):
    """
    x is a simple image, not a batch
    """
    x = tf.cast(x, dtype=tf.float32) / 255.
    x = tf.reshape(x, [28*28])
    y = tf.cast(y, dtype=tf.int32)
    y = tf.one_hot(y, depth=10)
    return x,y


batchsz = 128
(x, y), (x_val, y_val) = datasets.mnist.load_data()
print('datasets:', x.shape, y.shape, x.min(), x.max())



db = tf.data.Dataset.from_tensor_slices((x,y))
db = db.map(preprocess).shuffle(60000).batch(batchsz)
ds_val = tf.data.Dataset.from_tensor_slices((x_val, y_val))
ds_val = ds_val.map(preprocess).batch(batchsz)

sample = next(iter(db))
print(sample[0].shape, sample[1].shape)


network = Sequential([layers.Dense(256, activation='relu'),
                     layers.Dense(128, activation='relu'),
                     layers.Dense(64, activation='relu'),
                     layers.Dense(32, activation='relu'),
                     layers.Dense(10)])
network.build(input_shape=(None, 28*28))
network.summary()


custom_loss = tf.losses.CategoricalCrossentropy

network.compile(optimizer=optimizers.Adam(lr=0.01),
		loss=tf.losses.CategoricalCrossentropy(from_logits=True),
		metrics=['accuracy']
	)

network.fit(db, epochs=3, validation_data=ds_val, validation_freq=2)

network.evaluate(ds_val)

network.save('./save_model/model.h5')
print('saved total model.')
del network



print('load model from file')
network = tf.keras.models.load_model('./save_model/model.h5')
network.build(input_shape=(None, 28 * 28))

x_val = tf.cast(x_val, dtype=tf.float32) / 255.
x_val = tf.reshape(x_val, [-1, 28*28])
y_val = tf.cast(y_val, dtype=tf.int32)
result = network(x_val)

result = tf.argmax(result, axis=-1)
result = tf.cast(result, dtype=tf.int32)
accuracy = tf.reduce_sum(tf.cast(tf.equal(result, y_val), dtype=tf.int32))/y_val.shape[0]

print('accuracy:', float(accuracy))

saved_model

一般应用于工业环境的部署。
更加通用。

tf.saved_model.save(m, '/tmp/save_model/')

imported = tf.saved_model.load(path)
model = imported.signaures['serving_default'] 
print(model(x=tf.ones([1, 28, 28, 3])))

你可能感兴趣的:(tensorflow,深度学习,tensorflow)