对于FAMNIST中的十种动物和水果进行识别测试
Posted 卓晴
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对于FAMNIST中的十种动物和水果进行识别测试相关的知识,希望对你有一定的参考价值。
简 介: 对于有五中动物和五中水果组成的FAMNIST10(Fruit-Animal MNSIT数据集合),利用LeNet进行识别进行了测试。测试了不同的卷积核尺寸,不同的学习速率以及不同的数据集合的情况。通过测试也可以看到利用AI Studio中的GPU加速环境(至尊版环境)可以在训练模型过程中大大提高训练的效率。
关键词
: LeNet,FAMNIST
§01 准备数据
在 2021年人工神经网络第四次作业-第一题:LeNet识别水果与动物 制作了基于五种水果和五中动物的数据集合,总共907张图片。这个数据集合曾经是应用 第16届全国大学生智能车竞赛 中的 AI智能视觉组 的比赛数据集合。现在这个集合可以在 AI Studio FAMNIST 上下载。
1.1 解压缩文件
将FAMNIST.zip文件上载到AI Studio环境内,通过“加压缩文件”将famnist文件解压缩到 /data 子目录下。它包括有如下五个基本的数据集合:
├─famnist-all
├─famnist10
├─famnist10-64
├─famnist10-64-gray
└─famnist10-gray
1.1.1 解压缩程序
可以使用zipfile 软件包将zip文件进行解压缩。可以参谋如下的程序。
import sys,os,math,time
import matplotlib.pyplot as plt
from numpy import *
import zipfile
filename = '/home/aistudio/data/famnist.zip'
outpath = '/home/aistudio/data'
with zipfile.ZipFile(filename) as zfile:
zfile.extractall(outpath)
print("Zip file extract to dir : %s"%outpath)
§02 训练LeNet网络
2.1 处理famnist10-64-gray
2.1.1 LeNet程序
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# LENET.PY -- by Dr. ZhuoQing 2021-12-16
#
# Note:
#============================================================
from headm import * # =
import cv2
import paddle
import paddle.nn.functional as F
from paddle import to_tensor as TT
from paddle.nn.functional import square_error_cost as sqrc
#------------------------------------------------------------
famnist = '/home/aistudio/data/famnist'
imgdir = 'famnist10-64-gray'
#------------------------------------------------------------
def loadimgdata(imgdir):
'''
loadimgdata: Load test image data into RAM
Param imgdir: Directory for storing the image picture .
Return: imgdata,imglabel
'''
imgfile = os.listdir(imgdir)
imgdata = []
imglabel = []
for f in imgfile:
img = cv2.imread(os.path.join(imgdir, f))
imgdata.append(img.T[0][newaxis,:])
imglabel.append(int(f[:1]))
return array(imgdata), array(imglabel)
imgdata,imglabel = loadimgdata(os.path.join(famnist,imgdir))
print(len(imglabel))
print(imgdata.shape)
#------------------------------------------------------------
'''
printf(imgdata.shape)
printf(imglabel)
'''
#------------------------------------------------------------
class famnist(paddle.io.Dataset):
def __init__(self, num_samples):
super(famnist, self).__init__()
self.num_samples = num_samples
def __getitem__(self, index):
data = imgdata[index]/255
label = imglabel[index]
return TT(data, dtype='float32'), TT(label, dtype='int64')
def __len__(self):
return self.num_samples
_dataset = famnist(800)
train_loader = paddle.io.DataLoader(_dataset, batch_size=100, shuffle=True)
#------------------------------------------------------------
#data = train_loader().next()
#print(data[0].numpy())
'''
ROW_NUM = 3
COL_NUM = 5
plt.figure(figsize=(10,6))
for j in range(ROW_NUM):
for i in range(COL_NUM):
id = j*COL_NUM + i
img = data[0].numpy()[id][0].T
# print(img.shape)
plt.subplot(ROW_NUM, COL_NUM, id+1)
plt.imshow(img, cmap='gray')
plt.axis('off')
'''
#------------------------------------------------------------
imageSize = 64
ks = 5
L = ((imageSize-ks+1)//2-ks+1)//2
class mnist(paddle.nn.Layer):
def __init__(self, ):
super(mnist, self).__init__()
self.conv1 = paddle.nn.Conv2D(in_channels=1, out_channels=6, kernel_size=ks, stride=1, padding=0)
self.conv2 = paddle.nn.Conv2D(in_channels=6, out_channels=16, kernel_size=ks, stride=1, padding=0)
self.mp1 = paddle.nn.MaxPool2D(kernel_size=2, stride=2)
self.mp2 = paddle.nn.MaxPool2D(kernel_size=2, stride=2)
self.L1 = paddle.nn.Linear(in_features=16*L*L, out_features=120)
self.L2 = paddle.nn.Linear(in_features=120, out_features=86)
self.L3 = paddle.nn.Linear(in_features=86, out_features=10)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.mp1(x)
x = self.conv2(x)
x = F.relu(x)
x = self.mp2(x)
x = paddle.flatten(x, start_axis=1, stop_axis=-1)
x = self.L1(x)
x = F.relu(x)
x = self.L2(x)
x = F.relu(x)
x = self.L3(x)
return x
#------------------------------------------------------------
net = mnist()
EPOCH_NUM = 100
optimizer = paddle.optimizer.Adam(learning_rate=0.001, parameters=net.parameters())
for epoch in range(EPOCH_NUM):
for batchid, data in enumerate(train_loader()):
out = net(data[0])
loss = F.cross_entropy(out, data[1])
acc = paddle.metric.accuracy(out, data[1])
loss.backward()
optimizer.step()
optimizer.clear_grad()
if batchid %100==0:
print("Pass:, Loss:, Acc:".format(epoch,loss.numpy(), acc.numpy()))
#------------------------------------------------------------
paddle.save(net.state_dict(), './work/model.pdparams')
#------------------------------------------------------------
# END OF FILE : LENET.PY
#============================================================
(1)网络结构
paddle.summary(net, input_size=(100,1,64,64))
---------------------------------------------------------------------------
Layer (type) Input Shape Output Shape Param #
===========================================================================
Conv2D-1 [[100, 1, 64, 以上是关于对于FAMNIST中的十种动物和水果进行识别测试的主要内容,如果未能解决你的问题,请参考以下文章