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"""CIFAR10 small images classification dataset.""" 16 17import os 18 19import numpy as np 20 21from tensorflow.python.keras import backend 22from tensorflow.python.keras.datasets.cifar import load_batch 23from tensorflow.python.keras.utils.data_utils import get_file 24from tensorflow.python.util.tf_export import keras_export 25 26 27@keras_export('keras.datasets.cifar10.load_data') 28def load_data(): 29 """Loads the CIFAR10 dataset. 30 31 This is a dataset of 50,000 32x32 color training images and 10,000 test 32 images, labeled over 10 categories. See more info at the 33 [CIFAR homepage](https://www.cs.toronto.edu/~kriz/cifar.html). 34 35 The classes are: 36 37 | Label | Description | 38 |:-----:|-------------| 39 | 0 | airplane | 40 | 1 | automobile | 41 | 2 | bird | 42 | 3 | cat | 43 | 4 | deer | 44 | 5 | dog | 45 | 6 | frog | 46 | 7 | horse | 47 | 8 | ship | 48 | 9 | truck | 49 50 Returns: 51 Tuple of NumPy arrays: `(x_train, y_train), (x_test, y_test)`. 52 53 **x_train**: uint8 NumPy array of grayscale image data with shapes 54 `(50000, 32, 32, 3)`, containing the training data. Pixel values range 55 from 0 to 255. 56 57 **y_train**: uint8 NumPy array of labels (integers in range 0-9) 58 with shape `(50000, 1)` for the training data. 59 60 **x_test**: uint8 NumPy array of grayscale image data with shapes 61 (10000, 32, 32, 3), containing the test data. Pixel values range 62 from 0 to 255. 63 64 **y_test**: uint8 NumPy array of labels (integers in range 0-9) 65 with shape `(10000, 1)` for the test data. 66 67 Example: 68 69 ```python 70 (x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data() 71 assert x_train.shape == (50000, 32, 32, 3) 72 assert x_test.shape == (10000, 32, 32, 3) 73 assert y_train.shape == (50000, 1) 74 assert y_test.shape == (10000, 1) 75 ``` 76 """ 77 dirname = 'cifar-10-batches-py' 78 origin = 'https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz' 79 path = get_file( 80 dirname, 81 origin=origin, 82 untar=True, 83 file_hash= 84 '6d958be074577803d12ecdefd02955f39262c83c16fe9348329d7fe0b5c001ce') 85 86 num_train_samples = 50000 87 88 x_train = np.empty((num_train_samples, 3, 32, 32), dtype='uint8') 89 y_train = np.empty((num_train_samples,), dtype='uint8') 90 91 for i in range(1, 6): 92 fpath = os.path.join(path, 'data_batch_' + str(i)) 93 (x_train[(i - 1) * 10000:i * 10000, :, :, :], 94 y_train[(i - 1) * 10000:i * 10000]) = load_batch(fpath) 95 96 fpath = os.path.join(path, 'test_batch') 97 x_test, y_test = load_batch(fpath) 98 99 y_train = np.reshape(y_train, (len(y_train), 1)) 100 y_test = np.reshape(y_test, (len(y_test), 1)) 101 102 if backend.image_data_format() == 'channels_last': 103 x_train = x_train.transpose(0, 2, 3, 1) 104 x_test = x_test.transpose(0, 2, 3, 1) 105 106 x_test = x_test.astype(x_train.dtype) 107 y_test = y_test.astype(y_train.dtype) 108 109 return (x_train, y_train), (x_test, y_test) 110