In the balloon.py file in Detectron2 samples, I get a KeyError of 'regions' whenever I run the balloon.py on my custom dataset. I figured something was wrong with the json file in the train folder, so I first used the latest VIA 3 and then VIA 2.0.0. Both jsons create the same KeyError.
I compared the balloon's training VIA json to my training VIA json, and they have the same structure now, so I'm thinking it isn't a json issue anymore. Why would Python not be able to read a string as a key?
Here's balloon.py: https://github.com/matterport/Mask_RCNN/blob/master/samples/balloon/balloon.py
How was the JSON file for your custom dataset exported from the VIA tool?
Depending on the version of VIA, there are two export options:
(a) Using the "Export Annotations (as JSON)" option under the "Annotation" menu. The resulting JSON only contains the annotation data, which are at the top level.
or
(b) Using the Save Project option (the floppy disk icon). The resulting JSON contains details of the entire project, and the annotations are placed in the _via_img_metadata
property.
Based on my experience, VIA 1.0 only seems to have (a) available, while VIA 2.0 has both (a) and (b).
Screenshots below of the two JSON structures.
The matterport/Mask_RCNN code hasn't been updated in a long time, and although the comments mention VIA 2.0, the sample still seems to be largely based on VIA 1.0. So, the balloon dataset's JSON structure matches (a) above (even the name of the JSON file - via_region_data.json is the default name given by VIA 1.0.6).
I can't be completely sure about this without looking at the contents of your JSON file, but it's likely that your custom dataset's JSON file structure doesn't actually match the expected one.
Basically, I think you have exported the file using option (b), while the code expects the one from (a). Since (b) doesn't contain a regions
property at the top level, you get the KeyError
.
If I'm right about this, you can fix the problem by modifying line 112 of balloon.py
(that loads the JSON) to the following.
annotations = json.load(open(os.path.join(dataset_dir, "via_region_data.json")))['_via_img_metadata']
Based on a quick look, the JSON structure coming out of VIA 3.0 seems to be completely different from (a) and (b). Therefore, trying to use a VIA 3.0 JSON file with the sample code would also throw a KeyError
. Supporting the VIA 3.0 format would require a complete modification of the load_balloon
method.