Dataset:利用Python将已有mnist数据集通过移动像素上下左右的方法来扩大数据集为初始数据集的5倍—Jason niu
2018-01-18 12:54
1161 查看
from __future__ import print_function import cPickle import gzip import os.path import random import numpy as np print("Expanding the MNIST training set") if os.path.exists("../data/mnist_expanded.pkl.gz"): print("The expanded training set already exists. Exiting.") else: f = gzip.open("../data/mnist.pkl.gz", 'rb') training_data, validation_data, test_data = cPickle.load(f) f.close() expanded_training_pairs = [] j = 0 for x, y in zip(training_data[0], training_data[1]): expanded_training_pairs.append((x, y)) image = np.reshape(x, (-1, 28)) j += 1 if j % 1000 == 0: print("Expanding image number", j) for d, axis, index_position, index in [ (1, 0, "first", 0), (-1, 0, "first", 27), (1, 1, "last", 0), (-1, 1, "last", 27)]: new_img = np.roll(image, d, axis) if index_position == "first": new_img[index, :] = np.zeros(28) else: new_img[:, index] = np.zeros(28) expanded_training_pairs.append((np.reshape(new_img, 784), y)) random.shuffle(expanded_training_pairs) expanded_training_data = [list(d) for d in zip(*expanded_training_pairs)] print("Saving expanded data. This may take a few minutes.") f = gzip.open("../data/mnist_expanded.pkl.gz", "w") cPickle.dump((expanded_training_data, validation_data, test_data), f) f.close()
相关文章推荐
- jQuery实现通过方向键控制div块上下左右移动的方法【测试可用】
- Javascript 利用键盘上的上下左右(回车)键模拟出tab 键的功能上下左右移动焦点
- 利用键盘上的上下左右(回车)键模拟出tab 键的功能上下左右移动焦点
- java事件机制之通过上下左右键来控制小球为位置的移动演示
- jQuery-利用上下左右键移动输入焦点
- java事件机制之通过上下左右键来控制小球为位置的移动演示
- 利用键盘上的上下左右(回车)键模拟出tab 键的功能上下左右移动焦点
- 利用python实现对鼠标的移动点击操作(PyMouse 模块安装方法)
- Javascript控制Listbox左右/上下移动方法集合
- python3利用smtplib通过qq邮箱发送邮件方法示例
- 利用键盘上的上下左右(回车)键模拟出tab 键的功能上下左右移动焦点
- 利用键盘上的上下左右(回车)键模拟出tab 键的功能上下左右移动焦点
- 利用Python抓取行政区划码的方法
- Python中利用Scipy包的SIFT方法进行图片识别的实例教程
- 360°旋转 放大 上下左右移动
- python通过加号运算符操作列表的方法
- Wpf图片变换一:通过WPF实现图片的扩大、缩小、翻转、移动实例代码
- python下面通过ctypes模块调用c++库的方法
- python 通过字符串调用对象属性或方法的实例讲解
- 用Activity的onTouchEvent方法实现监听手指上下左右滑动