Skip to main content

Sliced Detection and Clustering Analysis Toolkit - Developed by MBARI

Project description

MBARI semantic-release License Python

sdcat

Sliced Detection and Clustering Analysis Toolkit

This repository processes images using a sliced detection and clustering workflow. If your images look something like the image below, and you want to detect objects in the images, and optionally cluster the detections, then this repository is for you.


Drone/UAV

ISIIS Plankton Imager


DeepSea Imaging System

DINO + HDBSCAN Clustering

The clustering is done with a DINO Vision Transformer (ViT) model, and a cosine similarity metric with the HDBSCAN algorithm. The DINO model is used to generate embeddings for the detections, and the HDBSCAN algorithm is used to cluster the detections. To reduce the dimensionality of the embeddings, the t-SNE algorithm is used to reduce the embeddings to 2D. The defaults are set to produce fine-grained clusters, but the parameters can be adjusted to produce coarser clusters. The algorithm workflow looks like this:

Installation

Pip install the sdcat package including all requirements with:

pip install sdcat

Alternatively, Docker can be used to run the code. A pre-built docker image is available at Docker Hub with the latest version of the code.

Detection

docker run -it -v $(pwd):/data mbari/sdcat detect --image-dir /data/images --save-dir /data/detections --model MBARI-org/uav-yolov5

Followed by clustering

docker run -it -v $(pwd):/data mbari/sdcat cluster detections --det-dir /data/detections/ --save-dir /data/detections --model MBARI-org/uav-yolov5

A 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. If running on a CPU, multiple cores are recommended and will speed up processing.

docker run -it --gpus all -v $(pwd):/data mbari/sdcat:latest-cuda12 detect --image-dir /data/images --save-dir /data/detections --model MBARI-org/uav-yolov5

Commands

To get all options available, use the --help option. For example:

sdcat --help

which will print out the following:

Usage: sdcat [OPTIONS] COMMAND [ARGS]...

  Process images from a command line.

Options:
  -V, --version  Show the version and exit.
  -h, --help     Show this message and exit.

Commands:
  cluster  Cluster detections.
  detect   Detect objects in images

To get details on a particular command, use the --help option with the command. For example, with the cluster command:

 sdcat  cluster --help 

which will print out the following:

Usage: sdcat cluster [OPTIONS]

  Cluster detections from a single collection.

Options:
  --det-dir TEXT      Input folder with raw detection results
  --save-dir TEXT     Output directory to save clustered detection results
  --device TEXT       Device to use.
  -h, --help          Show this message and exit.

File organization

The sdcat toolkit generates data in the following folders. Here, we assume both detection and clustering is output to the same root folder.:

/data/20230504-MBARI/
└── detections
    └── hustvl
        └── yolos-small                         # The model used to generate the detections
            ├── det_raw                         # The raw detections from the model
            │   └── csv                    
            │       ├── DSC01833.csv
            │       ├── DSC01859.csv
            │       ├── DSC01861.csv
            │       └── DSC01922.csv
            ├── det_filtered                    # The filtered detections from the model
            ├── det_filtered_clustered          # Clustered detections from the model
                ├── crops                       # Crops of the detections 
                ├── dino_vits8...date           # The clustering results - one folder per each run of the clustering algorithm
                ├── dino_vits8..exemplars.csv   # Exemplar embeddings - examples with the highest cosine similarity within a cluster
                ├── dino_vits8..detections.csv  # The detections with the cluster id
            ├── stats.txt                       # Statistics of the detections
            └── vizresults                      # Visualizations of the detections (boxes overlaid on images)
                ├── DSC01833.jpg
                ├── DSC01859.jpg
                ├── DSC01861.jpg
                └── DSC01922.jpg

Process images creating bounding box detections with the YOLOv5 model.

The YOLOv5s model is not as accurate as other models, but is fast and good for detecting larger objects in images, and good for experiments and quick results. Slice size is the size of the detection window. The default is to allow the SAHI algorithm to determine the slice size; a smaller slice size will take longer to process.

sdcat detect --image-dir <image-dir> --save-dir <save-dir> --model yolov5s --slice-size-width 900 --slice-size-height 900

Cluster detections from the YOLOv5 model

Cluster the detections from the YOLOv5 model. The detections are clustered using cosine similarity and embedding features from a FaceBook Vision Transformer (ViT) model.

sdcat cluster --det-dir <det-dir> --save-dir <save-dir> --model yolov5s

Related work

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sdcat-1.8.0.tar.gz (34.8 kB view details)

Uploaded Source

Built Distribution

sdcat-1.8.0-py3-none-any.whl (41.2 kB view details)

Uploaded Python 3

File details

Details for the file sdcat-1.8.0.tar.gz.

File metadata

  • Download URL: sdcat-1.8.0.tar.gz
  • Upload date:
  • Size: 34.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1023-azure

File hashes

Hashes for sdcat-1.8.0.tar.gz
Algorithm Hash digest
SHA256 db6d602625889a658c5a1100babb2729f23e518e2314d20abef0db575a91d7c7
MD5 6ed9a6c4f7f1da8430e38db72543b1a4
BLAKE2b-256 93b613bc5e48dc495647fea210c2fb4eced1aab7de643ea86788d792218a64b3

See more details on using hashes here.

File details

Details for the file sdcat-1.8.0-py3-none-any.whl.

File metadata

  • Download URL: sdcat-1.8.0-py3-none-any.whl
  • Upload date:
  • Size: 41.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1023-azure

File hashes

Hashes for sdcat-1.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7b6855a98d7c995bea47316b6e64792d57c4c72c7a32ed0f58b861a68e89ce84
MD5 1ea4f39c5514816614d7af5778f8358f
BLAKE2b-256 f2390d6037b73bbc18e19317f318164777f900c1fae25ef465cfb2be751f40cc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page