LogoLogo
API ReferenceGitHubSlackService StatusLogin
v3.1.5
v3.1.5
  • Deep Lake Docs
  • List of ML Datasets
  • Quickstart
  • Dataset Visualization
  • Storage & Credentials
    • Storage Options
    • Managed Credentials
      • Enabling CORS
      • Provisioning Role-Based Access
  • API Reference
  • Enterprise Features
    • Querying Datasets
      • Sampling Datasets
    • Performant Dataloader
  • EXAMPLE CODE
  • Getting Started
    • Step 1: Hello World
    • Step 2: Creating Deep Lake Datasets
    • Step 3: Understanding Compression
    • Step 4: Accessing Data
    • Step 5: Visualizing Datasets
    • Step 6: Using Activeloop Storage
    • Step 7: Connecting Deep Lake Datasets to ML Frameworks
    • Step 8: Parallel Computing
    • Step 9: Dataset Version Control
    • Step 10: Dataset Filtering
  • Tutorials (w Colab)
    • Creating Datasets
      • Creating Complex Datasets
      • Creating Object Detection Datasets
      • Creating Time-Series Datasets
      • Creating Datasets with Sequences
      • Creating Video Datasets
    • Training Models
      • Training an Image Classification Model in PyTorch
      • Training Models Using MMDetection
      • Training Models Using PyTorch Lightning
      • Training on AWS SageMaker
      • Training an Object Detection and Segmentation Model in PyTorch
    • Data Processing Using Parallel Computing
  • Playbooks
    • Querying, Training and Editing Datasets with Data Lineage
    • Evaluating Model Performance
    • Training Reproducibility Using Deep Lake and Weights & Biases
    • Working with Videos
  • API Summary
  • How Deep Lake Works
    • Data Layout
    • Version Control and Querying
    • Tensor Relationships
    • Visualizer Integration
    • Shuffling in ds.pytorch()
    • Storage Synchronization
    • How to Contribute
Powered by GitBook
On this page
  • How to Filter and Query Data in Deep Lake
  • Filtering using our SQL query language
  • Filtering with user-defined-functions (UDF)
  • Dataset Views

Was this helpful?

  1. Getting Started

Step 10: Dataset Filtering

Filtering datasets using user-defined-functions or SQL-style queries.

PreviousStep 9: Dataset Version ControlNextTutorials (w Colab)

Last updated 2 years ago

Was this helpful?

How to Filter and Query Data in Deep Lake

Filtering and querying is an important aspect of data engineering because analyzing and utilizing data in smaller units is much more productive than executing workflows on all data all the time.

Queries can be performed in Deep Lake enables with user-defined functions, or they can be executed in using our highly-performance SQL-style query language.

Filtering using our SQL query language

offers a highly-performant SQL-style query language that is built in C++ and is optimized for Deep Lake datasets. Queries and their results are executed and saved in the UI, and they can be accessed in Deep Lake using using the the Dataset Views API described below.

Full details about the query language are described in a .

Filtering with user-defined-functions (UDF)

The first step for querying using UDFs is to define a function that returns a boolean depending on whether an dataset sample meets the user-defined condition. In this example, we define a function that returns True if the labels in a tensor are in the desired labels_list. If there are inputs to the filtering function other than sample_in, it must be decorated with @deeplake.compute.

import deeplake
from PIL import Image

# Let's create a local copy of the dataset (Explanation is in the next section)
ds = deeplake.deepcopy('hub://activeloop/mnist-train', './mnist-train-local') 
labels_list = ['0', '8'] # Desired labels for filtering

@deeplake.compute
def filter_labels(sample_in, labels_list):
    
    return sample_in.labels.data()['text'][0] in labels_list
ds_view = ds.filter(filter_labels(labels_list), scheduler = 'threaded', num_workers = 0)
print(len(ds_view))

In most cases, multi-processing is not necessary for queries that involve simple data such as labels or bounding boxes. However, multi-processing significantly accelerates queries that must load rich data types such as images and videos.

Dataset Views

A Dataset View is any subset of a Deep Lake dataset that does not contains all of the samples. It can be an output of a query, filtering function, or regular indexing like ds[0:2:100].

The data in the returned ds_view can be accessed just like a regular dataset.

Image.fromarray(ds_view.images[10].numpy())

A Dataset View can be saved permanently using the method below, which stores its indices without copying the data:

ds_view.save_view(message = 'Samples with 0 and 8')

In order to maintain data lineage, Dataset Views are immutable and are connected to specific commits. Therefore, views can only be saved if the dataset has a commit and there are no uncommitted changes in the HEAD.

Each Dataset View has a unique id, and views can be examined or loaded using:

views = ds.get_views()

print(views)
ds_view = views[0].load()

# OR

# ds_view = ds.load_view(id)
print(len(ds_view))

Congrats! You just learned to filter and query data with Deep Lake! 🎈

The filtering function is executed using the ds.filter() command below, and it returns a Dataset View that only contains the indices that met the filtering condition (more details below). Just like in the , the sample_in parameter does not need to be passed into the filter function when evaluating it, and multi-processing can be specified using the scheduler and num_workers parameters.

In the filtering example above, we copied mnist-train locally in order to gain write access to the dataset. With write access, the views are saved as part of the dataset. Without write access, views are stored elsewhere or in custom paths, and full details are . Users have write access to their own datasets, regardless of whether the datasets are local or in the cloud.

Activeloop Platform
Activeloop Platform
standalone tutorial
Parallel Computing API
available here