In: Computer Science
I have sequences of 128X128 images of faces saved in a folder. I am trying to create a pytorch dataloader that will load the image sequences along with its timestamps and average heart rate value.
The Dataloader to loads the data(e.g., 128 video frames X, 128 points-timesteps Y, average heart rate value H) as tensors. Then forward it to forward the model.
I am new to pytorch and I am not sure how to create data loaders with pytorch and load sequencial image data from a folder and timestamps from a .txt file as a tensor.
The center important part of PyTorch data loading utility is the torch.utils.data.DataLoader class. It means a Python iterable over a dataset. Libraries in PyTorch offer built-in high-quality datasets for you to use in torch.utils.data.Dataset. These datasets are currently available in:
with more to come. Using the Yesno
dataset from torchaudio.datasets.YESNO
, we will
demonstrate how to effectively and efficiently load data from a
PyTorch Dataset
into a PyTorch
DataLoader
.
Setup:
Before we start, we need to install
torchaudio
to have access to the dataset.
pip install torchaudio
Steps:
1. Import required libraries for loading our data:
For this recipe, we will use
torch
and torchaudio
. Depending on what
built-in datasets you make use of, you can also install and import
torchvision
or torchtext
.
import torch import torchaudio
2. Access the data in the dataset:
The Yesno dataset in
torchaudio
reports sixty recordings of one individual
saying yes or no in Hebrew; with each recording exists eight words
long.
torchaudio.datasets.YESNO
creates a dataset for
YesNo.
torchaudio.datasets.YESNO( root, url='http://www.openslr.org/resources/1/waves_yesno.tar.gz', folder_in_archive='waves_yesno', download=False, transform=None, target_transform=None)
Each item or element in the dataset is a tuple of the form: (waveform, sample_rate, labels).
You must set a root
for
the Yesno dataset, which is where the training and testing dataset
will be. The other parameters are non mandatory, with their default
values shown. Here is some additional useful information on the
other parameters:
# Select data point number 3 to see an example of the the yesno_data: n = 3 waveform, sample_rate, labels = yesno_data[n] print("Waveform: {}\nSample rate: {}\nLabels: {}".format(waveform, sample_rate, labels))
When using this data in practice, it is best practice to provision the data into a “training” dataset and a “testing” dataset. This make sure that you have out-of-sample data to test the performance of your model.
3. Loading the data:
Now that we have entry to the
dataset, we must pass it through
torch.utils.data.DataLoader
. The
DataLoader
merges the dataset and a sampler, returning
an iterable over the dataset.
data_loader = torch.utils.data.DataLoader(yesno_data, batch_size=1, shuffle=True)
4. Iterate over the data:
Our data is now iterable using the
data_loader
. This will be necessary when we start
training our model! You will observe that now each data entry in
the data_loader
object is transformed to a tensor
containing tensors representing our waveform, sample rate, and
labels.
for data in data_loader: print("Data: ", data) print("Waveform: {}\nSample rate: {}\nLabels: {}".format(data[0], data[1], data[2])) break
5. [Optional] Visualize the data:
You can optionally visualize your
data to further understand the output from your
DataLoader
.
import matplotlib.pyplot as plt print(data[0][0].numpy()) plt.figure() plt.plot(waveform.t().numpy())