migueldeicaza / TensorFlowSharp

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

TensorFlowSharp return diffrent values to same model then calling in pyton #243

Open nivniv850 opened 6 years ago

nivniv850 commented 6 years ago

Hi all, I am new in TensorFlowSharp and I have a model I created in tensorflow (not sharp). I want to use this model in my c# app so I try to running the Example of Object Detection in the reposritoy and It return diffrent values then calling to the object detection runner without the TensorFlowSharp.

Thanks.

mustafakasap commented 6 years ago

Try to remove the operation "// float using (value - Mean)/Scale." in ImageUtil.cs file (or set the value of MEAN variable to 0). Final code should be: output = graph.Cast( graph.ResizeBilinear( images: graph.ExpandDims( input: graph.Cast(image, DstT: TFDataType.Float), dim: graph.Const(0, "make_batch")), size: graph.Const(new int[] { W, H }, "size") ), destinationDataType);

If it will not resolve, one another reason might be the image dimension in the above code snippet... i.e. your original image might be 4:9 scale and above code scales the image to have same width&height... In TensorFlow "keep_aspect_ratio_resizer" is used.

nivniv850 commented 6 years ago

Hi, First of all thank you about the response. Second, the two options didn't worked for me.

This is the "keep_aspect_ratio_resizer" from my faster_rcnn_resnet101.config: keep_aspect_ratio_resizer { min_dimension: 600 max_dimension: 1024 }

and this is the new code from ImageUtil.cs after your advice:

const int W = 600;
const int H = 1024;

var graph = new TFGraph ();
input = graph.Placeholder (TFDataType.String);

output = graph.Cast (
        graph.ResizeBilinear (
         images: graph.ExpandDims (
        input: graph.Cast (graph.DecodeJpeg (contents: input, channels: 3), DstT: TFDataType.Float),
        dim: graph.Const (0, "make_batch")),
        size: graph.Const (new int [] { W, H }, "size")
        ), destinationDataType);

            return graph;

Thanks.

nivniv850 commented 6 years ago

Hi, I am sorry, I pay attention that the image was formated as the defualt format when saving images witout format in c# which I think is png. I don't know how my object_runner.py eat this, but TensorFlowSharp didn't. So I recreate the image with Jpeg format and now it works - both of the suggestions you mention above were needed to make it work perfect! Thanks!

mustafakasap commented 6 years ago

One more think regarding above code: It resizes the image to have specified width and height (not keeping the aspect ratio)... In tensorflow (config portion that you shared), while it is resizing it keeps the aspect ratio too... this also affects the inference result... I give up implementing that portion (keep aspect ratio resizing...) but keep that in mind too.

mg021 commented 5 years ago

Hello @mustafakasap I have time problem my run() function took close 16 second . what is my mistake ???

here is my code https://gist.github.com/mg021/ecb4444986006ec2826f2d92fa942ebc