DEBUG: PIL.Image:Error closing: 'NoneType' object has no attribute 'close'

I am using DataLoader with ImageFolder, and logging set level to be logging.DEBUG.

train_dataset = datasets.ImageFolder(args.dataset, transform)
train_loader = DataLoader(train_dataset, batch_size=args.batch_size)

logging.basicConfig(filename=args.checkpoint_dir + ‘/log’, level=logging.DEBUG)

My training loop runs like this:
for batch_id, (x, _) in enumerate(train_loader):
both batch_id and x are evaluated during the loop.

However I am getting:
DEBUG: PIL.Image:Error closing: ‘NoneType’ object has no attribute 'close’
inside the log file, though the training process and resulting models are just working fine.

Does anyone know how can I get rid of these warnings?

if you set logging level debug, then PIL will give you debug warnings…

Thanks, I have tried INFO level which did not warn me about the PIL anymore.

But is there a way to get rid of the debug warning from PIL without changing the logging level?

There’s an open issue for this on the torchvision github. Although still not sure why PIL logs this

You could ignore the warnings with something like this:

with warnings.catch_warnings():
    warnings.simplefilter('ignore', SpecificWarningObject)

    #do something that raises a Warning

Or you could add a specific filter, e.g.

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)

So I looked into it a bit and have answered on the issue: https://github.com/pytorch/vision/issues/305

In short, either bump up your log level (not preferred) or give your logger a name and add your handlers to that logger

1 Like

Thanks for that.:smile:

@pandorana Figured out how to avoid this in torchvision exlplained here: https://github.com/pytorch/vision/issues/305