Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
uzfm
@uzfm
Is it possible to make predictions from Image or Bitmap ?
uzfm
@uzfm
I tried to create a Tensor from Image. prediction only works once. if I create Tensor from Image next time, Tensor is empty. I used var input = tf.data.Dataset.from_tensors (img_Ary).
This example "var input = ImageUtil.ReadImageFromFile (imgPath)" works fine
Is it possible to get Tensor not from a file?
Vitaliy Shakhlin
@vshakhlin

@Oceania2018 thank you for answer. I have a simple class that contains few methods Init() create my model and load weight from file and Predict method that load image and do prediction.

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var recognizeService = new RecognizeService();
            recognizeService.Init(); // create model and load weight from file

            recognizeService.PredictTest(); // this works fine

            new Thread(() =>
            {
                recognizeService.PredictTest(); // this do not work and got NullReferenceException on the Layer.Apply
            }).Start();

            Task.Run(() =>
            {
                recognizeService.PredictTest(); // the same not work and got NullReferenceException on the Layer.Apply
            });
        }

this is just an artificial example that I got the same behaviour. But in my main code I observe some events (EventHandler) and I want to call Predict method when these events happen but unfortunately got NullReferenceException. I start thinking why it works if I call Predict method when just click the button properly but does not work in the EventHandler? And realized that if I call it inside new Thread that also does not work.
I also attach Init and PredictTest methods

        public void Init()
        {
            _model = keras.Sequential();

            _model.add(layers.InputLayer((240, 240, 3)));
            _model.add(layers.Conv2D(32, (3, 3), padding: "same", activation: "relu"));
            _model.add(layers.Conv2D(16, (3, 3), padding: "same", activation: "relu"));
            _model.add(layers.MaxPooling2D((3, 3)));
            _model.add(layers.Dropout(0.2f));
            _model.add(layers.Flatten());
            _model.add(layers.Dense(128, activation: "relu"));
            _model.add(layers.Dense(256, activation: "relu"));
            _model.add(layers.Dense(14, activation: "softmax"));

            _model.compile(loss: keras.losses.CategoricalCrossentropy(),
                optimizer: keras.optimizers.Adam(0.001f),
                metrics: new[] { "accuracy" });

            _model.load_weights("w2.h5");
        }
        private string PredictTest(string imgPath)
        {
            var imgData = LoadImg(imgPath);
            var newImg = tf.expand_dims(imgData, 0);
            var result = _model.predict(newImg);
            var value = tf.math.argmax(result, 1).numpy()[0];
            return _labels[value];
        }
