{"id":22706711,"url":"https://github.com/mbari-org/sdcat","last_synced_at":"2025-04-13T12:12:55.201Z","repository":{"id":222419017,"uuid":"757235276","full_name":"mbari-org/sdcat","owner":"mbari-org","description":"Sliced Detection and Clustering Analysis Toolkit","archived":false,"fork":false,"pushed_at":"2025-03-19T21:16:30.000Z","size":31923,"stargazers_count":5,"open_issues_count":4,"forks_count":0,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-04-13T12:12:37.512Z","etag":null,"topics":["clustering","deepsea","drone","hdbscan-clustering-algorithm","object-detection","plankton","sahi","saliency-detection","uav","yolov11","yolov5","yolov8"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mbari-org.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-14T04:19:49.000Z","updated_at":"2025-03-19T21:16:34.000Z","dependencies_parsed_at":"2024-03-18T21:00:36.257Z","dependency_job_id":"4f65cd82-f3a9-4c37-ab9b-2cb0046708e9","html_url":"https://github.com/mbari-org/sdcat","commit_stats":null,"previous_names":["mbari-org/sdcat"],"tags_count":61,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mbari-org%2Fsdcat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mbari-org%2Fsdcat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mbari-org%2Fsdcat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mbari-org%2Fsdcat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mbari-org","download_url":"https://codeload.github.com/mbari-org/sdcat/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248710448,"owners_count":21149191,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clustering","deepsea","drone","hdbscan-clustering-algorithm","object-detection","plankton","sahi","saliency-detection","uav","yolov11","yolov5","yolov8"],"created_at":"2024-12-10T10:09:31.885Z","updated_at":"2025-04-13T12:12:55.193Z","avatar_url":"https://github.com/mbari-org.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![MBARI](https://www.mbari.org/wp-content/uploads/2014/11/logo-mbari-3b.png)](http://www.mbari.org)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\n[![Python](https://img.shields.io/badge/language-Python-blue.svg)](https://www.python.org/downloads/)\n[![Run pytest](https://github.com/mbari-org/sdcat/actions/workflows/pytest.yml/badge.svg)](https://github.com/mbari-org/sdcat/actions/workflows/pytest.yml)\n\n**sdcat** \n\n*Sliced Detection and Clustering Analysis Toolkit*\n\nThis repository processes images using a sliced detection and clustering workflow.\nIf your images look something like the image below, and you want to detect objects in the images, \nand optionally cluster the detections, then this repository may be useful to you.\nThe repository is designed to be run from the command line, and can be run in a Docker container,\nwithout or with a GPU (recommended).\n\n--- \n![](https://raw.githubusercontent.com/mbari-org/sdcat/main/docs/imgs/example_images.jpg)\n---\nDetection\n---\nDetection can be done with a fine-grained saliency-based detection model,  and/or one the following models run with the SAHI algorithm.\nBoth detections algorithms (saliency and object dtection) are run by default and combined to produce the final detections.\nSAHI is short for Slicing Aided Hyper Inference, and is a method to slice images into smaller windows and run a detection model \non the windows.\n\n| Object Detection Model           | Description                                                        |\n|----------------------------------|--------------------------------------------------------------------|\n| yolov8s                          | YOLOv8s model from Ultralytics                                     |\n| hustvl/yolos-small               | YOLOS model a Vision Transformer (ViT)                             |\n| hustvl/yolos-tiny                | YOLOS model a Vision Transformer (ViT)                             |\n| MBARI-org/megamidwater (default) | MBARI midwater YOLOv5x for general detection in midwater images    |\n| MBARI-org/uav-yolov5             | MBARI UAV YOLOv5x for general detection in UAV images              |\n| MBARI-org/yolov5x6-uavs-oneclass | MBARI UAV YOLOv5x for general detection in UAV images single class |\n| FathomNet/MBARI-315k-yolov5      | MBARI YOLOv5x for general detection in benthic images              |\n\n\nTo skip saliency detection, use the --skip-saliency option. \n\n```shell\nsdcat detect --skip-saliency --image-dir \u003cimage-dir\u003e --save-dir \u003csave-dir\u003e --model \u003cmodel\u003e --slice-size-width 900 --slice-size-height 900\n```\n\nTo skip using the SAHI algorithm, use --skip-sahi.   \n\n```shell\nsdcat detect --skip-sahi --image-dir \u003cimage-dir\u003e --save-dir \u003csave-dir\u003e --model \u003cmodel\u003e --slice-size-width 900 --slice-size-height 900\n````\n\n---\nViTS + HDBSCAN Clustering\n---\nOnce the detections are generated, the detections can be clustered.  Alternatively, \ndetections can be clustered from a collection of images, sometimes referred to as \nregion of interests (ROIs) by providing the detections in a folder with the roi option.\n    \n```shell\nsdcat cluster roi --roi \u003croi\u003e --save-dir \u003csave-dir\u003e --model \u003cmodel\u003e \n```\n\nThe clustering is done with a Vision Transformer (ViT) model, and a cosine similarity metric with the HDBSCAN algorithm.\nThe ViT model is used to generate embeddings for the detections, and the HDBSCAN algorithm is used to cluster the detections.\nWhat is an embedding?  An embedding is a vector representation of an object in an image.  \n\nThe defaults are set to produce fine-grained clusters, but the parameters can be adjusted to produce coarser clusters.\nThe algorithm workflow looks like this:\n\n![](https://raw.githubusercontent.com/mbari-org/sdcat/main/docs/imgs/cluster_workflow.png)\n\n| Vision Transformer (ViT) Models      | Description                                                                    |\n|--------------------------------------|--------------------------------------------------------------------------------|\n| google/vit-base-patch16-224(default) | 16 block size trained on ImageNet21k with 21k classes                          |\n| facebook/dino-vits8                  | trained on ImageNet which contains 1.3 M images with labels from 1000 classes  |\n| facebook/dino-vits16                 | trained on ImageNet which contains 1.3 M images with labels from 1000 classes  |\n| MBARI-org/mbari-uav-vit-b-16         | MBARI UAV vits16 model trained on 10425 UAV images with labels from 21 classes |\n\nSmaller block_size means more patches and more accurate fine-grained clustering on smaller objects, so\nViTS models with 8 block size are recommended for fine-grained clustering on small objects, and 16 is recommended for coarser clustering on \nlarger objects.  We recommend running with multiple models to see which model works best for your data,\nand to experiment with the --min-samples and --min-cluster-size options to get good clustering results.\n   \n# Installation\n \nPip install the sdcat package with:\n\n```bash\npip install sdcat\n```\n\nAlternatively, [Docker](https://www.docker.com) can be used to run the code. A pre-built docker image is available at [Docker Hub](https://hub.docker.com/r/mbari/sdcat) with the latest version of the code.  \n \nDetection\n```shell\ndocker run -it -v $(pwd):/data mbari/sdcat detect --image-dir /data/images --save-dir /data/detections --model MBARI-org/uav-yolov5\n```\nFollowed by clustering\n```shell\ndocker run -it -v $(pwd):/data mbari/sdcat cluster detections --det-dir /data/detections/ --save-dir /data/detections --model MBARI-org/uav-yolov5\n```\n\nA GPU is recommended for clustering and detection.  If you don't have a GPU, you can still run the code, but it will be slower.\nIf running on a CPU, multiple cores are recommended and will speed up processing.\n\n```shell\ndocker run -it --gpus all -v $(pwd):/data mbari/sdcat:cuda124 detect --image-dir /data/images --save-dir /data/detections --model MBARI-org/uav-yolov5\n```\n\n# Commands\n\nTo get all options available, use the --help option.  For example:\n\n```shell\nsdcat --help\n```\nwhich will print out the following:\n```shell\nUsage: sdcat [OPTIONS] COMMAND [ARGS]...\n\n  Process images from a command line.\n\nOptions:\n  -V, --version  Show the version and exit.\n  -h, --help     Show this message and exit.\n\nCommands:\n  cluster  Cluster detections.\n  detect   Detect objects in images\n\n```\n\nTo get details on a particular command, use the --help option with the command.  For example, with the **cluster** command:\n\n```shell\n sdcat  cluster --help \n```\n\nwhich will print out the following:\n```shell\nUsage: sdcat cluster [OPTIONS] COMMAND [ARGS]...\n\n  Commands related to clustering images\n\nOptions:\n  -h, --help  Show this message and exit.\n\nCommands:\n  detections  Cluster detections.\n  roi         Cluster roi.\n```\n\n## File organization\n\nThe sdcat toolkit generates data in the following folders. Here, we assume both detection and clustering is stored in the same root folder:\n \n```\n/data/20230504-MBARI/\n└── detections\n    └── hustvl\n        └── yolos-small                         # The model used to generate the detections\n            ├── det_raw                         # The raw detections from the model\n            │   └── csv                    \n            │       ├── DSC01833.csv\n            │       ├── DSC01859.csv\n            │       ├── DSC01861.csv\n            │       └── DSC01922.csv\n            ├── det_filtered                    # The filtered detections from the model\n                ├── crops                       # Crops of the detections \n                ├── dino_vits8...date           # The clustering results - one folder per each run of the clustering algorithm\n                ├── dino_vits8..exemplars.csv   # Exemplar embeddings - examples with the highest cosine similarity within a cluster\n                ├── dino_vits8..detections.csv  # The detections with the cluster id\n            ├── stats.txt                       # Statistics of the detections\n            └── vizresults                      # Visualizations of the detections (boxes overlaid on images)\n                ├── DSC01833.jpg\n                ├── DSC01859.jpg\n                ├── DSC01861.jpg\n                └── DSC01922.jpg\n\n```\n\n## Process images creating bounding box detections with the YOLOv8s model.\nThe YOLOv8s model is not as accurate as other models, but is fast and good for detecting larger objects in images,\nand good for experiments and quick results. \n**Slice size** is the size of the detection window.  The default is to allow the SAHI algorithm to determine the slice size;\na smaller slice size will take longer to process.\n\n```shell\nsdcat detect --image-dir \u003cimage-dir\u003e --save-dir \u003csave-dir\u003e --model yolov8s --slice-size-width 900 --slice-size-height 900\n```\n\n## Cluster detections from the YOLOv8s model, but use the classifications from the ViT model.\n\nCluster the detections from the YOLOv8s model.  The detections are clustered using cosine similarity and embedding\nfeatures from the default Vision Transformer (ViT) model `google/vit-base-patch16-224` \n\n```shell\nsdcat cluster --det-dir \u003cdet-dir\u003e/yolov8s/det_filtered --save-dir \u003csave-dir\u003e  --use-vits\n```\n  \n\n# Related work\n* https://github.com/obss/sahi SAHI\n* https://arxiv.org/abs/2010.11929 An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale\n* https://github.com/facebookresearch/dinov2 DINOv2\n* https://arxiv.org/pdf/1911.02282.pdf HDBSCAN\n* https://github.com/muratkrty/specularity-removal Specularity Removal\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmbari-org%2Fsdcat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmbari-org%2Fsdcat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmbari-org%2Fsdcat/lists"}