Allow reshaping blobs to size 0.
authorEric Tzeng <etzeng@eecs.berkeley.edu>
Fri, 6 May 2016 01:29:30 +0000 (18:29 -0700)
committerEric Tzeng <etzeng@eecs.berkeley.edu>
Fri, 6 May 2016 01:41:32 +0000 (18:41 -0700)
Also add a test that reshapes a blob to shape (0, 5).

src/caffe/blob.cpp
src/caffe/test/test_blob.cpp

index c86fd5d..4a34e4c 100644 (file)
@@ -30,7 +30,9 @@ void Blob<Dtype>::Reshape(const vector<int>& shape) {
   int* shape_data = static_cast<int*>(shape_data_->mutable_cpu_data());
   for (int i = 0; i < shape.size(); ++i) {
     CHECK_GE(shape[i], 0);
-    CHECK_LE(shape[i], INT_MAX / count_) << "blob size exceeds INT_MAX";
+    if (count_ != 0) {
+      CHECK_LE(shape[i], INT_MAX / count_) << "blob size exceeds INT_MAX";
+    }
     count_ *= shape[i];
     shape_[i] = shape[i];
     shape_data[i] = shape[i];
index a9d7d51..b885622 100644 (file)
@@ -51,6 +51,14 @@ TYPED_TEST(BlobSimpleTest, TestReshape) {
   EXPECT_EQ(this->blob_->count(), 120);
 }
 
+TYPED_TEST(BlobSimpleTest, TestReshapeZero) {
+  vector<int> shape(2);
+  shape[0] = 0;
+  shape[1] = 5;
+  this->blob_->Reshape(shape);
+  EXPECT_EQ(this->blob_->count(), 0);
+}
+
 TYPED_TEST(BlobSimpleTest, TestLegacyBlobProtoShapeEquals) {
   BlobProto blob_proto;