语言:python3.6
框架: tensorflow
数据集:mnist数据集(图片格式)
1、准备数据集
下载地址:http://yann.lecun.com/exdb/mnist/
用以下的代码将minist数据集转化为图片(也可以通过from tensorflow.examples.tutorials.mnist import input_data获得二进格式的数据)
"""
将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, len(x_train)):
path = "./data/" + 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")
"""
代码转自(https://blog.youkuaiyun.com/zzZ_CMing/article/details/81063977)
就会在所指定的目录下发现0~9文件夹,里面存放图片形式的minist数据集,接下来制作训练集(也可以用上面代码中的mnist_load_data方法)
2、制作训练集
将图片和相应标签对应起来,用以下代码实现。
class Dataset(object):
def __init__(self,file_path,batch_size,capacity):
self.file_path=file_path#路径文件
self.batch_size=batch_size#batch_size
self.capacity=capacity#队列容量
self.image_size=28
def num_batch(self):
image, label = self.get_files(self.file_path)
return len(label)//self.batch_size#一批样本中的batch个数
def get_batch(self):
image, label = self.get_files(self.file_path)
batch_image, batch_label=self.get_batches(image,label,self.image_size,self.image_size,self.batch_size,self.capacity)
return batch_image,batch_label
def get_files(self,file_path):
class_train = []
label_train = []
for train_class in os.listdir(file_path):
for pic_name in os.listdir(file_path + train_class):
class_train.append(file_path + train_class + '/' + pic_name)
label_train.append(train_class)
temp = np.array([class_train, label_tr

本文介绍如何将MNIST数据集转化为图片格式,并利用TensorFlow框架进行手写数字识别的训练过程。从数据集的下载、转换到图片,再到训练集的制作、网络设计、损失函数定义、优化算法选择,直至最终的训练和验证,文章提供了详细的代码实现。
最低0.47元/天 解锁文章
1784

被折叠的 条评论
为什么被折叠?



