Porting example to C++

I am trying to apply de deconvolution operations using C++ but I get runtime errors when trying to something as simple as

#include <chrono>
#include "mxnet-cpp/MxNetCpp.h"

using namespace std;
using namespace mxnet::cpp;

Symbol create_generator()
{   
  auto input  = Symbol::Variable("x");    
  
  auto d1 = Deconvolution(
                input,
                Symbol::Variable("w0"),
                Symbol::Variable("c0"), //bias 
                Shape(1,1),
                16, //kernel size,
                Shape(1,1)
                );
 

  return d1; 
}

int main(int argc, char **argv)
{
  Context ctx = Context::cpu();

  auto net = create_generator();

  std::map<string, NDArray> args;
  auto input = NDArray({1},Shape(1,1), ctx);
  
  args["x"] = input.Copy(ctx);
 
  net.InferArgsMap(ctx, &args, args);
  //net.SimpleBind(ctx, args);
}

This is what I get

./a.out 
terminate called after throwing an instance of 'dmlc::Error'
  what():  [20:19:53] /usr/local/include/mxnet-cpp/symbol.hpp:219: Check failed: MXSymbolInferShapeEx(GetHandle(), keys.size(), keys.data(), arg_ind_ptr.data(), arg_shape_data.data(), &in_shape_size, &in_shape_ndim, &in_shape_data, &out_shape_size, &out_shape_ndim, &out_shape_data, &aux_shape_size, &aux_shape_ndim, &aux_shape_data, &complete) == 0 (-1 vs. 0) : 
Stack trace:
  [bt] (0) ./a.out(dmlc::LogMessageFatal::~LogMessageFatal()+0x25) [0x4076ed]
  [bt] (1) ./a.out() [0x409c5f]
  [bt] (2) ./a.out() [0x40a8d0]
  [bt] (3) ./a.out() [0x405067]
  [bt] (4) /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1) [0x7f59b49fe2e1]
  [bt] (5) ./a.out() [0x4044ca]

I am using debian9 ,
last mxnet compiled from source ,
g++4.9 using -std=c++11

It’s possible that your input is too and it fails to infer the shape. Try with a sample input of 64x64 ?

I tested this on python

import mxnet as mx
a = mx.nd.array([[[[1,1],[1,1]]]])
b = mx.nd.array([[[[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]]])

rint(b.shape)
out = mx.nd.Deconvolution(data=b, weight=a, no_bias=True, kernel=(2,2), num_filter=1, pad=(0,0))

print(out)

works without problems … then I did what I think is the equivalent in C++

#include “mxnet-cpp/MxNetCpp.h”

using namespace std;
using namespace mxnet::cpp;

Symbol create_generator()
{
auto input = Symbol::Variable(“x”);

auto d1 = Deconvolution(
input,
Symbol::Variable(“w0”),
Symbol::Variable(“c0”), //bias
Shape(2,2),
1, //kernel size,
Shape(),
Shape(),
Shape(1,1)
);

return d1;
}

int main(int argc, char **argv)
{
Context ctx = Context::cpu();

auto net = create_generator();

vector<mx_float> ledata (16,1);
vector<mx_float> filter (4,1);
std::map<string, NDArray> args;

auto input = NDArray(ledata,Shape(1,1,4,4), ctx);
auto w = NDArray(filter,Shape(1,1,2,2), ctx);
cout << input << endl;

args[“w0”] = w.Copy(ctx);
args[“x”] = input.Copy(ctx);

net.InferArgsMap(ctx, &args, args);
net.SimpleBind(ctx, args);
}

But still is not working :c
what(): [11:43:45] /usr/local/include/mxnet-cpp/symbol.hpp:219: Check failed: MXSymbolInferShapeEx(GetHandle(), keys.size(), keys.data(), arg_ind_ptr.data(), arg_shape_data.data(), &in_shape_size, &in_shape_ndim, &in_shape_data, &out_shape_size, &out_shape_ndim, &out_shape_data, &aux_shape_size, &aux_shape_ndim, &aux_shape_data, &complete) == 0 (-1 vs. 0) :

Have you got this problem resolved? I’m trying to use Deconvolution in my C++ program directly and have got a similar problem.

I didn’t solve it with mxnet, I decided to move to tensorflow.

Fitus,

Thanks for a quick response. C++ implementation is important for production applications. MXNet can gain more areas to be deployed if it has a more friendly C++ support.

I had the same feeling, what I do now is. I train my models on python using tensorflow then I freeze the model and load it the lite version library. There is also an adafruit version which runs on microcontrollers.

I figured it out. With C++, by default, the deconvolution set the use_bias to false. However, by default, the function provides bias also, which will trigger a checking error.

You’ll need reload a no bias version of the function with use_bias beging false. And then, it works.

Hi @relaxli00,

How to call the ops precisely in C++?. Thanks