pytorch 用卷积神经网络做数字识别

pytorch 用卷积神经网络做数字识别

虽然说用卷积来做数字识别实在是大材小用,但用来学习pytorch和卷积神经网络还是不错的。(ps:还是我的电脑太low)
附上我的网络:

class CNN(nn.Module):
    def __init__(self,in_dim,n_class):
        super(CNN,self).__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(in_dim,6,kernel_size=3,stride=1,padding=1),
            # input shape(1*28*28),(28+1*2-3)/1+1=28 卷积后输出(6*28*28)
            # 输出图像大小计算公式:(n*n像素的图)(n+2p-k)/s+1
            nn.ReLU(True),        # 激活函数
            nn.MaxPool2d(2,2),    # 28/2=14 池化后(6*14*14)
            nn.Conv2d(6,16,5,stride=1,padding=0),  # (14-5)/1+1=10 卷积后(16*10*10)
            nn.ReLU(True),
            nn.MaxPool2d(2,2)    #池化后(16*5*5)=400,the input of full connection
        )
        self.fc = nn.Sequential(   #full connection layers.
            nn.Linear(400,120),
            nn.Linear(120,84),
            nn.Linear(84,n_class)
        )

    def forward(self, x):
        out = self.conv(x)                  #out shape(batch,16,5,5)
        out = out.view(out.size(0), -1)     #out shape(batch,400)
        out = self.fc(out)                  #out shape(batch,10)
        return out


net = CNN(1, 10)
print(net)

方便管理我将代码分为train.py test.py tester.py 你问我tester.py是啥。我随便写个数字来做一下检验。如下:

在这里插入图片描述
图片处理过程:

input_image = 'D:/4.png'
im = Image.open(input_image).resize((28, 28))     #取图片数据
im = im.convert('L') #灰度图
im_data = np.array(im)
im_data = torch.from_numpy(im_data).float()
im_data = im_data.view(1, 1, 28, 28)

识别结果:
在这里插入图片描述
附上我的全部代码链接:github

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值