dmlc / cxxnet

move forward to https://github.com/dmlc/mxnet

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Does CXXNET support grey scale images?

haowu3 opened this issue · comments

Hi,

I used img iterator (image list) for grey images, but it gives a shape mistach error. Then I traced into the program and found LoadImage function used in ImageIterator class loads images as RGB images. Its output will always have 3 channels.

Does any data iterator supports grey scale images?
If not, will implementing such an iterator work well? or there is some other part of the project also assumes all images are RGB therefore that will not work?

A fast workaround is to convert the grayscale image into color image... If you want to use grayscale image, you can implement a new data iterator for it. Other part does not assume this.

I made a quick and dirty version of the grayscale img iterator.
It worked.. no rgb assumptions further in the pipeline..

...
inline static void LoadImage(mshadow::TensorContainer<cpu,3> &img, DataInst &out,const char fname, int grayscale) {

cv::Mat res = cv::imread(fname);
utils::Assert(res.data != NULL, "LoadImage: Reading image %s failed.\n", fname);
//utils::Assert(img.size(0) == 1, "Grayscale requires one channel.\n");
int channel_count = 3;
if (grayscale!=0) channel_count = 1;

img.Resize(mshadow::Shape3(channel_count, res.rows, res.cols));
for(index_t y = 0; y < img.size(1); ++y) {
  for(index_t x = 0; x < img.size(2); ++x) {
    cv::Vec3b bgr = res.at<cv::Vec3b>(y, x);
    if (!grayscale) { 
        // store in RGB order
        img[2][y][x] = bgr[0];
        img[1][y][x] = bgr[1];
        img[0][y][x] = bgr[2];
    }
    else {
        img[0][y][x] = bgr[0];
    }
  }
}
out.data = img;
// free memory
res.release();

}
...

Hi @juliandewit Could you fix your coding style and submit a PR on this? I would like to merge it into master

Hello,
Atm I made a very wild "practise download" for a kaggle competion I'm doing.
I made a big mess of it but I learned a lot about the codebase.

In a few days I intend to make a real fork and then I hope that I can contribute.

Thanks @juliandewit Good luck with your competition!

Hi, @winstywang ,
I added an as_grey option to img and imgrec iter. I can see the loss decreases while training so I guess it works.
But then I found ImageAugmenter::Process looks to assume RGB images as well, is it true?

Well, I found there are 3 functions of ImageAugmenter::Process(). The one which is actually called has no problem. The rest two may have problems but I'm not sure.

Anyway, it seems to work. The PR is #229