如何使用已经训练过的inceptionV4模型

时间:2017-08-07 09:48:48

标签: tensorflow

我是tensorflow的新手,我使用预先训练过的inceptionV4模型训练我自己的数据 http://download.tensorflow.org/models/inception_v4_2016_09_09.tar.gz

python train_image_classifier.py \
--train_dir=${TRAIN_DIR} \
--dataset_name=stamps \
--dataset_split_name=train \
--dataset_dir=${DATASET_DIR} \
--model_name=inception_v4 \
--clone_on_cpu=true \
--checkpoint_path=${PRETRAINED_CHECKPOINT_DIR}/inception_v4.ckpt \
--checkpoint_exclude_scopes=InceptionV4/Logits,InceptionV4/AuxLogits \
--trainable_scopes=InceptionV4/Logits,InceptionV4/AuxLogits \
--max_number_of_steps=50 \
--batch_size=32 \
--learning_rate=0.01 \
--learning_rate_decay_type=fixed \
--save_interval_secs=60 \
--save_summaries_secs=60 \
--log_every_n_steps=100 \
--optimizer=rmsprop \
--weight_decay=0.00004

python eval_image_classifier.py \
--checkpoint_path=${TRAIN_DIR} \
--eval_dir=${TRAIN_DIR} \
--dataset_name=stamps \
--dataset_split_name=validation \
--dataset_dir=${DATASET_DIR} \
--model_name=inception_v4 \
--batch_size=32

使用下面的代码来冻结图表

import os, argparse

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

dir = os.path.dirname(os.path.realpath(__file__))

def freeze_graph(model_folder, output_node_names, output_graph):
    # We retrieve our checkpoint fullpath
    checkpoint = tf.train.get_checkpoint_state(model_folder)
    input_checkpoint = checkpoint.model_checkpoint_path

    # We precise the file fullname of our freezed graph
    #absolute_model_folder = "/".join(input_checkpoint.split('/')[:-1])
    #output_graph = absolute_model_folder + "/frozen_model.pb"

    # Before exporting our graph, we need to precise what is our output node
    # This is how TF decides what part of the Graph he has to keep and what part it can dump
    # NOTE: this variable is plural, because you can have multiple output nodes
    output_node_names = output_node_names

    # We clear devices to allow TensorFlow to control on which device it will load operations
    clear_devices = True

    # We import the meta graph and retrieve a Saver
    saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices)

    # We retrieve the protobuf graph definition
    graph = tf.get_default_graph()
    input_graph_def = graph.as_graph_def()

    # We start a session and restore the graph weights
    with tf.Session() as sess:
        saver.restore(sess, input_checkpoint)

        # We use a built-in TF helper to export variables to constants
        output_graph_def = graph_util.convert_variables_to_constants(
            sess, # The session is used to retrieve the weights
            input_graph_def, # The graph_def is used to retrieve the nodes 
            output_node_names.split(",") # The output node names are used to select the usefull nodes
        ) 

        # Finally we serialize and dump the output graph to the filesystem
        with tf.gfile.GFile(output_graph, "wb") as f:
            f.write(output_graph_def.SerializeToString())
        print("%d ops in the final graph." % len(output_graph_def.node))


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--model_folder", type=str, help="Model folder to export")
    parser.add_argument("--output_node_names", type=str, default="frozen_model.pb", help="output_node_names")
    parser.add_argument("--output_graph", type=str, help="output_graph name")
    args = parser.parse_args()

    freeze_graph(args.model_folder, args.output_node_names, args.output_graph)

我不知道要使用哪个output_node_name,现在我使用InceptionV4/Logits/Predictions

python ${OUTPUT_DIR}/freeze.py --model_folder=${TRAIN_DIR} \
--output_node_names=InceptionV4/Logits/Predictions \
--output_graph=${OUTPUT_DIR}/frozen_inception_v4.pb

那么如何将输出图frozen_inception_v4.pb用于图像标签?

2 个答案:

答案 0 :(得分:1)

查看label_image示例代码,了解使用冻结图像模型的示例:

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/label_image/label_image.py

大多数标记的默认值都应该没问题,但您需要--input_mean=-127--input_std=127--output_layer=InceptionV4/Logits/Prediction--graph=${OUTPUT_DIR}/frozen_inception_v4.pb

答案 1 :(得分:0)

我在尝试按照tensorflow doc重新训练inception_v3时遇到了同样的问题: https://www.tensorflow.org/tutorials/image_retraining

我无法弄清楚label_image.py中的input_layer应该是什么,但这似乎有效:

https://github.com/Dataweekends/inception-retrain/blob/master/label_image.py