test reshaping DATA and IMAGE_DATA

This commit is contained in:
Evan Shelhamer 2015-02-05 16:31:52 -08:00
Родитель 82551adbf8
Коммит d3b20106c0
2 изменённых файлов: 109 добавлений и 2 удалений

Просмотреть файл

@ -103,6 +103,70 @@ class DataLayerTest : public MultiDeviceTest<TypeParam> {
}
}
void TestReshape(DataParameter_DB backend) {
const int num_inputs = 5;
// Save data of varying shapes.
LOG(INFO) << "Using temporary dataset " << *filename_;
scoped_ptr<db::DB> db(db::GetDB(backend));
db->Open(*filename_, db::NEW);
scoped_ptr<db::Transaction> txn(db->NewTransaction());
for (int i = 0; i < num_inputs; ++i) {
Datum datum;
datum.set_label(i);
datum.set_channels(2);
datum.set_height(i % 2 + 1);
datum.set_width(i % 4 + 1);
std::string* data = datum.mutable_data();
const int data_size = datum.channels() * datum.height() * datum.width();
for (int j = 0; j < data_size; ++j) {
data->push_back(static_cast<uint8_t>(j));
}
stringstream ss;
ss << i;
string out;
CHECK(datum.SerializeToString(&out));
txn->Put(ss.str(), out);
}
txn->Commit();
db->Close();
// Load and check data of various shapes.
LayerParameter param;
DataParameter* data_param = param.mutable_data_param();
data_param->set_batch_size(1);
data_param->set_source(filename_->c_str());
data_param->set_backend(backend);
DataLayer<Dtype> layer(param);
layer.SetUp(blob_bottom_vec_, blob_top_vec_);
EXPECT_EQ(blob_top_data_->num(), 1);
EXPECT_EQ(blob_top_data_->channels(), 2);
EXPECT_EQ(blob_top_label_->num(), 1);
EXPECT_EQ(blob_top_label_->channels(), 1);
EXPECT_EQ(blob_top_label_->height(), 1);
EXPECT_EQ(blob_top_label_->width(), 1);
for (int iter = 0; iter < num_inputs; ++iter) {
layer.Forward(blob_bottom_vec_, blob_top_vec_);
EXPECT_EQ(blob_top_data_->height(), iter % 2 + 1);
EXPECT_EQ(blob_top_data_->width(), iter % 4 + 1);
EXPECT_EQ(iter, blob_top_label_->cpu_data()[0]);
const int channels = blob_top_data_->channels();
const int height = blob_top_data_->height();
const int width = blob_top_data_->width();
for (int c = 0; c < channels; ++c) {
for (int h = 0; h < height; ++h) {
for (int w = 0; w < width; ++w) {
const int idx = (c * height + h) * width + w;
EXPECT_EQ(idx, static_cast<int>(blob_top_data_->cpu_data()[idx]))
<< "debug: iter " << iter << " c " << c
<< " h " << h << " w " << w;
}
}
}
}
}
void TestReadCrop() {
const Dtype scale = 3;
LayerParameter param;
@ -285,6 +349,10 @@ TYPED_TEST(DataLayerTest, TestReadLevelDB) {
this->TestRead();
}
TYPED_TEST(DataLayerTest, TestReshapeLevelDB) {
this->TestReshape(DataParameter_DB_LEVELDB);
}
TYPED_TEST(DataLayerTest, TestReadCropTrainLevelDB) {
Caffe::set_phase(Caffe::TRAIN);
const bool unique_pixels = true; // all images the same; pixels different
@ -323,6 +391,10 @@ TYPED_TEST(DataLayerTest, TestReadLMDB) {
this->TestRead();
}
TYPED_TEST(DataLayerTest, TestReshapeLMDB) {
this->TestReshape(DataParameter_DB_LMDB);
}
TYPED_TEST(DataLayerTest, TestReadCropTrainLMDB) {
Caffe::set_phase(Caffe::TRAIN);
const bool unique_pixels = true; // all images the same; pixels different

Просмотреть файл

@ -25,17 +25,24 @@ class ImageDataLayerTest : public MultiDeviceTest<TypeParam> {
blob_top_data_(new Blob<Dtype>()),
blob_top_label_(new Blob<Dtype>()) {}
virtual void SetUp() {
MakeTempFilename(&filename_);
blob_top_vec_.push_back(blob_top_data_);
blob_top_vec_.push_back(blob_top_label_);
Caffe::set_random_seed(seed_);
// Create a Vector of files with labels
// Create test input file.
MakeTempFilename(&filename_);
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.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.close();
}
virtual ~ImageDataLayerTest() {
@ -45,6 +52,7 @@ class ImageDataLayerTest : public MultiDeviceTest<TypeParam> {
int seed_;
string filename_;
string filename_reshape_;
Blob<Dtype>* const blob_top_data_;
Blob<Dtype>* const blob_top_label_;
vector<Blob<Dtype>*> blob_bottom_vec_;
@ -107,6 +115,33 @@ TYPED_TEST(ImageDataLayerTest, TestResize) {
}
}
TYPED_TEST(ImageDataLayerTest, TestReshape) {
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_reshape_.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);
// fish-bike.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(), 323);
EXPECT_EQ(this->blob_top_data_->width(), 481);
}
TYPED_TEST(ImageDataLayerTest, TestShuffle) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter param;