migueldeicaza / TensorFlowSharp

TensorFlow API for .NET languages
MIT License
3.14k stars 578 forks source link

Error getting the final tensor #356

Closed bloodthiirst closed 5 years ago

bloodthiirst commented 5 years ago

Hey so am trying use a consume a simple classifier that i've already tried in python and works , and i wanna use it with TensorFlowSharp but when it comes to getting the output from the final tensor called 'final_result' it says that it (basically doesn't exist) is null with this exception :

System.NullReferenceException: 'Object reference not set to an instance of an object.' TensorFlow.TFGraph.this[string].get returned null.

here's the python code i used when testing :

            finalTensor = sess.graph.get_tensor_by_name('final_result:0')

            # convert the OpenCV image (numpy array) to a TensorFlow image
            tfImage = np.array(openCVImage)[:, :, 0:3]

            # run the network to get the predictions
            predictions = sess.run(finalTensor, {'DecodeJpeg:0': tfImage})

here's the C# code :

private void StartEval(object sender, RoutedEventArgs e)
        {
            using (var graph = new TFGraph())
            {
                var Image = File.ReadAllText(ImageURL);

                TFTensor image = ImageUtil.CreateTensorFromImageFile(ImageURL);

                graph.Import(File.ReadAllBytes(ModelURL));

                var session = new TFSession(graph);
                var runner = session.GetRunner();
                runner.AddInput(graph["DecodeJpeg"][0] , ImageUtil.CreateTensorFromImageFile(ImageURL));

                runner.Fetch(graph["final_result"][0]); // This is where the exception triggers

                var output = runner.Run();

                // Fetch the results from output:
                TFTensor result = output[0];

                EvalResult.Text = result.ToString();
            }
        }
bloodthiirst commented 5 years ago

Ok , so i rebuilt the model from python and changed the final tensor name to "output" and it works , but i've had an issue concerning the Dimensions of the input , ill close this for now

kamilk91 commented 5 years ago

@ahmedhoussem can you tell me how exactly did you built your model? I have the same exception with model trained with "Custom Vision". It is an Azure Service. Im trying to resolve this issue, but to be honest i stuck on point that there is a different between "tensor flow model" and "tensor flow lite model".