from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# load data with keras.datasets.mnist.load_data
The code of keras.datasets.mnist
from __future__ import absolute_import # use std lib
from __future__ import division # use accurate division
from __future__ import print_function # use py3 print
from ..utils.data_utils import get_file # fetch file
import numpy as np
def load_data(path='mnist.npz'):
# load data using keras.utils.data_utils.get_file
path = get_file(path, # path is the fname
origin='https://s3.amazonaws.com/img-datasets/mnist.npz',
file_hash='8a61469f7ea1b51cbae51d4f78837e45')
f = np.load(path) # open and read
x_train, y_train = f['x_train'], f['y_train'] # assgian dict values
x_test, y_test = f['x_test'], f['y_test']
f.close() # close the file
return (x_train, y_train), (x_test, y_test) # tuple of numpy arrays
the code of keras.utils.data_utils.get_file
def get_file(fname,
origin,
untar=False,
md5_hash=None,
file_hash=None,
cache_subdir='datasets',
hash_algorithm='auto',
extract=False,
archive_format='auto',
cache_dir=None):
if cache_dir is None: # if no dir is indicated, use the default dir
cache_dir = os.path.join(os.path.expanduser('~'), '.keras') # default dir is ~/.keras
if md5_hash is not None and file_hash is None: # if no hash is assigned to file
file_hash = md5_hash
hash_algorithm = 'md5' # use md5 algorithm to decode
datadir_base = os.path.expanduser(cache_dir) # ~/.keras
if not os.access(datadir_base, os.W_OK): # if unable to write
datadir_base = os.path.join('/tmp', '.keras') # change path to /tmp/.keras
datadir = os.path.join(datadir_base, cache_subdir) # assemble paths, ~/.keras/datasets
if not os.path.exists(datadir): # if dir is not exist
os.makedirs(datadir) # mkdir
if untar: # if using untar
untar_fpath = os.path.join(datadir, fname) # ~/.keras/datasets/mnist.npz
fpath = untar_fpath + '.tar.gz' # ~/.keras/datasets/mnist.npz.tar.gz
else: # if untar is not needed
fpath = os.path.join(datadir, fname)