Hi,
I’m a freshman to pytorch, recently I got some troubles when trying to convert a python models to c++. HELPPPPPP!!
enviroment:
pytorch1.1
When i trying to trace some custom layers which implemented by c++, i got an error like this:
could not export python function call <python_value>. Remove calls to Python functions before export. Did you forget add @script or @script_method annotation? If this is a nn.ModuleList, add it to __constants__.:
@torch.jit.script_method
def forward(self, x):
p1_conv1 = self.p1_conv1(x)
pool1 = self.pool1(p1_conv1) # Troubles is here
~~~~~~~~~~ <--- HERE
the code is here:
class corner_pool(torch.jit.ScriptModule):
def _init_layers(self, dim, pool1, pool2):
self.p1_conv1 = torch.jit.trace(convolution(3, dim, 128), torch.rand(1, 256, 64, 64))
self.pool1 = TopPool() #custom layers implemented in cpp
@torch.jit.script_method
def forward(self, x):
p1_conv1 = self.p1_conv1(x)
pool1 = self.pool1(p1_conv1) # Troubles is here
the toppool code is:
class TopPoolFunction(Function):
@staticmethod
def forward(ctx, input):
output = TopPool.forward(input)[0]
ctx.save_for_backward(input)
return output
@staticmethod
def backward(ctx, grad_output):
input = ctx.saved_variables[0]
output = TopPool.backward(input, grad_output)[0]
return output
class TopPool(nn.Module):
def forward(self, x):
result = TopPoolFunction.apply(x)
return result