CNTK icon indicating copy to clipboard operation
CNTK copied to clipboard

Image reader

Open Arminea opened this issue 10 years ago • 7 comments

Hi, I'm trying to use Image reader. At this time I have a dataset of 64x64 images but I would like to use a dataset of images with various sizes. My questions are

  1. Do I need any external preprocessing, for resizing images?
  2. If not, which parameters of the Image reader should I use?

I'm asking because a mean file is used by image reader and I assume that this file is made of resized images. My last question is about label dimension. I want to perform binary classification. In tutorial there is LDim set to 1 but when I set a label dimension to 1 I get an exception EXCEPTION occurred: Image 'D:\CNTK\data\dir_train.zip@\Image1_.png' has invalid class id '1'. Expected label dimension is '1' so I had to set label dimension to 2.

3. Is my label dimension correct for binary classification of images?

Arminea avatar Aug 31 '16 15:08 Arminea

For the label dimension, CNTK internally thinks of labels as one-hot vectors. labelDim must be equal to the number of classes. If you have 2 classes, then the label is a 2-dimensional vector with a 1 at the position of the correct class. Your file, however, stores the index (class id) of this position.

For the resizing question, ImageReader resizes images for you with the "Scale" transform. E.g.

            features = { transforms = (
               { type = "Scale" ; width = 64 ; height = 64 ; channels = 3 ; interpolations = "linear" } :
               { type = "Transpose" }
            )}

The question regarding the mean file I will need to leave to @eldakms.

frankseide avatar Aug 31 '16 16:08 frankseide

The mean file is optional, you can also just uncomment or remove that line in the config. In that case you might want to subtract a constant value from your features for normalization in your network definition, for example

featNorm = features - Constant (114)

A constant value works fine for large image corpora like ImageNet for example. If you want to use a mean file then yes it computed as the average over all resized images in your training set.

pkranen avatar Sep 01 '16 07:09 pkranen

Philipp explained that perfectly. I have also updated the error message to be more descriptive (on latest master). Thank you!

eldakms avatar Sep 01 '16 13:09 eldakms

@pkranen How do you determine the constant value for normalization?

Arminea avatar Sep 05 '16 15:09 Arminea

It's the global average over all pixels and color channels of all image in your training set. For ImageNet we use 114 as the constant (RGB values range from 0 to 255).

pkranen avatar Sep 06 '16 07:09 pkranen

Note that the impact of this is not on the model's expressiveness (a constant offset can be compensated by the first layer's bias), but for training convergence (the first layer's matrix' gradient is input*gradient^T, an additive constant on the input will affect the gradient in a non-beneficial way)

With that in mind, assuming a mean of 128 will get you quite far already, compared to not normalizing it at all.

frankseide avatar Sep 06 '16 15:09 frankseide

Closing this. Feel free to reopen if necessary.

n17s avatar Sep 07 '16 23:09 n17s