TensorFlowを使用するには、pbのtflearnモデルを保存します



Save Tflearn Model



解決:

画像 TRAIN_OPS 画像

注意:



1、tflearnの必要性model.save前面:

del tf.get_collection_ref(tf.GraphKeys.TRAIN_OPS)[:]



役割:トレーニングOPでモデルを削除します。

参照:https://github.com/tflearn/tflearn/issues/605#issuecomment-298478314

saved_modelモジュールでグラフと変数をエクスポートしようとしたときに同じ問題が発生しました。そして最後に、私はこの問題を修正するための散歩を見つけました:



output_node_names = 'Accuracy/prediction'を削除しますグラフコレクションからのコレクション。例:

output_node_names = 'FullyConnected_2/Softmax'

ダンプされたグラフ(tflearnによる)再度トレーニングに利用できない場合がありますが、予測と評価を実行できるはずです。。これは便利です別のモジュールまたは言語でモデルを提供する場合(例:テンソルフローサービングまたはテンソルフローゴーバインディング)。これについてさらにテストを行います。

モデルを再トレーニングする場合は、組み込みの「save」メソッドを使用してグラフを再構築し、再トレーニング時に保存されたデータをロードしてください。

2、このコード行を変更する必要があるかもしれません、

''' Tensorflow graph freezer Converts Tensorflow trained models in .pb Code adapted from: https://gist.github.com/morgangiraud/249505f540a5e53a48b0c1a869d370bf#file-medium-tffreeze-1-py ''' import os, argparse os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tensorflow as tf from tensorflow.python.framework import graph_util def freeze_graph(model_folder,output_graph='frozen_model.pb'): # We retrieve our checkpoint fullpath try: checkpoint = tf.train.get_checkpoint_state(model_folder) input_checkpoint = checkpoint.model_checkpoint_path print('[INFO] input_checkpoint:', input_checkpoint) except: input_checkpoint = model_folder print('[INFO] Model folder', model_folder) # 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 output_node_names = 'FullyConnected/Softmax' # NOTE: Change here # 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)) print('[INFO] output_graph:',output_graph) print('[INFO] all done') if __name__ == '__main__': parser = argparse.ArgumentParser(description='Tensorflow graph freezer
Converts trained models to .pb file', prefix_chars='-') parser.add_argument('--mfolder', type=str, help='model folder to export') parser.add_argument('--ograph', type=str, help='output graph name', default='frozen_model.pb') args = parser.parse_args() print(args,'
') freeze_graph(args.mfolder,args.ograph) # However, before doing model.save(...) on TFLearn i have to do # ************************************************************ # del tf.get_collection_ref(tf.GraphKeys.TRAIN_OPS)[:] # ************************************************************ ''' Then I call this command python tf_freeze.py --mfolder= Note The must not have the '.data-00000-of-00001'. The output_node_names variable may change depending on your architecture. The thing is that you must reference the layer that has the softmax activation function. '''

@ vparikh10 @ratfury @rakashi 私はあなたと同じ状況に直面しました。
私が理解したことから、あなたはこれを変更しなければならないかもしれません ライン ネットワーク定義に従って。
私の場合、

with dnn.graph.as_default(): del tf.get_collection_ref(tf.GraphKeys.TRAIN_OPS)[:]
の代わりに、
output_node_names = 'FullyConnected/Softmax' # NOTE: Change here Reference: https: //gist.github.com/morgangiraud/249505f540a5e53a48b0c1a869d370bf#file-medium-tffreeze-1-py 
があります。

ソフトマックス

これを読んだ後、この変更を加えました 提案

 Speaking for myself,Softmax written or Softmax will not be tolerated! Then I will print out all the node names: Print method: 
画像
 with tf.Session() as sess: model = get_cnn_model(max_len, volcab_size) model.fit(trainX, trainY, validation_set=(testX, testY), show_metric=True, batch_size=1000, n_epoch=1) init_op = tf.initialize_all_variables() sess.run(init_op) for v in sess.graph.get_operations(): print(v.name)
画像

次に、output_node_namesが内部にあることを確認します。

 Annex: gist code in the output node names to parameter 
画像
import os, argparse import tensorflow as tf # The original freeze_graph function # from tensorflow.python.tools.freeze_graph import freeze_graph   dir = os.path.dirname(os.path.realpath(__file__)) def freeze_graph(model_dir, output_node_names): '''Extract the sub graph defined by the output nodes and convert all its variables into constant Args: model_dir: the root folder containing the checkpoint state file output_node_names: a string, containing all the output node's names, comma separated ''' if not tf.gfile.Exists(model_dir): raise AssertionError( 'Export directory doesn't exists. Please specify an export ' 'directory: %s' % model_dir) if not output_node_names: print('You need to supply the name of a node to --output_node_names.') return -1 # We retrieve our checkpoint fullpath checkpoint = tf.train.get_checkpoint_state(model_dir) input_checkpoint = checkpoint.model_checkpoint_path # We precise the file fullname of our freezed graph absolute_model_dir = '/'.join(input_checkpoint.split('/')[:-1]) output_graph = absolute_model_dir + '/frozen_model.pb' # We clear devices to allow TensorFlow to control on which device it will load operations clear_devices = True # We start a session using a temporary fresh Graph with tf.Session(graph=tf.Graph()) as sess: # We import the meta graph in the current default Graph saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices) # We restore the weights  saver.restore(sess, input_checkpoint) # We use a built-in TF helper to export variables to constants output_graph_def = tf.graph_util.convert_variables_to_constants( sess, # The session is used to retrieve the weights tf.get_default_graph().as_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)) return output_graph_def if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--model_dir', type=str, default='', help='Model folder to export') parser.add_argument('--output_node_names', type=str, default='', help='The name of the output nodes, comma separated.') args = parser.parse_args() freeze_graph(args.model_dir, args.output_node_names)
画像