Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/datasets/fashion_mnist.py: 35%
23 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# Copyright 2017 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"""Fashion-MNIST dataset."""
17import gzip
18import os
20import numpy as np
22from keras.src.utils.data_utils import get_file
24# isort: off
25from tensorflow.python.util.tf_export import keras_export
28@keras_export("keras.datasets.fashion_mnist.load_data")
29def load_data():
30 """Loads the Fashion-MNIST dataset.
32 This is a dataset of 60,000 28x28 grayscale images of 10 fashion categories,
33 along with a test set of 10,000 images. This dataset can be used as
34 a drop-in replacement for MNIST.
36 The classes are:
38 | Label | Description |
39 |:-----:|-------------|
40 | 0 | T-shirt/top |
41 | 1 | Trouser |
42 | 2 | Pullover |
43 | 3 | Dress |
44 | 4 | Coat |
45 | 5 | Sandal |
46 | 6 | Shirt |
47 | 7 | Sneaker |
48 | 8 | Bag |
49 | 9 | Ankle boot |
51 Returns:
52 Tuple of NumPy arrays: `(x_train, y_train), (x_test, y_test)`.
54 **x_train**: uint8 NumPy array of grayscale image data with shapes
55 `(60000, 28, 28)`, containing the training data.
57 **y_train**: uint8 NumPy array of labels (integers in range 0-9)
58 with shape `(60000,)` for the training data.
60 **x_test**: uint8 NumPy array of grayscale image data with shapes
61 (10000, 28, 28), containing the test data.
63 **y_test**: uint8 NumPy array of labels (integers in range 0-9)
64 with shape `(10000,)` for the test data.
66 Example:
68 ```python
69 (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
70 assert x_train.shape == (60000, 28, 28)
71 assert x_test.shape == (10000, 28, 28)
72 assert y_train.shape == (60000,)
73 assert y_test.shape == (10000,)
74 ```
76 License:
77 The copyright for Fashion-MNIST is held by Zalando SE.
78 Fashion-MNIST is licensed under the [MIT license](
79 https://github.com/zalandoresearch/fashion-mnist/blob/master/LICENSE).
81 """
82 dirname = os.path.join("datasets", "fashion-mnist")
83 base = "https://storage.googleapis.com/tensorflow/tf-keras-datasets/"
84 files = [
85 "train-labels-idx1-ubyte.gz",
86 "train-images-idx3-ubyte.gz",
87 "t10k-labels-idx1-ubyte.gz",
88 "t10k-images-idx3-ubyte.gz",
89 ]
91 paths = []
92 for fname in files:
93 paths.append(get_file(fname, origin=base + fname, cache_subdir=dirname))
95 with gzip.open(paths[0], "rb") as lbpath:
96 y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)
98 with gzip.open(paths[1], "rb") as imgpath:
99 x_train = np.frombuffer(imgpath.read(), np.uint8, offset=16).reshape(
100 len(y_train), 28, 28
101 )
103 with gzip.open(paths[2], "rb") as lbpath:
104 y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)
106 with gzip.open(paths[3], "rb") as imgpath:
107 x_test = np.frombuffer(imgpath.read(), np.uint8, offset=16).reshape(
108 len(y_test), 28, 28
109 )
111 return (x_train, y_train), (x_test, y_test)