- Part 1 - Dataset Enrichment with Zero-Shot Classification Models
- Part 2 - Dataset Enrichment with Zero-Shot Detection Models
- Part 3 - Dataset Enrichment with Zero-Shot Segmentation Models
👍 Purpose This notebook shows how you can enrich the metadata of your visual dataset using open-source zero-shot models image segmentation model Segment Anything (SAM). By the end of this notebook, you’ll learn how to:
- Install and load the SAM in fastdup.
- Enrich your dataset using masks generated by the SAM model.
- Run inference using SAM on a single image.
Installation
First, let’s install the necessary packages:- fastdup - To analyze issues in the dataset.
- Segment Anything Model - To use the SAM model.
- gdown - To download demo data hosted on Google Drive.
🚧 CUDA Runtime fastdup runs perfectly on CPUs, but larger models like SAM runs much slower on CPU compared to GPU. This codes in this notebook can be run on CPU or GPU. But, we highly recommend running in CUDA-enabled environment to reduce the run time. Running this notebook in Google Colab or Kaggle is a good start!
Download Dataset
Download the coco-minitrain dataset - A curated mini-training set consisting of 20% of COCO 2017 training dataset. Thecoco-minitrain consists of 25,000 images and annotations.
First, let’s load the dataset from the coco-minitrain dataset.
Zero-Shot Segmentation with SAM
In addition to the zero-shot recognition and detection modes, fastdup also supports zero-shot segmentation using the Segment Anything Model (SAM) from MetaAI. SAM produces high-quality object masks from input prompts such as points or boxes, and it can be used to generate masks for all objects in an image.1. Inference on a bulk of images
In Part 2 of the enrichment notebook series, we utilized Grounding DINO as a zero-shot detection model and ran an inference over the images in our dataset. We ended up with a DataFrame consisting offilename,ram_tags, grounding_dino_bboxes, grounding_dino_scores and grounding_dino_labels column as follows.
If you’d like to reproduce the above DataFrame, Part 2 notebook details the code you need to run.
Similar to all previous examples, you can use the enrich method to add masks to your DataFrame of images.
In the following code snippet, we load the SAM model and specify input_col='grounding_dino_bboxes' to allow SAM to use the bounding boxes as inputs.
'sam_masks'which contains the instance segmentation masks for each bounding box.
📘 More onTo verify the results, let’s plot the images with bounding boxes and masks.fd.enrichEnriches an inputDataFrameby applying a specified model to perform a specific task. Currently supports the following parameters:
2. Inference on a single image
To run an inference using the SAM model, import theSegmentAnythingModel class and provide an image-bounding box pair as the input.
Let’s suppose we’d like to run an inference on the following image.
run_inferencemethod and provide the image and bounding box as the input argument.
The value torch.tensor((1.47, 1.45, 638.46, 241.37))) is a bounding box of the sky for the image above.
sam_vit_b weights and the sam_vit_l weights from into your local folder and load them into the constructor as follows.
Wrap Up
In this tutorial, we showed how you can run Segment Anything Model as a zero-shot segmentation model to enrich your dataset. This notebook is Part 3 of the dataset enrichment notebook series where we utilize various zero-shot models to enrich datasets.- Part 1 - Dataset Enrichment with Zero-Shot Classification Models
- Part 2 - Dataset Enrichment with Zero-Shot Detection Models
- Part 3 - Dataset Enrichment with Zero-Shot Segmentation Models
👍 Next Up Try out the Google Colab and Kaggle notebook to reproduce this example.Questions about this tutorial? Reach out to us on our Slack channel!
VL Profiler - A faster and easier way to diagnose and visualize dataset issues
The team behind fastdup also recently launched VL Profiler, a no-code cloud-based platform that lets you leverage fastdup in the browser. VL Profiler lets you find:- Duplicates/near-duplicates.
- Outliers.
- Mislabels.
- Non-useful images.
👍 Free Usage Use VL Profiler for free to analyze issues on your dataset with up to 1,000,000 images. Get started for free.Not convinced yet? Interact with a collection of datasets like ImageNet-21K, COCO, and DeepFashion here. No sign-ups needed.