SciSharp / SciSharp-Stack-Examples

Practical examples written in SciSharp's machine learning libraries
http://scisharpstack.org
Apache License 2.0
319 stars 103 forks source link

Unable to let ImageBackgroundRemoval to works #13

Closed Udanita closed 3 years ago

Udanita commented 4 years ago

Hi all and you are amazing; this is a question. I'm trying to create a simple function for my wpf project that remove background from a given image. my relevant piece of code is:

        'private static NumSharp.NDArray ReadTensorFromImageFile(string file_name,
                            int input_height = 224,
                            int input_width = 224,
                            int input_mean = 117,
                            int input_std = 1)
        {
        var graph = tf.Graph().as_default();
        var file_reader = tf.read_file(file_name, "file_reader");
        var decodeJpeg = tf.image.decode_jpeg(file_reader, channels: 3, name: "DecodeJpeg");
        var cast = tf.cast(decodeJpeg, tf.float32);
        var dims_expander = tf.expand_dims(cast, 0);
        var resize = tf.constant(new int[] { input_height, input_width });
        var bilinear = tf.image.resize_bilinear(dims_expander, resize);
        var sub = tf.subtract(bilinear, new float[] { input_mean });
        var normalized = tf.divide(sub, new float[] { input_std });

        using (var sess = tf.Session(graph))
            return sess.run(normalized);
    }
    public bool Run(string fileName)
    {
        var nd = ReadTensorFromImageFile(fileName);
        var graph = new Graph();
        graph.Import("frozen_inference_graph.pb");
        Tensor output = graph.OperationByName("SemanticPredictions");
        Tensor imgTensor = graph.OperationByName("ImageTensor");
        using (var sess = tf.Session(graph))
        {
            var results = sess.run(output, new FeedItem(imgTensor, nd));
            return true;
        }

        return false;
    }

`

I got this error: OverflowException: Overflow_Byte NumSharp.Backends.Unmanaged.UnmanagedMemoryBlock.CastTo.AnonymousMethod25(long) in UnmanagedMemoryBlock.Casting.cs System.Threading.Tasks.Parallel.ForWorker64.AnonymousMethod1() System.Threading.Tasks.Task.InnerInvokeWithArg(System.Threading.Tasks.Task) System.Threading.Tasks.Task.ExecuteSelfReplicating.AnonymousMethod__0(object)

what i'm doing wrong? (obviously lot of things...)

dahanco commented 4 years ago

can you make it works? because I almost have the same problem

Udanita commented 4 years ago

I left that project Dahanco.... Maybe if an expert will give an eye on that, he can solve quickly for sure

emepetres commented 4 years ago

Same issue here, it's a pitty we cannot run deeplab on Tensorflow.Net

malhosary commented 3 years ago

I was able to fix it by commenting these 2 lines:

 var sub = tf.subtract(bilinear, new float[] { input_mean });
 var normalized = tf.divide(sub, new float[] { input_std });