Repository: roboflow/supervision
File: supervision/dataset/core.py (Python)

Code before:

    def as_multiclass_folder_structure(self, output_directory_path: str) -> None:
        """
        Saves the dataset as a multi-class folder structure.

        Args:
            output_directory_path (str): The path to the directory where the dataset will be saved.

        Example:
            ```python
            >>> import supervision as sv

            >>> cd = sv.ClassificationDataset(...)

            >>> cd.as_multiclass_folder_structure(
            ...     root_directory_path="./images/valid/",
            ...     output_directory_path="./out",
            ... )
        """
        os.makedirs(output_directory_path, exist_ok=True)

        for class_name in self.classes:
            os.makedirs(os.path.join(output_directory_path, class_name), exist_ok=True)

            for image in self.annotations:
                for class_id in self.annotations[image].class_id:
                    if class_id == self.classes.index(class_name):
                        cv2.imwrite(
                            os.path.join(output_directory_path, class_name, image),
                            self.images[image],
                        )

    @classmethod
    def from_multiclass_folder_structure(
        cls, root_directory_path: str
    ) -> "ClassificationDataset":
        """
        Load data from a multiclass folder structure into a ClassificationDataset.

        Args:
            root_directory_path (str): The path to the dataset directory.

        Returns:
            ClassificationDataset: The dataset.

        Example:
            ```python
            >>> import supervision as sv

            >>> cd = sv.ClassificationDataset.from_multiclass_folder_structure(
            ...     root_directory_path="./dataset",

Review comment (suggestion):
This way we will save the same image multiple times. If we would get `Classification` as a result of the inference of `ResNet` trained on the `ImageNet` dataset we would probably save each image 1000 times. I think we need to adjust that strategy. 

Code after:

    def as_multiclass_folder_structure(self, root_directory_path: str) -> None:
        """
        Saves the dataset as a multi-class folder structure.

        Args:
            root_directory_path (str): The path to the directory where the dataset will be saved.
        """
        os.makedirs(root_directory_path, exist_ok=True)

        for class_name in self.classes:
            os.makedirs(os.path.join(root_directory_path, class_name), exist_ok=True)

        for image_name in self.images:
            classification = self.annotations[image_name]
            image = self.images[image_name]
            class_id = (
                classification.class_id[0]
                if classification.confidence is None
                else classification.get_top_k(1)[0]
            )
            class_name = self.classes[class_id]
            image_path = os.path.join(root_directory_path, class_name, image_name)
            cv2.imwrite(image_path, image)

    @classmethod
    def from_multiclass_folder_structure(
        cls, root_directory_path: str
    ) -> ClassificationDataset:
        """
        Load data from a multiclass folder structure into a ClassificationDataset.

        Args:
            root_directory_path (str): The path to the dataset directory.

        Returns:
            ClassificationDataset: The dataset.

        Example:
            ```python
            >>> import roboflow
            >>> from roboflow import Roboflow
            >>> import supervision as sv

            >>> roboflow.login()

            >>> rf = Roboflow()

            >>> project = rf.workspace(WORKSPACE_ID).project(PROJECT_ID)
            >>> dataset = project.version(PROJECT_VERSION).download("folder")