|
解析mnist手写数字图像:http://yann.lecun.com/exdb/mnist/index.html上下载相应的mnist百度网盘链接:
链接:http://pan.baidu.com/s/1c1Z8BQg
具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1
- from PIL import Image
- import struct
- def read_image(filename, saveFilename):
- 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(saveFilename + 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('./t10k-images-idx3-ubyte/t10k-images.idx3-ubyte', 'Test/test/')
- read_label('./t10k-labels-idx1-ubyte/t10k-labels.idx1-ubyte', 'Test/test_label.txt')
- read_image('./train-images-idx3-ubyte/train-images.idx3-ubyte', 'Train/train/')
- read_label('./train-labels-idx1-ubyte/train-labels.idx1-ubyte', 'Train/train_label.txt')
复制代码
|
|