Dogs vs Cats#

This topic describes how to manage the Dogs vs Cats Dataset, which is a dataset with Classification label.

Authorize a Client Instance#

An accesskey is needed to authenticate identity when using TensorBay.

from tensorbay import GAS

# Please visit `` to get the AccessKey.

Create Dataset#


Organize Dataset#

Normally, and catalog.json are required to organize the “Dogs vs Cats” dataset into the Dataset instance. In this example, they are stored in the same directory like:

Dogs vs Cats/

Step 1: Write the Catalog#

A catalog contains all label information of one dataset, which is typically stored in a json file like catalog.json.

3        "categories": [{ "name": "cat" }, { "name": "dog" }]
4    }

The only annotation type for “Dogs vs Cats” is Classification, and there are 2 category types.


By passing the path of the catalog.json, load_catalog() supports loading the catalog into dataset.


See catalog table for more catalogs with different label types.

Step 2: Write the Dataloader#

A dataloader is needed to organize the dataset into a Dataset instance.

 1#!/usr/bin/env python3
 3# Copyright 2021 Graviti. Licensed under MIT License.
 5# pylint: disable=invalid-name
 7"""Dataloader of DogsVsCats dataset."""
 9import os
11from tensorbay.dataset import Data, Dataset
12from tensorbay.label import Classification
13from tensorbay.opendataset._utility import glob
15DATASET_NAME = "DogsVsCats"
16_SEGMENTS = {"train": True, "test": False}
19def DogsVsCats(path: str) -> Dataset:
20    """`Dogs vs Cats <>`_ dataset.
22    The file structure should be like::
24        <path>
25            train/
26                cat.0.jpg
27                ...
28                dog.0.jpg
29                ...
30            test/
31                1000.jpg
32                1001.jpg
33                ...
35    Arguments:
36        path: The root directory of the dataset.
38    Returns:
39        Loaded :class:`~tensorbay.dataset.dataset.Dataset` instance.
41    """
42    root_path = os.path.abspath(os.path.expanduser(path))
43    dataset = Dataset(DATASET_NAME)
44    dataset.load_catalog(os.path.join(os.path.dirname(__file__), "catalog.json"))
46    for segment_name, is_labeled in _SEGMENTS.items():
47        segment = dataset.create_segment(segment_name)
48        image_paths = glob(os.path.join(root_path, segment_name, "*.jpg"))
49        for image_path in image_paths:
50            data = Data(image_path)
51            if is_labeled:
52                data.label.classification = Classification(os.path.basename(image_path)[:3])
53            segment.append(data)
55    return dataset

See Classification annotation for more details.

There are already a number of dataloaders in TensorBay SDK provided by the community. Thus, in addition to writing, importing an available dataloadert is also feasible.

from tensorbay.opendataset import DogsVsCats

dataset = DogsVsCats("<path/to/dataset>")


Note that catalogs are automatically loaded in available dataloaders, users do not have to write them again.


See dataloader table for more examples of dataloaders with different label types.

Visualize Dataset#

Optionally, the organized dataset can be visualized by Pharos, which is a TensorBay SDK plug-in. This step can help users to check whether the dataset is correctly organized. Please see Visualization for more details.

Upload Dataset#

The organized “Dogs vs Cats” dataset can be uploaded to TensorBay for sharing, reuse, etc.

dataset_client = gas.upload_dataset(dataset, jobs=8)
dataset_client.commit("initial commit")

Similar with Git, the commit step after uploading can record changes to the dataset as a version. If needed, do the modifications and commit again. Please see Version Control for more details.

Read Dataset#

Now “Dogs vs Cats” dataset can be read from TensorBay.

dataset = Dataset("DogsVsCats", gas)

In dataset “Dogs vs Cats”, there are two segments: train and test. Get the segment names by listing them all.


Get a segment by passing the required segment name.

segment = dataset["train"]

In the train segment, there is a sequence of data, which can be obtained by index.

data = segment[0]

In each data, there is a sequence of Classification annotations, which can be obtained by index.

category = data.label.classification.category

There is only one label type in “Dogs vs Cats” dataset, which is classification. The information stored in category is one of the names in “categories” list of catalog.json. See Classification label format for more details.

Delete Dataset#