How to interpret the output from AlexNet
I've been trying to get a meaningful output from my model trained with the AlexNet configuration (slightly modified) on my own data. I've used the C# wrapper to test my model on some test data, but I have no idea what to do with the data and if I'm even looking at the right thing.
Modifications I made to AlexNet: I changed the number of labels from 1000 to 2 as I'm training a binary classifier. I added a constant mean of 128 as suggested by #132.
I modified the CSEvalClient project to use my model and read the "OutputNodes.z" node. I traced through the training code to see how the data was processed, so as I saw in the code I made sure to load my image data as BGR-BGR-BGR-... and subtract the mean.
Now, no matter which image I process, the result for OutputNodes.z[0] is always close to -7.0 and OutputNodes.z[1] is always around +8.0, more or less 1.0 or 2.0. So all images, labelled as "0" or "1" have similar output, which would lead me to say that ALL samples have a label of "1" (hypothesis: threshold the output at 0.0, I have no idea if this is true). I applied the "Test" command and the ErrorPrediction is about 1.5%. I was easily able to identify the images which are detected as fails by the Test and I see nothing special in their output (still -7.0/+8.0).
I tried to debug the code while running the Test command (on GPU) but I got lost and couldn't figure out where the decision was taken about if a sample is a pass or a fail. I tried to trace with CPU-only but the Test ErrorPrediction is 30% on CPU and I hit an assert deep in the code. I guess the CPU code isn't production-ready yet.
Could someone point to me where I should look in the code to see how the pass/fail decision is taken, this way I could more easily compare what happens when I run my code from C# and see if I made a mistake somewhere with the input/output ?
Also, it would be nice if you could document how to actually integrate a model in a real application. I saw that two new tutorials have been put in place very recently but they still lack any information on how to use the models from a C# or even a C++ application.
This issue is related to #144 ( @NexYY), I'm not sure if this guy finally succeeded at doing something with his code.
Hi David, How did you train your network on GPU? In particular, did you use cuDNN engine or legacy?
And could you share the training- progression, such as the TrainingLoss and EvalErr?
What is your prior distribution? How many images are passes vs. fails in your training set?
If that ratio is somewhat balanced, then an ErrorPrediction value of 1.5% means that the training learned something, then we need to look software. If the distribution is very off-balance (e.g. 1.5:98.5), then the network may simply have learned to output a constant, and it would be a convergence problem (inappropriate SGD configuration).
From: Alexey Kamenev [mailto:[email protected]] Sent: Thursday, February 25, 2016 15:10 To: Microsoft/CNTK [email protected] Subject: Re: [CNTK] How to interpret the output from AlexNet (#160)
Hi David, How did you train your network on GPU? In particular, did you use cuDNN engine or legacy?
— Reply to this email directly or view it on GitHubhttps://github.com/Microsoft/CNTK/issues/160#issuecomment-189025692.
Basically, if you just changed the output layer dims and nothing else then this sample will use cuDNN engine with corresponding layout. Now, cuDNN layout means your image has to be in BBB..., GGG..., RRR format, not R,G,B...,R,G,B. Can you please try changing your input layout and see if it helps?
@Alexey-Kamenev I see the following line in my NDL file, so I guess I run with cudnn. features = ImageInput(ImageW, ImageH, ImageC, tag = feature, imageLayout = "cudnn") Note that after the last epoch training I see the following warning 8 times in a row: "WARNING: trying to use cuDNN on unsupported platform. It is safe to ignore the warning if it's produced during model editing command." I see from the code that this warning appears when the Engine mode is Legacy. The commands I run are "Train:AddTop5Eval:Test". I don't specifically use the edit command, but I see in the doc (https://github.com/Microsoft/CNTK/wiki/Top-level%20commands) that using a MEL script seems to call the edit command, and the AddTop5Eval is a MEL script, so I guess I can ignore this warning.
@frankseide My training data ratio is quite unbalanced -> 16(pass):84(fail) and my test data ratio is also unbalanced -> 6(p):94(f). My train/test statistics are: Finished Epoch[90 of 90]: [Training Set] TrainLossPerSample = 0.041771609; EvalErrPerSample = 0.015000001; AvgLearningRatePerSample = 2.3437501e-006; EpochTime=29.5416 Final Results: Minibatch[1-53]: Samples Seen = 6745 Err: ErrorPrediction/Sample = 0.028762046 CE: CrossEntropyWithSoftmax/Sample = 0.087509308 Perplexity = 1.0914524
And if I run the Test command on a test dataset composed of only pass samples I get an error rate of 1,25%, so I guess it is learning something. Final Results: Minibatch[1-4]: Samples Seen = 400 Err: ErrorPrediction/Sample = 0.0125 CE: CrossEntropyWithSoftmax/Sample = 0.034617463 Perplexity = 1.0352236
Note that it would be nice to have more documentation on these output, along with a valid range of values and their interpretation. I can guess some of them but I have no clue of the meaning of others (like perplexity). Right now maybe some of these values clearly indicate that something is wrong with my training/testing/data/etc. but I can't even know!
Also, sorry for repeating myself, but what output values should I expect. I have two output values, in which range should they be, [0.0, 1.0] ? Should there always be one positive and one negative value ? How do I know which label is the "detected" one ? I was expecting some kind of probability as output, but I don't see how my current output of -7/+8 could be a probability.
@Alexey-Kamenev I updated my input data format and now the output is a little bit different, sometimes (175/800=22%) the OutputNodes.z[0] is higher than OutputNodes.z[1], which was never the case before, but if this is the condition to determine which label is detected, I'm still very far from the expected error rate. One thing that I noticed is that the input image has to be resized to fit in the 224x224 network input. Right now my images have different resolution (i.e. 399x365, 426x180, 410x146, etc.) so I resize them to 224x224 without keeping the original aspect ratio. Is this ok, how is it done when training/testing ? Again, this (the image data input format) should be documented somewhere, right now it is only a guessing game.
Let me know if you need more info and thanks for your help!
A 16:84 prior ratio sounds sane. I would not expect the system to go into extremes where it finds it is easier to just output the same class all the time (I do see such behavior with CTC models where one class is hundreds or thousands times more likely than all others).
Also, both your training and eval errors (2.876%, 1.25%) are way better than chance.
The cuDNN warning is because the MEL command is “executed” on the CPU (MEL loads the model into the CPU rather than the GPU), for which “cudnn” is not implemented. Since MEL does not actually perform math on the model, this warning can indeed be ignored.
However, if you end up using the CPU code on a GPU-trained model, you will get garbage since the CPU routines interpret the data differently (which is what the warning tells you). So we should verify whether the C# wrapper does use the GPU. (If not, you should get the above warning, but maybe it is swallowed; not sure how stderr/logging is set up for the C# wrapper). One way to find out could be the “write” command.
This is your own data set, right? 6745 seems a rather small number. You may be overfitting. (Although your test error rate seems to counter this, assuming you did not accidentally have overlap between training and test data.) It may be worthwhile to cut the model parameters quite a bit.
Resizing your input images could be a problem if not done consistently between training and test.
Also, sorry for repeating myself, but what output values should I expect.
You are outputting outputNodes.z, which is the input to a Softmax function (which you don’t explicitly see in your graph because it is built into the CrossEntropyWithSoftmax criterion node, since that makes things faster). The input to the Softmax is equal to the log of the Softmax plus a common offset (log denominator).
If you want a value between 0 and 1, you can either add an explicit Softmax node to the graph, or convert it yourself (exp z / sum_all z’ exp z’). For classification (picking the max), both input and output of Softmax are equivalent.
So if you get +8 vs. -7, that means that the +8 output is exp(8-(-7)) more probable (something like 3 million). This is so far away from the test error rate you got, that I think your model is fine, we are dealing with a software configuration issue. I suspect GPU/CPU.
From: David Rolland [mailto:[email protected]] Sent: Friday, February 26, 2016 0:01 To: Microsoft/CNTK [email protected] Cc: Frank Seide [email protected] Subject: Re: [CNTK] How to interpret the output from AlexNet (#160)
@Alexey-Kamenevhttps://github.com/Alexey-Kamenev I see the following line in my NDL file, so I guess I run with cudnn. features = ImageInput(ImageW, ImageH, ImageC, tag = feature, imageLayout = "cudnn") Note that after the last epoch training I see the following warning 8 times in a row: "WARNING: trying to use cuDNN on unsupported platform. It is safe to ignore the warning if it's produced during model editing command." I see from the code that this warning appears when the Engine mode is Legacy. The commands I run are "Train:AddTop5Eval:Test". I don't specifically use the edit command, but I see in the doc (https://github.com/Microsoft/CNTK/wiki/Top-level%20commands) that using a MEL script seems to call the edit command, and the AddTop5Eval is a MEL script, so I guess I can ignore this warning.
@frankseidehttps://github.com/frankseide My training data ratio is quite unbalanced -> 16(pass):84(fail) and my test data ratio is also unbalanced -> 6(p):94(f). My train/test statistics are: Finished Epoch[90 of 90]: [Training Set] TrainLossPerSample = 0.041771609; EvalErrPerSample = 0.015000001; AvgLearningRatePerSample = 2.3437501e-006; EpochTime=29.5416 Final Results: Minibatch[1-53]: Samples Seen = 6745 Err: ErrorPrediction/Sample = 0.028762046 CE: CrossEntropyWithSoftmax/Sample = 0.087509308 Perplexity = 1.0914524
And if I run the Test command on a test dataset composed of only pass samples I get an error rate of 1,25%, so I guess it is learning something. Final Results: Minibatch[1-4]: Samples Seen = 400 Err: ErrorPrediction/Sample = 0.0125 CE: CrossEntropyWithSoftmax/Sample = 0.034617463 Perplexity = 1.0352236
Note that it would be nice to have more documentation on these output, along with a valid range of values and their interpretation. I can guess some of them but I have no clue of the meaning of others (like perplexity). Right now maybe some of these values clearly indicate that something is wrong with my training/testing/data/etc. but I can't even know!
Also, sorry for repeating myself, but what output values should I expect. I have two output values, in which range should they be, [0.0, 1.0] ? Should there always be one positive and one negative value ? How do I know which label is the "detected" one ? I was expecting some kind of probability as output, but I don't see how my current output of -7/+8 could be a probability.
@Alexey-Kamenevhttps://github.com/Alexey-Kamenev I updated my input data format and now the output is a little bit different, sometimes (175/800=22%) the OutputNodes.z[0] is higher than OutputNodes.z[1], which was never the case before, but if this is the condition to determine which label is detected, I'm still very far from the expected error rate. One thing that I noticed is that the input image has to be resized to fit in the 224x224 network input. Right now my images have different resolution (i.e. 399x365, 426x180, 410x146, etc.) so I resize them to 224x224 without keeping the original aspect ratio. Is this ok, how is it done when training/testing ? Again, this (the image data input format) should be documented somewhere, right now it is only a guessing game.
Let me know if you need more info and thanks for your help!
— Reply to this email directly or view it on GitHubhttps://github.com/Microsoft/CNTK/issues/160#issuecomment-189154215.
David, could you please let me know how you are initializing the C# wrapper? Are you passing a config file or string, and which one?
As for the warning: yes, it is safe to ignore the warning if it is produced during model editing command (MEL) which is exactly your case.
As for the testing and image transformations, CNTK can perform the same transformations in the test stage as during the training. This is specified in the corresponding reader section. The differences are that during testing CNTK takes the center crop (cropType=Center) with no scale jittering (cropRatio=1 - default) while during the training it is usually random crop (cropType=Random) with scale jittering (e.g. cropRatio=0.46666:0.875). Also, there is no horizontal flip during testing.
In your code you should do the same: take the center crop and isotropically(!) rescale it to match input layer size (e.g. 224x224x3).
I agree, we should have this in our sample code, let me see if I can come up with something quick.
@frankseide & @Alexey-Kamenev : I finally managed to obtain the same results with the C# wrapper as with the test command using CNTK.exe. The remaining issues were caused by the crop and the mean.
Doing an isotropic crop, and centered, is indeed important. I had seen how the resize was done in the code but for some reason I had skipped over the crop part which was my mistake. Once this was corrected my results were better but still not equivalent to the test phase. My input data processing so far looked like this: LoadImage->Crop->ResizeTo224x224->ExtractToArray->SubtractMean(128)->SendToModelForEvaluation. So I just tried without removing the mean and this did the trick. I didn't take the time to trace through the code to see when the mean is applied but I guess that since it is defined in the NDL it is executed by the wrapper's Evaluate() method so that my mean subtraction was one time too much. I hope this can help anyone using the wrappers. If anyone else has issues with the wrapper don't hesitate to ping me in case I already hit the same problem.
Now, just to answer what you suggested in a previous post:
- Yes, I double-checked that I was executing my wrapper on GPU and I made sure I wasn't triggering the "trying to use cuDNN on unsupported platform..." warning. I had already noticed that running on CPU wasn't giving interesting error rates and I thought it was caused by the CPU code not yet finished so I didn't spend more time with CPU anymore.
- I'm currently practicing on Caltech101 data as I don't have access to AlexNet. This explains the limited number of samples, but I thought that the images would be similar hence the network configuration would be adapted to this dataset.
- @frankseide I haven't finished reading the CNTK book (my understanding is still limited that's why I used a prebuilt model configuration) so I'm not sure what you mean by "cut the model parameters...". Is the it maxEpochs, the SGD, etc. ?
- @frankseide thanks for the Softmax probability computation equation, I have meaningful results now!
- My C# code is directly inspired from your CSEvalClient project so the model loading is done the same way. And my model configuration is directly inspired from AlexNet example where I disabled the mean file as I don't have it (replaced by hardcoded mean of 128).
In conclusion, please add more documentation on how to get from zero to production code.
Thanks for you efforts, I appreciate your efficiency at answering all issues reported for CNTK.
David, thank you VERY much for writing this! We definitely have some work to do to improve our samples so people don't have to repeat the same steps all over again. To answer your question about mean: mean subtraction in case you are using mean file is applied in ImageReader transform so in such case it has to be done "manually" in the client.
Hi Guys, I am beginner in Microsoft CNTK but I have some theoretical knowledge on machine learning, deep learning and even c# programming. I try to implement the Microsoft CNTK c# code in my own microsoft visual studio but could not success. I tried with the help of https://github.com/Microsoft/CNTK/wiki/Setup-CNTK-on-Windows. It will be my pleasure if you provide detail information or detail steps to implements c# code in own way.