aymericdamien / TensorFlow-Examples

TensorFlow Tutorial and Examples for Beginners (support TF v1 & v2)
Other
43.37k stars 14.95k forks source link

Can't freeze random forest to generate .pb #219

Open shreyaspimpalgaonkar opened 6 years ago

shreyaspimpalgaonkar commented 6 years ago

Code that is copy pasted from the repository, created a node called "output" and named the placeholder X as "input"

import numpy as np
import json
from xlrd import open_workbook
import os
from math import log
from tensorflow.python.tools import freeze_graph
from tensorflow.python.tools import optimize_for_inference_lib
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import pandas
import tensorflow as tf
from tensorflow.contrib.tensor_forest.python import tensor_forest
from tensorflow.python.ops import resources

import os
os.environ["CUDA_VISIBLE_DEVICES"] = ""

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=False)

num_steps = 500 # Total steps to train
batch_size = 1024 # The number of samples per batch
num_classes = 10 # The 10 digits
num_features = 784 # Each image is 28x28 pixels
num_trees = 10
max_nodes = 1000

cell = "0"

X = tf.placeholder(tf.float32, shape=[None, num_features],name="Input")
Y = tf.placeholder(tf.int32, shape=[None])

hparams = tensor_forest.ForestHParams(num_classes=num_classes,
                                      num_features=num_features,
                                      num_trees=num_trees,
                                      max_nodes=max_nodes).fill()

forest_graph = tensor_forest.RandomForestGraphs(hparams)
train_op = forest_graph.training_graph(X, Y)
loss_op = forest_graph.training_loss(X, Y)

infer_op, _, _ = forest_graph.inference_graph(X)
out = tf.argmax(infer_op, 1,name="output")
correct_prediction = tf.equal(tf.argmax(infer_op, 1), tf.cast(Y, tf.int64))
accuracy_op = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

init_vars = tf.group(tf.global_variables_initializer(),
    resources.initialize_resources(resources.shared_resources()))

sess = tf.Session()

sess.run(init_vars)

 for i in range(1, num_steps+1):
    batch_x, batch_y = mnist.train.next_batch(batch_size)
    _, l = sess.run([train_op, loss_op], feed_dict={X: batch_x, Y: batch_y})
    if i % 50 == 0 or i == 1:
        acc = sess.run(accuracy_op, feed_dict={X: batch_x, Y: batch_y})
        print('Step %i, Loss: %f, Acc: %f' % (i, l, acc))

test_x, test_y = mnist.test.images, mnist.test.labels
print("Test Accuracy:", sess.run(accuracy_op, feed_dict={X: test_x, Y: test_y}))

tf.train.Saver().save(sess,cell + '/' + cell +'.ckpt')
tf.train.write_graph(sess.graph_def, cell + '/', cell +'.pbtxt')

This part causes the error

MODEL_NAME = cell
input_graph_path = cell + '/' + cell + '.pbtxt'
checkpoint_path =cell + '/' + cell +'.ckpt'
input_saver_def_path = ''
input_binary = False
output_node_names = 'output'
restore_op_name = cell + '_restore'
filename_tensor_name = cell + '_const'
output_frozen_graph_name = cell+  '/frozen_graph.pb'
output_optimized_graph_name = cell+'/optimized_graph.pb'
clear_devices = True

freeze_graph.freeze_graph(input_graph_path,input_saver_def_path,
  input_binary,checkpoint_path,output_node_names,restore_op_name,
  filename_tensor_name,
  output_frozen_graph_name,
  clear_devices,
  '')

gives the following error

WARNING:tensorflow:Error encountered when serializing resources.
Type is unsupported, or the types of the items don't match field type in CollectionDef.
'_Resource' object has no attribute 'name'
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3066, in     _as_graph_element_locked
    op_name, out_n = name.split(":")
ValueError: too many values to unpack (expected 2)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "random_forest.py", line 116, in <module>
    '')
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/tools/freeze_graph.py", line 239, in freeze_graph
    input_meta_graph_def, input_saved_model_dir, saved_model_tags.split(","))
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/tools/freeze_graph.py", line 121, in freeze_graph_with_def_protos
    tensor = sess.graph.get_tensor_by_name(key + ":0")
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3207, in get_tensor_by_name
    return self.as_graph_element(name, allow_tensor=True, allow_operation=False)
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3035, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3071, in _as_graph_element_locked
    "form \"<op_name>:<output_index>\"." % repr(name))
ValueError: The name 'tree-9:0:0' looks a like a Tensor name, but is not a valid one. Tensor names must be of the form "<op_name>:<output_index>".
sudhanshunathmishra commented 6 years ago

I have the same error

snewi commented 6 years ago

Same issue here

c-zv commented 6 years ago

I have the same problem. Has anyone solved this issue?

ManSoSec commented 6 years ago

Same issue on the latest version

ksevta commented 6 years ago

same issue !

frankiehe commented 6 years ago

same issue

Aester8 commented 5 years ago

same here..

cainiaoyexiangfei commented 5 years ago

same error!

cainiaoyexiangfei commented 5 years ago

Friends,I assume that you use the Pb file for the purpose of porting the model to the product. Me too. I used “TF_LoadSessionFromSavedModel” instead of “freeze_graph” or other methods in python, to successfully transplant the model to C products.

In C API,Some details, “session” returned by “TF_LoadSessionFromSavedModel” is the first parameter of TF_SessionRun, graph returned by “TF_LoadSessionFromSavedModel” can get the OP name through “TF_GraphOperationByName”.

IriscShih commented 5 years ago

Has anyone solved this?