migueldeicaza / TensorFlowSharp

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

[Windows] TFGraph CPU x86 System.BadImageFormatException: 'An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)' #438

Open kamilk91 opened 5 years ago

kamilk91 commented 5 years ago

Describe the bug


System.BadImageFormatException: 'An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)'

To Reproduce i generated model with

https://github.com/teresawu/tensor-image-model/

^ this sample photos.

(...)

string modelFile = "tmp/output_graph.pb";

(...)
 public Bitmap Detector()
        {
            using (var graph = new TFGraph())
            {
                var model = File.ReadAllBytes(modelFile);
                graph.Import(new TFBuffer(model));
using (var session = new TFSession(graph))
                {
                    Console.WriteLine("Detecting objects");

                    var tensor = ImageUtil.CreateTensorFromImageFile(input, TFDataType.UInt8);

                    var runner = session.GetRunner();

                    runner
                        .AddInput(graph["image_tensor"][0], tensor)
                        .Fetch(
                        graph["detection_boxes"][0],
                        graph["detection_scores"][0],
                        graph["detection_classes"][0],
                        graph["num_detections"][0]);
                    var output = runner.Run();

                    var boxes = (float)output[0].GetValue(jagged: false);
                    var scores = (float)output[1].GetValue(jagged: false);
                    var classes = (float)output[2].GetValue(jagged: false);
                    var num = (float)output[3].GetValue(jagged: false);

                    Bitmap edited = ie.AddBox(input, boxes, scores, classes, MIN_SCORE_FOR_OBJECT_HIGHLIGHTING);
                    return edited;
}

Note: I do not have time to create models, train models, create samples from scratch and then adding your 3-4 lines of code to a sample to debug your problem. You need to provide a complete test case.

Expected behavior Use learned model

Screenshots Not needed

Desktop (please complete the following information):

Additional context When im trying to execute thiss method, Exception is throwed every time. I tried to change Build option to x64, then this step went good, but my app needs x86 build (im using other Nugets which works only on x64).

Is it correct way to use model? Maybe im doing something wrong.

When im using x64 this step working, but i have another exception. But it does not matter now.

gmlwns2000 commented 5 years ago

Hi,

You cannot use x64 binary in x86. You should use x86 TF binary and it should be compiled yourself.

Or if you just try to run model, WinML would be nice alternative.