对于FAMNIST中的十种动物和水果进行识别测试
简 介: 对于有五中动物和五中水果组成的FAMNIST10(Fruit-Animal MNSIT数据集合),利用LeNet进行识别进行了测试。测试了不同的卷积核尺寸,不同的学习速率以及不同的数据集合的情况。通过测试也可以看到利用AI Studio中的GPU加速环境(至尊版环境)可以在训练模型过程中大大提高训练的效率。
关键词
: LeNet,FAMNIST
Contents
famnist10-64-gray
§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 zipfilefilename = '/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 cv2import 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 RAMParam 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_samplesdef __getitem__(self, index):data = imgdata[index]/255label = 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 = 5plt.figure(figsize=(10,6))for j in range(ROW_NUM):for i in range(COL_NUM):id = j*COL_NUM + iimg = 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)//2class 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, 64]] [100, 6, 60, 60] 156 MaxPool2D-1 [[100, 6, 60, 60]] [100, 6, 30, 30] 0 Conv2D-2 [[100, 6, 30, 30]] [100, 16, 26, 26] 2,416 MaxPool2D-2 [[100, 16, 26, 26]] [100, 16, 13, 13] 0 Linear-1 [[100, 2704]] [100, 120] 324,600 Linear-2 [[100, 120]] [100, 86] 10,406 Linear-3 [[100, 86]] [100, 10] 870
===========================================================================
Total params: 338,448
Trainable params: 338,448
Non-trainable params: 0
---------------------------------------------------------------------------
Input size (MB): 1.56
Forward/backward pass size (MB): 31.08
Params size (MB): 1.29
Estimated Total Size (MB): 33.93
---------------------------------------------------------------------------{'total_params': 338448, 'trainable_params': 338448}
2.1.2 训练结果
(1)训练速度
- 训练时间为:16.285秒。
▲ 图2.1.1 训练过程中的Loss与Accuracy的变化曲线
strid = 5
tspgetdopstring(-strid)
strall = clipboard.paste().split('\r\n')accdim = []
lossdim = []
for s in strall:ss = s.split(',')acc = ss[-1].replace('Acc:[','').replace(']','')loss = ss[1].replace('Loss:[','').replace(']','')accdim.append(float(acc.strip(' ')))lossdim.append(float(loss))printf(accdim)plt.plot(accdim, label='Accuracy')
plt.plot(lossdim, label='Loss')
plt.xlabel("Step")
plt.ylabel("Accuracy")
plt.legend(loc='upper right')
plt.grid(True)
plt.title("Train Accuracy")
plt.tight_layout()
plt.show()
2.1.3 不同尺寸的网络结构
(1)卷积核边长为3
---------------------------------------------------------------------------Layer (type) Input Shape Output Shape Param #
===========================================================================Conv2D-3 [[100, 1, 64, 64]] [100, 6, 62, 62] 60 MaxPool2D-3 [[100, 6, 62, 62]] [100, 6, 31, 31] 0 Conv2D-4 [[100, 6, 31, 31]] [100, 16, 29, 29] 880 MaxPool2D-4 [[100, 16, 29, 29]] [100, 16, 14, 14] 0 Linear-4 [[100, 3136]] [100, 120] 376,440 Linear-5 [[100, 120]] [100, 86] 10,406 Linear-6 [[100, 86]] [100, 10] 870
===========================================================================
Total params: 388,656
Trainable params: 388,656
Non-trainable params: 0
---------------------------------------------------------------------------
Input size (MB): 1.56
Forward/backward pass size (MB): 34.82
Params size (MB): 1.48
Estimated Total Size (MB): 37.86
---------------------------------------------------------------------------
kernal_size = 3
训练时间: 10.673秒
▲ 图2.1.2 Loss与Accuracy曲线
(2)卷积核变成为4
---------------------------------------------------------------------------Layer (type) Input Shape Output Shape Param #
===========================================================================Conv2D-11 [[100, 1, 64, 64]] [100, 6, 61, 61] 102 MaxPool2D-11 [[100, 6, 61, 61]] [100, 6, 30, 30] 0 Conv2D-12 [[100, 6, 30, 30]] [100, 16, 27, 27] 1,552 MaxPool2D-12 [[100, 16, 27, 27]] [100, 16, 13, 13] 0 Linear-16 [[100, 2704]] [100, 120] 324,600 Linear-17 [[100, 120]] [100, 86] 10,406 Linear-18 [[100, 86]] [100, 10] 870
===========================================================================
Total params: 337,530
Trainable params: 337,530
Non-trainable params: 0
---------------------------------------------------------------------------
Input size (MB): 1.56
Forward/backward pass size (MB): 32.28
Params size (MB): 1.29
Estimated Total Size (MB): 35.13
---------------------------------------------------------------------------
▲ 图A2.1.3 Loss 与Accuracy曲线
(3)卷积核边长为7
训练时间:10.529秒
---------------------------------------------------------------------------Layer (type) Input Shape Output Shape Param #
===========================================================================Conv2D-5 [[100, 1, 64, 64]] [100, 6, 58, 58] 300 MaxPool2D-5 [[100, 6, 58, 58]] [100, 6, 29, 29] 0 Conv2D-6 [[100, 6, 29, 29]] [100, 16, 23, 23] 4,720 MaxPool2D-6 [[100, 16, 23, 23]] [100, 16, 11, 11] 0 Linear-7 [[100, 1936]] [100, 120] 232,440 Linear-8 [[100, 120]] [100, 86] 10,406 Linear-9 [[100, 86]] [100, 10] 870
===========================================================================
Total params: 248,736
Trainable params: 248,736
Non-trainable params: 0
---------------------------------------------------------------------------
Input size (MB): 1.56
Forward/backward pass size (MB): 27.35
Params size (MB): 0.95
Estimated Total Size (MB): 29.86
---------------------------------------------------------------------------
▲ 图2.1.3 Loss与Accuracy曲线
(4)卷积核边长为2
---------------------------------------------------------------------------Layer (type) Input Shape Output Shape Param #
===========================================================================Conv2D-9 [[100, 1, 64, 64]] [100, 6, 63, 63] 30 MaxPool2D-9 [[100, 6, 63, 63]] [100, 6, 31, 31] 0 Conv2D-10 [[100, 6, 31, 31]] [100, 16, 30, 30] 400 MaxPool2D-10 [[100, 16, 30, 30]] [100, 16, 15, 15] 0 Linear-13 [[100, 3600]] [100, 120] 432,120 Linear-14 [[100, 120]] [100, 86] 10,406 Linear-15 [[100, 86]] [100, 10] 870
===========================================================================
Total params: 443,826
Trainable params: 443,826
Non-trainable params: 0
---------------------------------------------------------------------------
Input size (MB): 1.56
Forward/backward pass size (MB): 36.47
Params size (MB): 1.69
Estimated Total Size (MB): 39.72
---------------------------------------------------------------------------
▲ 图A2.1.4 Loss 与Accuracy曲线
(5)卷积核边长为1
---------------------------------------------------------------------------Layer (type) Input Shape Output Shape Param #
===========================================================================Conv2D-7 [[100, 1, 64, 64]] [100, 6, 64, 64] 12 MaxPool2D-7 [[100, 6, 64, 64]] [100, 6, 32, 32] 0 Conv2D-8 [[100, 6, 32, 32]] [100, 16, 32, 32] 112 MaxPool2D-8 [[100, 16, 32, 32]] [100, 16, 16, 16] 0 Linear-10 [[100, 4096]] [100, 120] 491,640 Linear-11 [[100, 120]] [100, 86] 10,406 Linear-12 [[100, 86]] [100, 10] 870
===========================================================================
Total params: 503,040
Trainable params: 503,040
Non-trainable params: 0
---------------------------------------------------------------------------
Input size (MB): 1.56
Forward/backward pass size (MB): 39.23
Params size (MB): 1.92
Estimated Total Size (MB): 42.71
---------------------------------------------------------------------------
▲ 图2.1.4 Loss与Accuracy曲线
2.1.4 不同的学习速率
选择卷积核为5的情况下,测试不同的学习速率对于训练结果的影响。
(1)rate=0.1
▲ 图2.1.7 Loss and Accuracy
(2)0.01
▲ 图2.1.8 Loss and Accuracy
(3)0.001
▲ 图2.1.9 Loss and Accuracy
(4)rate=0.0001
▲ 图2.1.10 Loss and Accuracy
(5)0.00001
▲ 图2.1.11 Loss and Accuracy
2.1.5 细分不同学习速率下的收敛情况
plt.figure(figsize=(10, 6))
EPOCH_NUM = 100for i in linspace(1, 4, 20):lr = 1/exp((i+1)*log(10))net = mnist()net.__init__()optimizer = paddle.optimizer.Adam(learning_rate=lr, parameters=net.parameters())lossdim = []accdim = []print('Step:%d, LR:%0.6f'%(i+1, lr))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 epoch %10==0:print("Pass:{}, Loss:{}, Acc:{}".format(epoch,loss.numpy(), acc.numpy()))lossdim.append(loss.numpy())accdim.append(acc.numpy())plt.plot(accdim, label='lr:%.6f'%lr)plt.xlabel("Step")plt.ylabel("Accuracy")plt.legend(loc='upper right')plt.grid(True)plt.tight_layout()plt.show()
▲ 图2.1.12 不同学习速率对应的收敛情况
▲ 图2.1.13 不同学习速率对应的收敛情况
▲ 图2.1.14 不同学习速率对应的收敛情况
§03 测试LeNet网络
3.1 在CPU下训练网络
- 训练参数:
-
ks=5
:
lr=0.01
:
训练时间
:75.17s
训练样本
:906
3.1.1 网络结构
---------------------------------------------------------------------------Layer (type) Input Shape Output Shape Param #
===========================================================================Conv2D-5 [[100, 1, 64, 64]] [100, 6, 60, 60] 156 MaxPool2D-5 [[100, 6, 60, 60]] [100, 6, 30, 30] 0 Conv2D-6 [[100, 6, 30, 30]] [100, 16, 26, 26] 2,416 MaxPool2D-6 [[100, 16, 26, 26]] [100, 16, 13, 13] 0 Linear-7 [[100, 2704]] [100, 120] 324,600 Linear-8 [[100, 120]] [100, 86] 10,406 Linear-9 [[100, 86]] [100, 10] 870
===========================================================================
Total params: 338,448
Trainable params: 338,448
Non-trainable params: 0
---------------------------------------------------------------------------
Input size (MB): 1.56
Forward/backward pass size (MB): 31.08
Params size (MB): 1.29
Estimated Total Size (MB): 33.93
---------------------------------------------------------------------------
3.1.2 训练收敛情况
▲ 图3.1.1 Loss and Accuracy
3.2 测试网络
data = train_loader().next()
out = net(data[0][:30])
out1 = paddle.argmax(out, axis=1).numpy()print(out1)
print(data[1][:30].numpy().T.flatten())
[0 9 9 0 5 7 2 4 4 8 0 6 2 7 4 7 4 8 2 6 4 6 3 9 4 1 4 7 3 2]
[0 9 9 0 5 7 2 4 4 8 0 6 2 7 4 7 4 8 2 6 4 6 3 9 4 1 4 7 3 2]
§04 测试FAMNIST10
4.1 测试FAMNIST10-Gray
4.1.1 网络结构
---------------------------------------------------------------------------Layer (type) Input Shape Output Shape Param #
===========================================================================Conv2D-7 [[100, 1, 32, 32]] [100, 6, 28, 28] 156 MaxPool2D-7 [[100, 6, 28, 28]] [100, 6, 14, 14] 0 Conv2D-8 [[100, 6, 14, 14]] [100, 16, 10, 10] 2,416 MaxPool2D-8 [[100, 16, 10, 10]] [100, 16, 5, 5] 0 Linear-10 [[100, 400]] [100, 120] 48,120 Linear-11 [[100, 120]] [100, 86] 10,406 Linear-12 [[100, 86]] [100, 10] 870
===========================================================================
Total params: 61,968
Trainable params: 61,968
Non-trainable params: 0
---------------------------------------------------------------------------
Input size (MB): 0.39
Forward/backward pass size (MB): 6.18
Params size (MB): 0.24
Estimated Total Size (MB): 6.80
---------------------------------------------------------------------------
4.1.2 训练结果
▲ 图4.1.1 收敛情况
可以看到LeNet对于FAMNIST10也同样具有很好的识别特性。
4.2 测试FAMNIST10
4.2.1 网络结构
---------------------------------------------------------------------------Layer (type) Input Shape Output Shape Param #
===========================================================================Conv2D-15 [[100, 3, 32, 32]] [100, 6, 28, 28] 456 MaxPool2D-15 [[100, 6, 28, 28]] [100, 6, 14, 14] 0 Conv2D-16 [[100, 6, 14, 14]] [100, 16, 10, 10] 2,416 MaxPool2D-16 [[100, 16, 10, 10]] [100, 16, 5, 5] 0 Linear-22 [[100, 400]] [100, 120] 48,120 Linear-23 [[100, 120]] [100, 86] 10,406 Linear-24 [[100, 86]] [100, 10] 870
===========================================================================
Total params: 62,268
Trainable params: 62,268
Non-trainable params: 0
---------------------------------------------------------------------------
Input size (MB): 1.17
Forward/backward pass size (MB): 6.18
Params size (MB): 0.24
Estimated Total Size (MB): 7.59
---------------------------------------------------------------------------
4.2.2 训练结果
▲ 图4.2.1 训练收敛情况
4.3 测试原始图像
- 原始图像参数:
-
尺寸
:283×283
色彩
:彩色图像
在AI Studio 至尊版环境下,花费了88.68秒训练完毕。
4.3.1 网络结构
----------------------------------------------------------------------------Layer (type) Input Shape Output Shape Param #
============================================================================Conv2D-1 [[100, 3, 283, 283]] [100, 6, 279, 279] 456 MaxPool2D-1 [[100, 6, 279, 279]] [100, 6, 139, 139] 0 Conv2D-2 [[100, 6, 139, 139]] [100, 16, 135, 135] 2,416 MaxPool2D-2 [[100, 16, 135, 135]] [100, 16, 67, 67] 0 Linear-1 [[100, 71824]] [100, 120] 8,619,000 Linear-2 [[100, 120]] [100, 86] 10,406 Linear-3 [[100, 86]] [100, 10] 870
============================================================================
Total params: 8,633,148
Trainable params: 8,633,148
Non-trainable params: 0
----------------------------------------------------------------------------
Input size (MB): 91.65
Forward/backward pass size (MB): 722.21
Params size (MB): 32.93
Estimated Total Size (MB): 846.80
----------------------------------------------------------------------------
+--------------+---------------------+---------------------+---------+------------+
| Layer Name | Input Shape | Output Shape | Params | Flops |
+--------------+---------------------+---------------------+---------+------------+
| conv2d_0 | [100, 3, 283, 283] | [100, 6, 279, 279] | 456 | 3549549568 |
| conv2d_1 | [100, 6, 139, 139] | [100, 16, 135, 135] | 2416 | 4403160064 |
| max_pool2d_0 | [100, 6, 279, 279] | [100, 6, 139, 139] | 0 | 0 |
| max_pool2d_1 | [100, 16, 135, 135] | [100, 16, 67, 67] | 0 | 0 |
| linear_0 | [100, 71824] | [100, 120] | 8619000 | 861888000 |
| linear_1 | [100, 120] | [100, 86] | 10406 | 1032000 |
| linear_2 | [100, 86] | [100, 10] | 870 | 86000 |
+--------------+---------------------+---------------------+---------+------------+
Total Flops: 8815715632 Total Params: 8633148
4.3.2 训练情况
▲ 图4.3.1 网络收敛情况
※ 测试总结 ※
对于有五中动物和五中水果组成的FAMNIST10(Fruit-Animal MNSIT数据集合),利用LeNet进行识别进行了测试。测试了不同的卷积核尺寸,不同的学习速率以及不同的数据集合的情况。
通过测试也可以看到利用AI Studio中的GPU加速环境(至尊版环境)可以在训练模型过程中大大提高训练的效率。
■ 相关文献链接:
- 404
- 第十六届全国大学智能汽车竞赛竞速比赛规则
- 第十六届智能汽车竞赛AI视觉组分赛区数据集发布
- AI Studio FAMNIST
● 相关图表链接:
- 图2.1.1 训练过程中的Loss与Accuracy的变化曲线
- 图2.1.2 Loss与Accuracy曲线
- 图A2.1.3 Loss 与Accuracy曲线
- 图2.1.3 Loss与Accuracy曲线
- 图A2.1.4 Loss 与Accuracy曲线
- 图2.1.4 Loss与Accuracy曲线
- 图2.1.7 Loss and Accuracy
- 图2.1.8 Loss and Accuracy
- 图2.1.9 Loss and Accuracy
- 图2.1.10 Loss and Accuracy
- 图2.1.11 Loss and Accuracy
- 图2.1.12 不同学习速率对应的收敛情况
- 图2.1.13 不同学习速率对应的收敛情况
- 图2.1.14 不同学习速率对应的收敛情况
- 图3.1.1 Loss and Accuracy
- 图4.1.1 收敛情况
- 图4.2.1 训练收敛情况
- 图4.3.1 网络收敛情况
一、实验程序
1、解压缩数据文件
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# UNZIP.PY -- by Dr. ZhuoQing 2021-12-16
#
# Note:
#============================================================from headm import * # =
import zipfilefilename = '/home/aistudio/data/famnist.zip'
outpath = '/home/aistudio/data'with zipfile.ZipFile(filename) as zfile:zfile.extractall(outpath)printf("Zip file extract to dir : %s"%outpath)#------------------------------------------------------------
# END OF FILE : UNZIP.PY
#============================================================
2、绘制训练曲线
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# DRAW.PY -- by Dr. ZhuoQing 2021-12-16
#
# Note:
#============================================================
from headm import *
strid = 8
#tspgetdopstring(-strid)
tspsendwindowkey('BML CodeLab', "c", control=1)
strall = clipboard.paste().split('\r\n')
accdim = []
lossdim = []
for s in strall:ss = s.split(',')acc = ss[-1].replace('Acc:[','').replace(']','')loss = ss[1].replace('Loss:[','').replace(']','')accdim.append(float(acc.strip(' ')))lossdim.append(float(loss))
printf(accdim)
plt.plot(accdim, label='Accuracy')
plt.plot(lossdim, label='Loss')
plt.xlabel("Step")
plt.ylabel("Accuracy")
plt.legend(loc='upper right')
plt.grid(True)
plt.title("Train Accuracy")
plt.tight_layout()
plt.show()
#------------------------------------------------------------
# END OF FILE : DRAW.PY
#============================================================
3、实验程序
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# LENET.PY -- by Dr. ZhuoQing 2021-12-16
#
# Note:
#============================================================from headm import * # =
import cv2import 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 = 'famnist-all'#------------------------------------------------------------
def loadimgdata(imgdir):'''loadimgdata: Load test image data into RAMParam 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)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_samplesdef __getitem__(self, index):data = imgdata[index]/255label = imglabel[index]return TT(data, dtype='float32'), TT(label, dtype='int64')def __len__(self):return self.num_samples_dataset = famnist(len(imglabel))
train_loader = paddle.io.DataLoader(_dataset, batch_size=100, shuffle=True)#------------------------------------------------------------#data = train_loader().next()
#print(data[0].numpy())'''ROW_NUM = 3
COL_NUM = 5plt.figure(figsize=(10,6))for j in range(ROW_NUM):for i in range(COL_NUM):id = j*COL_NUM + iimg = 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 = 283
ks = 5
in_channel=3
L = ((imageSize-ks+1)//2-ks+1)//2class mnist(paddle.nn.Layer):def __init__(self, ):super(mnist, self).__init__()self.conv1 = paddle.nn.Conv2D(in_channels=in_channel, 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.01, 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()))#------------------------------------------------------------
'''
plt.figure(figsize=(10, 6))
EPOCH_NUM = 100for i in linspace(0, 2, 20):lr = 1/exp((i+1)*log(10))net = mnist()net.__init__()optimizer = paddle.optimizer.Adam(learning_rate=lr, parameters=net.parameters())lossdim = []accdim = []printf('Step:%d, LR:%0.6f'%(i+1, lr))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 epoch %10==0:print("Pass:{}, Loss:{}, Acc:{}".format(epoch,loss.numpy(), acc.numpy()))lossdim.append(loss.numpy())accdim.append(acc.numpy())plt.plot(accdim, label='lr:%.6f'%lr)plt.xlabel("Step")plt.ylabel("Accuracy")plt.legend(loc='upper right')plt.grid(True)plt.tight_layout()plt.show()'''
#------------------------------------------------------------
paddle.save(net.state_dict(), './work/model3.pdparams')#------------------------------------------------------------
print(' ')
paddle.summary(net, input_size=(100,in_channel,imageSize,imageSize))paddle.flops(net, input_size=(100, in_channel, imageSize, imageSize), print_detail=True)#------------------------------------------------------------
'''
data = train_loader().next()
out = net(data[0][:30])
out1 = paddle.argmax(out, axis=1).numpy()printf(out1)
printf(data[1][:30].numpy().T.flatten())'''
#------------------------------------------------------------
# END OF FILE : LENET.PY
#============================================================
对于FAMNIST中的十种动物和水果进行识别测试相关推荐
- 利用LeNet识别十种动物和水果
简 介: ※利用经典的LeNet深度学习网络,可以完成对于智能车竞赛中智能视觉组对搬运物品分类的要求.虽然实际比赛中还会包含有五种交通工具,那么利用LeNet仍然是可以完成小类别的识别与定位的.仅仅使 ...
- 生活中的十种保养皮肤的食物
生活中的十种保养皮肤的食物 1.西兰花--它含有丰富的维生素A.维生素C和胡萝卜素,能增强皮肤的抗损伤能力.有助于保持皮肤弹性. 2.胡萝卜--胡萝卜素有助于维持皮肤细胞组织的正常机能.减少皮肤皱纹, ...
- Spring实例(IOC)——动物吃水果
Spring实例"IOC"--动物吃水果 一.问题:在控制台打印XX(动物)吃XX(水果) 二.解决方法 2.1.常规方法(new对象) 2.2.Spring-ioc(控制反转) ...
- 学数学建模算法对计算机的好处,数学建模中常见十种算法 (期末论文).doc
数学建模中常见十种算法 (期末论文) 数 学 系 毕 业 论 文 论文 (设计)题目: 数学建模中常见的十种算法 姓 名 黄小芬______ 学 号 100501313 专 业 数学与应用数学 班 级 ...
- 英文投稿过程中的十种状态
以下是英文投稿过程中的十种状态. 1. Submitted to Journal 当上传结束后,显示的状态是Submitted to Journal,这bai个状态是自然形成的无需处理. 2. Wit ...
- Animals-10 数据集下载,十种动物
kaggle上的一个数据集 十种动物 狗,猫,马,大象,蝴蝶,鸡,牛,羊,蜘蛛,松鼠 每个种类一两千图片, 也有四五千的. 格式.jpeg 官方下载地址:https://www.kaggle.com/ ...
- 【02】水果蔬菜识别系统-基于tensorflow2.3开发
------------------------------------------------2021年6月18日重大更新-------------------------------------- ...
- 水果数据集(Fruit-Dataset )+水果分类识别训练代码(支持googlenet, resnet, inception_v3, mobilenet_v2)
水果数据集(Fruit-Dataset )+水果分类识别训练代码(支持googlenet, resnet, inception_v3, mobilenet_v2) 目录 Fruit-Dataset水果 ...
- 基于卷积神经网络CNN的水果分类预测,卷积神经网络水果等级识别
目录 背影 卷积神经网络CNN的原理 卷积神经网络CNN的定义 卷积神经网络CNN的神经元 卷积神经网络CNN的激活函数 卷积神经网络CNN的传递函数 卷积神经网络CNN水果分类预测 基本结构 主要参 ...
最新文章
- C# WinForm 通过URL取得服务器上的某图片文件到本地
- JVM 监控以及内存分析
- Spark2.11 两种流操作 + Kafka
- .htaccess更改目录下的默认主页
- AD16更改器件封装如管脚间距等常规设置
- python具有可嵌入性_如何构建可嵌入Python
- Acrel-3200远程预付费电能管理系统的功能介绍
- 【小程序实战系列】电商平台源码及功能实现
- yahoo也推出站点统计了
- 斗地主牌型基本算法升级版本
- “校园知网”5月5日冲刺计划书
- C# 通过Http获取网页内容
- 建筑工程测量与测绘毕业论文范文
- html5语文答题制作,语文万能答题模板
- Event-triggered MPC Design for Distributed Systems With Network Communications
- python编程题12-计算并联电阻
- 环境变量配置以mysql为例
- 目前最值得收藏的100个各类资源站
- 使用Python修改可执行文件-EXE的图标
- 百度IFE2018任务--20-21天