大家好,又见面了,我是你们的朋友全栈君。
记录用不同的方法读取Mnist数据集
1、Python的PIL模块读取Mnist图片
#读取文件夹mnist下的60000张图片,图片为灰度图,所以为1通道,如果是将彩色图作为输入,则将1替换为3,图像大小28*28
def load_data():
data = np.empty((60000,1,28,28),dtype="float32")
label = np.empty((60000,),dtype="uint8")
imgs = os.listdir("E:\\DataSet\\mnist")
num = len(imgs)
for i in range(num):
img = Image.open("E:\\DataSet\\mnist\\"+imgs[i])
arr = np.asarray(img,dtype="float32")
data[i,:,:,:] = arr
label[i] = int(imgs[i].split('.')[0])
#归一化和零均值化
data /= np.max(data)
data -= np.mean(data)
return data,label
if __name__ == '__main__':
load_data()
2、keras自带的 load_data( ) 方法
from keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train.shape (60000, 28, 28),X_test.shape (10000, 28, 28); y_train.shape (60000,),y_test.shape (10000,)。
这个方法未进行归一化,像素点数据范围是0-255。
参考:
http://keras-cn.readthedocs.io/en/latest/other/datasets/
https://github.com/wepe/MachineLearning/
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/129521.html原文链接:https://javaforall.cn
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有