CNTK icon indicating copy to clipboard operation
CNTK copied to clipboard

CSEvalClient: Multiple input values - batches

Open Mikoor opened this issue 10 years ago • 4 comments

Hi all,

I successfully use CSEvalClient for my DNN application in C#. I’d like to ask, is there a way to evaluate multiple input values (images) or batches at once? It would significantly improve our performance.

Something like: var dictionary = new Dictionary<string, List<float>> { { "features1", imageTensor1 }, { "features2", imageTensor2 } }; var output = evaluateModelManagedF.Evaluate(dictionary, “OutputNodes.z”); //where output is f.e. Dictionary<string, List<float>>

Thanks for your response.

I wish you all the good luck with CNTK.

Regards, Miro

Mikoor avatar Jun 14 '16 11:06 Mikoor

I was wondering the same thing with the C++ EvalDll, but it pretty much ends up to the same question.

julienlopez avatar Jun 15 '16 08:06 julienlopez

Yes you can batch multiple sets in the input vector, and expect the output also in the output vector. The interface is not as user-friendly as proposed by Mikoor (but we will take his suggestion for review :-) ) In the meantime you can do the following (note this is a simple Times(Input, Constant) ) network used for illustration purposes only:

private static void EvaluateModelBatchedInput()
    {
        try
        {
            string modelDefinition = "precision = \"float\" \n" +
                "traceLevel = 1 \n" +
                "run=NDLNetworkBuilder \n" +
                "NDLNetworkBuilder=[ \n" +
                "i1 = Input(1) \n" +
                "o1 = Times(Constant(3), i1, tag=\"output\") \n" +
                "FeatureNodes = (i1) \n" +
                "] \n";

            Dictionary<string, List<float>> outputs;

            using (var model = new IEvaluateModelManagedF())
            {
                // Create the network
                model.CreateNetwork(modelDefinition, deviceId: -1);

                // We can call the evaluate method and get back the results (single layer)...
                // We are passing 3 values per batch, but the network computes a result for each value
                Dictionary<string, List<float>> inputs = new Dictionary<string, List<float>>() { { "i1", new List<float>() { 1, 2, 3 } } };
                outputs = new Dictionary<string, List<float>>() { { "o1", new List<float>(2) {0, 0, 0} } }; ;
                model.Evaluate(inputs, outputs);
            }

            OutputResults("o1", outputs["o1"]);
        }
        catch (CNTKException ex)
        {
            Console.WriteLine("Error: {0}\nNative CallStack: {1}\n Inner Exception: {2}", ex.Message, ex.NativeCallStack, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: {0}\nCallStack: {1}\n Inner Exception: {2}", ex.Message, ex.StackTrace, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception");
        }
    }

So, basically the network defines the dimensions of the input and output nodes, from there it "takes" the necessary values from the input, and computes the corresponding output(s). It then repeats this until all values are consumed. The proposed method could be implemented as an extension method.

Let me know if this answers your questions, so I can close this issue. Gaizka

gaizkan avatar Jun 15 '16 11:06 gaizkan

Hi Gaizka,

Yes, thats exactly what I was looking for.

Meanwhile I also updated my copy of EvalWrapper.cpp, so I don't have to worry about creating and sizing of output dictionary "outputs". This way I can directly use Evaluate method overload with outputKey only and get output dictionary on return of Evaluate method.

Thank you for your promt response it helped me a lot.

Regards, Miro

Mikoor avatar Jun 16 '16 07:06 Mikoor

Correction: The evaluation library doesn't do batches. In my example it takes an input of [1 x *] and computes the product with a constant (which results in a [1 x *] vector). This is different than processing [1 x 1] with a constant 3 times. I haven't tested it with images, but I don't think this will work.

We've added this request for the next version of the evaluation dll. Thanks, Gaizka

gaizkan avatar Jun 16 '16 07:06 gaizkan