1 框架介绍
1.1 PaddlePaddle
PaddlePaddle 官网
PaddlePaddle 中文文档
深度学习平台飞桨(PaddlePaddle)是主流深度学习框架中一款完全国产化的产品,与 Google TensorFlow、Facebook Pytorch 齐名。2016 年飞桨正式开源,是国内首个全面开源开放、技术领先、功能完备的产业级深度学习平台。相比国内其他平台,飞桨是一个功能完整的深度学习平台,也是唯一成熟稳定、具备大规模推广条件的深度学习平台。
1.2 PyTorch
PyTorch 官网
PyTorch 中文文档
PyTorch 是一个基于 Torch 的 Python 开源机器学习库,用于自然语言处理等应用程序。它主要由 Facebook 的人工智能小组开发,不仅能够实现强大的GPU 加速,同时还支持动态神经网络。 PyTorch 提供了两个高级功能: 1. 具有强大的 GPU 加速的张量计算;2. 包含自动求导系统的深度神经网络。
2 数据集介绍
2.1 CIFAR-10
CIFAR-10 官网
CIFAR-10 数据集共有 60000 张彩色图像,其中 50000 张用于训练,10000 张用于测试,图像分辨率为 32×32,分为 10 个类别(airplane,automobile,bird,cat,deer,dog,frog,horse,ship,truck),每个类别有 6000 张图像。
2.2 MNIST
MNIST 官网
MNIST 数据集共有 70000 张黑白图像,其中训练集包含 60000 张图像,测试集包含 10000 张图像,图像分辨率为 28×28,分为 10 个类别(0~9)
MNIST 数据集包含四个压缩的二进制文件
文件名 | 内容 |
---|---|
train-images-idx3-ubyte.gz | 60000 张训练集图片 |
train-labels-idx1-ubyte.gz | 60000 张训练集图片对应的标签 |
t10k-images-idx3-ubyte.gz | 10000 张测试集图片 |
t10k-labels-idx1-ubyte.gz | 10000 张测试集图片对应的标签 |
存储图像的二进制文件:
1~4 字节 | 5~8 字节 | 9~12 字节 | 13~16 字节 | 17~ 字节 |
---|---|---|---|---|
文件的 magic number(2051) | 图像数量 | 图像的高度 | 图像的宽度 | 像素值 |
存储标签的二进制文件:
1~4 字节 | 5~8 字节 | 9~ 字节 |
---|---|---|
文件的 magic number(2049) | 标签数量 | 标签值 |
3 数据加载
3.1 CIFAR-10
datasets.py
'''
datasets.py
'''
import os
import gzip
from struct import unpack
import numpy as np
# 从文件中加载 CIFAR-10 数据集
def cifar10(data_path='./data', data_split=None):
if data_split is None:
split = ['data_batch', 'test_batch']
elif data_split == 'train':
split = ['data_batch', ]
elif data_split == 'test':
split = ['test_batch', ]
else:
raise ValueError('data_split should be in (train, test)')
print('loading CIFAR-10 dataset ......')
data = []
with tarfile.open(os.path.join(data_path, 'cifar-10-python.tar.gz'), mode='r') as f:
for item in split:
images, labels = [], []
for file_name in f:
if item in file_name.name:
temp = pickle.load(f.extractfile(file_name.name), encoding='bytes')
images.append(temp[b'data'])
labels += temp[b'labels']
images = np.concatenate(images, axis=0).reshape(-1, 3, 32, 32)
labels = np.array(labels).reshape(-1, 1)
assert images.shape[0] == labels.shape[0], \
'length of images({}) should be the same as labels({})'.format(images.shape[0], labels.shape[0])
data.append((images, labels))
return tuple(data)
3.2 MNIST
datasets.py
'''
datasets.py
'''
import os
import tarfile
import pickle
import numpy as np
# 从文件中加载 MNIST 数据集
def mnist(data_path='./data', data_split=None):
if data_split is None:
split = ['train', 't10k']
elif data_split == 'train':
split = ['train',]
elif data_split == 'test':
split = ['t10k',]
else:
raise ValueError('data_split should be in (train, test)')
print('loading MNIST dataset ......')
data = []
for item in split:
images_file = os.path.join(data_path, item + '-images-idx3-ubyte.gz')
labels_file = os.path.join(data_path, item + '-labels-idx1-ubyte.gz')
with gzip.open(images_file, mode='rb') as f:
magic, nums, rows, cols = unpack('>4I', f.read(16))
images = np.frombuffer(f.read(), dtype=np.uint8).reshape(nums, 1, rows, cols)
with gzip.open(labels_file, mode='rb') as f:
magic, nums = unpack('>2I', f.read(8))
labels = np.frombuffer(f.read(), dtype=np.uint8).reshape(nums, 1)
assert images.shape[0] == labels.shape[0], \
'length of images({}) should be the same as labels({})'.format(images.shape[0], labels.shape[0])
data.append((images, labels))
return tuple(data)
3.3 数据加载器
datasets.py
'''
datasets.py
'''
import random
import numpy as np
# 数据加载器
def data_loader(data, batch_size=100, shuffle=False):
images = (data[0].astype('float32') / 255.0 - 0.5) / 0.5 # 数据归一化: 0~255 -->> -1~1
labels = data[1].astype('int64')
assert images.shape[0] == labels.shape[0], \
'length of images({}) should be the same as labels({})'.format(images.shape[0], labels.shape[0])
data_ids = list(range(labels.shape[0]))
def generator():
if shuffle:
random.shuffle(data_ids)
batch_image, batch_label = [], []
for i in data_ids:
batch_image.append(images[i])
batch_label.append(labels[i])
# 返回批数据
if len(batch_label) == batch_size:
yield np.array(batch_image), np.array(batch_label)
batch_image, batch_label = [], []
if len(batch_label) > 0:
yield np.array(batch_image), np.array(batch_label)
return generator
if __name__ == '__main__':
for data_name in ('mnist', 'cifar10'):
train_data, test_data = eval(data_name)()
print(data_name)
print('train_data', train_data[0].shape, train_data[1].shape, train_data[0].dtype, train_data[1].dtype)
print('test_data', test_data[0].shape, test_data[1].shape, test_data[0].dtype, test_data[1].dtype)
train_loader = data_load