Vitaliy Shakhlin
@vshakhlin
hello @uzfm, I load my image from file like this maybe it will helpfull for you (all my imgages has 240x240 px). And I not sure that you need reshape method. I use this for prepare Tensor for my Conv2D layer that has 240, 240, 3 shape.
        private NDArray LoadImg(string imgPath)
        {
            var imgWidth = 240;
            var imgHeight = 240;
            using var src = Cv2.ImRead(imgPath, ImreadModes.Color);

            try
            {
                byte[] data = new byte[imgWidth * imgHeight * 3];
                Marshal.Copy(src.Data, data, 0, imgWidth * imgHeight * 3);

                var img = np.array(data);
                img = img.reshape((imgWidth, imgHeight, 3));
                return img;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return null;
        }
Vitaliy Shakhlin
@vshakhlin
@uzfm sorry I was wrong it not helpfull. You use EmguCV I use OpenCvSharp4 library that don't have method GetData() on the Mat
uzfm
@uzfm
   static SciSharp.Models.IImageClassificationTask task ;
   tensorflow tf = new tensorflow();
   SciSharp.Models.ModelWizard wizard = new SciSharp.Models.ModelWizard();

    public void Predict(string PascImage) {

        var img_bgr = CvInvoke.Imread(PascImage, Emgu.CV.CvEnum.ImreadModes.Color);

        Mat output = new Mat();

        CvInvoke.CvtColor(img_bgr, output, Emgu.CV.CvEnum.ColorConversion.  Bgr2Rgb);

        var output1 = output.ToImage<Rgb,byte>().Resize(299,299,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);

        //keras.backend.clear_session();

        Tensor tensorr  = tf.convert_to_tensor( imgR);

        var img_final = tf.expand_dims(tensorr, 0);


        //predict image
        if (task == null) {
            task = wizard.AddImageClassificationTask<TransferLearning>(new SciSharp.Models.TaskOptions{
                ModelPath = @"image_classification_v1\saved_model.pb"
            }
             );}

        //works well but only from the file
        //var imgPath = Path.Join(PascImage);
        //input = SciSharp.Models.ImageUtil.ReadImageFromFile(imgPath);

        SciSharp.Models.ModelPredictResult result = task.Predict(img_final);
    }
I can't create a Tensor after this feature
task = wizard.AddImageClassificationTask<TransferLearning>(new SciSharp.Models.TaskOptions{
ModelPath = @"image_classification_v1\saved_model.pb"
}
uzfm
@uzfm
I can make one prediction. Then I have to clear the session. keras.backend.clear_session ();
it makes slow.
uzfm
@uzfm
This function makes Tensor work well without a cleaning session.
var imgPath = Path.Join(PascImage);
input = SciSharp.Models.ImageUtil.ReadImageFromFile(imgPath);
I need to download a picture not from a file. Is there such a possibility?
Haiping
@Oceania2018
@uzfm Are you saying the img_final is empty when second prediction?
Can you try var tensorr = tf.constant(imgR) to initialize a tensor?
Vitaliy Shakhlin
@vshakhlin
@uzfm I put to tf.expand_dims not a Tensor but just a NDArray and it works even if I call LoadImg and Predict several times
Vitaliy Shakhlin
@vshakhlin
image.png
@Oceania2018 hello. I clone the repository and add additional check if Value in the callCantext is null create new ThreadLocal (on screen changes)
3 replies
this is my test program. predictions from the picture "Image"
@uzfm Are you saying the img_final is empty when second prediction? Yes.
I tried many options but nothing helped.
"var tensorr = tf.constant (imgR)" also doesn't work
Haiping
@Oceania2018
@vshakhlin The syntax change seems act the same behaviour.
1 reply
Haiping
@Oceania2018
@uzfm @vshakhlin Just upgrade to v0.3.1 https://www.nuget.org/packages/SciSharp.Models.ImageClassification/0.3.1, it will resolve the prediction issue.
uzfm
@uzfm

@uzfm @vshakhlin Just upgrade to v0.3.1 https://www.nuget.org/packages/SciSharp.Models.ImageClassification/0.3.1, it will resolve the prediction issue.

Thank you very much!!! works great.
Is it possible to see the result of the prediction for all classes?

Vitaliy Shakhlin
@vshakhlin
@Oceania2018 this screen demonstrate difference your current code on the left I add on the right my changes
image.png
Haiping
@Oceania2018
@uzfm You can input multiple images at one time.
@vshakhlin You're correct, do you mind PR your fix?
1 reply
Haiping
@Oceania2018
@vshakhlin The fix is working great.
Zulfadhli
@ampangboy
Hi, I have a problem with using np.where or condition operator using numpy.net. Seems like numpy net doesn't implement operator but looking resources online, its looks like it have been implemented. Is there any troubleshooting guide?
it says Operator '!=' cannot be applied to operands of type 'NDarray' and 'int'
dss
@digital-synapse
Really love the project and what you all are trying to do but it looks like there are still some gaps in support / implementation. Any guesses when we will see a 1.0 release? Would love to be able to use tensorflow.net in production environments!
Haiping
@Oceania2018
@digital-synapse Next release will be v0.70.x aligned with tensorflow v2.7.0 & keras v2.7.0.
After that we're planning to release v1.0, it's usable in production in terms of architectual. Acutally it's already stable although there are many APIs/ NumPy not implemented, those missing stuff will be added gradually.
@digital-synapse Thanks for your fixing PR.
dss
@digital-synapse
Thanks hailing. Glad to help. Any tips for getting 100% core utilization when using the CPU backend?
Franck Malka
@franckm:matrix.org
[m]
Hi does any know if it is possible to recognize keywords from audio streams using tensorflow.net?
Haiping
@Oceania2018
Franck Malka
@franckm:matrix.org
[m]
Yes, i saw that. I am looking for the equivalent in tensorflow.NET
am new to this
Franck Malka
@franckm:matrix.org
[m]
or some guide that maps the terms from python to .net
Haiping
@Oceania2018
@franckm:matrix.org Just use the almost same syntax to translate.
Or you can use https://www.bountysource.com/ to ask someone else to help.
Sam Luedtke
@thislookshard
Has anyone been able to figure out a workaround for loading HDF5 (.h5) Keras models and using them for prediction? It's crucial to my project that these models can be imported directly without having to rebuild them every time, but there still doesn't seem to be a keras.models.load_model() implementation that returns a usable model yet.
Haiping
@Oceania2018
@thislookshard You just need to build once and load_weights. build model is not time cosuming.
Zulfadhli
@pallynor:matrix.org
[m]
hey.. any body experience memory leak in numpy.net?
uzfm
@uzfm
Hello.If I want to make a prediction two different models I receive error. Everything is going for the first time good. In second time I get error on the first model.
Haiping
@Oceania2018
@uzfm Are you using the latest version?
uzfm
@uzfm
ver. 0.3.1
Haiping
@Oceania2018
I'm saying the tf .net version.
uzfm
@uzfm
Tf 1.15.1
Haiping
@Oceania2018
As I know the latest version fixed the issue.
uzfm
@uzfm
image.png