mxnet多层感知器、卷积神经网络测试【转】

来自:http://blog.csdn.net/xinfeng2005/article/details/53380700?locationNum=8&fps=1

# coding=utf-8
import mxnet as mx
import matplotlib.pyplot as plt
import numpy as np
import struct
import pickle

def ImageToFloat(img):
    return img.reshape(img.shape[0],1,28,28).astype(np.float32)/255

#选择lenet
model=mx.model.FeedForward.load('lenet',10)
#选择MLP
#model=mx.model.FeedForward.load('mpl_mnist',10)

# 测试集单张图像识别情况
with open('./mnist/t10k-labels-idx1-ubyte')as flbl:
    magic, num = struct.unpack(">II", flbl.read(8))
    label = np.fromstring(flbl.read(), dtype=np.int8)
with open('./mnist/t10k-images-idx3-ubyte', '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)
plt.subplot(5,5,1)
for x in range(25):
    plt.subplot(5,5,x+1)
    plt.imshow(255-image[x], cmap='Greys_r')
    prob = model.predict(ImageToFloat(image[x:x+1]))[0]
    print'Classified as %d with probability %f' % (prob.argmax(), max(prob))
    plt.title('%s %s'%(str(label[x]),str(max(prob))))

    plt.axis('off')
plt.show()
val_iter = mx.io.NDArrayIter(ImageToFloat(image), label, batch_size=100)
print'Text accuracy: %f%%' % (model.score(val_iter) * 100,)

#训练集识别精度
with open('./mnist/train-labels-idx1-ubyte')as flbl:
    magic, num = struct.unpack(">II", flbl.read(8))
    label_train = np.fromstring(flbl.read(), dtype=np.int8)
with open('./mnist/train-images-idx3-ubyte', 'rb')as fimg:
    magic, num, rows, cols = struct.unpack(">IIII", fimg.read(16))
    image_train = np.fromstring(fimg.read(), dtype=np.uint8).reshape(len(label_train), rows, cols)
plt.axis('off')
plt.imshow(255-image_train[0], cmap='Greys_r')
prob = model.predict(ImageToFloat(image_train[0:1]))[0]
print'Classified as %d with probability %f' % (prob.argmax(), max(prob))
plt.show()

train_iter = mx.io.NDArrayIter(ImageToFloat(image_train), label_train, batch_size=100)
print'Train accuracy: %f%%' % (model.score(train_iter) * 100,)

迭代10次后
MLP:精度Text accuracy: 97.390000% Train accuracy: 98.821667%
LeNet:精度Text accuracy: 99.170000% Train accuracy: 99.995000%

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值