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

Added as_grey option to img and imgrec iterator #229

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions src/io/iter_image_recordio-inl.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ class ImageRecordIOParser {
dmlc::InputSplit *source_;
/*! \brief label information, if any */
ImageLabelMap *label_map_;
/*! \brief forces to load images as grey */
int as_grey_;
};

inline void ImageRecordIOParser::Init(void) {
Expand Down Expand Up @@ -210,6 +212,9 @@ SetParam(const char *name, const char *val) {
if (!strcmp(name, "label_width")) {
label_width_ = atoi(val);
}
if (!strcmp(name, "as_grey")) {
as_grey_ = atoi(val);
}
}

inline bool ImageRecordIOParser::
Expand All @@ -233,18 +238,36 @@ ParseNext(std::vector<InstVector> *out_vec) {
cv::Mat res;
rec.Load(blob.dptr, blob.size);
cv::Mat buf(1, rec.content_size, CV_8U, rec.content);
res = cv::imdecode(buf, 1);
if (as_grey_ == 1) {
res = cv::imdecode(buf, CV_LOAD_IMAGE_GRAYSCALE);
} else {
res = cv::imdecode(buf, 1);
}
res = augmenters_[tid]->Process(res, prnds_[tid]);
out.Push(static_cast<unsigned>(rec.image_index()),
mshadow::Shape3(3, res.rows, res.cols),
mshadow::Shape1(label_width_));
if (as_grey_ == 1) {
out.Push(static_cast<unsigned>(rec.image_index()),
mshadow::Shape3(1, res.rows, res.cols),
mshadow::Shape1(label_width_));
} else {
out.Push(static_cast<unsigned>(rec.image_index()),
mshadow::Shape3(3, res.rows, res.cols),
mshadow::Shape1(label_width_));
}
DataInst inst = out.Back();
for (int i = 0; i < res.rows; ++i) {
for (int j = 0; j < res.cols; ++j) {
cv::Vec3b bgr = res.at<cv::Vec3b>(i, j);
inst.data[0][i][j] = bgr[2];
inst.data[1][i][j] = bgr[1];
inst.data[2][i][j] = bgr[0];
if (as_grey_ == 1) {
for (int i = 0; i < res.rows; ++i) {
for (int j = 0; j < res.cols; ++j) {
inst.data[0][i][j] = res.at<uchar>(i, j);
}
}
} else {
for (int i = 0; i < res.rows; ++i) {
for (int j = 0; j < res.cols; ++j) {
cv::Vec3b bgr = res.at<cv::Vec3b>(i, j);
inst.data[0][i][j] = bgr[2];
inst.data[1][i][j] = bgr[1];
inst.data[2][i][j] = bgr[0];
}
}
}
if (label_map_ != NULL) {
Expand Down
50 changes: 39 additions & 11 deletions src/io/iter_img-inl.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ImageIterator : public IIterator< DataInst >{
shuffle_ = 0;
data_index_ = 0;
label_width_ = 1;
as_grey_ = 0;
}
virtual ~ImageIterator(void) {
if(fplst_ != NULL) fclose(fplst_);
Expand All @@ -35,6 +36,8 @@ class ImageIterator : public IIterator< DataInst >{
if(!strcmp(name, "silent" )) silent_ = atoi(val);
if(!strcmp(name, "shuffle" )) shuffle_ = atoi(val);
if(!strcmp(name, "label_width" )) label_width_ = atoi(val);
if(!strcmp(name, "as_grey" )) as_grey_ = atoi(val);

}
virtual void Init(void) {
fplst_ = utils::FopenCheck(path_imglst_.c_str(), "r");
Expand Down Expand Up @@ -70,11 +73,11 @@ class ImageIterator : public IIterator< DataInst >{
if (data_index_ < static_cast<int>(order_.size())) {
size_t index = order_[data_index_];
if (path_imgdir_.length() == 0) {
LoadImage(img_, out_, filenames_[index].c_str());
LoadImage(img_, out_, filenames_[index].c_str(), as_grey_);
} else {
char sname[256];
sprintf(sname, "%s%s", path_imgdir_.c_str(), filenames_[index].c_str());
LoadImage(img_, out_, sname);
LoadImage(img_, out_, sname, as_grey_);
}
out_.index = index_list_[index];
mshadow::Tensor<cpu, 1> label_(&(labels_[0]) + label_width_ * index,
Expand All @@ -92,16 +95,39 @@ class ImageIterator : public IIterator< DataInst >{
inline static void LoadImage(mshadow::TensorContainer<cpu,3> &img,
DataInst &out,
const char *fname) {
cv::Mat res = cv::imread(fname);
LoadImage(img, out, fname, 0);
}

inline static void LoadImage(mshadow::TensorContainer<cpu,3> &img,
DataInst &out,
const char *fname, int as_grey_) {
cv::Mat res;

if (as_grey_) {
res = cv::imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
} else {
res = cv::imread(fname);
}
CHECK(res.data != NULL) << "LoadImage: Reading image" << fname << "failed.";
img.Resize(mshadow::Shape3(3, 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);
// store in RGB order
img[2][y][x] = bgr[0];
img[1][y][x] = bgr[1];
img[0][y][x] = bgr[2];

if (as_grey_) {
img.Resize(mshadow::Shape3(1, res.rows, res.cols));
for(index_t y = 0; y < img.size(1); ++y) {
for(index_t x = 0; x < img.size(2); ++x) {
img[0][y][x] = res.at<uchar>(y, x);
}
}
}
else {
img.Resize(mshadow::Shape3(3, 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);
// store in RGB order
img[2][y][x] = bgr[0];
img[1][y][x] = bgr[1];
img[0][y][x] = bgr[2];
}
}
}
out.data = img;
Expand All @@ -125,6 +151,8 @@ class ImageIterator : public IIterator< DataInst >{
int label_width_;
// denotes the current data index
int data_index_;
// forces to load images as grey scale
int as_grey_;
// stores the reading orders
std::vector<int> order_;
// stores the labels of data
Expand Down