Saving and restoring load Tensorflow model - TensorFlow study notes (IX)

Recently made some anti-work, in addition to using common rules and other means to match the filter, also adopted a number of machine learning methods to classify forecast. We use TensorFlow training model, the trained model needs to be saved, we need the forecast period will use the model to load reduction, which involves TensorFlow save and restore the model is loaded.

To summarize Tensorflow common way to preserve the model.

Save checkpoint model file (.ckpt)

First, TensorFlow provides a very convenient api, tf.train.Saver()to save and restore a machine learning model.

Save model

Use tf.train.Saver()to save the model file is very easy, here's a simple example:

import tensorflow as tf
import os

def save_model_ckpt(ckpt_file_path):
    x = tf.placeholder(tf.int32, name='x')
    y = tf.placeholder(tf.int32, name='y')
    b = tf.Variable(1, name='b')
    xy = tf.multiply(x, y)
    op = tf.add(xy, b, name='op_to_store')

    sess = tf.Session()
    sess.run(tf.global_variables_initializer())

    path = os.path.dirname(os.path.abspath(ckpt_file_path))
    if os.path.isdir(path) is False:
        os.makedirs(path)

    tf.train.Saver().save(sess, ckpt_file_path)
    
    # test
    feed_dict = {x: 2, y: 3}
    print(sess.run(op, feed_dict))
复制代码

Program generated and saved four files (prior to version 0.11 will generate three files: checkpoint, model.ckpt, model.ckpt.meta)

  • checkpoint text files, records the path information list of model files
  • model.ckpt.data-00000-of-00001 Network Weights
  • model.ckpt.index .data and .index these two files are binary files, save the variable parameters in the model (weight) information
  • model.ckpt.meta binary file, save the configuration information of FIG calculation model (structural model of the network) Protobuf

These are the tf.train.Saver().save()basic usage, save()methods there are many configurable parameters:

tf.train.Saver().save(sess, ckpt_file_path, global_step=1000)
复制代码

Plus global_step parameter representative of Save the model after every 1000 iterations will add "-1000", model.ckpt-1000.index, model.ckpt-1000.meta after the model file, model.ckpt.data-1000- 00000-of-00001

Each iteration saving time 1000 model, but the model information file structure will not change, it will only save time with 1000 iterations, without saving every 1000 corresponding time, so when we do not need to save the meta file, you can add write_meta_graph=Falseparameters, as follows:

tf.train.Saver().save(sess, ckpt_file_path, global_step=1000, write_meta_graph=False)
复制代码

If you want to save the model once every two hours, and only save the latest four models can be added using max_to_keep(the default is 5, if you want a train every epoch to save time, you can set it to 0 or None, but nothing use is not recommended), keep_checkpoint_every_n_hoursparameters, as follows:

tf.train.Saver().save(sess, ckpt_file_path, max_to_keep=4, keep_checkpoint_every_n_hours=2)
复制代码

While in tf.train.Saver()class, if we do not specify anything, it will save all parameter information, we can also specify the part you want to save, for example, save only the x, y parameters (list parameters can be passed or dict):

tf.train.Saver([x, y]).save(sess, ckpt_file_path)
复制代码

ps. In the model training process required to get after saving variable or attribute name parameter name can not be lost, can not be otherwise after the model reducing get_tensor_by_name()acquisition.

Load reduction model

Examples of the above model for saving, reduction of the process model are as follows:

import tensorflow as tf

def restore_model_ckpt(ckpt_file_path):
    sess = tf.Session()
    saver = tf.train.import_meta_graph('./ckpt/model.ckpt.meta')  # 加载模型结构
    saver.restore(sess, tf.train.latest_checkpoint('./ckpt'))  # 只需要指定目录就可以恢复所有变量信息

    # 直接获取保存的变量
    print(sess.run('b:0'))

    # 获取placeholder变量
    input_x = sess.graph.get_tensor_by_name('x:0')
    input_y = sess.graph.get_tensor_by_name('y:0')
    # 获取需要进行计算的operator
    op = sess.graph.get_tensor_by_name('op_to_store:0')

    # 加入新的操作
    add_on_op = tf.multiply(op, 2)

    ret = sess.run(add_on_op, {input_x: 5, input_y: 5})
    print(ret)
