Skip to content

Instantly share code, notes, and snippets.

@aidin-foroughi
Last active October 4, 2023 16:16
Show Gist options
  • Select an option

  • Save aidin-foroughi/1d053b25abe20029f92ae2c91baeb6cf to your computer and use it in GitHub Desktop.

Select an option

Save aidin-foroughi/1d053b25abe20029f92ae2c91baeb6cf to your computer and use it in GitHub Desktop.
fiftyone

Some useful fiftyone tips

Visualize dataset using fifty one

import fiftyone
import fire
import pathlib


def hash_str(text: str):
    h = 0
    for ch in text:
        h = (h * 281 ^ ord(ch) * 997) & 0xFFFFFFFF
    return h


def visualize(dataset_path: str):
    """Visualizes a coco dataset using fiftyone desktop application"""
    input_dir = pathlib.Path(dataset_path).resolve()
    dataset_name = str(hash_str(str(input_dir)))
    if input_dir.exists() and input_dir.is_dir():
        images_path = input_dir.joinpath("images")
        instances_path = input_dir.joinpath("instances.json")
        if images_path.exists() and instances_path.exists():
            if dataset_name in fiftyone.list_datasets():
                fiftyone.delete_dataset(dataset_name)
            dataset = fiftyone.Dataset.from_dir(
                data_path=str(images_path),
                labels_path=str(instances_path),
                dataset_type=fiftyone.types.COCODetectionDataset,
                name=dataset_name,
            )
            session = fiftyone.launch_app(dataset, desktop=True)
            session.wait()
        else:
            print(
                "expected instances.json and images directory at path: {}".format(
                    input_dir
                )
            )
    else:
        print("{} not a valid directory".format(input_dir))


if __name__ == "__main__":
    fire.Fire(visualize)

Split dataset

import fiftyone.utils.splits as fous

fous.random_split(dataset, {"train": 0.9, "val": 0.1})

train_view = dataset.match_tags("train")

val_view = dataset.match_tags("val")

train_view.export("d:/brackets/samples/train", fiftyone.types.COCODetectionDataset)

val_view.export("d:/brackets/samples/val", fiftyone.types.COCODetectionDataset)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment