图片格式mnist数据集转换
来源:互联网 发布:黑帽seo劫持跳转代码 编辑:程序博客网 时间:2024/06/08 21:07
mnist源数据集下载自http://yann.lecun.com/exdb/mnist/。原数据共有四个文件:train-images-idx3-ubyte为训练图片,共60000张,每张灰度图片存为一个1×784维向量;train-labels-idx1-ubyte为训练图片的标签,共60000个;t10k-images-idx3-ubyte为测试图片,共10000张,每张亦为1×784维向量;t10k-labels-idx1-ubyte为测试图片的标签。
为进行图片卷积操作准备,将原数据中1×784的向量转为分辨率为28×28的图片。代码如下:
生成train和test两个文件夹,每个文件夹下为0~9共10共十个子文件夹,子文件夹下是相应的图片。
import numpy as npimport osimport cv2import structdef load_mnist(path, kind='train'): """Load MNIST data from `path`""" labels_path = os.path.join(path, '%s-labels-idx1-ubyte' % kind) images_path = os.path.join(path, '%s-images-idx3-ubyte' % kind) with open(labels_path, 'rb') as lbpath: magic, n = struct.unpack('>II', lbpath.read(8)) labels = np.fromfile(lbpath, dtype=np.uint8) with open(images_path, 'rb') as imgpath: magic, num, rows, cols = struct.unpack(">IIII", imgpath.read(16)) images = np.fromfile(imgpath, dtype=np.uint8).reshape(len(labels), 784) return images, labelsX_train, y_train = load_mnist('', kind='train')print('Rows: %d, columns: %d' % (X_train.shape[0], X_train.shape[1]))X_test, y_test = load_mnist('', kind='t10k')print('Rows: %d, columns: %d' % (X_test.shape[0], X_test.shape[1]))count = np.zeros(10)nTrain = len(X_train)for i in xrange(nTrain): label = y_train[i] count[label] += 1 filename = './train/' + str(label) + '/' + str(label) + '_' + str(int(count[label])) + '.png' img = X_train[i].reshape(28,28) cv2.imwrite(filename, img)count = np.zeros(10)nTest = len(X_test)for i in xrange(nTest): label = y_test[i] count[label] += 1 filename = './test/' + str(label) + '/' + str(label) + '_' + str(int(count[label])) + '.png' img = X_test[i].reshape(28,28) cv2.imwrite(filename, img)
0 0
- 图片格式mnist数据集转换
- MNIST数据集转为.jpg图片格式
- MNIST数据集转换为图像
- MNIST数据集转换为图像
- MNIST数据集的格式转换
- 将MNIST手写数字数据集二进制格式转化为.jpg图片格式
- Opencv + C++转换mnist数据集文件为图像
- 使用OpenCV把二进制mnist数据集转换为图片
- mnist数据集格式转换---机器学习(0)
- JPG、PNG与MNIST数据集之间的转换
- MNIST数据集规则化
- mnist手写数据集
- MNIST数据集分析
- 详解 MNIST 数据集
- tensorflow MNIST数据集
- MNIST数据集处理
- MNIST数据集入门
- 详解 MNIST 数据集
- 前端小白训练营DAY2
- htmlUnit使用
- web前端性能优化总结
- CMS垃圾收集器介绍
- TK1配置cuda6.5
- 图片格式mnist数据集转换
- The first girlfriend -- AngularJS
- 表单form中对回车键的感应
- Android应用设置多个启动图标,动态列换应用图标
- 二级域名绑定二级目录的两种方法
- Vue学习笔记_02
- Easy 8 Remove Element(27)
- C++几个常用的关键字
- js、jquery操作父子iframe