1 #include <cstring>
2 #include <cuda_runtime.h>
4 #include "gtest/gtest.h"
5 #include "caffeine/blob.hpp"
6 #include "caffeine/common.hpp"
7 #include "caffeine/filler.hpp"
8 #include "caffeine/vision_layers.hpp"
9 #include "caffeine/test/test_gradient_check_util.hpp"
11 #include "caffeine/test/test_caffeine_main.hpp"
13 namespace caffeine {
15 extern cudaDeviceProp CAFFEINE_TEST_CUDA_PROP;
17 template <typename Dtype>
18 class Im2colLayerTest : public ::testing::Test {
19 protected:
20 Im2colLayerTest()
21 : blob_bottom_(new Blob<Dtype>(2, 3, 6, 5)),
22 blob_top_(new Blob<Dtype>()) {
23 // fill the values
24 FillerParameter filler_param;
25 GaussianFiller<Dtype> filler(filler_param);
26 filler.Fill(this->blob_bottom_);
27 blob_bottom_vec_.push_back(blob_bottom_);
28 blob_top_vec_.push_back(blob_top_);
29 };
30 virtual ~Im2colLayerTest() { delete blob_bottom_; delete blob_top_; }
31 Blob<Dtype>* const blob_bottom_;
32 Blob<Dtype>* const blob_top_;
33 vector<Blob<Dtype>*> blob_bottom_vec_;
34 vector<Blob<Dtype>*> blob_top_vec_;
35 };
37 typedef ::testing::Types<float, double> Dtypes;
38 TYPED_TEST_CASE(Im2colLayerTest, Dtypes);
40 TYPED_TEST(Im2colLayerTest, TestSetup) {
41 LayerParameter layer_param;
42 layer_param.set_kernelsize(3);
43 layer_param.set_stride(2);
44 Im2colLayer<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(), 27);
48 EXPECT_EQ(this->blob_top_->height(), 2);
49 EXPECT_EQ(this->blob_top_->width(), 2);
50 }
52 TYPED_TEST(Im2colLayerTest, TestCPU) {
53 LayerParameter layer_param;
54 layer_param.set_kernelsize(3);
55 layer_param.set_stride(2);
56 Im2colLayer<TypeParam> layer(layer_param);
57 Caffeine::set_mode(Caffeine::CPU);
58 layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_));
59 layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
60 // We are lazy and will only check the top left block
61 for (int c = 0; c < 27; ++c) {
62 EXPECT_EQ(this->blob_top_->data_at(0, c, 0, 0),
63 this->blob_bottom_->data_at(0, (c / 9), (c / 3) % 3, c % 3));
64 }
65 }
67 TYPED_TEST(Im2colLayerTest, TestGPU) {
68 LayerParameter layer_param;
69 layer_param.set_kernelsize(3);
70 layer_param.set_stride(2);
71 Im2colLayer<TypeParam> layer(layer_param);
72 Caffeine::set_mode(Caffeine::GPU);
73 layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_));
74 layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
75 // We are lazy and will only check the top left block
76 for (int c = 0; c < 27; ++c) {
77 EXPECT_EQ(this->blob_bottom_->data_at(0, (c / 9), (c / 3) % 3, c % 3),
78 this->blob_top_->data_at(0, c, 0, 0));
79 }
80 }
82 TYPED_TEST(Im2colLayerTest, TestCPUGradient) {
83 LayerParameter layer_param;
84 layer_param.set_kernelsize(3);
85 layer_param.set_stride(2);
86 Caffeine::set_mode(Caffeine::CPU);
87 Im2colLayer<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 }
92 }