cifar.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. CIFAR dataset.
  16. This module will download dataset from https://dataset.bj.bcebos.com/cifar/cifar-10-python.tar.gz and https://dataset.bj.bcebos.com/cifar/cifar-100-python.tar.gz, parse train/test set into
  17. paddle reader creators.
  18. The CIFAR-10 dataset consists of 60000 32x32 color images in 10 classes,
  19. with 6000 images per class. There are 50000 training images and 10000 test
  20. images.
  21. The CIFAR-100 dataset is just like the CIFAR-10, except it has 100 classes
  22. containing 600 images each. There are 500 training images and 100 testing
  23. images per class.
  24. """
  25. import pickle
  26. import tarfile
  27. import numpy
  28. import paddle.dataset.common
  29. from paddle.utils import deprecated
  30. __all__ = []
  31. URL_PREFIX = 'https://dataset.bj.bcebos.com/cifar/'
  32. CIFAR10_URL = URL_PREFIX + 'cifar-10-python.tar.gz'
  33. CIFAR10_MD5 = 'c58f30108f718f92721af3b95e74349a'
  34. CIFAR100_URL = URL_PREFIX + 'cifar-100-python.tar.gz'
  35. CIFAR100_MD5 = 'eb9058c3a382ffc7106e4002c42a8d85'
  36. def reader_creator(filename, sub_name, cycle=False):
  37. def read_batch(batch):
  38. data = batch[b'data']
  39. labels = batch.get(b'labels', batch.get(b'fine_labels', None))
  40. assert labels is not None
  41. for sample, label in zip(data, labels):
  42. yield (sample / 255.0).astype(numpy.float32), int(label)
  43. def reader():
  44. while True:
  45. with tarfile.open(filename, mode='r') as f:
  46. names = (
  47. each_item.name
  48. for each_item in f
  49. if sub_name in each_item.name
  50. )
  51. for name in names:
  52. batch = pickle.load(f.extractfile(name), encoding='bytes')
  53. yield from read_batch(batch)
  54. if not cycle:
  55. break
  56. return reader
  57. @deprecated(
  58. since="2.0.0",
  59. update_to="paddle.vision.datasets.Cifar100",
  60. level=1,
  61. reason="Please use new dataset API which supports paddle.io.DataLoader",
  62. )
  63. def train100():
  64. """
  65. CIFAR-100 training set creator.
  66. It returns a reader creator, each sample in the reader is image pixels in
  67. [0, 1] and label in [0, 99].
  68. :return: Training reader creator
  69. :rtype: callable
  70. """
  71. return reader_creator(
  72. paddle.dataset.common.download(CIFAR100_URL, 'cifar', CIFAR100_MD5),
  73. 'train',
  74. )
  75. @deprecated(
  76. since="2.0.0",
  77. update_to="paddle.vision.datasets.Cifar100",
  78. level=1,
  79. reason="Please use new dataset API which supports paddle.io.DataLoader",
  80. )
  81. def test100():
  82. """
  83. CIFAR-100 test set creator.
  84. It returns a reader creator, each sample in the reader is image pixels in
  85. [0, 1] and label in [0, 99].
  86. :return: Test reader creator.
  87. :rtype: callable
  88. """
  89. return reader_creator(
  90. paddle.dataset.common.download(CIFAR100_URL, 'cifar', CIFAR100_MD5),
  91. 'test',
  92. )
  93. @deprecated(
  94. since="2.0.0",
  95. update_to="paddle.vision.datasets.Cifar10",
  96. level=1,
  97. reason="Please use new dataset API which supports paddle.io.DataLoader",
  98. )
  99. def train10(cycle=False):
  100. """
  101. CIFAR-10 training set creator.
  102. It returns a reader creator, each sample in the reader is image pixels in
  103. [0, 1] and label in [0, 9].
  104. :param cycle: whether to cycle through the dataset
  105. :type cycle: bool
  106. :return: Training reader creator
  107. :rtype: callable
  108. """
  109. return reader_creator(
  110. paddle.dataset.common.download(CIFAR10_URL, 'cifar', CIFAR10_MD5),
  111. 'data_batch',
  112. cycle=cycle,
  113. )
  114. @deprecated(
  115. since="2.0.0",
  116. update_to="paddle.vision.datasets.Cifar10",
  117. level=1,
  118. reason="Please use new dataset API which supports paddle.io.DataLoader",
  119. )
  120. def test10(cycle=False):
  121. """
  122. CIFAR-10 test set creator.
  123. It returns a reader creator, each sample in the reader is image pixels in
  124. [0, 1] and label in [0, 9].
  125. :param cycle: whether to cycle through the dataset
  126. :type cycle: bool
  127. :return: Test reader creator.
  128. :rtype: callable
  129. """
  130. return reader_creator(
  131. paddle.dataset.common.download(CIFAR10_URL, 'cifar', CIFAR10_MD5),
  132. 'test_batch',
  133. cycle=cycle,
  134. )
  135. @deprecated(
  136. since="2.0.0",
  137. update_to="paddle.vision.datasets.Cifar10",
  138. level=1,
  139. reason="Please use new dataset API which supports paddle.io.DataLoader",
  140. )
  141. def fetch():
  142. paddle.dataset.common.download(CIFAR10_URL, 'cifar', CIFAR10_MD5)
  143. paddle.dataset.common.download(CIFAR100_URL, 'cifar', CIFAR100_MD5)