赞
踩
1、请注意运行代码存入的文件夹的名称,要与代码中的path
路径对应一致;
2、下载MNIST数据集(四个压缩包),并将四个压缩包的内容解压出来,如下图①;
3、在运行代码目录下,建立data
文件夹,data
文件夹下包含两个子文件夹data_a
、data_c
,最后在data_c
文件夹下建立以0~9为名的十个文件夹,如下图②③;
–-----------------------------------------------------------------------------—------------------------------------------------
–-----------------------------------------------------------------------------—------------------------------------------------
1、这里提供两种路径选择,第一个是将所有的图片不区分索引,全部存入data_a
文件夹内,第二个是按照图片索引的不同,存入data_c
不同文件夹下;
2、可以通过range()
函数,指定打印出图片的张数;
3、注意path
对应的路径是否一致
# 将打印出的MNIST数据集中所有的图片存入一个data文件夹下
for i in range(0, 10):
path = "../CNN+Kreas框架+MNIST/data/data_a/"
name = str(i) + ".png"
mnist_save_img(x_train[i], path, name)
"""
# 按图片标签的不同,打印MNIST数据集的图片存入不同文件夹下
for i in range(0, 50):
path = "../CNN+Kreas框架+MNIST/data/data_c/" + str(y_train[i]) +"/"
name = str(i)+".png"
mnist_save_img(x_train[i], path, name)
"""
–-----------------------------------------------------------------------------—------------------------------------------------
–-----------------------------------------------------------------------------—------------------------------------------------
# -*- coding: utf-8 -*- # -*- author:zzZ_CMing CSDN address:https://blog.csdn.net/zzZ_CMing # -*- 2018/07/09; 15:18 # -*- python3.5 """ 将MNIST数据集由二进制文件转为图片形式,保存于指定文件夹下 """ import os import struct import numpy as np import matplotlib.pyplot as plt # 读MNIST数据集的图片数据 def mnist_load_img(img_path): with open(img_path, "rb") as fp: # >是以大端模式读取,i是整型模式,读取前四位的标志位, # unpack()函数:是将4个字节联合后再解析成一个数,(读取后指针自动后移) msb = struct.unpack('>i', fp.read(4))[0] # 标志位为2051,后存图像数据;标志位为2049,后存图像标签 if msb == 2051: # 读取样本个数60000,存入cnt cnt = struct.unpack('>i', fp.read(4))[0] # rows:行数28;cols:列数28 rows = struct.unpack('>i', fp.read(4))[0] cols = struct.unpack('>i', fp.read(4))[0] imgs = np.empty((cnt, rows, cols), dtype="int") for i in range(0, cnt): for j in range(0, rows): for k in range(0, cols): # 16进制转10进制 pxl = int(hex(fp.read(1)[0]), 16) imgs[i][j][k] = pxl return imgs else: return np.empty(1) # 读MNIST数据集的图片标签 def mnist_load_label(label_path): with open(label_path, "rb") as fp: msb = struct.unpack('>i', fp.read(4))[0]; if msb == 2049: cnt = struct.unpack('>i', fp.read(4))[0]; labels = np.empty(cnt, dtype="int"); for i in range(0, cnt): label = int(hex(fp.read(1)[0]), 16); labels[i] = label; return labels; else: return np.empty(1); # 分割训练、测试集的图片数据与图片标签 def mnist_load_data(train_img_path, train_label_path, test_img_path, test_label_path): x_train = mnist_load_img(train_img_path); y_train = mnist_load_label(train_label_path); x_test = mnist_load_img(test_img_path); y_test = mnist_load_label(test_label_path); return (x_train, y_train), (x_test, y_test); # 输出打印图片 def mnist_plot_img(img): (rows, cols) = img.shape; plt.figure(); plt.gray(); plt.imshow(img); plt.show(); # 按指定位置保存图片 def mnist_save_img(img, path, name): if not os.path.exists(path): os.mkdir(path) (rows, cols) = img.shape fig = plt.figure() plt.gray() plt.imshow(img) # 在既定路径里保存图片 fig.savefig(path + name) # [start] x_train = mnist_load_img("train-images.idx3-ubyte") y_train = mnist_load_label("train-labels.idx1-ubyte") # 将打印出的MNIST数据集中所有的图片存入一个data文件夹下 for i in range(0, 10): path = "../CNN+Kreas框架+MNIST/data/data_a/" name = str(i) + ".png" mnist_save_img(x_train[i], path, name) """ # 按图片标签的不同,打印MNIST数据集的图片存入不同文件夹下 for i in range(0, 50): path = "../CNN+Kreas框架+MNIST/data/data_c/" + str(y_train[i]) +"/" name = str(i)+".png" mnist_save_img(x_train[i], path, name) """ #mnist_plot_img(x_train[0, :, :]) """ x_test = mnist_load_img("t10k-images.idx3-ubyte") y_test = mnist_load_label("t10k-labels.idx1-ubyte") """
效果展示:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。