RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn kitti dataset

allenpeng: <p>Hello everyone:</p>
<p>I’m trying to paint the point cloud by seg result.</p>
<p>To do so, I loaded the deeplab3+ model when processing kitti dataset and inference the image, after points painted, I use these points to train 3d object detection model.</p>
<p>and I encounter the following bugs <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>File “./tools/train.py”, line 129, in <br>
main()<br>
File “./tools/train.py”, line 124, in main<br>
logger=logger,<br>
File “/root/Det3D/det3d/torchie/apis/train.py”, line 343, in train_detector<br>
trainer.run(data_loaders, cfg.workflow, cfg.total_epochs, local_rank=cfg.local_rank)<br>
File “/root/Det3D/det3d/torchie/trainer/trainer.py”, line 536, in run<br>
epoch_runner(data_loaders[i], self.epoch, **kwargs)<br>
File “/root/Det3D/det3d/torchie/trainer/trainer.py”, line 411, in train<br>
self.call_hook(“after_train_iter”)<br>
File “/root/Det3D/det3d/torchie/trainer/trainer.py”, line 325, in call_hook<br>
getattr(hook, fn_name)(self)<br>
File “/root/Det3D/det3d/torchie/trainer/hooks/optimizer.py”, line 17, in after_train_iter<br>
trainer.outputs[“loss”].backward()<br>
File “/usr/local/lib/python3.6/dist-packages/torch/tensor.py”, line 107, in backward<br>
torch.autograd.backward(self, gradient, retain_graph, create_graph)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/autograd/<strong>init</strong>.py”, line 93, in backward<br>
allow_unreachable=True)  # allow_unreachable flag<br>
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn</p>
<p>I was wondering it’s the problem of using the segmentation model in the wrong way.<br>
def seg_inference(image):</p>
<p>The following code is how I use segmentation model for inference, I don’t need to train it.</p>
<pre><code>device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

checkpoint = torch.load("/root/Det3D/deeplab/run/cityscapes/deeplab-resnet/model_best.pth.tar")

seg_model = DeepLab(num_classes=19 , backbone='resnet',output_stride=16,sync_bn=True,freeze_bn=False)
seg_model.load_state_dict(checkpoint['state_dict'])
seg_model.eval()
seg_model.to(device)

def transform(image):
        return tr.Compose([tr.Resize((513,513)), tr.ToTensor(),
        tr.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))])(image)

torch.set_grad_enabled(False)
for param in seg_model.parameters():
        param.requires_grad = False
inputs = transform(image).to(device)
output = seg_model(inputs.unsqueeze(0)).squeeze().cpu().numpy()
output = np.resize(output, (19,image.size[0],image.size[1]))[[13,11,12,18]]
output = np.transpose(output,(2,1,0))
#pred = np.argmax(output, axis=0)
return output
</code></pre>
<p>Hope someone can help me. Thank you very much!</p>

ptrblck: <p>It seems your code tries to train the model using some <code>trainer</code> class, which isn’t defined in your code snippet.<br>
If you freeze all parameters or disable the gradient calculation via <code>torch.set_grad_enabled(False)</code>, you won’t be able to calculate the gradients with a <code>backward</code> call.</p>
<p>Could you recheck your code and see, where the <code>trainer</code> comes from?</p>

allenpeng: <p>Thank you very much!</p>
<p>I found when I use first model for inference, I set torch.set_grad_enabled(False).</p>