Creating Datasets with Sequences
Hub sequences are a powerful tool for storing temporal annotations such as bounding boxes in each frame of a video.

How to create a dataset with sequences of images and labels

This tutorial is also available as a Colab Notebook

Deep learning with computer vision is increasingly moving in a direction of temporal data, where video frames and their labels are stored as sequences, rather than independent images. Models trained on this data directly account for the temporal information content, rather than making predictions frame-by-frame and then fusing them with non-deep-learning techniques.

Create the Hub Dataset

The first step is to download the dataset Multiple Object Tracking Benchmark. Additional information about this data and its format is in this GitHub Repo.
The dataset has the following folder structure:
1
data_dir
2
|_train
3
|_MOT16_N (Folder with sequence N)
4
|_det
5
|_gt (Folder with ground truth annotations)
6
|_img1 (Folder with images the sequence)
7
|_00000n.jpg (image of n-th frame in sequence)
8
|_MOT16_M
9
....
10
|_test (same structure as _train)
Copied!
The annotations in gt.txt have the format below, and the last 4 items (conf->z) are not used in the hub dataset:
1
frame, id, bb_left, bb_top, bb_width, bb_height, conf, x, y, z
Copied!
Now we're ready to create a Hub Dataset in the ./mot_2016_train folder by running:
1
import hub
2
import os
3
import pandas as pd
4
import numpy as np
5
from PIL import Image, ImageDraw
6
7
ds = hub.empty('./mot_2015_train') # Create the dataset locally
Copied!
Next, let's write code to inspect the folder structure for the downloaded dataset and create a list of folders containing the sequences:
1
dataset_folder = '/MOT16/train'
2
3
sequences = [ item for item in os.listdir(dataset_folder) if os.path.isdir(os.path.join(dataset_folder, item)) ]
Copied!
Finally, let's create the tensors by using the sequence[...] htype, iterate through each sequence, and iterate through each frame within the sequence, one-by-one.
Data is appended to sequence[...] htypes using lists. The list contains the whole sample, and the individual elements of the list are the individual data points, such as the image frame, the bounding boxes in a particular frame, etc.
See end of code block below.
1
with ds:
2
# Define tensors
3
ds.create_tensor('frames', htype = 'sequence[image]', sample_compression = 'jpg')
4
ds.create_tensor('boxes', htype = 'sequence[bbox]')
5
ds.create_tensor('ids', htype = 'sequence[]', dtype = 'uint32') # Ids are not uploaded as htype = 'class_labels' because they don't contain information about the class of an object.
6
7
ds.boxes.info.update(coords = {'type': 'pixel', 'mode': 'LTWH'}) # Bounding box format is left, top, width, height
8
9
# Iterate through each sequence
10
for sequence in sequences:
11
12
# Define root directory for that sequence
13
root_local = os.path.join(dataset_folder,sequence, 'img1')
14
15
# Get a list of all the image paths
16
img_paths = [os.path.join(root_local, item) for item in sorted(os.listdir(root_local))]
17
18
# Read the annotations and convert to dataframe
19
with open(os.path.join(dataset_folder,sequence, 'gt', 'gt.txt')) as f:
20
anns = [line.rstrip('\n') for line in f]
21
22
anns_df = pd.read_csv(os.path.join(dataset_folder, sequence, 'gt', 'gt.txt'), header = None)
23
24
# Get the frames from the annotations and make sure they're of equal length as the images
25
frames = pd.unique(anns_df[0])
26
assert len(frames) == len(img_paths)
27
28
# Iterate through each frame and add data to sequence
29
boxes_seq = []
30
ids_seq = []
31
for frame in frames:
32
ann_df = anns_df[anns_df[0] == frame] # Find annotations in the specific frame
33
34
boxes_seq.append(ann_df.loc[:, [2, 3, 4, 5]].to_numpy().astype('float32')) # Box coordinates are in the 3rd-6th column
35
36
ids_seq.append(ann_df.loc[:, 1].to_numpy().astype('uint32')) # ids are in the second column
37
38
# Append the sequences to the hub dataset
39
ds.append({
40
"frames": [hub.read(path) for path in img_paths],
41
"boxes": boxes_seq,
42
"ids": ids_seq})
Copied!
This dataset identifies objects by id, where each id represents an instance of an object. However, the id does not identify the class of the object, such person, car, truck, etc. Therefore, the ids were not uploaded as htype = "class_label".

Inspect the Hub Dataset

Let's check out the 10th frame in the 6th sequence in this dataset. A complete visualization of this dataset is available in Activeloop Platform.
1
# Draw bounding boxes for the 10th frame in the 6th sequence
2
3
seq_ind = 5
4
frame_ind = 9
5
img = Image.fromarray(ds.frames[seq_ind][frame_ind].numpy())
6
draw = ImageDraw.Draw(img)
7
(w,h) = img.size
8
boxes = ds.boxes[seq_ind][frame_ind].numpy()
9
10
for b in range(boxes.shape[0]):
11
(x1,y1) = (int(boxes[b][0]), int(boxes[b][1]))
12
(x2,y2) = (int(boxes[b][0]+boxes[b][2]), int(boxes[b][1]+boxes[b][3]))
13
draw.rectangle([x1,y1,x2,y2], width=2, outline = 'red')
Copied!
1
# Display the frame and its bounding boxes
2
img
Copied!
Congrats! You just created a dataset using sequences! 🎉