Skip to content

Commit

Permalink
Merge pull request #4059 from crazytan/master
Browse files Browse the repository at this point in the history
handle spaces in image file names

Thanks @bchu for an earlier implementation.
  • Loading branch information
longjon committed May 18, 2016
2 parents 8523f5d + a8cc860 commit e79bc8f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
Binary file added examples/images/cat gray.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 6 additions & 3 deletions src/caffe/layers/image_data_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ void ImageDataLayer<Dtype>::DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
const string& source = this->layer_param_.image_data_param().source();
LOG(INFO) << "Opening file " << source;
std::ifstream infile(source.c_str());
string filename;
string line;
size_t pos;
int label;
while (infile >> filename >> label) {
lines_.push_back(std::make_pair(filename, label));
while (std::getline(infile, line)) {
pos = line.find_last_of(' ');
label = atoi(line.substr(pos + 1).c_str());
lines_.push_back(std::make_pair(line.substr(0, pos), label));
}

if (this->layer_param_.image_data_param().shuffle()) {
Expand Down
44 changes: 41 additions & 3 deletions src/caffe/test/test_image_data_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,24 @@ class ImageDataLayerTest : public MultiDeviceTest<TypeParam> {
std::ofstream outfile(filename_.c_str(), std::ofstream::out);
LOG(INFO) << "Using temporary file " << filename_;
for (int i = 0; i < 5; ++i) {
outfile << EXAMPLES_SOURCE_DIR "images/cat.jpg " << i;
outfile << EXAMPLES_SOURCE_DIR "images/cat.jpg " << i << std::endl;
}
outfile.close();
// Create test input file for images of distinct sizes.
MakeTempFilename(&filename_reshape_);
std::ofstream reshapefile(filename_reshape_.c_str(), std::ofstream::out);
LOG(INFO) << "Using temporary file " << filename_reshape_;
reshapefile << EXAMPLES_SOURCE_DIR "images/cat.jpg " << 0;
reshapefile << EXAMPLES_SOURCE_DIR "images/fish-bike.jpg " << 1;
reshapefile << EXAMPLES_SOURCE_DIR "images/cat.jpg " << 0 << std::endl;
reshapefile << EXAMPLES_SOURCE_DIR "images/fish-bike.jpg " << 1
<< std::endl;
reshapefile.close();
// Create test input file for images with space in names
MakeTempFilename(&filename_space_);
std::ofstream spacefile(filename_space_.c_str(), std::ofstream::out);
LOG(INFO) << "Using temporary file " << filename_space_;
spacefile << EXAMPLES_SOURCE_DIR "images/cat.jpg " << 0 << std::endl;
spacefile << EXAMPLES_SOURCE_DIR "images/cat gray.jpg " << 1 << std::endl;
spacefile.close();
}

virtual ~ImageDataLayerTest() {
Expand All @@ -54,6 +62,7 @@ class ImageDataLayerTest : public MultiDeviceTest<TypeParam> {
int seed_;
string filename_;
string filename_reshape_;
string filename_space_;
Blob<Dtype>* const blob_top_data_;
Blob<Dtype>* const blob_top_label_;
vector<Blob<Dtype>*> blob_bottom_vec_;
Expand Down Expand Up @@ -177,5 +186,34 @@ TYPED_TEST(ImageDataLayerTest, TestShuffle) {
}
}

TYPED_TEST(ImageDataLayerTest, TestSpace) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter param;
ImageDataParameter* image_data_param = param.mutable_image_data_param();
image_data_param->set_batch_size(1);
image_data_param->set_source(this->filename_space_.c_str());
image_data_param->set_shuffle(false);
ImageDataLayer<Dtype> layer(param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
EXPECT_EQ(this->blob_top_label_->num(), 1);
EXPECT_EQ(this->blob_top_label_->channels(), 1);
EXPECT_EQ(this->blob_top_label_->height(), 1);
EXPECT_EQ(this->blob_top_label_->width(), 1);
// cat.jpg
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
EXPECT_EQ(this->blob_top_data_->num(), 1);
EXPECT_EQ(this->blob_top_data_->channels(), 3);
EXPECT_EQ(this->blob_top_data_->height(), 360);
EXPECT_EQ(this->blob_top_data_->width(), 480);
EXPECT_EQ(this->blob_top_label_->cpu_data()[0], 0);
// cat gray.jpg
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
EXPECT_EQ(this->blob_top_data_->num(), 1);
EXPECT_EQ(this->blob_top_data_->channels(), 3);
EXPECT_EQ(this->blob_top_data_->height(), 360);
EXPECT_EQ(this->blob_top_data_->width(), 480);
EXPECT_EQ(this->blob_top_label_->cpu_data()[0], 1);
}

} // namespace caffe
#endif // USE_OPENCV
9 changes: 6 additions & 3 deletions tools/convert_imageset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,13 @@ int main(int argc, char** argv) {

std::ifstream infile(argv[2]);
std::vector<std::pair<std::string, int> > lines;
std::string filename;
std::string line;
size_t pos;
int label;
while (infile >> filename >> label) {
lines.push_back(std::make_pair(filename, label));
while (std::getline(infile, line)) {
pos = line.find_last_of(' ');
label = atoi(line.substr(pos + 1).c_str());
lines.push_back(std::make_pair(line.substr(0, pos), label));
}
if (FLAGS_shuffle) {
// randomly shuffle data
Expand Down

0 comments on commit e79bc8f

Please sign in to comment.