Skip to content

multilabel classification upload with the CLI 😎 #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion roboflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from roboflow.models import CLIPModel, GazeModel # noqa: F401
from roboflow.util.general import write_line

__version__ = "1.1.65"
__version__ = "1.1.66"


def check_key(api_key, model, notebook, num_retries=0):
Expand Down
2 changes: 2 additions & 0 deletions roboflow/core/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ def _save_annotation(image_id, imagedesc):
if isinstance(annotationdesc, dict):
if annotationdesc.get("type") == "classification_folder":
annotation_path = annotationdesc.get("classification_label")
elif annotationdesc.get("type") == "classification_multilabel":
annotation_path = json.dumps(annotationdesc.get("labels", []))
elif annotationdesc.get("rawText"):
annotation_path = annotationdesc
elif annotationdesc.get("file"):
Expand Down
26 changes: 22 additions & 4 deletions roboflow/util/folderparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ def _filterIndividualAnnotations(image, annotation, format, imgRefMap, annotatio
return _annotation
else:
return None
elif format == "multilabel_csv":
rows = [r for r in parsed["rows"] if r["file_name"] == image["name"]]
if rows:
labels = rows[0]["labels"]
return {"type": "classification_multilabel", "labels": labels}
else:
return None
Comment on lines +198 to +203
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid iterating all data if you need only first item.

Suggested change
rows = [r for r in parsed["rows"] if r["file_name"] == image["name"]]
if rows:
labels = rows[0]["labels"]
return {"type": "classification_multilabel", "labels": labels}
else:
return None
rows = [r for r in parsed["rows"] if r["file_name"] == image["name"]]
row = next(r for r in parsed["rows"] if r["file_name"] == image["name"], None)
if row:
labels = row["labels"]
return {"type": "classification_multilabel", "labels": labels}
else:
return None

elif format == "jsonl":
jsonlLines = [json.dumps(line) for line in parsed if line["image"] == image["name"]]
if jsonlLines:
Expand All @@ -218,8 +225,9 @@ def _loadAnnotations(folder, annotations):
ann["parsed"] = _read_jsonl(f"{folder}{ann['file']}")
ann["parsedType"] = "jsonl"
elif extension == ".csv":
ann["parsedType"] = "csv"
ann["parsed"] = _parseAnnotationCSV(f"{folder}{ann['file']}")
parsed = _parseAnnotationCSV(f"{folder}{ann['file']}")
ann["parsed"] = parsed
ann["parsedType"] = parsed.get("type", "csv")
return annotations


Expand All @@ -241,10 +249,20 @@ def _parseAnnotationCSV(filename):
# TODO: use a proper CSV library?
with open(filename) as f:
lines = f.readlines()
headers = lines[0]
headers = [h.strip() for h in lines[0].split(",")]
# Multi-label classification csv typically named _classes.csv
if os.path.basename(filename) == "_classes.csv":
parsed_lines = []
for line in lines[1:]:
parts = [p.strip() for p in line.split(",")]
file_name = parts[0]
labels = [headers[i] for i, v in enumerate(parts[1:], start=1) if v == "1"]
parsed_lines.append({"file_name": file_name, "labels": labels})
return {"type": "multilabel_csv", "rows": parsed_lines, "headers": headers}
header_line = lines[0]
lines = [{"file_name": ld.split(",")[0].strip(), "line": ld} for ld in lines[1:]]
return {
"headers": headers,
"headers": header_line,
"lines": lines,
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Skin-Problem-MultiLabel > 2023-12-26 4:26pm
https://universe.roboflow.com/parin-kittipongdaja-vwmn3/skin-problem-multilabel

Provided by a Roboflow user
License: CC BY 4.0
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
482 changes: 482 additions & 0 deletions tests/datasets/skinproblem-multilabel-classification/test/_classes.csv

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3,379 changes: 3,379 additions & 0 deletions tests/datasets/skinproblem-multilabel-classification/train/_classes.csv

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from unittest.mock import patch

import requests
Expand Down Expand Up @@ -625,3 +626,43 @@ def capture_annotation_calls(annotation_path, **kwargs):
finally:
for mock in mocks.values():
mock.stop()

def test_multilabel_classification_dataset_upload(self):
from roboflow.util import folderparser

multilabel_folder = "tests/datasets/skinproblem-multilabel-classification"
parsed_dataset = folderparser.parsefolder(multilabel_folder, is_classification=True)

self.project.type = "classification"
self.project.multilabel = True
annotation_calls = []

def capture_annotation_calls(annotation_path, **kwargs):
annotation_calls.append(annotation_path)
return ({"success": True}, 0.1, 0)

mocks = {
"parser": patch("roboflow.core.workspace.folderparser.parsefolder", return_value=parsed_dataset),
"upload": patch(
"roboflow.core.workspace.Project.upload_image",
return_value=({"id": "test-id", "success": True}, 0.1, 0),
),
"save_annotation": patch(
"roboflow.core.workspace.Project.save_annotation", side_effect=capture_annotation_calls
),
"get_project": patch(
"roboflow.core.workspace.Workspace._get_or_create_project", return_value=(self.project, False)
),
}
for mock in mocks.values():
mock.start()
try:
self.workspace.upload_dataset(dataset_path=multilabel_folder, project_name=PROJECT_NAME, num_workers=1)
self.assertEqual(len(annotation_calls), len(parsed_dataset["images"]))
for call in annotation_calls:
labels = json.loads(call)
self.assertIsInstance(labels, list)
self.assertGreater(len(labels), 0)
finally:
for mock in mocks.values():
mock.stop()
9 changes: 9 additions & 0 deletions tests/util/test_folderparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ def test_parse_classification_folder_structure(self):
self.assertEqual(img["annotationfile"]["type"], "classification_folder")
self.assertEqual(img["annotationfile"]["classification_label"], "no-corrosion")

def test_parse_multilabel_classification_csv(self):
folder = f"{thisdir}/../datasets/skinproblem-multilabel-classification"
parsed = folderparser.parsefolder(folder, is_classification=True)
images = {img["name"]: img for img in parsed["images"]}
img1 = images.get("101_jpg.rf.ffb91e580c891eb04b715545274b2469.jpg")
self.assertIsNotNone(img1)
self.assertEqual(img1["annotationfile"]["type"], "classification_multilabel")
self.assertEqual(set(img1["annotationfile"]["labels"]), {"Blackheads"})


def _assertJsonMatchesFile(actual, filename):
with open(filename) as file:
Expand Down