|
| 1 | +// Copyright 2013 Yangqing Jia |
| 2 | + |
| 3 | +#include <cstring> |
| 4 | +#include <cuda_runtime.h> |
| 5 | + |
| 6 | +#include "gtest/gtest.h" |
| 7 | +#include "caffe/blob.hpp" |
| 8 | +#include "caffe/common.hpp" |
| 9 | +#include "caffe/filler.hpp" |
| 10 | +#include "caffe/vision_layers.hpp" |
| 11 | +#include "caffe/test/test_gradient_check_util.hpp" |
| 12 | + |
| 13 | +#include "caffe/test/test_caffe_main.hpp" |
| 14 | + |
| 15 | +namespace caffe { |
| 16 | + |
| 17 | +extern cudaDeviceProp CAFFE_TEST_CUDA_PROP; |
| 18 | + |
| 19 | +template <typename Dtype> |
| 20 | +class FlattenLayerTest : public ::testing::Test { |
| 21 | + protected: |
| 22 | + FlattenLayerTest() |
| 23 | + : blob_bottom_(new Blob<Dtype>(2, 3, 6, 5)), |
| 24 | + blob_top_(new Blob<Dtype>()) { |
| 25 | + // fill the values |
| 26 | + FillerParameter filler_param; |
| 27 | + GaussianFiller<Dtype> filler(filler_param); |
| 28 | + filler.Fill(this->blob_bottom_); |
| 29 | + blob_bottom_vec_.push_back(blob_bottom_); |
| 30 | + blob_top_vec_.push_back(blob_top_); |
| 31 | + }; |
| 32 | + virtual ~FlattenLayerTest() { delete blob_bottom_; delete blob_top_; } |
| 33 | + Blob<Dtype>* const blob_bottom_; |
| 34 | + Blob<Dtype>* const blob_top_; |
| 35 | + vector<Blob<Dtype>*> blob_bottom_vec_; |
| 36 | + vector<Blob<Dtype>*> blob_top_vec_; |
| 37 | +}; |
| 38 | + |
| 39 | +typedef ::testing::Types<float, double> Dtypes; |
| 40 | +TYPED_TEST_CASE(FlattenLayerTest, Dtypes); |
| 41 | + |
| 42 | +TYPED_TEST(FlattenLayerTest, TestSetup) { |
| 43 | + LayerParameter layer_param; |
| 44 | + FlattenLayer<TypeParam> layer(layer_param); |
| 45 | + layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); |
| 46 | + EXPECT_EQ(this->blob_top_->num(), 2); |
| 47 | + EXPECT_EQ(this->blob_top_->channels(), 3 * 6 * 5); |
| 48 | + EXPECT_EQ(this->blob_top_->height(), 1); |
| 49 | + EXPECT_EQ(this->blob_top_->width(), 1); |
| 50 | +} |
| 51 | + |
| 52 | +TYPED_TEST(FlattenLayerTest, TestCPU) { |
| 53 | + LayerParameter layer_param; |
| 54 | + FlattenLayer<TypeParam> layer(layer_param); |
| 55 | + Caffe::set_mode(Caffe::CPU); |
| 56 | + layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); |
| 57 | + layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_)); |
| 58 | + for (int c = 0; c < 3 * 6 * 5; ++c) { |
| 59 | + EXPECT_EQ(this->blob_top_->data_at(0, c, 0, 0), |
| 60 | + this->blob_bottom_->data_at(0, c / (6 * 5), (c / 5) % 6, c % 5)); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +TYPED_TEST(FlattenLayerTest, TestGPU) { |
| 65 | + LayerParameter layer_param; |
| 66 | + FlattenLayer<TypeParam> layer(layer_param); |
| 67 | + Caffe::set_mode(Caffe::GPU); |
| 68 | + layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_)); |
| 69 | + layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_)); |
| 70 | + for (int c = 0; c < 3 * 6 * 5; ++c) { |
| 71 | + EXPECT_EQ(this->blob_top_->data_at(0, c, 0, 0), |
| 72 | + this->blob_bottom_->data_at(0, c / (6 * 5), (c / 5) % 6, c % 5)); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +TYPED_TEST(FlattenLayerTest, TestCPUGradient) { |
| 77 | + LayerParameter layer_param; |
| 78 | + Caffe::set_mode(Caffe::CPU); |
| 79 | + FlattenLayer<TypeParam> layer(layer_param); |
| 80 | + GradientChecker<TypeParam> checker(1e-2, 1e-2); |
| 81 | + checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_); |
| 82 | +} |
| 83 | + |
| 84 | +TYPED_TEST(FlattenLayerTest, TestGPUGradient) { |
| 85 | + LayerParameter layer_param; |
| 86 | + Caffe::set_mode(Caffe::GPU); |
| 87 | + FlattenLayer<TypeParam> layer(layer_param); |
| 88 | + GradientChecker<TypeParam> checker(1e-2, 1e-2); |
| 89 | + checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_); |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +} |
0 commit comments