I’ve been trying to use max_pool2d using the C++ API in a sequential container. However I can’t figure out the proper way to use it. This is how far I’ve managed to come after referring to the available C++ examples on the PyTorch repository as well as the library source code:
//
// Created by satrajit-c on 6/12/19.
//
#ifndef BASEMODEL_H
#define BASEMODEL_H
#include <torch/torch.h>
#include <torch/nn/options/pooling.h>
#include <vector>
using namespace std;
using namespace torch;
const int num_classes=6;
struct Intensity : nn::Module {
Intensity():
features(
nn::Conv2d(nn::Conv2dOptions(/*in channels*/ 3, /*out channels*/ 64, /*kernel*/ 3).padding(2)),
nn::Functional(relu),
nn::Conv2d(nn::Conv2dOptions(/*in channels*/ 64, /*out channels*/ 64, /*kernel*/ 3).padding(2)),
nn::Functional(relu),
nn::Functional(max_pool2d, nn::MaxPool2dOptions(ExpandingArray<2>({2,2}))),
){}
torch::Tensor forward(const torch::Tensor& input){
auto x = features->forward(input);
return x;
}
nn::Sequential features;
};
#endif //BASEMODEL_H
The above code is a bit trimmed down to just refer to the essential parts. However this is the error that’s occurring:
Scanning dependencies of target OpenFace
[ 50%] Building CXX object CMakeFiles/OpenFace.dir/Template.cpp.o
In file included from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/c10/core/DeviceType.h:11:0,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/c10/core/Device.h:3,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/c10/core/Allocator.h:6,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/ATen/ATen.h:3,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/types.h:3,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/data/dataloader_options.h:4,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/data/dataloader/base.h:3,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/data/dataloader/stateful.h:3,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/data/dataloader.h:3,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/data.h:3,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/all.h:4,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/torch.h:3,
from /home/satrajit-c/Documents/My_Projects/OpenFace/Template.cpp:7:
/usr/include/c++/7/functional: In instantiation of ‘struct std::_Bind_check_arity<at::Tensor (*)(const at::Tensor&, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, bool), const std::_Placeholder<1>&, torch::nn::MaxPoolOptions<2> >’:
/usr/include/c++/7/functional:854:12: required from ‘struct std::_Bind_helper<false, at::Tensor (*&)(const at::Tensor&, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, bool), const std::_Placeholder<1>&, torch::nn::MaxPoolOptions<2> >’
/usr/include/c++/7/functional:875:5: required by substitution of ‘template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = at::Tensor (*&)(const at::Tensor&, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, bool); _BoundArgs = {const std::_Placeholder<1>&, torch::nn::MaxPoolOptions<2>}]’
/home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/nn/modules/container/functional.h:70:28: required from ‘torch::nn::FunctionalImpl::FunctionalImpl(SomeFunction, Args&& ...) [with SomeFunction = at::Tensor (*)(const at::Tensor&, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, bool); Args = {torch::nn::MaxPoolOptions<2>}; <template-parameter-1-3> = void]’
/home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/nn/pimpl.h:64:15: required from ‘torch::nn::ModuleHolder<Contained>::ModuleHolder(Head&&, Tail&& ...) [with Head = at::Tensor (&)(const at::Tensor&, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, bool); Tail = {torch::nn::MaxPoolOptions<2>}; <template-parameter-2-3> = void; Contained = torch::nn::FunctionalImpl]’
/home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/nn/modules/container/functional.h:101:1: required from here
/usr/include/c++/7/functional:825:7: error: static assertion failed: Wrong number of arguments for function
static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
^~~~~~~~~~~~~
In file included from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/nn/modules.h:5:0,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/nn.h:7,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/all.h:7,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/torch.h:3,
from /home/satrajit-c/Documents/My_Projects/OpenFace/Template.cpp:7:
/home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/nn/modules/container/functional.h: In instantiation of ‘torch::nn::FunctionalImpl::FunctionalImpl(SomeFunction, Args&& ...) [with SomeFunction = at::Tensor (*)(const at::Tensor&, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, bool); Args = {torch::nn::MaxPoolOptions<2>}; <template-parameter-1-3> = void]’:
/home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/nn/pimpl.h:64:15: required from ‘torch::nn::ModuleHolder<Contained>::ModuleHolder(Head&&, Tail&& ...) [with Head = at::Tensor (&)(const at::Tensor&, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, bool); Tail = {torch::nn::MaxPoolOptions<2>}; <template-parameter-2-3> = void; Contained = torch::nn::FunctionalImpl]’
/home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/nn/modules/container/functional.h:101:1: required from here
/home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/nn/modules/container/functional.h:73:41: error: no matching function for call to ‘std::function<at::Tensor(at::Tensor)>::function(std::_Bind_helper<false, at::Tensor (*&)(const at::Tensor&, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, bool), const std::_Placeholder<1>&, torch::nn::MaxPoolOptions<2> >::type)’
std::forward<Args>(args)...)) {
^
In file included from /usr/include/c++/7/functional:58:0,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/c10/core/DeviceType.h:11,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/c10/core/Device.h:3,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/c10/core/Allocator.h:6,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/ATen/ATen.h:3,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/types.h:3,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/data/dataloader_options.h:4,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/data/dataloader/base.h:3,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/data/dataloader/stateful.h:3,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/data/dataloader.h:3,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/data.h:3,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/all.h:4,
from /home/satrajit-c/Documents/My_Projects/OpenFace/libtorch/include/torch/csrc/api/include/torch/torch.h:3,
from /home/satrajit-c/Documents/My_Projects/OpenFace/Template.cpp:7:
/usr/include/c++/7/bits/std_function.h:685:7: note: candidate: template<class _Functor, class, class> std::function<_Res(_ArgTypes ...)>::function(_Functor)
function<_Res(_ArgTypes...)>::
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/7/bits/std_function.h:685:7: note: template argument deduction/substitution failed:
/usr/include/c++/7/bits/std_function.h:464:9: error: no type named ‘type’ in ‘class std::result_of<std::_Bind<at::Tensor (*(std::_Placeholder<1>, torch::nn::MaxPoolOptions<2>))(const at::Tensor&, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, bool)>&(at::Tensor)>’
typename = _Requires<_Callable<_Functor>, void>>
^~~~~~~~
/usr/include/c++/7/bits/std_function.h:441:7: note: candidate: std::function<_Res(_ArgTypes ...)>::function(std::function<_Res(_ArgTypes ...)>&&) [with _Res = at::Tensor; _ArgTypes = {at::Tensor}]
function(function&& __x) noexcept : _Function_base()
^~~~~~~~
/usr/include/c++/7/bits/std_function.h:441:7: note: no known conversion for argument 1 from ‘std::_Bind_helper<false, at::Tensor (*&)(const at::Tensor&, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, bool), const std::_Placeholder<1>&, torch::nn::MaxPoolOptions<2> >::type {aka std::_Bind<at::Tensor (*(std::_Placeholder<1>, torch::nn::MaxPoolOptions<2>))(const at::Tensor&, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, bool)>}’ to ‘std::function<at::Tensor(at::Tensor)>&&’
/usr/include/c++/7/bits/std_function.h:671:5: note: candidate: std::function<_Res(_ArgTypes ...)>::function(const std::function<_Res(_ArgTypes ...)>&) [with _Res = at::Tensor; _ArgTypes = {at::Tensor}]
function<_Res(_ArgTypes...)>::
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/7/bits/std_function.h:671:5: note: no known conversion for argument 1 from ‘std::_Bind_helper<false, at::Tensor (*&)(const at::Tensor&, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, bool), const std::_Placeholder<1>&, torch::nn::MaxPoolOptions<2> >::type {aka std::_Bind<at::Tensor (*(std::_Placeholder<1>, torch::nn::MaxPoolOptions<2>))(const at::Tensor&, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, bool)>}’ to ‘const std::function<at::Tensor(at::Tensor)>&’
/usr/include/c++/7/bits/std_function.h:421:7: note: candidate: std::function<_Res(_ArgTypes ...)>::function(std::nullptr_t) [with _Res = at::Tensor; _ArgTypes = {at::Tensor}; std::nullptr_t = std::nullptr_t]
function(nullptr_t) noexcept
^~~~~~~~
/usr/include/c++/7/bits/std_function.h:421:7: note: no known conversion for argument 1 from ‘std::_Bind_helper<false, at::Tensor (*&)(const at::Tensor&, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, bool), const std::_Placeholder<1>&, torch::nn::MaxPoolOptions<2> >::type {aka std::_Bind<at::Tensor (*(std::_Placeholder<1>, torch::nn::MaxPoolOptions<2>))(const at::Tensor&, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, c10::ArrayRef<long int>, bool)>}’ to ‘std::nullptr_t’
/usr/include/c++/7/bits/std_function.h:414:7: note: candidate: std::function<_Res(_ArgTypes ...)>::function() [with _Res = at::Tensor; _ArgTypes = {at::Tensor}]
function() noexcept
^~~~~~~~
/usr/include/c++/7/bits/std_function.h:414:7: note: candidate expects 0 arguments, 1 provided
CMakeFiles/OpenFace.dir/build.make:62: recipe for target 'CMakeFiles/OpenFace.dir/Template.cpp.o' failed
make[2]: *** [CMakeFiles/OpenFace.dir/Template.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/OpenFace.dir/all' failed
make[1]: *** [CMakeFiles/OpenFace.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
(base) satrajit-c@INDRA:~/Documents/My_Projects/OpenFace/build$
This is the original traceback of the entire code.
I’d really appreciate any help I can get on this as I can’t seem to figure this out.