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. 16""" 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21import gzip 22import os 23 24import numpy as np 25 26from tensorflow.python.keras.utils.data_utils import get_file 27from tensorflow.python.util.tf_export import keras_export 28 29 30@keras_export('keras.datasets.fashion_mnist.load_data') 31def load_data(): 32 """Loads the Fashion-MNIST dataset. 33 34 This is a dataset of 60,000 28x28 grayscale images of 10 fashion categories, 35 along with a test set of 10,000 images. This dataset can be used as 36 a drop-in replacement for MNIST. The class labels are: 37 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 | 50 51 Returns: 52 Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`. 53 54 **x_train, x_test**: uint8 arrays of grayscale image data with shape 55 (num_samples, 28, 28). 56 57 **y_train, y_test**: uint8 arrays of labels (integers in range 0-9) 58 with shape (num_samples,). 59 60 License: 61 The copyright for Fashion-MNIST is held by Zalando SE. 62 Fashion-MNIST is licensed under the [MIT license]( 63 https://github.com/zalandoresearch/fashion-mnist/blob/master/LICENSE). 64 65 """ 66 dirname = os.path.join('datasets', 'fashion-mnist') 67 base = 'https://storage.googleapis.com/tensorflow/tf-keras-datasets/' 68 files = [ 69 'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz', 70 't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz' 71 ] 72 73 paths = [] 74 for fname in files: 75 paths.append(get_file(fname, origin=base + fname, cache_subdir=dirname)) 76 77 with gzip.open(paths[0], 'rb') as lbpath: 78 y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8) 79 80 with gzip.open(paths[1], 'rb') as imgpath: 81 x_train = np.frombuffer( 82 imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28) 83 84 with gzip.open(paths[2], 'rb') as lbpath: 85 y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8) 86 87 with gzip.open(paths[3], 'rb') as imgpath: 88 x_test = np.frombuffer( 89 imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28) 90 91 return (x_train, y_train), (x_test, y_test) 92