mnist数据库可以通过caffe里的get_mnist.sh文件下载,路径是: caffe-master/data/mnist/get_mnist.sh,get_mnist.sh内容如下:

#!/usr/bin/env sh
# This scripts downloads the mnist data and unzips it.

DIR="$( cd "$(dirname "$0")" ; pwd -P )"
cd "$DIR"

echo "Downloading..."

for fname in train-images-idx3-ubyte train-labels-idx1-ubyte t10k-images-idx3-ubyte t10k-labels-idx1-ubyte
do
    if [ ! -e $fname ]; then
        wget --no-check-certificate http://yann.lecun.com/exdb/mnist/${fname}.gz
        gunzip ${fname}.gz
    fi
done

在get_mnist.sh目录下终端执行命令 sudo sh get_mnist.sh ,会自动下载mnist数据库到当前目录下。

下载下来的mnist数据库是二进制文件格式,使用Python转换成图片:

# -*- coding: utf-8 -*-
import numpy as np
import struct
from PIL import Image
import os

data_file = '/caffe-master/data/mnist/train-images-idx3-ubyte'  # mnist二进制训练数据库文件路径
# 训练数据库的大小是47.0MB,47,040,016 字节;测试数据库的大小是7.8MB,7,840,016 字节
data_file_size = 47040016
data_file_size = 47040016
data_file_size = str(data_file_size - 16) + 'B'

data_buf = open(data_file, 'rb').read()

magic, numImages, numRows, numColumns = struct.unpack_from(
    '>IIII', data_buf, 0)
datas = struct.unpack_from(
    '>' + data_file_size, data_buf, struct.calcsize('>IIII'))
datas = np.array(datas).astype(np.uint8).reshape(
    numImages, 1, numRows, numColumns)

label_file = '/caffe-master/data/mnist/train-labels-idx1-ubyte'  # mnist二进制训练标签文件路径

# 训练标签文件的大小是60.0KB,60,008 字节,测试标签文件的大小是10.0 KB,10008字节
label_file_size = 60008
label_file_size = 60008
label_file_size = str(label_file_size - 8) + 'B'

label_buf = open(label_file, 'rb').read()

magic, numLabels = struct.unpack_from('>II', label_buf, 0)
labels = struct.unpack_from(
    '>' + label_file_size, label_buf, struct.calcsize('>II'))
labels = np.array(labels).astype(np.int64)

datas_root = './mnist_train'  # 生成的mnist图片保存路径
if not os.path.exists(datas_root):
    os.mkdir(datas_root)

for i in range(10):
    file_name = datas_root + os.sep + str(i)
    if not os.path.exists(file_name):
        os.mkdir(file_name)

for ii in range(numLabels):
    img = Image.fromarray(datas[ii, 0, 0:28, 0:28])
    label = labels[ii]
    file_name = datas_root + os.sep + str(label) + os.sep + \
                str(label) +'_'+ str(ii) + '.png'
    img.save(file_name)

posted on 2018-01-29 19:23  未雨愁眸  阅读(1834)  评论(0编辑  收藏  举报