Cifar10

class paddle.vision.datasets. Cifar10 ( data_file=None, mode='train', transform=None, download=True, backend=None ) [source]

Implementation of Cifar-10 dataset, which has 10 categories.

Parameters
  • data_file (str) – path to data file, can be set None if download is True. Default None, default data path: ~/.cache/paddle/dataset/cifar

  • mode (str) – ‘train’, ‘test’ mode. Default ‘train’.

  • transform (callable) – transform to perform on image, None for no transform.

  • download (bool) – download dataset automatically if data_file is None. Default True

  • backend (str, optional) – Specifies which type of image to be returned: PIL.Image or numpy.ndarray. Should be one of {‘pil’, ‘cv2’}. If this option is not set, will get backend from paddle.vsion.get_image_backend , default backend is ‘pil’. Default: None.

Returns

instance of cifar-10 dataset

Return type

Dataset

Examples

import paddle
import paddle.nn as nn
from paddle.vision.datasets import Cifar10
from paddle.vision.transforms import Normalize

class SimpleNet(paddle.nn.Layer):
    def __init__(self):
        super(SimpleNet, self).__init__()
        self.fc = nn.Sequential(
            nn.Linear(3072, 10),
            nn.Softmax())

    def forward(self, image, label):
        image = paddle.reshape(image, (1, -1))
        return self.fc(image), label

paddle.disable_static()

normalize = Normalize(mean=[0.5, 0.5, 0.5],
                      std=[0.5, 0.5, 0.5],
                      data_format='HWC')
cifar10 = Cifar10(mode='train', transform=normalize)

for i in range(10):
    image, label = cifar10[i]
    image = paddle.to_tensor(image)
    label = paddle.to_tensor(label)

    model = SimpleNet()
    image, label = model(image, label)
    print(image.numpy().shape, label.numpy().shape)