caffe
caffe copied to clipboard
HDF5 for color images
Hi all,
I created a hdf5 with color images, but when I check the reading of hdf5, I get a grayscale image. Is it a bug of hdf5 or not?
Here the way I check, with my image input as h = 60, w = 60,
template <typename Dtype>
Dtype HDF5DataLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
const int batch_size = this->layer_param_.hdf5_data_param().batch_size();
const int data_count = (*top)[0]->count() / (*top)[0]->num();
const int label_data_count = (*top)[1]->count() / (*top)[1]->num();
LOG(INFO) << "Reading next: batch_size " << batch_size << " data_count " << data_count << " label_data_count " << label_data_count;
for (int i = 0; i < batch_size; ++i, ++current_row_) {
if (current_row_ == data_blob_.num()) {
if (num_files_ > 1) {
current_file_ += 1;
if (current_file_ == num_files_) {
current_file_ = 0;
LOG(INFO) << "looping around to first file";
}
LoadHDF5FileData(hdf_filenames_[current_file_].c_str());
}
current_row_ = 0;
}
memcpy(&(*top)[0]->mutable_cpu_data()[i * data_count],
&data_blob_.cpu_data()[current_row_ * data_count],
sizeof(Dtype) * data_count);
memcpy(&(*top)[1]->mutable_cpu_data()[i * label_data_count],
&label_blob_.cpu_data()[current_row_ * label_data_count],
sizeof(Dtype) * label_data_count);
//////////////////////////////////CHECK BEGIN ////////////////////////////////////////////
const Dtype* data = &(*top)[0]->mutable_cpu_data()[i * data_count];
int h = 60, w = 60;
cv::Mat img = cv::Mat(h,w,CV_8UC3);
for(int y = 0; y < h; y++){
for(int x = 0; x < w; x++){
int r = (int)(data[ 0 * w * h + y * w + x] * 255);
int g = (int)(data[ 1 * w * h + y * w + x] * 255);
int b = (int)(data[ 2 * w * h + y * w + x] * 255);
//if( r == g && g == b)
//LOG(INFO) << "gray image";
r = std::min(std::max(r,0),255);
g = std::min(std::max(r,0),255);
b = std::min(std::max(r,0),255);
img.at<cv::Vec3b>(y,x) = cv::Vec3b(r,g,b);
}// end x
}// end y
char fname[255];
sprintf(fname, "img_from_h5_%d.jpg", i);
cv::imwrite(fname, img);
//////////////////////////////////END CHECK/////////////////////////////////////////
}
return Dtype(0.);
}
That my stupid fault. Sorry, no problem in reading color image of hdf5.