2017-08-07 148 views
0

我是新来tensorflow,我用预先训练inceptionV4模型训练我自己的数据下面的代码 http://download.tensorflow.org/models/inception_v4_2016_09_09.tar.gz如何使用已经训练有素inceptionV4模型

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进行图像标签?

+0

官方[示例](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/label_image/label_image.py)需要一个input_layer和output_layer,但我不知道使用哪种方式inceptionV4 – Lee

回答

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

+0

它看起来像defa ult输入层找不到.. 'python tensorflow/tensorflow/examples/label_image/label_image.py --image = 11323.jpg \ > --input_mean = -127 --input_std = 127 --output_layer = InceptionV4/Logits /预测--graph = frozen_inception_v4.pb KeyError:“名称'导入/输入'是指图中没有的操作。”' – Lee