cocodataset / panopticapi

COCO 2018 Panoptic Segmentation Task API (Beta version)
Other
418 stars 184 forks source link

Fix TypeError in JSON serialization in 2channels2panoptic_coco_format for PY3 #46

Closed yucornetto closed 3 years ago

yucornetto commented 3 years ago

Currently, 2channels2panoptic_coco_format.py can break with the following error for when calling save_json with PY3:

TypeError: Object of type int64 is not JSON serializable

To reproduce the errors, run the provided sample commands with PY3:

python3 converters/2channels2panoptic_coco_format.py \ --source_folder sample_data/panoptic_examples_2ch_format \ --images_json_file sample_data/images_info_examples.json \ --predictions_json_file converted_data/panoptic_coco_from_2ch.json

The issue can be caused by different behavior of the following lines: isinstance(np.int64(4), int) returns True in PY2 but False in PY3, which was also found in https://mail.python.org/pipermail/numpy-discussion/2015-June/073031.html

isinstance is used in python's JSON serialization to identify types: https://github.com/python/cpython/blob/main/Lib/json/encoder.py#L309 so in PY3 it will no longer directly serialize numpy scalars.

A solution to fix is to add a '.item()' to explicitly convert the numpy into int, which works for PY3.

alexander-kirillov commented 3 years ago

Great! Thanks a lot for the PR!