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 = input.new()
    indices = input.new().long()
    self.save_for_backward(input)
    self.indices = indices
    self._backend = type2backend[type(input)]
    self._backend.SpatialAdaptiveMaxPooling_updateOutput(
            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 = grad_output.new()
    self._backend.SpatialAdaptiveMaxPooling_updateGradInput(
            self._backend.library_state, input, grad_output, grad_input,
            indices)
    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 = rois.data.float()
num_rois = rois.size(0)

rois[:,1:].mul_(spatial_scale)
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 torch.cat(output, 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/roi_pooling.py”, line 54, in roi_pooling
output.append(adaptive_max_pool(im, size))
File “/home/user/lr/twostage/roi_pooling.py”, line 37, in adaptive_max_pool
return AdaptiveMaxPool2d(size[0],size[1])(input)
File “/home/user/lr/twostage/roi_pooling.py”, line 21, in forward
self._backend = type2backend[type(input)]
File “/usr/local/lib/python2.7/dist-packages/torch/_thnn/init.py”, 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?

Hi,

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()].

4 Likes

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 “demo.py”, line 275, in
fps_pred, y_pred = model_conv(x)
File “/home/fan/anaconda3/envs/tf/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 493, in call
result = self.forward(*input, **kwargs)
File “/home/fan/anaconda3/envs/tf/lib/python3.7/site-packages/torch/nn/parallel/data_parallel.py”, 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/module.py”, line 493, in call
result = self.forward(*input, **kwargs)
File “demo.py”, line 230, in forward
roi1 = roi_pooling_ims(_x1, boxNew.mm(p1), size=(16, 8))
File “/home/fan/CCPD-master/rpnet/roi_pooling.py”, line 74, in roi_pooling_ims
output.append(adaptive_max_pool(im, size))
File “/home/fan/CCPD-master/rpnet/roi_pooling.py”, line 36, in adaptive_max_pool
return AdaptiveMaxPool2d(size[0], size[1])(input)
File “/home/fan/CCPD-master/rpnet/roi_pooling.py”, line 20, in forward
self._backend.SpatialAdaptiveMaxPooling_updateOutput(
File “/home/fan/anaconda3/envs/tf/lib/python3.7/site-packages/torch/_thnn/utils.py”, line 27, in getattr
raise NotImplementedError
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.

Hi,

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