pytorch / pytorch/vision

ImageFolder dataset: "too many open files" error

Open
#539 12 comments 0 reactions 1 assignee View on GitHub

@fmassa is already working on this.

Since Aug 13, 2018.

module: datasets
Dominant language
Python
Stars
17.9k
Forks
7.3k
Avg merge
1d 15h
Merged PRs (30d)
13

Description

Hi,
I am using ImageFolder dataset to train on imagenet.
I repeatedly get "too many open files" OSError after training for several hours.
I suspect the issue comes from [here][1]:

def pil_loader(path):
    # open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
    with open(path, 'rb') as f:
        img = Image.open(f)
        return img.convert('RGB')  # <--

The return is inside with ... clause, thus (I suspect) the file handle f is not closed properly when the function returns.
Shouldn't the function be

def pil_loader(path):
    # open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
    with open(path, 'rb') as f:
        img = Image.open(f)
    return img.convert('RGB')  # <--  not indented

Thanks,
-Shai
[1]: https://github.com/pytorch/vision/blob/master/torchvision/datasets/folder.py#L156

cc @pmeier

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.