Skip to content

Commit

Permalink
catch and ignore potential exif transpose bug (#361)
Browse files Browse the repository at this point in the history
Summary:
There is a bug in Pillow not yet fixed when exif-transposing images:

python-pillow/Pillow#3973

and this can crash the whole training, this PR is to catch the bug and ignore and use the image as is

I believe that if the exif data was valuable for the user, he will check before hand that they are correct, so this PR should not impact anything meaniningfull

error to be fixed:

```
Original Traceback (most recent call last):
  File "/miniconda/envs/sterblue/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
    data = fetcher.fetch(index)
  File "/miniconda/envs/sterblue/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/miniconda/envs/sterblue/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/miniconda/envs/sterblue/lib/python3.7/site-packages/detectron2/data/common.py", line 39, in __getitem__
    data = self._map_func(self._dataset[cur_idx])
  File "/miniconda/envs/sterblue/lib/python3.7/site-packages/detectron2/data/dataset_mapper.py", line 74, in __call__
    image = utils.read_image(dataset_dict["file_name"], format=self.img_format)
  File "/miniconda/envs/sterblue/lib/python3.7/site-packages/detectron2/data/detection_utils.py", line 49, in read_image
    image = ImageOps.exif_transpose(image)
  File "/miniconda/envs/sterblue/lib/python3.7/site-packages/PIL/ImageOps.py", line 549, in exif_transpose
    transposed_image.info["exif"] = exif.tobytes()
  File "/miniconda/envs/sterblue/lib/python3.7/site-packages/PIL/Image.py", line 3213, in tobytes
    return b"Exif\x00\x00" + head + ifd.tobytes(offset)
  File "/miniconda/envs/sterblue/lib/python3.7/site-packages/PIL/TiffImagePlugin.py", line 837, in tobytes
    count = len(data)
TypeError: object of type 'int' has no len()
```
Pull Request resolved: facebookresearch/detectron2#361

Differential Revision: D18630767

Pulled By: ppwwyyxx

fbshipit-source-id: 79ec76454ee853a2dbd82c750071b69ff535df53
  • Loading branch information
obendidi authored and facebook-github-bot committed Nov 21, 2019
1 parent f485b46 commit a4f6fc0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion detectron2/data/detection_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ def read_image(file_name, format=None):
with PathManager.open(file_name, "rb") as f:
image = Image.open(f)

image = ImageOps.exif_transpose(image)
# capture and ignore this bug: https://github.com/python-pillow/Pillow/issues/3973
try:
image = ImageOps.exif_transpose(image)
except Exception:
pass

if format is not None:
# PIL only supports RGB, so convert to RGB and flip channels over below
Expand Down
2 changes: 1 addition & 1 deletion tools/plain_train_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def get_evaluator(cfg, dataset_name, output_folder=None):

def do_test(cfg, model):
results = OrderedDict()
for idx, dataset_name in enumerate(cfg.DATASETS.TEST):
for dataset_name in cfg.DATASETS.TEST:
data_loader = build_detection_test_loader(cfg, dataset_name)
evaluator = get_evaluator(
cfg, dataset_name, os.path.join(cfg.OUTPUT_DIR, "inference", dataset_name)
Expand Down

0 comments on commit a4f6fc0

Please sign in to comment.