神经网络 tensorflow教程 2.2 下载MNIST 数据集(保存所有图片)

场景:

语言 :python3.5(建议使用 Anaconda3-4.2.0-Windows-x86_64.exe)

操作系统: windos7



数据集下载地址:   http://yann.lecun.com/exdb/mnist/

下载对应的四个文件   :   t10k-images.idx3-ubyte       t10k-labels.idx1-ubyte      train-images.idx3-ubyte      train-labels.idx1-ubyte    

(下载完之后更改文件名 : 例如  t10k-images-idx3-ubyte    改为  t10k-images.idx3-ubyte   )


 解压文件到   F:\mnist

新建文件夹: F:\mnist\test    和   F:\mnist\test_label




解析全部文件:

以上环境具备后,执行如下python3.5代码:


#!/usr/bin/env python
# -*- coding: utf-8 -*-


from PIL import Image
import struct


def read_image(filename):
   f = open(filename, 'rb')

   index = 0
   buf = f.read()

   f.close()

   magic, images, rows, columns = struct.unpack_from('>IIII', buf, index)

   index += struct.calcsize('>IIII')

   for i in range(images):
      # for i in xrange(2000):
      image = Image.new('L', (columns, rows))

      for x in range(rows):
         for y in range(columns):
            image.putpixel((y, x), int(struct.unpack_from('>B', buf, index)[0]))
            index += struct.calcsize('>B')

      print ('save ' + str(i) + 'image')
      image.save(r'f:\mnist\test\test' + str(i) + '.png')


def read_label(filename, saveFilename):
   f = open(filename, 'rb')
   index = 0
   buf = f.read()

   f.close()

   magic, labels = struct.unpack_from('>II', buf, index)
   index += struct.calcsize('>II')

   labelArr = [0] * labels
   # labelArr = [0] * 2000

   for x in range(labels):
      # for x in xrange(2000):
      labelArr[x] = int(struct.unpack_from('>B', buf, index)[0])
      index += struct.calcsize('>B')

   save = open(saveFilename, 'w')

   save.write(','.join(map(lambda x: str(x), labelArr)))
   save.write('\n')

   save.close()
   print ('save labels success')


if __name__ == '__main__':
   read_image(r'f:\mnist\t10k-images.idx3-ubyte')
   read_label(r'f:\mnist\t10k-labels.idx1-ubyte', r'f:\mnist\test_label\label.txt')


即可以生成 图片     F:\mnist\test下

 


神经网络 tensorflow教程 2.2 下载MNIST 数据集(保存所有图片)_第1张图片


你可能感兴趣的:(神经网络,tensorflow)