1# Copyright 2015 The TensorFlow 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"""CIFAR100 small images classification dataset.
16"""
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21import os
22
23import numpy as np
24
25from tensorflow.python.keras import backend as K
26from tensorflow.python.keras.datasets.cifar import load_batch
27from tensorflow.python.keras.utils.data_utils import get_file
28from tensorflow.python.util.tf_export import keras_export
29
30
31@keras_export('keras.datasets.cifar100.load_data')
32def load_data(label_mode='fine'):
33  """Loads [CIFAR100 dataset](https://www.cs.toronto.edu/~kriz/cifar.html).
34
35  This is a dataset of 50,000 32x32 color training images and
36  10,000 test images, labeled over 100 fine-grained classes that are
37  grouped into 20 coarse-grained classes. See more info at the
38  [CIFAR homepage](https://www.cs.toronto.edu/~kriz/cifar.html).
39
40  Args:
41      label_mode: one of "fine", "coarse". If it is "fine" the category labels
42      are the fine-grained labels, if it is "coarse" the output labels are the
43      coarse-grained superclasses.
44
45  Returns:
46      Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.
47
48      **x_train, x_test**: uint8 arrays of RGB image data with shape
49        `(num_samples, 3, 32, 32)` if `tf.keras.backend.image_data_format()` is
50        `'channels_first'`, or `(num_samples, 32, 32, 3)` if the data format
51        is `'channels_last'`.
52
53      **y_train, y_test**: uint8 arrays of category labels with shape
54        (num_samples, 1).
55
56  Raises:
57      ValueError: in case of invalid `label_mode`.
58  """
59  if label_mode not in ['fine', 'coarse']:
60    raise ValueError('`label_mode` must be one of `"fine"`, `"coarse"`.')
61
62  dirname = 'cifar-100-python'
63  origin = 'https://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz'
64  path = get_file(
65      dirname,
66      origin=origin,
67      untar=True,
68      file_hash=
69      '85cd44d02ba6437773c5bbd22e183051d648de2e7d6b014e1ef29b855ba677a7')
70
71  fpath = os.path.join(path, 'train')
72  x_train, y_train = load_batch(fpath, label_key=label_mode + '_labels')
73
74  fpath = os.path.join(path, 'test')
75  x_test, y_test = load_batch(fpath, label_key=label_mode + '_labels')
76
77  y_train = np.reshape(y_train, (len(y_train), 1))
78  y_test = np.reshape(y_test, (len(y_test), 1))
79
80  if K.image_data_format() == 'channels_last':
81    x_train = x_train.transpose(0, 2, 3, 1)
82    x_test = x_test.transpose(0, 2, 3, 1)
83
84  return (x_train, y_train), (x_test, y_test)
85