Skip to content

Commit

Permalink
Fix case of missing label in Forest Damage dataset (microsoft#499)
Browse files Browse the repository at this point in the history
* fix case of missing label

* mypy fix
  • Loading branch information
nilsleh authored and remtav committed May 26, 2022
1 parent e83f0f5 commit 50f676f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 11 deletions.
Binary file modified tests/data/forestdamage/Data_Set_Larch_Casebearer.zip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<annotation><filename>B01_0004.xml</filename><size><width>32</width><height>32</height><depth>3</depth></size><object><damage>other</damage><bndbox><xmin>8</xmin><ymin>8</ymin><xmax>24</xmax><ymax>24</ymax></bndbox></object></annotation>
<annotation><filename>B01_0004.xml</filename><size><width>32</width><height>32</height><depth>3</depth></size><object><damage>other</damage><bndbox><xmin>8</xmin><ymin>8</ymin><xmax>24</xmax><ymax>24</ymax></bndbox></object><object><bndbox><xmin>8</xmin><ymin>8</ymin><xmax>24</xmax><ymax>24</ymax></bndbox></object></annotation>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<annotation><filename>B01_0005.xml</filename><size><width>32</width><height>32</height><depth>3</depth></size><object><damage>other</damage><bndbox><xmin>8</xmin><ymin>8</ymin><xmax>24</xmax><ymax>24</ymax></bndbox></object></annotation>
<annotation><filename>B01_0005.xml</filename><size><width>32</width><height>32</height><depth>3</depth></size><object><damage>other</damage><bndbox><xmin>8</xmin><ymin>8</ymin><xmax>24</xmax><ymax>24</ymax></bndbox></object><object><bndbox><xmin>8</xmin><ymin>8</ymin><xmax>24</xmax><ymax>24</ymax></bndbox></object></annotation>
17 changes: 10 additions & 7 deletions tests/data/forestdamage/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"Bebehojd_20190527/Annotations/B01_0004.xml",
"Bebehojd_20190527/Annotations/B01_0005.xml",
],
"labels": [True, False],
}


Expand All @@ -38,15 +39,17 @@ def create_annotation(path: str) -> None:
ET.SubElement(size, "height").text = str(SIZE)
ET.SubElement(size, "depth").text = str(3)

annotation = ET.SubElement(root, "object")
for label in PATHS["labels"]:
annotation = ET.SubElement(root, "object")

ET.SubElement(annotation, "damage").text = "other"
if label:
ET.SubElement(annotation, "damage").text = "other"

bbox = ET.SubElement(annotation, "bndbox")
ET.SubElement(bbox, "xmin").text = str(0 + int(SIZE / 4))
ET.SubElement(bbox, "ymin").text = str(0 + int(SIZE / 4))
ET.SubElement(bbox, "xmax").text = str(SIZE - int(SIZE / 4))
ET.SubElement(bbox, "ymax").text = str(SIZE - int(SIZE / 4))
bbox = ET.SubElement(annotation, "bndbox")
ET.SubElement(bbox, "xmin").text = str(0 + int(SIZE / 4))
ET.SubElement(bbox, "ymin").text = str(0 + int(SIZE / 4))
ET.SubElement(bbox, "xmax").text = str(SIZE - int(SIZE / 4))
ET.SubElement(bbox, "ymax").text = str(SIZE - int(SIZE / 4))

tree = ET.ElementTree(root)
tree.write(path)
Expand Down
2 changes: 1 addition & 1 deletion tests/datasets/test_forestdamage.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def dataset(self, monkeypatch: MonkeyPatch, tmp_path: Path) -> ForestDamage:

url = os.path.join(data_dir, "Data_Set_Larch_Casebearer.zip")

md5 = "a6adc19879c1021cc1ba8d424e19c9e0"
md5 = "52d82ac38899e6e6bb40aacda643ee15"

monkeypatch.setattr(ForestDamage, "url", url)
monkeypatch.setattr(ForestDamage, "md5", md5)
Expand Down
7 changes: 6 additions & 1 deletion torchgeo/datasets/forestdamage.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ def parse_pascal_voc(path: str) -> Dict[str, Any]:
int(bndbox.find("xmax").text), # type: ignore[union-attr, arg-type]
int(bndbox.find("ymax").text), # type: ignore[union-attr, arg-type]
]
label = obj.find("damage").text # type: ignore[union-attr]

label_var = obj.find("damage")
if label_var is not None:
label = label_var.text
else:
label = "other"
bboxes.append(bbox)
labels.append(label)
return dict(filename=filename, bboxes=bboxes, labels=labels)
Expand Down

0 comments on commit 50f676f

Please sign in to comment.