I have been experimenting with a Keras example, which needs to import MNIST data
from keras.datasets import mnist
import numpy as np
(x_train, _), (x_test, _) =
Keras file is located into a new path in Google Cloud Storage (Before it was in AWS S3):
https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
When using:
tf.keras.datasets.mnist.load_data()
You can pass a path
parameter.
load_data()
will call get_file()
which takes as parameter fname
, if path is a full path and file exists, it will not be downloaded.
Example:
# gsutil cp gs://tensorflow/tf-keras-datasets/mnist.npz /tmp/data/mnist.npz
# python3
>>> import tensorflow as tf
>>> path = '/tmp/data/mnist.npz'
>>> (train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data(path)
>>> len(train_images)
>>> 60000
https://s3.amazonaws.com/img-datasets/mnist.npz
mnist.npz
to .keras/datasets/
directoryLoad data
import keras
from keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
Well, the keras.datasets.mnist
file is really short. You can manually simulate the same action, that is:
.
import gzip
f = gzip.open('mnist.pkl.gz', 'rb')
if sys.version_info < (3,):
data = cPickle.load(f)
else:
data = cPickle.load(f, encoding='bytes')
f.close()
(x_train, _), (x_test, _) = data
You do not need additional code for that but can tell load_data
to load a local version in the first place:
~/.keras/datasets/
(on Linux and macOS)load_data(path='mnist.npz')
with the right file name