migueldeicaza / TensorFlowSharp

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

retrained image classifer in tensorflowsharp? #300

Open bzburr opened 6 years ago

bzburr commented 6 years ago

Hi there

I've retrained an image classifier using tensorflow ImageNet retraining sample. I have my files

-{DIR}bottleneck -{DIR}retrain_logs -checkpoint -output_graph.pb -output_labels.txt -_retrain_checkpoint.data-00000-of-00001 -_retrain_checkpoint.index -_retrain_checkpoint.meta

When i try and load the .pb file into TensorflowSharp Graph object - get the following mess of an object image

What do i need to do to load the retrained imagenet model into Tensorflow sharp and how do i do it?

thanks :)

migueldeicaza commented 6 years ago

Did you use TFSession.FromSavedModel to load?

It needs to be saved like this:

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md

bzburr commented 6 years ago

no i used... theGraph = new TFGraph(); var model = File.ReadAllBytes(classifierModelPath); theGraph.Import(new TFBuffer(model)); ...

bzburr commented 6 years ago

So i have _tfGraph = new TFGraph(); using (var tmpSess = new TFSession(_tfGraph)) using (var tfSessionOptions = new TFSessionOptions()) using (var metaGraphUnused = new TFBuffer()) { //for some reason FromSavedModel is not static _tfSession = tmpSess.FromSavedModel(tfSessionOptions, null, @"D:\AI\savedfolder\", new[] { "serve" }, _tfGraph, metaGraphUnused); }

_tfGraph doesnt look great

000486

my saved folder looks like 000479

my instantiation looks like

 _tfGraph = new TFGraph();
            using (var tmpSess = new TFSession(_tfGraph))
            using (var tfSessionOptions = new TFSessionOptions())
            using (var metaGraphUnused = new TFBuffer())
            {
                //for some reason FromSavedModel is not static
                _tfSession = tmpSess.FromSavedModel(tfSessionOptions, null, @"D:\AI\savedfolder\", new[] { "serve" }, _tfGraph, metaGraphUnused);
            }

my function to call the classifier is

public ClassificationResult ClassifyImageFromSavedModel(string theTempFile)
        {
            var bestIdx = 0;
            float best = 0;
            using (var session = new TFSession(_tfGraph))
            {
                var tensor = ClasssifierImageUtil.CreateTensorFromImageFile(theTempFile, 299);
                var runner = _tfSession.GetRunner();
                runner.AddInput(_tfGraph["image"][0], tensor).Fetch(_tfGraph["prediction"][0]);
                var output = runner.Run();
                var result = output[0];

                var probabilities = ((float[][])result.GetValue(jagged: true))[0];
                for (int i = 0; i < probabilities.Length; i++)
                {
                    if (probabilities[i] > best)
                    {
                        bestIdx = i;
                        best = probabilities[i];
                    }
                }
            }

when i hit

runner.AddInput(_tfGraph["image"][0], tensor).Fetch(_tfGraph["prediction"][0]);

i get an exception..which to be fair isnt unxpected 000485

I'm assuming theres an issue in my saved model...?

bzburr commented 6 years ago

saved_model_cli show --dir thefolder --all gave me the info i needed,.... runner.AddInput(theGraph["Placeholder"][0], tensor).Fetch(theGraph["final_result"][0]);

hurray

Now to test tensorflowsharp vs label_image.py

bzburr commented 6 years ago

well....running label_image.py works and correctly classifies the image given

but when i run the classifier from tensorflowsharp... i get an incorrect classification....hmm tfsharp seems to want to classify any image given to it as index 8.

bzburr commented 6 years ago

This is turning into quite the stream of consciousness - for this that follow you have to head into ConstructGraphToNormalizeImage and you need to examime const float Mean and const float Scale ...and find the values used when the model was trained. Whack those in and the model works a lot better :)

diwadu commented 5 years ago

Hi, could you provide code example how to improve efficiency in comparison to python? In may case is like 49% in TF# vs 90% in Python, so quite a lot.