EvalWrapper evaluate method for 3D inputs (features = ImageInput(...)...)
Hello, thank you for CNTK. Fair warning: I am an EE playing around with software.
Anyway, I am trying to write a program in C# to use the EvalWrapper to evaluate the CIFAR-10 trained networks from the Microsoft tutorials. I am following the CSEvalClient example, but just switching to using CIFAR-10 networks instead of MNIST networks.
I have not been successful -- when the 01_Conv network is loaded and called with (model.Evaluate(...)) no input is returned. I just started digging through CNTK source code, but it seems like 01_Conv.cntk network might be able to use the Evaluate method since it shares a reader UCIFastReader with the MNIST example.
On the other hand it's looking like the Evaluate method for the IEvaluateModelManaged
Does the Evaluate wrapper only work for networks that take 1D inputs? Is this just not implemented yet?
Thanks for any help!
Hi LowerySJ, First of all, we won't hold it against you for being an EE and not a CSE...Nobody is perfect. :-)
I am explaining more than you requested, just so that anyone else looking at this issue gets a bit more context.
Looking at the 01_Conv.cntk file, it shows in the reader section that the "features" layer has a dimension of 3072. If you also look at the 01_Convolution.ndl, is shows that the features are a combination of width, height and Channel (32 * 32 * 3 = 3072), so each row in the data file (train.txt or test.txt) corresponds to a single image (input), this is equivalent to a matrix of 32 x 32 with a Tensor element of 3. However, there are 3073 values in each line, this is due to the first number corresponding to the Index of the expected output label. (label(1) data(3072)) The C# Evaluate method uses a dictionary to map input (and output) layers to the data, so in this case a layer named "features" with a vector of size 3072 is used for input. Once you've trained your model, you can evaluate it using the C# interface, by loading one line at a time from the Test.txt file, removing the first number (before the first tab, as it corresponds to the output label number 0-9). Then convert each number (separated by tab) from string to float and pass them in the input layer named "features". Here is a snippet:
// model has already been created
string dataFile = Path.Combine(Environment.CurrentDirectory, @"test.txt");
foreach (var line in File.ReadLines(dataFile))
{
var features = line.Substring(line.IndexOf('t') + 1).Split('\t').Select(float.Parse).ToList();
var inputs = new Dictionary<string, List<float>>();
inputs.Add("features", features);
// We can request the output layer names(s) and dimension, or specify it if known.
var outDims = model.GetNodeDimensions(NodeGroup.nodeOutput);
outputLayerName = "OutputNodes.z"; //outDims.First().Key;
// We can call the evaluate method and get back the results (single layer)...
outputs = model.Evaluate(inputs, outputLayerName);
OutputResults(outputLayerName, outputs);
}
Note: the number that gets removed at the beginning of each line (before the first tab in the line) corresponds to the index of the labels. When you get the output listed, the INDEX of the number with the highest value corresponds to the INDEX of the label.
The latest CSEvalClient has been simplified, it should be easier to follow.
Thanks, Gaizka
Hi, Gaizka. Thanks for your help so far!
My problem was that I was using the wrong output layer name ("ol.z" rather than "OutputNodes.z"). (o1.z is a layer in the 01_Convolution.ndl file, but it is output as "OutputNodes.z").This change allowed me to run the CIFAR-10 test set through the 01_Convolution.cntk example, getting the expected 80% accuracy.
As a note to other people who are following, there is a specific format for the list
[0th row of Red pixels, 1st row of red pixels... Nth row of red pixels, 0th row of green, 1st row of green... Nth row of green, 0th row of blue, 1st row of blue... last row of blue]
I am now trying to figure out how to run the 03_ResNet.cntk example. It isn't throwing an error, but it isn't evaluating the test data at the expected 92% accuracy. Gaizka, you mentioned in the last post that the features definition for the reader in the .cntk file determines how the data should be put in. For the 03_ResNet.cntk example, this section is
' features = [ width = 32 height = 32 channels = 3 cropType = "random" cropRatio = 0.8 jitterType = "uniRatio" interpolations = "linear" meanFile = "$DataDir$/cifar-10-batches-py/CIFAR-10_mean.xml" '
Does this change how the image data should be passed to the IEvaluateModelManagedF.Evaluate method? Using the format that worked for the 01_Convolution.cntk model, which I described above (red rows then green rows then blue rows), I am getting 70% accuracy with the 03_ResNet model rather than 92% but no error is thrown.
Are these formats documented somewhere? Is there a name for these image data representations?
Thank you for any help!
EDIT: It's looking like this reader is expecting the data to have the averages subtracted out, where the averages are given in the meanFile. I'm starting to get better accuracies by subtracting out these mean values...
Ok, so the order of data is different for 03_ResNet. It's
blue rows, green rows, red rows.
I altered the order of the data given to the IEvaluateModelManagedF.Evaluate function AND subtracted out the averages given in cifar-10-batches-py/CIFAR-10_mean.xml.
Now I'm getting the expected 92% accuracy.
Again though: is there documentation for the form of the inputs for the C# wrapper?
Thanks!
Hi LowerySJ,
I am glad to hear you're up and running.
The input order really depends on how the model is configured (and trained). The model for 01_Conv.cntk uses the data from CIFAR (http://www.cs.toronto.edu/~kriz/cifar.html), there you will notice under the "Dataset Layout" the format that you mentioned (first the red plane, then the green plane, then the blue plane). Other datasets may work differently (as you discovered). Typically, each dataset provider will document their format on their website, we usually provide some link in the ReadMe files. I will add some documentation to the Wiki with regards to how the EvalDLL (which lies underneath the C# wrapper) converts the vector to the minibatch used by the network.
As an additional node, the updated EvalWrapper has been committed, it should make calling the Evaluation method easier/less code.
I will close this issue.
Best regards, Gaizka
For future references, here are two stackoverflow threads which address some issues regarding processing images using CNTK's EvalWrapper API: http://stackoverflow.com/questions/37300946/how-to-use-rgb-image-as-input-for-the-c-sharp-evaldll-wrapper/37661111#37661111 http://stackoverflow.com/questions/36990833/c-sharp-wrapper-for-cntk-steps/36991145#36991145