How can I import the MNIST dataset that has been manually downloaded?

前端 未结 4 1105
北海茫月
北海茫月 2021-02-05 18:36

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, _) =          


        
相关标签:
4条回答
  • 2021-02-05 18:56

    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
    
    0 讨论(0)
  • 2021-02-05 19:02
    1. Download file https://s3.amazonaws.com/img-datasets/mnist.npz
    2. Move mnist.npz to .keras/datasets/ directory
    3. Load data

      import keras
      from keras.datasets import mnist
      
      (X_train, y_train), (X_test, y_test) = mnist.load_data()
      
    0 讨论(0)
  • 2021-02-05 19:03

    Well, the keras.datasets.mnist file is really short. You can manually simulate the same action, that is:

    1. Download a dataset from https://s3.amazonaws.com/img-datasets/mnist.pkl.gz
    2. .

      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
      
    0 讨论(0)
  • 2021-02-05 19:09

    You do not need additional code for that but can tell load_data to load a local version in the first place:

    1. You can download the file https://s3.amazonaws.com/img-datasets/mnist.npz from another computer with proper (proxy) access (taken from https://github.com/keras-team/keras/blob/master/keras/datasets/mnist.py),
    2. copy it to the the directory ~/.keras/datasets/ (on Linux and macOS)
    3. and run load_data(path='mnist.npz') with the right file name
    0 讨论(0)
提交回复
热议问题