惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
B
Blog
U
Unit 42
B
Blog RSS Feed
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
腾讯CDC
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Visual Studio Blog
博客园 - 聂微东
MyScale Blog
MyScale Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
Engineering at Meta
Engineering at Meta

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Writing Your Own Custom Dataset for Classification in PyT...
2017-10-23 · via jdhao's digital space

In this post, I’d like to talk about how to create your own dataset, process it and make data batches ready to be fed into your neural networks, with the help of PyTorch.

In PyTorch, in order to feed your own training data into the network, you will mainly deal with two classes: the Dataset class and the Dataloader class. Now I will explain in more detail what they do.

Create your Dataset class#

Overview#

Dataset class is used to provide an interface for accessing all the training or testing samples in your dataset. In order to achieve this, you have to implement at least two methods, __getitem__ and __len__ so that each training sample (in image classification, a sample means an image plus its class label) can be accessed by its index.

In the initialization part of the class, you should collect a list of all the images and its labels in the dataset. When we want to get a particular sample, we then read the image, transform it and return the transformed image and the corresponding label.

A good example is ImageFolder class provided by torchvision package, you can check its source code here to get a sense of how it actually works.

Data augmentation and preprocessing#

Data augmentation and preprocessing is an important part of the whole work-flow. In PyTorch, we do it by providing a transform parameter to the Dataset class. Transform are class object which are called to process the given input. You can cascade a series of transforms by providing a list of transforms to torchvision.transforms.Compose method. Then the given transforms will be performed on the input in the order they appear.

It should be noted that some of the transforms are for PIL image object, such as RandomCrop() and Resize(). Other transforms are for torch Tensor, such as Normalize. If your dataset contains images, you should first perform all transforms expecting PIL image object, then convert PIL image to Tensor using ToTensor() method. The ToTensor transform will convert PIL image to torch Tensor of shape $H\times W\times C$, with its values in the range [0.0, 1.0].

The Normalize transform expects torch tensors. Its parameters are the means and standard deviations of RGB channels of all the training images. For ImageNet, the devs have already done that for us, the normalize transform should be

normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])

For your own dataset, you have to calculate the statistics yourself.

Create data batch using Dataloader#

Although we can access all the training data using the Dataset class, but that is not enough. For deep learning, we need the functionality such as batching, shuffling, multiprocess data loading, etc. This is what the Dataloader class do.

The Dataloader class accept a dataset and other parameters such as batch_size, batch_sampler and number of workers to load the data and so on… Then we can iterate over the Dataloader to get batches of training data and train our models.

Loading variable size input images#

By default, Dataloader use collate_fn method to pack a series of images and target as tensors (first dimension of tensor is batch size). The default collate_fn expects all the images in a batch to have the same size because it uses torch.stack() to pack the images. If the images provided by Dataset have variable size, you have to provide your custom collate_fn. A simple example is shown below:

# a simple custom collate function, just to show the idea

# `batch` is a list of tuple where first element is image tensor and

# second element is corresponding label

def my_collate(batch):
    data = [item[0] for item in batch]  # just form a list of tensor

    target = [item[1] for item in batch]
    target = torch.LongTensor(target)
    return [data, target]

Conclusion#

In this post, I give an introduction to the use of Dataset and Dataloader in PyTorch. Dataset is used to access single sample from your dataset and transform it, while Dataloader is used to load a batch of samples for training or testing your models. If your training images have variable size, you may also have to use your own custom collate_fn.

References#