Step 2: Creating Hub Datasets Manually

Creating and storing Hub Datasets manually.

Creating Hub datasets is simple, you have full control over connecting your source data (files, images, etc.) to specific tensors in the Hub Dataset.

Let's follow along with the example below to create our first dataset. First, download and unzip the small classification dataset below called animals dataset.

The dataset has the following folder structure:

_animals
|_cats
|_image_1.jpg
|_image_2.jpg
|_dogs
|_image_3.jpg
|_image_4.jpg

Now that you have the data, you can create a Hub Dataset and initialize its tensors. Running the following code will create Hub dataset inside of the ./animals_hubfolder.

import hub
from PIL import Image
import numpy as np
import os
โ€‹
ds = hub.empty('./animals_hub') # Creates the dataset

Next, let's inspect the folder structure for the source dataset './animals' to find the class names and the files that need to be uploaded to the Hub dataset.

# Find the class_names and list of files that need to be uploaded
dataset_folder = './animals'
โ€‹
class_names = os.listdir(dataset_folder)
โ€‹
files_list = []
for dirpath, dirnames, filenames in os.walk(dataset_folder):
for filename in filenames:
files_list.append(os.path.join(dirpath, filename))

Next, let's create the dataset tensors and upload metadata. Check out our page on Storage Synchronization for details about the with syntax below.

with ds:
# Create the tensors with names of your choice.
ds.create_tensor('images', htype = 'image', sample_compression = 'jpeg')
ds.create_tensor('labels', htype = 'class_label', class_names = class_names)
# Add arbitrary metadata - Optional
ds.info.update(description = 'My first Hub dataset')
ds.images.info.update(camera_type = 'SLR')

Specifying htype and dtype is not required, but it is highly recommended in order to optimize performance, especially for large datasets. Usedtypeto specify the numeric type of tensor data, and usehtypeto specify the underlying data structure. More information on htype can be found here.

Finally, let's populate the data in the tensors.

with ds:
# Iterate through the files and append to hub dataset
for file in files_list:
label_text = os.path.basename(os.path.dirname(file))
label_num = class_names.index(label_text)
ds.images.append(hub.read(file)) # Append to images tensor using hub.read
ds.labels.append(np.uint32(label_num)) # Append to labels tensor

ds.images.append(hub.read(path)) is functionally equivalent to ds.image.append(PIL.Image.fromarray(path)). However, the hub.read() method is significantly faster because it does not decompress and recompress the image if the compression matches thesample_compression for that tensor. Further details are available in Understanding Compression.

Check out the first image from this dataset. More details about Accessing Data are available in Step 5.

Image.fromarray(ds.images[0].numpy())

Congrats! You just created your first dataset! ๐ŸŽ‰

Creating Tensor Hierarchies - Coming Soon

Often it's important to create tensors hierarchically, because information between tensors may be inherently coupledโ€”such as bounding boxes and their corresponding labels. Hierarchy can be created using the following lines of code:

ds.create_tensor('localization/bbox')
ds.create_tensor('localization/label')
โ€‹
# Tensors are accessed via:
ds.localization.bbox
ds.localization.label

For more detailed information regarding accessing datasets and their tensors, check out the next section.