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

24 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"""CIFAR100 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.cifar100.load_data") 

30def load_data(label_mode="fine"): 

31 """Loads the CIFAR100 dataset. 

32 

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

34 10,000 test images, labeled over 100 fine-grained classes that are 

35 grouped into 20 coarse-grained classes. See more info at the 

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

37 

38 Args: 

39 label_mode: one of "fine", "coarse". If it is "fine" the category labels 

40 are the fine-grained labels, if it is "coarse" the output labels are the 

41 coarse-grained superclasses. 

42 

43 Returns: 

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

45 

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

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

48 from 0 to 255. 

49 

50 **y_train**: uint8 NumPy array of labels (integers in range 0-99) 

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

52 

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

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

55 from 0 to 255. 

56 

57 **y_test**: uint8 NumPy array of labels (integers in range 0-99) 

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

59 

60 Example: 

61 

62 ```python 

63 (x_train, y_train), (x_test, y_test) = keras.datasets.cifar100.load_data() 

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

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

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

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

68 ``` 

69 """ 

70 if label_mode not in ["fine", "coarse"]: 

71 raise ValueError( 

72 '`label_mode` must be one of `"fine"`, `"coarse"`. ' 

73 f"Received: label_mode={label_mode}." 

74 ) 

75 

76 dirname = "cifar-100-python" 

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

78 path = get_file( 

79 dirname, 

80 origin=origin, 

81 untar=True, 

82 file_hash=( # noqa: E501 

83 "85cd44d02ba6437773c5bbd22e183051d648de2e7d6b014e1ef29b855ba677a7" 

84 ), 

85 ) 

86 

87 fpath = os.path.join(path, "train") 

88 x_train, y_train = load_batch(fpath, label_key=label_mode + "_labels") 

89 

90 fpath = os.path.join(path, "test") 

91 x_test, y_test = load_batch(fpath, label_key=label_mode + "_labels") 

92 

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

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

95 

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

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

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

99 

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

101