CNTK icon indicating copy to clipboard operation
CNTK copied to clipboard

Need a tool to create meanFile

Open rold2007 opened this issue 10 years ago • 8 comments

I would like to use ImageNet config files (.cntk) with my own training data. Unfortunately this kind of network depend on a mean file. CNTK doesn't offer (yet) any tool to generate this mean file for custom data. Will such a tool be developed soon ? How bad is it not to use a mean file with this kind of network (loss of accuracy, other?) ?

rold2007 avatar Feb 18 '16 01:02 rold2007

Hi David, I will check in the xml file for ImageNet today. The only reason it's needed is to avoid recomputing it on each run. For your own training data, the same (or similar) functionality can be achieved by using Mean node or, even simpler, by subtracting an approximate pixel mean (e.g. 128) from your features. See this example: https://github.com/Microsoft/CNTK/blob/master/Examples/Image/Miscellaneous/CIFAR-10/01_Convolution.ndl Not using mean (or approximate mean) may produce worse results, however, you can still try it. Another option is to scale your inputs (see MNIST example) but then you will need to update your hyper-parameters (e.g. learning rate) accordingly.

Alexey-Kamenev avatar Feb 19 '16 00:02 Alexey-Kamenev

Ok, thanks I'll try using the constant subtraction to see if it helps. At the moment my result don't make much sense. The test phase said my error ratio is 4.5% but when I use the C# wrapper my output is between 19 and 27. I was expected something between 0.0 and 1.0 or 0.0 and 100.0. I'm not sure if the issue comes from my input (not sure if the input format is RGB, BGR, RRR...-GGG...-BBB... and if it expects to loop y-first as it often happens with Matlab), the mean or something else. Tomorrow I'll try to trace through the test phase to see how the input and output are processed. I noticed that a tutorial was taking form, and this is really appreciated, but more documentation is really needed! I'll update/close this issue once I will have done my tests in case I can give more info about the importance of the mean.

rold2007 avatar Feb 21 '16 07:02 rold2007

I did some trainings with and without a hardcoded mean of 128 and so far I don't see much change to the accuracy. Maybe I'll have to try with bigger train/test sets. I asked about it on StackOverflow and the only answer I had says that it should improve the accuracy.

@Alexey-Kamenev I still don't see the ImageNet mean file in the repository, is it waiting to be merged ? Also, as explained on StackOverflow the mean file should be computed on my own training data (obviously!) so I'm not sure it is really useful to add this file as anyone who has access to the AlexNet will have access to the mean file. Instead, I think we should go back to my original question which is "Need a tool to create meanFile". Do you have such a tool ? If not, I can create one, but if I do so will you reference to it, do you have any restriction on how I should create it (I'm planning to build it in C#) ?

rold2007 avatar Mar 03 '16 19:03 rold2007

As for the mean file script: when I added CIFAR-10 I also added a script which converts CIFAR dataset to actual images and creates mean file along the way: Examples/Image/Miscellaneous/CIFAR-10/CifarConverter.py. Look for saveMean function. Not sure if this will work equally well for large datasets like ImageNet but you can port it to C# (actually, this is exactly what I did for ImageNet - wrote a simple, ugly C++ app which computed mean and saved it to file :) ).

Alexey-Kamenev avatar Mar 03 '16 22:03 Alexey-Kamenev

The Mean node can be used to compute the mean. The intended way to create a meanfile with this is:

  • create a simple model with a Mean node
  • train with maxEpochs = 0
  • use the "dumpnode" action with printMetadata = false, which generates the right format to be read as a mean file into a LearnableParameter

Unfortunately, there are two problems currently:

  • A known pending bug (Issue #181) that breaks Mean for >1D tensors, such as images. This is on my plate. I think a possible workaround could be to use a regular Input instead of an ImageInput with a dimension of the product of all 3 image dimensions.
  • Setting maxEpochs to 0 will currently skip the entire training altogether. The workaround would be to set maxEpochs to 1, wait until the PreCompute action has completed, which will write out a checkpoint model, then kill the job. Use that checkpoint model for the "dumpnode" step. Sorry for the inconvenience, I created Issue #199 to address this.

It would be great if you could let me know if you get it to work. We can then include this in the documentation (hopefully the need for the workarounds would be fixed as well).

frankseide avatar Mar 04 '16 01:03 frankseide

Ok, thanks for your suggestions. I'll try to have a look at this in the next few days and let you know about the result.

rold2007 avatar Mar 04 '16 05:03 rold2007

Sorry, I was referring to something else: A file that contains the mean in a representation to initialize a LearnableParameter from. You probably can still use the above approach and convert the file. @Alexey-Kamenev, you know better about the format of the mean file and how easy/hard it would be to create it from a flat text file of numbers.

BTW Issue #181 is fixed, the Mean operation should work now for any tensor dimension.

frankseide avatar Mar 05 '16 04:03 frankseide

@frankseide : I tried to apply your suggestions but many things are still unclear to me so I couldn't achieve anything useful. For my tests I used this version (which should include #181): 41a05a51a151f1a12a1411655d0e51a4dbe2bf46

  • Create a simple model with a Mean node: This is better said than done. I saw some references about the Mean node in Tutorial2 and in the CNTK book. Trying to add this to my AlexNet configuration file was a failure. I had too many issues (error messages, crashes, etc.) to report them here, most were probably my mistake but modifying the .cntk/.ndl/.mel files still feels like random trial-and-error to me. Most error messages are internal to CNTK (like "%ls %ls operation: Sample [%s] is not a column vector or matrix (1D or 2D tensor).") and give me no clue about what I did wrong in the configuration.

I was still able to generate a model.0 file using your instructions (stopping right after PreCompute) but either I didn't use the mean node properly or dumpnode doesn't work as expected. The output had two lines and nothing that looks like a mean file:

CE=CrossEntropyWithSoftmax ( labels , OutputNodes.z ) 
conv1.b=LearnableParameter

If I wait for #199, will it be more straightforward to produce the mean file ?

@mradmila : I don't understand why you closed this issue.

rold2007 avatar Mar 16 '16 19:03 rold2007