TensorFlow.NET icon indicating copy to clipboard operation
TensorFlow.NET copied to clipboard

Simultaneous predictions from different models not possible

Open uzfm opened this issue 4 years ago • 8 comments

        static SciSharp.Models.IImageClassificationTask[] task = new SciSharp.Models.IImageClassificationTask[2];
        static tensorflow tf = new tensorflow();
        static SciSharp.Models.ModelWizard  wizard = new SciSharp.Models.ModelWizard();
        static string _assetsPath = Path.Combine(TFdt.DT.PathAssets, TFdt.DT.PathDataSet);
        static string [] Model_Pach = { Path.Combine(_assetsPath, "Model_Camera 1.pb"), Path.Combine(_assetsPath, "Model_Camera 2.pb") };
        static public string[] LablePach = { Path.Combine(_assetsPath, "Labels_Camera 1.txt"), Path.Combine(_assetsPath, "Labels_Camera 2.txt") };

 static public ModelPredictResult Predict(Image<Bgr, byte> img_bgr ,int ID) {
      
            Mat output = new Mat();
            CvInvoke.CvtColor(img_bgr, output, Emgu.CV.CvEnum.ColorConversion.Bgr2Rgb);
            var output1 = output.ToImage<Rgb, byte>().Resize(300, 300, Emgu.CV.CvEnum.Inter.Linear);
            output1.ToUMat().ConvertTo(output, Emgu.CV.CvEnum.DepthType.Cv32F, 1.0f / 255.0f);
            //NDArray imgR = np.array(output.GetData(), TF_DataType.TF_FLOAT);
            Tensor tensorr = tf.convert_to_tensor(output.GetData());
            var img_final = tf.expand_dims(tensorr, 0);
            //predict image
            if (task[ID] == null)
            {
                task[ID]= wizard.AddImageClassificationTask<TransferLearning>(new SciSharp.Models.TaskOptions
                {
                    ModelPath = Model_Pach[ID],
                    LabelPath = LablePach[ID]
           

                }); ;
            }
        
              var result = task[ID].Predict(img_final);
            return result;
        }

Tensorflow.RuntimeError: 'Current graph is not default graph. Default Graph Key: graph-2/, Current Graph Key: graph-1/'

uzfm avatar Jan 13 '22 20:01 uzfm

Have you tried the latest version (0.70.0) ? There was some significant changes to support multi-threading (with one model per thread)

CrimyTheBold avatar Jan 14 '22 09:01 CrimyTheBold

ghb

Everything works great with one model. If I add another model for the next prediction I get an error

uzfm avatar Jan 14 '22 10:01 uzfm

I use SciSharp.TensorFlow.Redist-Windows-GPU 2.7.0 SciSharp.Models.ImageClassification 0.4.0 TensorFlowSharp 1.15.1 RTX 3080

uzfm avatar Jan 14 '22 10:01 uzfm

I can download a small test program https://drive.google.com/file/d/1R57DewCRe66w_CTnHerWuvk76ZKdxkgV/view?usp=sharing

uzfm avatar Jan 14 '22 10:01 uzfm

@uzfm I downloaded your sample project, it works well, can't reproduce from my side. image

Oceania2018 avatar Jan 15 '22 16:01 Oceania2018

@uzfm I downloaded your sample project, it works well, can't reproduce from my side. image

Prediction is good only for the first time. If we make prediction next time we will get error!

uzfm avatar Jan 16 '22 10:01 uzfm

Image1 Next time the prediction gets an error. If make a prediction from one model, everything is fine. I need to make a prediction from different models at the same time.

uzfm avatar Jan 16 '22 11:01 uzfm

@uzfm This issue has been fixed, Just upgrade to ImageClassification v0.4.2 and TensorFlow.NET v0.70.1

Oceania2018 avatar Jan 16 '22 21:01 Oceania2018