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:
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
from hub import Dataset, loadhub_dataset_path = './animals_hub'ds = Dataset(hub_dataset_path) # Creates the dataset# Create the tensors with names of your choice.ds.create_tensor('images', htype = 'image', sample_compression = 'jpeg')ds.create_tensor('labels', htype = 'class_label')
Next populate data in the tensors using the following code:
from PIL import Imageimport numpy as npimport osimport globdataset_folders = glob.glob('./animals/*') #Paths to source data# Iterate through the subfolders (/dogs, /cats)for label, folder_path in enumerate(dataset_folders):paths = glob.glob(os.path.join(folder_path, '*')) # Get subfolders# Iterate through images in the subfoldersfor path in paths:ds.images.append(load(path)) # Append to images tensor using hub.loadds.labels.append(np.uint32(label)) # Append to labels tensor
ds.images.append(load(path)) is functionally equivalent to
ds.image.append(PIL.Image.fromarray(path)). However, the
hub.load() method is significantly faster because it does not decompress and recompress the image if the compression matches the
sample_compression for that tensor. Further details are available in Understanding Compression.
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.bboxds.localization.label
For more detailed information regarding accessing datasets and their tensors, check out the next section.