京东-优惠雷达
新人页面
精选商品
首月0月租体验,领12个月京东PLUS
自营热卖

利用动态图机制实现手写数字识别

清风归客 1年前   阅读数 132 0

【推荐阅读】微服务还能火多久?>>>

# 查看当前挂载的数据集目录, 该目录下的变更重启环境后会自动还原
# View dataset directory. This directory will be recovered automatically after resetting environment. 
!ls /home/aistudio/data
In[  ]
# 查看工作区文件, 该目录下的变更将会持久保存. 请及时清理不必要的文件, 避免加载过慢.
# View personal work directory. All changes under this directory will be kept even after reset. Please clean unnecessary files in time to speed up environment loading.
!ls /home/aistudio/work
 

动态图机制-DyGraph

动态图机制不同于以往的静态图,无需构建整个图就可以立即执行结果。这使得我们在编写代码以及调试代码时更加直观、方便,我们无需编写静态图框架,这省去了我们大量的时间成本。利用动态图机制,我们能够更加快捷、直观地构建我们的深度学习网络。这里我通过构建动态图,重新编写了一下我们熟悉的手写数字识别,供大家交流,学习。同时,项目有什么问题欢迎大家指正。

 

一、获取训练数据

In[1]
import paddle
import paddle.fluid as fluid
import numpy as np

#获取训练数据
train_set=paddle.dataset.mnist.train()
train_reader=paddle.batch(train_set,batch_size=16)
#获取测试数据
test_set=paddle.dataset.mnist.test()
test_reader=paddle.batch(test_set,batch_size=32)
 

二、模型设计

采用3层全连接层构建神经网络

In[2]
import paddle.fluid as fluid
from paddle.fluid.dygraph import FC

class net(fluid.dygraph.Layer):
    def __init__(self,name_scope):
        super(net,self).__init__(name_scope)
        name_scope=self.full_name()
        self.hidden1=FC(name_scope,size=100,act='relu')#隐藏层1,全连接层,输出大小为100,激活函数为relu
        self.hidden2=FC(name_scope,size=100,act='relu')#隐藏层2,全连接层,输出大小为100,激活函数为relu
        self.fc=FC(name_scope,size=10,act='softmax')#输出层,全连接层,输出大小为10,对应结果的十个类别,激活函数为softmax
            
    def forward(self,x):
        x=self.hidden1(x)
        x=self.hidden2(x)
        y=self.fc(x)
        return y
 

三、模型训练与保存

迭代次数为2,训练的batch_size为16,测试的batch_size为32,优化器选取SGD,学习率为0.001

In[3]
with fluid.dygraph.guard():
    model=net('mnist')#模型实例化
    model.train()#训练模式
    opt=fluid.optimizer.SGDOptimizer(learning_rate=0.001)#优化器选用SGD随机梯度下降,学习率为0.001
    epochs_num=2#迭代次数为2
    for pass_num in range(epochs_num):
        
        for batch_id,data in enumerate(train_reader()):
            images=np.array([x[0].reshape(1, 28, 28) for x in data],np.float32)
            labels=np.array([x[1] for x in data]).astype('int64').reshape(-1,1)
            
            # print(labels)
            image=fluid.dygraph.to_variable(images)
            label=fluid.dygraph.to_variable(labels)
            
            predict=model(image)#预测
            
            loss=fluid.layers.cross_entropy(predict,label)
            avg_loss=fluid.layers.mean(loss)#获取loss值
            
            acc=fluid.layers.accuracy(predict,label)#计算精度
            
            if batch_id!=0 and batch_id%1000==0:
                print("pass:{},batch_id:{},train_loss:{},train_acc:{}".format(pass_num,batch_id,avg_loss.numpy(),acc.numpy()))
            
            avg_loss.backward()
            opt.minimize(avg_loss)
            model.clear_gradients()
        for batch_id,data in enumerate(test_reader()):#测试集
            images=np.array([x[0].reshape(1, 28, 28) for x in data],np.float32)
            labels=np.array([x[1] for x in data]).astype('int64').reshape(-1,1)
            
            # print(labels)
            image=fluid.dygraph.to_variable(images)
            label=fluid.dygraph.to_variable(labels)
            
            predict=model(image)
            
            loss=fluid.layers.cross_entropy(predict,label)
            avg_loss=fluid.layers.mean(loss)
            
            acc=fluid.layers.accuracy(predict,label)
            
            if batch_id!=0 and batch_id%300==0:
                print("pass:{},batch_id:{},test_loss:{},test_acc:{}".format(pass_num,batch_id,avg_loss.numpy(),acc.numpy()))
            
            
    fluid.save_dygraph(model.state_dict(),'mnist')#保存模型
pass:0,batch_id:1000,train_loss:[1.0861783],train_acc:[0.6875]
pass:0,batch_id:2000,train_loss:[0.40404856],train_acc:[0.9375]
pass:0,batch_id:3000,train_loss:[0.42510095],train_acc:[0.9375]
pass:0,batch_id:300,test_loss:[0.34076428],test_acc:[0.90625]
pass:1,batch_id:1000,train_loss:[0.37576067],train_acc:[0.875]
pass:1,batch_id:2000,train_loss:[0.16800977],train_acc:[1.]
pass:1,batch_id:3000,train_loss:[0.30528328],train_acc:[0.9375]
pass:1,batch_id:300,test_loss:[0.23094185],test_acc:[0.9375]
 

四、预测

加载待预测的图片进行预测

In[7]
import paddle.fluid as fluid
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

def load_image(file):
    img=Image.open(file).convert('L')#以灰度图的方式读取待测图片
    img=img.resize((28,28),Image.ANTIALIAS)#调整大小
    img=np.array(img).reshape(1,1,28,28).astype('float32')
    img=img/255*2.0-1.0#归一化
    return img

infer_path_3='data/data1910/infer_3.png'#数字3
infer_path_9='data/data2195/infer_9.jpg'#数字9
#显示待预测的图片
image=Image.open(infer_path_3)
plt.imshow(image)
plt.show()

#构建预测动态图过程
with fluid.dygraph.guard():
    model=net('mnist')#模型实例化
    model_dict,_=fluid.load_dygraph('mnist')
    model.load_dict(model_dict)#加载模型参数
    model.#评估模式
    img=load_image(infer_path_3)
    img=fluid.dygraph.to_variable(img)#将np数组转换为dygraph动态图的variable
    result=model(img)
    print('预测的结果是:{}'.format(np.argmax(result.numpy())))
    
预测的结果是:3
 

请点击此处查看本环境基本用法. 
Please click here for more detailed instructions.

 点击链接,使用AI Studio一键上手实践项目吧:https://aistudio.baidu.com/aistudio/projectdetail/291073


注意:本文归作者所有,未经作者允许,不得转载

全部评论: 0

    我有话说: