Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/datasets/cifar10.py: 32%

28 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

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 keras.src import backend 

22from keras.src.datasets.cifar import load_batch 

23from keras.src.utils.data_utils import get_file 

24 

25# isort: off 

26from tensorflow.python.util.tf_export import keras_export 

27 

28 

29@keras_export("keras.datasets.cifar10.load_data") 

30def load_data(): 

31 """Loads the CIFAR10 dataset. 

32 

33 This is a dataset of 50,000 32x32 color training images and 10,000 test 

34 images, labeled over 10 categories. See more info at the 

35 [CIFAR homepage](https://www.cs.toronto.edu/~kriz/cifar.html). 

36 

37 The classes are: 

38 

39 | Label | Description | 

40 |:-----:|-------------| 

41 | 0 | airplane | 

42 | 1 | automobile | 

43 | 2 | bird | 

44 | 3 | cat | 

45 | 4 | deer | 

46 | 5 | dog | 

47 | 6 | frog | 

48 | 7 | horse | 

49 | 8 | ship | 

50 | 9 | truck | 

51 

52 Returns: 

53 Tuple of NumPy arrays: `(x_train, y_train), (x_test, y_test)`. 

54 

55 **x_train**: uint8 NumPy array of grayscale image data with shapes 

56 `(50000, 32, 32, 3)`, containing the training data. Pixel values range 

57 from 0 to 255. 

58 

59 **y_train**: uint8 NumPy array of labels (integers in range 0-9) 

60 with shape `(50000, 1)` for the training data. 

61 

62 **x_test**: uint8 NumPy array of grayscale image data with shapes 

63 `(10000, 32, 32, 3)`, containing the test data. Pixel values range 

64 from 0 to 255. 

65 

66 **y_test**: uint8 NumPy array of labels (integers in range 0-9) 

67 with shape `(10000, 1)` for the test data. 

68 

69 Example: 

70 

71 ```python 

72 (x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data() 

73 assert x_train.shape == (50000, 32, 32, 3) 

74 assert x_test.shape == (10000, 32, 32, 3) 

75 assert y_train.shape == (50000, 1) 

76 assert y_test.shape == (10000, 1) 

77 ``` 

78 """ 

79 dirname = "cifar-10-batches-py" 

80 origin = "https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz" 

81 path = get_file( 

82 dirname, 

83 origin=origin, 

84 untar=True, 

85 file_hash=( # noqa: E501 

86 "6d958be074577803d12ecdefd02955f39262c83c16fe9348329d7fe0b5c001ce" 

87 ), 

88 ) 

89 

90 num_train_samples = 50000 

91 

92 x_train = np.empty((num_train_samples, 3, 32, 32), dtype="uint8") 

93 y_train = np.empty((num_train_samples,), dtype="uint8") 

94 

95 for i in range(1, 6): 

96 fpath = os.path.join(path, "data_batch_" + str(i)) 

97 ( 

98 x_train[(i - 1) * 10000 : i * 10000, :, :, :], 

99 y_train[(i - 1) * 10000 : i * 10000], 

100 ) = load_batch(fpath) 

101 

102 fpath = os.path.join(path, "test_batch") 

103 x_test, y_test = load_batch(fpath) 

104 

105 y_train = np.reshape(y_train, (len(y_train), 1)) 

106 y_test = np.reshape(y_test, (len(y_test), 1)) 

107 

108 if backend.image_data_format() == "channels_last": 

109 x_train = x_train.transpose(0, 2, 3, 1) 

110 x_test = x_test.transpose(0, 2, 3, 1) 

111 

112 x_test = x_test.astype(x_train.dtype) 

113 y_test = y_test.astype(y_train.dtype) 

114 

115 return (x_train, y_train), (x_test, y_test) 

116