Running computations and processing data in parallel.
Hub enables you to easily run computations in parallel and significantly accelerate your data processing workflows. This example primarily focuses on parallel dataset uploading, and other use cases such as dataset transformations can be found in this tutorial.
Parallel compute using Hub has two core elements: #1. defining a function or pipeline that will run in parallel and #2. evaluating it using the appropriate inputs and outputs. Let's start with #1 by defining a function that processes files and appends their data to the labels and images tensors.
Defining the parallel computing function
The first step for running parallel computations is to define a function that will run in parallel by decorating it using @hub.compute. In the example below, file_to_hub converts data from files into hub format, just like in Step 2: Creating Hub Datasets Manually. If you have not completed Step 2, please download and unzip the example image classification dataset below:
In all functions decorated using @hub.compute, the first argument must be a single element of any input iterable that is being processed in parallel. In this case, that is a filename file_name, because file_to_hub reads image files and populates data in the dataset's tensors.
The second argument is a dataset sample sample_out, which can be operated on using similar syntax to dataset objects, such as sample_out.append(...), sample_out.extend(...), etc.
The function decorated using @hub.compute must return sample_out, which represents the data that is added or modified by that function.
Executing the parallel computation
To execute the parallel computation, you must define the dataset that will be modified.
ds = hub.empty('./animals_hub_transform')# Creates the dataset
Next, you define the input iterable that describes the information that will be operated on in parallel. In this case, that is a list of files files_list:
# Find the class_names and list of files that need to be uploaded
class_names = os.listdir(dataset_folder)
for dirpath, dirnames, filenames in os.walk(dataset_folder):
You can now create the tensors for the dataset and run the parallel computation using the .eval syntax. Pass the optional input arguments to file_to_hub and skip the first two default arguments file_name and sample_out.
The input iterable files_list and output dataset ds is passed to the .eval method as the first and second argument respectively.