Tensorflow模型保存与加载

1
2
3
4
5
6
import  os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

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

1
2
3
4
5
6
7
8
9
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
1
2
3
4
5
6
7
8
9
10
11
12
13
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)
datasets: (60000, 28, 28) (60000,) 0 255
(128, 784) (128, 10)

1.保存权值

模型创建

1
2
3
4
5
6
7
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()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                multiple                  200960    
_________________________________________________________________
dense_1 (Dense)              multiple                  32896     
_________________________________________________________________
dense_2 (Dense)              multiple                  8256      
_________________________________________________________________
dense_3 (Dense)              multiple                  2080      
_________________________________________________________________
dense_4 (Dense)              multiple                  330       
=================================================================
Total params: 244,522
Trainable params: 244,522
Non-trainable params: 0
_________________________________________________________________

模型训练

1
2
3
4
5
6
7
8
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)
Epoch 1/3
469/469 [==============================] - 10s 22ms/step - loss: 0.2594 - accuracy: 0.9232
Epoch 2/3
469/469 [==============================] - 12s 25ms/step - loss: 0.1406 - accuracy: 0.9619 - val_loss: 0.1288 - val_accuracy: 0.9629
Epoch 3/3
469/469 [==============================] - 9s 20ms/step - loss: 0.1076 - accuracy: 0.9703
79/79 [==============================] - 3s 39ms/step - loss: 0.1237 - accuracy: 0.9689





[0.12368294241494805, 0.9689]

保存权值 删除之前创建的网络

network.save_weights

1
2
3
network.save_weights('weights.ckpt')
print('saved weights.')
del network
saved weights.

创建与之前结构相同的网络 并加载保存的权值

1
2
3
4
5
6
7
8
9
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

1
2
3
network.load_weights('weights.ckpt')
print('loaded weights!')
network.evaluate(ds_val)
loaded weights!
79/79 [==============================] - 3s 40ms/step - loss: 0.1237 - accuracy: 0.9689





[0.12368294241494805, 0.9689]

2.保存模型

保存模型的所有信息 效率较低 重新加载前无需创建与之前结构相同的网络

模型创建&训练

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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=2, validation_data=ds_val, validation_freq=2)

network.evaluate(ds_val)
Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_10 (Dense)             multiple                  200960    
_________________________________________________________________
dense_11 (Dense)             multiple                  32896     
_________________________________________________________________
dense_12 (Dense)             multiple                  8256      
_________________________________________________________________
dense_13 (Dense)             multiple                  2080      
_________________________________________________________________
dense_14 (Dense)             multiple                  330       
=================================================================
Total params: 244,522
Trainable params: 244,522
Non-trainable params: 0
_________________________________________________________________
Epoch 1/2
469/469 [==============================] - 11s 23ms/step - loss: 0.2920 - accuracy: 0.9103
Epoch 2/2
469/469 [==============================] - 13s 28ms/step - loss: 0.1434 - accuracy: 0.9591 - val_loss: 0.1399 - val_accuracy: 0.9636
79/79 [==============================] - 4s 46ms/step - loss: 0.1399 - accuracy: 0.9636





[0.1399224520914398, 0.9636]

保存模型所有信息

network.save

1
2
3
network.save('model.h5')
print('saved total model.')
del network
saved total model.

加载模型 评估效果

tf.keras.models.load_model

1
2
3
4
print('loaded model from file.')
network = tf.keras.models.load_model('model.h5')

network.evaluate(ds_val)
loaded model from file.
WARNING:tensorflow:Sequential models without an `input_shape` passed to the first layer cannot reload their optimizer state. As a result, your model isstarting with a freshly initialized optimizer.
79/79 [==============================] - 3s 35ms/step - loss: 0.1399 - accuracy: 0.9636





[0.1399224520914398, 0.9636]
1