KeyError: <class 'torch.Tensor'>

Li_Rong: <p>I want to use roi_pooling module which is defined in fast r-cnn, as follows:<br>
class AdaptiveMaxPool2d(Function):<br>
def <strong>init</strong>(self, out_w, out_h):<br>
super(AdaptiveMaxPool2d, self).<strong>init</strong>()<br>
self.out_w = out_w<br>
self.out_h = out_h</p>
<pre><code>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
</code></pre>
<p>def adaptive_max_pool(input, size):<br>
return AdaptiveMaxPool2d(size[0],size[1])(input)</p>
<p>def roi_pooling(input, rois, size=(7,7), spatial_scale=1.0):<br>
assert(rois.dim() == 2)<br>
assert(rois.size(1) == 5)<br>
output = []<br>
rois = rois.data.float()<br>
num_rois = rois.size(0)</p>
<pre><code>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)
</code></pre>
<p>my code for call the function is as:<br>
roi_pooling(myfeaturemap, rois, size=(1,1), spatial_scale=1.0/16)</p>
<p>where the type of myfeaturemap is torch.Tensor</p>
<p>but I got errors ,like follows:<br>
File “/home/user/lr/twostage/roi_pooling.py”, line 54, in roi_pooling<br>
output.append(adaptive_max_pool(im, size))<br>
File “/home/user/lr/twostage/roi_pooling.py”, line 37, in adaptive_max_pool<br>
return AdaptiveMaxPool2d(size[0],size[1])(input)<br>
File “/home/user/lr/twostage/roi_pooling.py”, line 21, in forward<br>
self._backend = type2backend[type(input)]<br>
File “/usr/local/lib/python2.7/dist-packages/torch/_thnn/<strong>init</strong>.py”, line 15, in <strong>getitem</strong><br>
return self.backends[name].load()<br>
KeyError: &lt;class ‘torch.Tensor’&gt;</p>
<p>, I don’t have ideas about above error, could anybody help me?</p>

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

Li_Rong: <p>Hi, thank you for your suggestion.</p>

ASMIftekhar: <p>You are absolutely right, thank you.</p>

Fansgithub2019: <p>The same error occurred. Fixed it by your method, but a new error occurred.</p>
<p>Traceback (most recent call last):<br>
File “demo.py”, line 275, in <br>
fps_pred, y_pred = model_conv(x)<br>
File “/home/fan/anaconda3/envs/tf/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 493, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “/home/fan/anaconda3/envs/tf/lib/python3.7/site-packages/torch/nn/parallel/data_parallel.py”, line 150, in forward<br>
return self.module(*inputs[0], **kwargs[0])<br>
File “/home/fan/anaconda3/envs/tf/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 493, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “demo.py”, line 230, in forward<br>
roi1 = roi_pooling_ims(_x1, boxNew.mm(p1), size=(16, 8))<br>
File “/home/fan/CCPD-master/rpnet/roi_pooling.py”, line 74, in roi_pooling_ims<br>
output.append(adaptive_max_pool(im, size))<br>
File “/home/fan/CCPD-master/rpnet/roi_pooling.py”, line 36, in adaptive_max_pool<br>
return AdaptiveMaxPool2d(size[0], size[1])(input)<br>
File “/home/fan/CCPD-master/rpnet/roi_pooling.py”, line 20, in forward<br>
self._backend.SpatialAdaptiveMaxPooling_updateOutput(<br>
File “/home/fan/anaconda3/envs/tf/lib/python3.7/site-packages/torch/_thnn/utils.py”, line 27, in <strong>getattr</strong><br>
raise NotImplementedError<br>
NotImplementedError</p>
<p>How to solve the problem？</p>

ttoosi: <p>Is there no support for type2backend in the new Pytorch?  type2backend.SpatialConvolutionMM_updateOutput  throws NotImplemeted Error but just for new Pytorch cuda10.</p>

albanD: <p>Hi,</p>
<p>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 <code>nn.ConvNd</code> for more details.</p>

Nishikanta_Parida: <p>Thank you very much.</p>