Search code examples
pythonpytorchnonetype

Why do print statements sometimes fail to print in Pytorch?


I am working with the pycocotools library, and with an implementation of DETR (deformable transformers for object detection).

I am debugging a portion of the code which makes a call to the loadRes function found [here][1].

I added print statements before an after the line res = COCO(), because the line res.dataset['images'] = [img for img in self.dataset['images']] is giving an error (AttributeError: 'NoneType' object has no attribute 'dataset').

When I try to trace through the code and find the current value of res (in particular, why it is None), I notice that none of my print statements are appearing in output, despite clearly being present in the traceback.

Why might this be happening?

AttributeError                            Traceback (most recent call last)
<ipython-input-4-ff0c7180ea84> in <module>
    175 
--> 176     test_stats, coco_evaluator = evaluate(
    177         model, criterion, postprocessors, data_loader_val, base_ds, "cuda", args["output_dir"]
    178     )

~/anaconda3/lib/python3.8/site-packages/torch/autograd/grad_mode.py in decorate_context(*args, **kwargs)
     25         def decorate_context(*args, **kwargs):
     26             with self.clone():
---> 27                 return func(*args, **kwargs)
     28         return cast(F, decorate_context)
     29 

~/Desktop/traffic_sign_relevance-main/attention/engine.py in evaluate(model, criterion, postprocessors, data_loader, base_ds, device, output_dir)
    128         res = {target['image_id'].item(): output for target, output in zip(targets, results)}
    129         if coco_evaluator is not None:
--> 130             coco_evaluator.update(res)
    131 
    132         if panoptic_evaluator is not None:

~/Desktop/traffic_sign_relevance-main/attention/datasets/coco_eval.py in update(self, predictions)
     52             with open(os.devnull, 'w') as devnull:
     53                 with contextlib.redirect_stdout(devnull):
---> 54                     coco_dt = COCO.loadRes(self.coco_gt, results) if results else COCO()
     55             coco_eval = self.coco_eval[iou_type]
     56 

~/anaconda3/lib/python3.8/site-packages/pycocotools/coco.py in loadRes(self, resFile)
    316         print("In loadRes")
    317         print(type(res))
--> 318         res.dataset['images'] = [img for img in self.dataset['images']]
    319 
    320         print('Loading and preparing results...')

AttributeError: 'NoneType' object has no attribute 'dataset' ```




  [1]: https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocotools/coco.py

Solution

  • This happens because of the lines 52, 53 which block pycocotools prints. If you comment out the block 52-54 and take coco_dt = ... out of the with blocks like

     51             """
     52             with open(os.devnull, 'w') as devnull:
     53                 with contextlib.redirect_stdout(devnull):
     54                     coco_dt = COCO.loadRes(self.coco_gt, results) if results else COCO() 
     55             """
     56             coco_dt = COCO.loadRes(self.coco_gt, results) if results else COCO()
     57             coco_eval = self.coco_eval[iou_type]
    

    your print statements will be shown on screen.