Import from Metagraph not working.
Hi - I am trying to save a graph into a file(in python) and then load it using tensorflowsharp. I am seeing that i am able to load the graphdefinition but not able to load the metagraph
-
Exporting the graph definition (Import in TensorSharp works for this) tf.train.write_graph(sess.graph_def, "WriteGraphOutput", "graph.pb", False)
-
Exporting the metagraph (Import in TensorSharp fails for this) tf.train.export_meta_graph("metagraph")
Import Code in tensorflowsharp
var graph = new TFGraph();
// Load the serialized GraphDef from a file.
var model = File.ReadAllBytes(modelFile);
try
{
graph.Import(model, "");
}
catch(Exception ex)
{
Console.Out.WriteLine(ex.Message); // It says "Invalid GraphDef"
}
Any idea why this would be ? I am new to Tensorsharp , Apologies if am doing some basic mistake.
Thanks Gaurav
I found this thread and tried to generate a frozen model. I was able to load the model but ran into following error while trig-erring evaluation ( https://github.com/migueldeicaza/TensorFlowSharp/issues/85 )
{[TFStatus: StatusCode=InvalidArgument, StatusMessage=In[0] is not a matrix [[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_x_0_0, W/read)]]]}
My code is
var graph = new TFGraph();
// Load the serialized GraphDef from a file.
var model = File.ReadAllBytes(modelFile);
graph.Import(model, "");
using (var session = new TFSession(graph))
{
var runner = session.GetRunner();
float[] sample = new float[675] ;
TFTensor t = new TFTensor(sample);
var nRunner = runner.AddInput(graph["x"][0], t).Fetch(graph["y"][0]);
TFStatus status = new TFStatus();
var output = runner.Run(status);
}
}
Python code which generates the ckpt and pb file which I then freeze seperately -
x = tf.placeholder(tf.float32, [None, rowscolsfeature*(classes-1)], name="x") W = tf.Variable(tf.zeros([rowscolsfeature*(classes-1), classes]), name="W") b = tf.Variable(tf.zeros([classes]), name="b") y = tf.add( tf.matmul(x, W), b, name="y")
y_ = tf.placeholder(tf.float32, [None, classes])
cross_entropy = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(labels=y_, logits=y)) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) sess = tf.InteractiveSession() tf.global_variables_initializer().run()
for _ in range(range_p): batch_xs, batch_ys = vise.train.next_batch(batch_size) sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
saver = tf.train.Saver()
save_path = saver.save(sess, "c:\python\Profile.ckpt")
tf.train.write_graph(sess.graph_def, "LogFolder", "Profile.pb", as_text=True)
Any idea why this error would be coming ?
found the reason. x was a 2D Array
Hey gaurav how did you solve the previous error?
Never mind I fixed the error. Converted my input one dimensional array to two dimensional
@akuchotrani @ggrav Hello, can you expand on how you fixed the "In[0] is not a matrix" problem? I'm having the same problem.
For anyone else stumbling upon this, I manged to get my setup working by changing:
tensor = new TFTensor(...);
where "..." is an array of floats of length 76
to
tensor = TFTensor.FromBuffer(new TFShape(1, 76), ..., 0, 76);
The idea is the model created by Keras is expecting a 2d array for the inputs, even though there's only one.