MXNET笔记(二)准备数据

MXNET并不直接读入图像,而是读入其自定义的一种格式。为了生成这种格式,需要利用/mxnet/tools/im2rec.py工具来对数据库图像进行处理而生成。我现在手头没有现成的数据库可以使用,而现在一般的数据库又太大了,所以我把Rec格式的MNIST数据库还原成图像文件。#利用mxnet提供的代码下载MNIST数据库import numpy as npimport osimport url
摘要由CSDN通过智能技术生成

MXNET并不直接读入图像,而是读入其自定义的一种格式。为了生成这种格式,需要利用/mxnet/tools/im2rec.py工具来对数据库图像进行处理而生成。我现在手头没有现成的数据库可以使用,而现在一般的数据库又太大了,所以我把Rec格式的MNIST数据库还原成图像文件。

#利用mxnet提供的代码下载MNIST数据库
import numpy as np
import os
import urllib
import gzip
import struct
def download_data(url, force_download=True): 
    fname = url.split("/")[-1]
    if force_download or not os.path.exists(fname):
        urllib.urlretrieve(url, fname)
    return fname

def read_data(label_url, image_url):
    with gzip.open(download_data(label_url)) as flbl:
        magic, num = struct.unpack(">II", flbl.read(8))
        label = np.fromstring(flbl.read(), dtype=np.int8)
    with gzip.open(download_data(image_url), 'rb') as fimg:
        magic, num, rows, cols = struct.unpack(">IIII", fimg.read(16))
        image = np.fromstring(fimg.read(), dtype=np.uint8).reshape(len(label), rows, cols)
    return (label, image)

path='http://yann.lecun.com/exdb/mnist/'
(train_lbl, train_img) = read_data(
    path+'train-labels-idx1-ubyte.gz', path+'train-images-idx3-ubyte.gz')
(val_lbl, val_img) = read_data(
    path+'t10k-labels-idx1-ubyte.gz', path+'t10k-images-idx3-ubyte.gz'
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值