KeyError: <class 'torch.Tensor'>

I want to use roi_pooling module which is defined in fast r-cnn, as follows:
class AdaptiveMaxPool2d(Function):
def init(self, out_w, out_h):
super(AdaptiveMaxPool2d, self).init()
self.out_w = out_w
self.out_h = out_h

def forward(self, input):
    output =
    indices =
    self.indices = indices
    self._backend = type2backend[type(input)]
            self._backend.library_state, input, output, indices,
            self.out_w, self.out_h)
    return output

def backward(self, grad_output):
    input, = self.saved_tensors
    indices = self.indices
    grad_input =
            self._backend.library_state, input, grad_output, grad_input,
    return grad_input, None

def adaptive_max_pool(input, size):
return AdaptiveMaxPool2d(size[0],size[1])(input)

def roi_pooling(input, rois, size=(7,7), spatial_scale=1.0):
assert(rois.dim() == 2)
assert(rois.size(1) == 5)
output = []
rois =
num_rois = rois.size(0)

rois = rois.long()
for i in range(num_rois):
    roi = rois[i]
    im_idx = roi[0]
    im = input.narrow(0, im_idx, 1)[..., roi[2]:(roi[4]+1), roi[1]:(roi[3]+1)]
    output.append(adaptive_max_pool(im, size))

return, 0)

my code for call the function is as:
roi_pooling(myfeaturemap, rois, size=(1,1), spatial_scale=1.0/16)

where the type of myfeaturemap is torch.Tensor

but I got errors ,like follows:
File “/home/user/lr/twostage/”, line 54, in roi_pooling
output.append(adaptive_max_pool(im, size))
File “/home/user/lr/twostage/”, line 37, in adaptive_max_pool
return AdaptiveMaxPool2d(size[0],size[1])(input)
File “/home/user/lr/twostage/”, line 21, in forward
self._backend = type2backend[type(input)]
File “/usr/local/lib/python2.7/dist-packages/torch/_thnn/”, line 15, in getitem
return self.backends[name].load()
KeyError: <class ‘torch.Tensor’>

, I don’t have ideas about above error, could anybody help me?


I think the version of the roi_pooling you’re using is made for an older version of pytorch.
In particular, with 0.4+, self._backend = type2backend[type(input)] should be replaced with self._backend = type2backend[input.type()].


Hi, thank you for your suggestion.

You are absolutely right, thank you.

The same error occurred. Fixed it by your method, but a new error occurred.

Traceback (most recent call last):
File “”, line 275, in
fps_pred, y_pred = model_conv(x)
File “/home/fan/anaconda3/envs/tf/lib/python3.7/site-packages/torch/nn/modules/”, line 493, in call
result = self.forward(*input, **kwargs)
File “/home/fan/anaconda3/envs/tf/lib/python3.7/site-packages/torch/nn/parallel/”, line 150, in forward
return self.module(*inputs[0], **kwargs[0])
File “/home/fan/anaconda3/envs/tf/lib/python3.7/site-packages/torch/nn/modules/”, line 493, in call
result = self.forward(*input, **kwargs)
File “”, line 230, in forward
roi1 = roi_pooling_ims(_x1,, size=(16, 8))
File “/home/fan/CCPD-master/rpnet/”, line 74, in roi_pooling_ims
output.append(adaptive_max_pool(im, size))
File “/home/fan/CCPD-master/rpnet/”, line 36, in adaptive_max_pool
return AdaptiveMaxPool2d(size[0], size[1])(input)
File “/home/fan/CCPD-master/rpnet/”, line 20, in forward
File “/home/fan/anaconda3/envs/tf/lib/python3.7/site-packages/torch/_thnn/”, line 27, in getattr
raise NotImplementedError

How to solve the problem?

Is there no support for type2backend in the new Pytorch? type2backend.SpatialConvolutionMM_updateOutput throws NotImplemeted Error but just for new Pytorch cuda10.


I’m not sure. But I think convolution was moved out of thnn. And so does not use this anymore. You can check the implementation of nn.ConvNd for more details.

1 Like

Thank you very much.