复制代码

First, restore the structure of the model, and then restore the variable (parameter) information, and then we can get a variety of information model has been trained in the (save variables, placeholder variable, operator, etc.), and can add a variety of new variables obtained operation (see above comment codes).

And we can also load part of the model, on the basis of adding other operations, specifically refer to the official documentation and demo.

For Save and Restore ckpt model file, there is on a stackoverflow answer explanation clearer, you can reference.

Meanwhile TensorFlow model preservation and restoration of cv-tricks.com above tutorial is also very good, you can reference.

"Tensorflow 1.0 Learning: Saving and restoring the model (Saver)" There are some tips Saver.

Save a single model file (.pb)

I have run demo Tensorflow's inception-v3 found after the end of the run will generate a .pb model file, this file is migrated as a follow predict or learn to use, just one file, very cool, very convenient.

The main idea of this process is graph_def file does not contain Variable value network (typically stored weights), but it contains a constant value, so if we can convert Variable constant (using the graph_util.convert_variables_to_constants()function), can be achieved by using a At the same time file storage networking architecture and weight goals.

ps: Here .pb model file extension is, of course, we can also use other suffixes (use google .pb and consistent ╮ (╯ ▽ ╰) ╭)

Save model

Also according to the above example, a simple demo:

import tensorflow as tf
import os
from tensorflow.python.framework import graph_util

def save_mode_pb(pb_file_path):
    x = tf.placeholder(tf.int32, name='x')
    y = tf.placeholder(tf.int32, name='y')
    b = tf.Variable(1, name='b')
    xy = tf.multiply(x, y)
    # 这里的输出需要加上name属性
    op = tf.add(xy, b, name='op_to_store')

    sess = tf.Session()
    sess.run(tf.global_variables_initializer())

    path = os.path.dirname(os.path.abspath(pb_file_path))
    if os.path.isdir(path) is False:
        os.makedirs(path)

    # convert_variables_to_constants 需要指定output_node_names,list(),可以多个
    constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph_def, ['op_to_store'])
    with tf.gfile.FastGFile(pb_file_path, mode='wb') as f:
        f.write(constant_graph.SerializeToString())

    # test
    feed_dict = {x: 2, y: 3}
    print(sess.run(op, feed_dict))
复制代码

Program to generate and save a file

  • model.pb binary files, while preserving the structure and model of network parameters (weights) information

Load reduction model

Examples of the above model for saving, reduction of the process model are as follows:

import tensorflow as tf
from tensorflow.python.platform import gfile

def restore_mode_pb(pb_file_path):
    sess = tf.Session()
    with gfile.FastGFile(pb_file_path, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        sess.graph.as_default()
        tf.import_graph_def(graph_def, name='')

    print(sess.run('b:0'))

    input_x = sess.graph.get_tensor_by_name('x:0')
    input_y = sess.graph.get_tensor_by_name('y:0')

    op = sess.graph.get_tensor_by_name('op_to_store:0')

    ret = sess.run(op, {input_x: 5, input_y: 5})
    print(ret)
复制代码

Reduction process model and about the same checkpoint.

CSDN "will TensorFlow network exported as a single file," describes the way TensorFlow save files on a single model, much the same, you can look at.

Think

Saving and loading model only TensorFlow one of the most basic part, though simple but essential, in the practical application also need to pay attention to when saving model, which variables need to save, how to design to achieve load transfer learning and so on.

Meanwhile TensorFlow functions and classes have been changes in the update, and they will likely save a richer models and methods for reducing the appearance.

Select Save, as the business case checkpoint or a single file pb may be, no major differences. checkpoint save the feeling will be more flexible, pb files more suitable for online deploy it (personal opinion).


Reprinted from: https://liuyan731.github.io/2017/11/25/Tensorflow-Model-Save-And-Restore/

Published 47 original articles · won praise 121 · views 680 000 +

Guess you like

Origin blog.csdn.net/guoyunfei123/article/details/82871403