microsoft / CNTK

Microsoft Cognitive Toolkit (CNTK), an open source deep-learning toolkit
https://docs.microsoft.com/cognitive-toolkit/
Other
17.5k stars 4.29k forks source link

Keras CNTK model evaluating in c# #2398

Open aench2023 opened 6 years ago

aench2023 commented 6 years ago

Hi,

We have a model that is trained using Keras and CNTK, I would like to evaluate the model using C#, how do I go about doing that? Also the trained model is inception_v3

cha-zhang commented 6 years ago

Have you tried it? A few examples here: https://github.com/Microsoft/CNTK/tree/master/Examples/Evaluation

aench2023 commented 6 years ago

I tried the example, I am getting the following error:

Additional information: Values for 1 required arguments 'Input('input_1', [3 x ? x ?], [#])', that the requested output(s) 'Output('Softmax22978_Output_0', [39], [#])' depend on, have not been provided.

cha-zhang commented 6 years ago

Could you provide us a small repro for this? Thanks!

ritalaezza commented 6 years ago

I believe this is the same question as in issue #2250. I would like to know the correct code in order to achieve these steps:

1) Save a trained Keras model in such a way that it can be opened in a C# application. 2) Load model into C# application (with appropriate NuGet file installed) 3) Evaluate model with real-time data

Note: in my case I have a quite simple LSTM model.

aench2023 commented 6 years ago

As Rita mentioned, I was trying to do the same, I have an inceptionv3 model created using keras, I saved it as CNTK model and I am trying to read it into c#

aench2023 commented 6 years ago

I am using the code below:

Console.WriteLine("\n===== Evaluate single image =====");

            string modelFilePath = "inception_cntk";
            ThrowIfFileNotExist(modelFilePath, string.Format("Error: The model '{0}' does not exist. Please follow instructions in README.md in <CNTK>/Examples/Image/Classification/ResNet to create the model.", modelFilePath));
            Function modelFunc = Function.Load(modelFilePath, device);

            Variable inputVar = modelFunc.Arguments.Single();

            NDShape inputShape = inputVar.Shape;
            int imageWidth = inputShape[0];
            int imageHeight = inputShape[1];
            int imageChannels = inputShape[2];

            Variable outputVar = modelFunc.Output;

            var inputDataMap = new Dictionary<Variable, Value>();
            var outputDataMap = new Dictionary<Variable, Value>();

            string sampleImage = "00000.png";
            ThrowIfFileNotExist(sampleImage, string.Format("Error: The sample image '{0}' does not exist. Please see README.md in <CNTK>/Examples/Image/DataSets/CIFAR-10 about how to download the CIFAR-10 dataset.", sampleImage));
            Bitmap bmp = new Bitmap(Bitmap.FromFile(sampleImage));
            var resized = bmp.Resize((int)imageWidth, (int)imageHeight, true);
            List<float> resizedCHW = resized.ParallelExtractCHW();

            Value inputVal = Value.CreateBatch(inputVar.Shape, resizedCHW, device);
            inputDataMap.Add(inputVar, inputVal);
            outputDataMap.Add(outputVar, null);

            modelFunc.Evaluate(inputDataMap, outputDataMap, device);

            var outputVal = outputDataMap[outputVar];
            var outputData = outputVal.GetDenseData<float>(outputVar);

            PrintOutput(inputVar.Shape.TotalSize, outputData);

To convert the model to CNTK model I used the following code:

import cntk as C from keras.models import load_model keras_model = load_model('inceptionv3-ft.model') C.combine(keras_model.outputs).save('inception_cntk')

liqunfu commented 6 years ago

@akegramener. This does not make sense. There is only one argument as input. Is there any way you can share me the model. Or point me to the source so I can repro? Thanks

aench2023 commented 6 years ago

Below is the link to the model https://drive.google.com/file/d/0Bwfks2dAvyXcTW15TmVER0FPY1U/view

Also this is the link to the code https://github.com/akegramener/Model-Evaluation-CSharp

Thanks!

liqunfu commented 6 years ago

@akegramener Sorry for the delay. It looks like a bug of CNTK to handle free and inferred dimensions. The attached model has input with dimensions being -1 (inferred dim). However current CNTK can only handle input with free dimensions (-3). For example, this code works: x = C.input((-3,-3, 2)) data = np.random.random((10, 10, 2)) y = x + 1 y.eval({x:[data]})

But this code does not: x = C.input((-1,-1, 2)) data = np.random.random((10, 10, 2)) y = x + 1 y.eval({x:[data]})

Note that the attached model also has input dim being -1. I mark this issue as a bug and track with it. Thanks.