-
Notifications
You must be signed in to change notification settings - Fork 18.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Very simple version of ReshapeLayer #2088
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <vector> | ||
|
||
#include "caffe/common_layers.hpp" | ||
#include "caffe/layer.hpp" | ||
|
||
namespace caffe { | ||
|
||
template <typename Dtype> | ||
void ReshapeLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom, | ||
const vector<Blob<Dtype>*>& top) { | ||
top[0]->Reshape(this->layer_param_.reshape_param().shape()); | ||
top[0]->ShareData(*bottom[0]); | ||
top[0]->ShareDiff(*bottom[0]); | ||
} | ||
|
||
template <typename Dtype> | ||
void ReshapeLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom, | ||
const vector<Blob<Dtype>*>& top) { | ||
CHECK_EQ(top[0]->count(), bottom[0]->count()) | ||
<< "new shape must have the same count as input"; | ||
} | ||
|
||
INSTANTIATE_CLASS(ReshapeLayer); | ||
REGISTER_LAYER_CLASS(Reshape); | ||
|
||
} // namespace caffe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#include <cstring> | ||
#include <vector> | ||
|
||
#include "gtest/gtest.h" | ||
|
||
#include "caffe/blob.hpp" | ||
#include "caffe/common.hpp" | ||
#include "caffe/filler.hpp" | ||
#include "caffe/vision_layers.hpp" | ||
|
||
#include "caffe/test/test_caffe_main.hpp" | ||
#include "caffe/test/test_gradient_check_util.hpp" | ||
|
||
namespace caffe { | ||
|
||
template <typename TypeParam> | ||
class ReshapeLayerTest : public MultiDeviceTest<TypeParam> { | ||
typedef typename TypeParam::Dtype Dtype; | ||
protected: | ||
ReshapeLayerTest() | ||
: blob_bottom_(new Blob<Dtype>(2, 3, 6, 5)), | ||
blob_top_(new Blob<Dtype>()) { | ||
Caffe::set_random_seed(1701); | ||
// fill the values | ||
FillerParameter filler_param; | ||
GaussianFiller<Dtype> filler(filler_param); | ||
filler.Fill(this->blob_bottom_); | ||
blob_bottom_vec_.push_back(blob_bottom_); | ||
blob_top_vec_.push_back(blob_top_); | ||
} | ||
virtual ~ReshapeLayerTest() { delete blob_bottom_; delete blob_top_; } | ||
Blob<Dtype>* const blob_bottom_; | ||
Blob<Dtype>* const blob_top_; | ||
vector<Blob<Dtype>*> blob_bottom_vec_; | ||
vector<Blob<Dtype>*> blob_top_vec_; | ||
}; | ||
|
||
TYPED_TEST_CASE(ReshapeLayerTest, TestDtypesAndDevices); | ||
|
||
TYPED_TEST(ReshapeLayerTest, TestSetup) { | ||
typedef typename TypeParam::Dtype Dtype; | ||
LayerParameter layer_param; | ||
BlobShape* shape = layer_param.mutable_reshape_param()->mutable_shape(); | ||
shared_ptr<ReshapeLayer<Dtype> > layer; | ||
|
||
shape->Clear(); | ||
shape->add_dim(2 * 3 * 6 * 5); | ||
layer.reset(new ReshapeLayer<Dtype>(layer_param)); | ||
layer->SetUp(this->blob_bottom_vec_, this->blob_top_vec_); | ||
ASSERT_EQ(this->blob_top_->num_axes(), 1); | ||
EXPECT_EQ(this->blob_top_->shape(0), 2 * 3 * 6 * 5); | ||
|
||
shape->Clear(); | ||
shape->add_dim(2 * 3 * 6); | ||
shape->add_dim(5); | ||
layer.reset(new ReshapeLayer<Dtype>(layer_param)); | ||
layer->SetUp(this->blob_bottom_vec_, this->blob_top_vec_); | ||
ASSERT_EQ(this->blob_top_->num_axes(), 2); | ||
EXPECT_EQ(this->blob_top_->shape(0), 2 * 3 * 6); | ||
EXPECT_EQ(this->blob_top_->shape(1), 5); | ||
|
||
shape->Clear(); | ||
shape->add_dim(6); | ||
shape->add_dim(1); | ||
shape->add_dim(2); | ||
shape->add_dim(3); | ||
shape->add_dim(1); | ||
shape->add_dim(5); | ||
layer.reset(new ReshapeLayer<Dtype>(layer_param)); | ||
layer->SetUp(this->blob_bottom_vec_, this->blob_top_vec_); | ||
ASSERT_EQ(this->blob_top_->num_axes(), 6); | ||
EXPECT_EQ(this->blob_top_->shape(0), 6); | ||
EXPECT_EQ(this->blob_top_->shape(1), 1); | ||
EXPECT_EQ(this->blob_top_->shape(2), 2); | ||
EXPECT_EQ(this->blob_top_->shape(3), 3); | ||
EXPECT_EQ(this->blob_top_->shape(4), 1); | ||
EXPECT_EQ(this->blob_top_->shape(5), 5); | ||
} | ||
|
||
TYPED_TEST(ReshapeLayerTest, TestForward) { | ||
typedef typename TypeParam::Dtype Dtype; | ||
LayerParameter layer_param; | ||
BlobShape* shape = layer_param.mutable_reshape_param()->mutable_shape(); | ||
shape->add_dim(6); | ||
shape->add_dim(2); | ||
shape->add_dim(3); | ||
shape->add_dim(5); | ||
ReshapeLayer<Dtype> layer(layer_param); | ||
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_); | ||
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_); | ||
for (int i = 0; i < this->blob_bottom_->count(); ++i) { | ||
EXPECT_EQ(this->blob_top_->cpu_data()[i], | ||
this->blob_bottom_->cpu_data()[i]); | ||
} | ||
} | ||
|
||
TYPED_TEST(ReshapeLayerTest, TestForwardAfterReshape) { | ||
typedef typename TypeParam::Dtype Dtype; | ||
LayerParameter layer_param; | ||
BlobShape* shape = layer_param.mutable_reshape_param()->mutable_shape(); | ||
shape->add_dim(6); | ||
shape->add_dim(2); | ||
shape->add_dim(3); | ||
shape->add_dim(5); | ||
ReshapeLayer<Dtype> layer(layer_param); | ||
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_); | ||
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_); | ||
// We know the above produced the correct result from TestForward. | ||
// Reshape the bottom and call layer.Reshape, then try again. | ||
vector<int> new_bottom_shape(1, 2 * 3 * 6 * 5); | ||
this->blob_bottom_->Reshape(new_bottom_shape); | ||
layer.Reshape(this->blob_bottom_vec_, this->blob_top_vec_); | ||
FillerParameter filler_param; | ||
GaussianFiller<Dtype> filler(filler_param); | ||
filler.Fill(this->blob_bottom_); | ||
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_); | ||
for (int i = 0; i < this->blob_bottom_->count(); ++i) { | ||
EXPECT_EQ(this->blob_top_->cpu_data()[i], | ||
this->blob_bottom_->cpu_data()[i]); | ||
} | ||
} | ||
|
||
TYPED_TEST(ReshapeLayerTest, TestGradient) { | ||
typedef typename TypeParam::Dtype Dtype; | ||
LayerParameter layer_param; | ||
BlobShape* shape = layer_param.mutable_reshape_param()->mutable_shape(); | ||
shape->add_dim(6); | ||
shape->add_dim(2); | ||
shape->add_dim(3); | ||
shape->add_dim(5); | ||
ReshapeLayer<Dtype> layer(layer_param); | ||
GradientChecker<Dtype> checker(1e-2, 1e-2); | ||
checker.CheckGradientEltwise(&layer, this->blob_bottom_vec_, | ||
this->blob_top_vec_); | ||
} | ||
|
||
|
||
} // namespace caffe |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@longjon I guess this should technically all be done in
LayerSetUp
since the top shape doesn't vary with the bottom shape? (But then if special options were added when certain dims of the shape are set to 0 or -1, the top would then vary with the bottom shape, so it would be moved toReshape
then? Maybe better to keep it inReshape
then?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, to me it feels like this should go in
LayerSetUp
. If special options were added so that top shape depended on bottom shape, thenReshape
would be needed. But that isn't the case in this code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed. I kept the
CHECK_EQ
forcount
s inReshape
though, which still seems right.