leovandriel / caffe2_cpp_tutorial

C++ transcripts of the Caffe2 Python tutorials and other C++ example code
BSD 2-Clause "Simplified" License
431 stars 94 forks source link

Tensor is a deleted function - Print method #75

Open CarlosYeverino opened 6 years ago

CarlosYeverino commented 6 years ago

Hi all,

I am getting 2 errors during building solution. I checked the header file tensor.h and I saw the following: /**

The mentioned 2 errors: Severity Code Description Project File Line Suppression State Error (active) function "caffe2::Tensor::Tensor(const caffe2::Tensor &src) [with Context=caffe2::CPUContext]" (declared at line 706 of "d:\Yeverino\git_projects\pytorch\caffe2\core\tensor.h") cannot be referenced -- it is a deleted function ConsoleApplication1 d:\Yeverino\visual studio projects\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 11

Severity Code Description Project File Line Suppression State Error C2280 'caffe2::Tensor::Tensor(const caffe2::Tensor &)': attempting to reference a deleted function ConsoleApplication1 d:\yeverino\visual studio projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 11

How should I modify the print method included in the tutorials in order to fix the errors? void print(const Blob* blob, const std::string& name) { auto tensor = blob->Get(); const auto& data = tensor.data(); std::cout << name << "(" << tensor.dims() << "): " << std::vector(data, data + tensor.size()) << std::endl; }

rednoah91 commented 6 years ago

As the comment mentioned: "Delete the copy constructor and use Clone explicitly" const auto& data = tensor.Clone().data();

CarlosYeverino commented 6 years ago

Hi @rednoah91 ,

thanks for your reply.

I tried what you mentioned, however, errors are still there. Errors are caused by line 11 in the print method (auto tensor = blob->Get();). Do you know how to fix it?

capture

ferasboulala commented 6 years ago

auto tensor = blob->Get<TensorCPU>().Clone()

Just add the .Clone() whenever you refer to a TensorCPU object.

zacario-li commented 6 years ago

@CarlosYeverino refer this code https://github.com/zacario-li/caffe2_Cpp_example/tree/master/src

CarlosYeverino commented 6 years ago

Hi @zacario-li @ferasboulala ,

for some cases your suggestion fixed the error, however, I also get the following error:

carlos@carlos-ubuntu:~/Documents/git/caffe2_cpp_tutorial$ c++ src/caffe2/binaries/pretrained.cc -o bin/pretrained -std=gnu++11 -Iinclude -I/usr/include/eigen3 -I/usr/include/opencv -lgflags -lglog -lprotobuf -lopencv_core -lopencv_imgproc -lopencv_highgui -lCaffe2_CPU
In file included from /usr/local/include/caffe2/core/net.h:12:0,
                 from src/caffe2/binaries/pretrained.cc:2:
/usr/local/include/caffe2/core/blob.h: In instantiation of ‘T* caffe2::Blob::GetMutable() [with T = caffe2::Tensor]’:
src/caffe2/binaries/pretrained.cc:107:68:   required from here
/usr/local/include/caffe2/core/blob.h:121:5: error: static assertion failed: GetMutable can't be called with non-default-constructible types. Try using specialized methods
     static_assert(
     ^
/usr/local/include/caffe2/core/blob.h:129:22: error: use of deleted function ‘caffe2::Tensor::Tensor()’
       return Reset<T>(new T());
                      ^
In file included from /usr/local/include/caffe2/core/blob.h:13:0,
                 from /usr/local/include/caffe2/core/net.h:12,
                 from src/caffe2/binaries/pretrained.cc:2:
/usr/local/include/caffe2/core/tensor.h:94:3: note: declared here
   Tensor() = delete;
   ^

The relevant code for the error:

  Workspace workspace("tmp");
  CAFFE_ENFORCE(workspace.RunNetOnce(init_net));
  auto input = workspace.CreateBlob("data")->GetMutable<TensorCPU>();

I added .Clone after GetMutable<TensorCPU>() but then I got the following error:

carlos@carlos-ubuntu:~/Documents/git/caffe2_cpp_tutorial$ c++ src/caffe2/binaries/pretrained.cc -o bin/pretrained -std=gnu++11 -Iinclude -I/usr/include/eigen3 -I/usr/include/opencv -lgflags -lglog -lprotobuf -lopencv_core -lopencv_imgproc -lopencv_highgui -lCaffe2_CPU
src/caffe2/binaries/pretrained.cc: In function ‘void caffe2::run()’:
src/caffe2/binaries/pretrained.cc:107:70: error: request for member ‘Clone’ in ‘workspace.caffe2::Workspace::CreateBlob(std::__cxx11::basic_string<char>(((const char*)"data"), std::allocator<char>()))->caffe2::Blob::GetMutable<caffe2::Tensor>()’, which is of pointer type ‘caffe2::Tensor*’ (maybe you meant to use ‘->’ ?)
   auto input = workspace.CreateBlob("data")->GetMutable<TensorCPU>().Clone();
                                                                      ^
In file included from /usr/local/include/caffe2/core/net.h:12:0,
                 from src/caffe2/binaries/pretrained.cc:2:
/usr/local/include/caffe2/core/blob.h: In instantiation of ‘T* caffe2::Blob::GetMutable() [with T = caffe2::Tensor]’:
src/caffe2/binaries/pretrained.cc:107:68:   required from here
/usr/local/include/caffe2/core/blob.h:121:5: error: static assertion failed: GetMutable can't be called with non-default-constructible types. Try using specialized methods
     static_assert(
     ^
/usr/local/include/caffe2/core/blob.h:129:22: error: use of deleted function ‘caffe2::Tensor::Tensor()’
       return Reset<T>(new T());
                      ^
In file included from /usr/local/include/caffe2/core/blob.h:13:0,
                 from /usr/local/include/caffe2/core/net.h:12,
                 from src/caffe2/binaries/pretrained.cc:2:
/usr/local/include/caffe2/core/tensor.h:94:3: note: declared here
   Tensor() = delete;
   ^

Any ideas to fix this error?