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

ACCESS_KEY = "Accesskey-*****"
gas = GAS(ACCESS_KEY)

Create Dataset

gas.create_dataset("DogsVsCats")

Organize Dataset

Normally, dataloader.py 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/
    catalog.json
    dataloader.py

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.

1{
2    "CLASSIFICATION": {
3        "categories": [{ "name": "cat" }, { "name": "dog" }]
4    }
5}

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

Note

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

Important

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
 2#
 3# Copyright 2021 Graviti. Licensed under MIT License.
 4#
 5# pylint: disable=invalid-name
 6
 7"""Dataloader of DogsVsCats dataset."""
 8
 9import os
10
11from tensorbay.dataset import Data, Dataset
12from tensorbay.label import Classification
13from tensorbay.opendataset._utility import glob
14
15DATASET_NAME = "DogsVsCats"
16_SEGMENTS = {"train": True, "test": False}
17
18
19def DogsVsCats(path: str) -> Dataset:
20    """`Dogs vs Cats <https://www.kaggle.com/c/dogs-vs-cats>`_ dataset.
21
22    The file structure should be like::
23
24        <path>
25            train/
26                cat.0.jpg
27                ...
28                dog.0.jpg
29                ...
30            test/
31                1000.jpg
32                1001.jpg
33                ...
34
35    Arguments:
36        path: The root directory of the dataset.
37
38    Returns:
39        Loaded :class:`~tensorbay.dataset.dataset.Dataset` instance.
40
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"))
45
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)
54
55    return dataset

See Classification annotation for more details.

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

from tensorbay.opendataset import DogsVsCats

dataset = DogsVsCats("path/to/dataset/directory")

Note

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

Important

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.

dataset.keys()

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

gas.delete_dataset("DogsVsCats")