Skip to content

Commit 72ebbb9

Browse files
davidnichols-opsdevin-ai-integration[bot]
andcommitted
feat: add option to preserve class ordering during model deployment
Add DISABLE_CLASS_SORTING configuration option to prevent automatic alphabetical sorting of class names during YOLO model deployment. Users can set ROBOFLOW_DISABLE_CLASS_SORTING=true to maintain custom class ordering from model checkpoints. - Add DISABLE_CLASS_SORTING to config.py (defaults to false) - Update model_processor.py to conditionally apply sorting - Include warning comment about user responsibility for proper class ordering - Maintain backwards compatibility with default behavior Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 107fb34 commit 72ebbb9

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

roboflow/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def get_conditional_configuration_variable(key, default):
8686

8787
RF_WORKSPACES = get_conditional_configuration_variable("workspaces", default={})
8888
TQDM_DISABLE = os.getenv("TQDM_DISABLE", None)
89+
DISABLE_CLASS_SORTING = os.getenv("ROBOFLOW_DISABLE_CLASS_SORTING", "false").lower() == "true"
8990

9091

9192
def load_roboflow_api_key(workspace_url=None):

roboflow/util/model_processor.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import yaml
88

99
from roboflow.config import (
10+
DISABLE_CLASS_SORTING,
1011
TASK_CLS,
1112
TASK_DET,
1213
TASK_OBB,
@@ -224,7 +225,11 @@ def _process_yolo(model_type: str, model_path: str, filename: str) -> tuple[str,
224225
class_names = []
225226
for i, val in enumerate(model_instance.names):
226227
class_names.append((val, model_instance.names[val]))
227-
class_names.sort(key=lambda x: x[0])
228+
# NOTE: When DISABLE_CLASS_SORTING is enabled, users are responsible for ensuring
229+
# their model's names dict has properly ordered/sequential keys. Non-sequential keys
230+
# may result in incorrect class-to-index mappings.
231+
if not DISABLE_CLASS_SORTING:
232+
class_names.sort(key=lambda x: x[0])
228233
class_names = [x[1] for x in class_names]
229234

230235
if (

0 commit comments

Comments
 (0)