Skip to content
This repository has been archived by the owner on May 24, 2018. It is now read-only.

Does CXXNET support grey scale images? #200

Open
haowu3 opened this issue Jul 17, 2015 · 7 comments
Open

Does CXXNET support grey scale images? #200

haowu3 opened this issue Jul 17, 2015 · 7 comments

Comments

@haowu3
Copy link

haowu3 commented Jul 17, 2015

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?

@winstywang
Copy link
Contributor

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.

@juliandewit
Copy link

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();

}
...

@winstywang
Copy link
Contributor

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

@juliandewit
Copy link

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.

@winstywang
Copy link
Contributor

Thanks @juliandewit Good luck with your competition!

@haowu3
Copy link
Author

haowu3 commented Aug 18, 2015

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?

@haowu3
Copy link
Author

haowu3 commented Aug 20, 2015

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants