Download Mnist dataset in TensorFlow

Source: Internet
Author: User
Tags assert shuffle
#coding: Utf-8 from __future__ import absolute_import from __future__ Import division from __future__ import print_function Import gzip import OS import numpy from six.moves import urllib from six.moves import xrange # pylint:disable=redefined -builtin "" "TensorFlow input_data.py Load mnist DataSet" "#SOURCE_URL = ' http://yann.lecun.com/exdb/mnist/' Source_url = ' mnist/' #离线下载数据集保存到mnist文件夹下 def maybe_download (filename, work_directory): "" "Download the data from Yann ' s website,
  Unless it ' s already here. "" "
  If not os.path.exists (work_directory): Os.mkdir (work_directory) filepath = os.path.join (work_directory, filename) If not os.path.exists (filepath): filepath, _ = Urllib.request.urlretrieve (Source_url + filename, filepath) statin
  FO = os.stat (filepath) print (' successfully downloaded ', filename, statinfo.st_size, ' bytes. ') Return filepath def _read32 (bytestream): dt = Numpy.dtype (Numpy.uint32). Newbyteorder (' > ') return Numpy.frombuffer (b Ytestream.read (4), DTYPE=DT) [0] def extract_images (filename): "" "extract the images into a 4D uint8 numpy array [index, y, x, depth]." " Print (' extracting ', filename) with gzip.open (filename) as Bytestream:magic = _read32 (ByteStream) if magic! = 20
    51:raise valueerror (' Invalid magic number%d in MNIST image file:%s '% (magic, filename)) Num_images = _read32 (bytestream) rows = _read32 (bytestream) cols = _read32 (bytestream) buf = Bytestream.read ( Rows * cols * num_images) data = Numpy.frombuffer (buf, dtype=numpy.uint8) data = Data.reshape (num_images, rows, CO LS, 1) return Data def dense_to_one_hot (Labels_dense, num_classes=10): "" "Convert class labels from scalars to One-h
  OT vectors. "" " Num_labels = labels_dense.shape[0] Index_offset = Numpy.arange (num_labels) * num_classes labels_one_hot = Numpy.zeros (
  (Num_labels, num_classes)) Labels_one_hot.flat[index_offset + labels_dense.ravel ()] = 1 return labels_one_hot def Extract_labeLS (filename, one_hot=false): "" "Extract the labels into a 1D uint8 numpy array [index]." "" Print (' extracting ', filename) with gzip.open (filename) as Bytestream:magic = _read32 (ByteStream) if magic! = 20
    49:raise valueerror (' Invalid magic number%d in MNIST label file:%s '% (magic, filename)) Num_items = _read32 (bytestream) buf = Bytestream.read (num_items) labels = Numpy.frombuffer (buf, Dtype=numpy.uint8 ) if One_hot:return dense_to_one_hot (labels) return labels Class DataSet (object): Def __init__ (self, imag  ES, labels, fake_data=false): If Fake_data:self._num_examples = 10000 Else:assert images.shape[0] = =
                                                 Labels.shape[0], ("Images.shape:%s labels.shape:%s"% (Images.shape, Labels.shape)) Self._num_examples = images.shape[0] # Convert shape from [num examples, rows, column s, depth] # to [num examples, rows*columns] (Assuming depth = = 1) assert images.shape[3] = = 1 images = Images.reshape (images.shape[0],
      IMAGES.SHAPE[1] * images.shape[2]) # Convert from [0, 255], [0.0, 1.0]. Images = Images.astype (numpy.float32) images = numpy.multiply (images, 1.0/255.0) self._images = Images sel F._labels = Labels self._epochs_completed = 0 Self._index_in_epoch = 0 @property def images (self): return Self._images @property def labels (self): return self._labels @property def num_examples (self): return self . _num_examples @property def epochs_completed (self): return self._epochs_completed def next_batch (self, batch_si
    Ze, Fake_data=false): "" "Return the Next ' batch_size ' examples from this data set." " If fake_data:fake_image = [1.0 for _ in Xrange (784)] Fake_label = 0 return [Fake_image for _ in Xrange ( Batch_size)], [Fake_label for _ in Xrange (batch_size)] Start = Self._index_in_epoch Self._index_in_epoch + = batch_size if Self._index_in_epoch > Self._num_examples:
      # finished Epoch self._epochs_completed + = 1 # Shuffle the data perm = Numpy.arange (self._num_examples) Numpy.random.shuffle (perm) self._images = self._images[perm] Self._labels = self._labels[perm] # S
    Tart Next Epoch start = 0 Self._index_in_epoch = batch_size assert batch_size <= self._num_examples End = Self._index_in_epoch return self._images[start:end], Self._labels[start:end] def read_data_sets (Train_dir, FA Ke_data=false, One_hot=false): Class DataSets (object): Pass data_sets = DataSets () if FAKE_DATA:DATA_SETS.T Rain = DataSet ([], [], fake_data=true) data_sets.validation = DataSet ([], [], fake_data=true) data_sets.test = Dat ASet ([], [], fake_data=true) return data_sets train_images = ' train-images-idx3-ubyte.gz ' #mnist文件夹下四个文件 train_la BELs = ' Train-labels-idx1-ubyte.gz ' test_images = ' t10k-images-idx3-ubyte.gz ' test_labels = ' t10k-labels-idx1-ubyte.gz ' VALIDATION_SIZE = 50 xx local_file = Maybe_download (train_images, train_dir) train_images = Extract_images (local_file) Local_file = maybe _download (Train_labels, train_dir) train_labels = Extract_labels (Local_file, one_hot=one_hot) Local_file = maybe_downl Oad (Test_images, train_dir) test_images = Extract_images (local_file) local_file = Maybe_download (TEST_LABELS, Train_di r) Test_labels = Extract_labels (Local_file, one_hot=one_hot) validation_images = train_images[:validation_size] Vali Dation_labels = train_labels[:validation_size] Train_images = train_images[validation_size:] Train_labels = Train_labe Ls[validation_size:] Data_sets.train = DataSet (Train_images, train_labels) data_sets.validation = DataSet (Validation_
 Images, validation_labels) data_sets.test = DataSet (test_images, test_labels) return data_sets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.