目录
1. Mnist数据集介绍
2. 训练步骤与结果
3. Matlab的GUI设计
4. 测试
5. Mnist数据集
6. GUI完整代码
Mnist数据集是美国国家标准与技术研究所发起整理的,其官网提供的数据集的下载包含四个文件:训练集、训练集标签、测试集、测试集标签
train-images-idx3-ubyte.gz: training set images (9912422 bytes)
train-labels-idx1-ubyte.gz: training set labels (28881 bytes)
t10k-images-idx3-ubyte.gz: test set images (1648877 bytes)
t10k-labels-idx1-ubyte.gz: test set labels (4542 bytes)
但是对于Matlab的deepNetworkDesigner训练模型不太方便,所以在此提供一个解析后的版本,其百度网盘链接放最后啦。数据集包括训练集和测试集,两部分分别包括0-9的手写数字图片。
使用Matlab的deepNetworkDesigner训练神经网络模型,该网络模型一共有7层。
① 输入层
因为数据集中所有图片均为28*28的黑白图片,所有输入层的大小为28*28*1,如图3所示。
② 卷积层
卷积层的滤波器为5*5的矩阵,梯度为1,经过卷积层后,输入图片变成24*24的矩阵。滤波器数量为32个。如图4所示。
③ 池化层
池化层为了减少数据量,选用图5所示的最大值池化层,大小为3*3,梯度为1,将24*24的图片缩小到8*8。
④ 归一化
归一化层对每个观察点的所有通道的小批数据进行独立归一化。
⑤ 全连接层
全连接层的输出大小为10,即0-9十个数字
⑥ 激活层
⑦ 输出层
完整网络模型如图10所示。
导入文件夹数据,设置训练参数,如图11所示。
训练结果如图12所示,验证准确度为:98.30%。
Matlab提供了很方便的GUI绘制app,在命令行中输入guide,打开GUI如图13所示。从左边的任务栏拖动插件到编辑区即可。设计好后点击保存即自动生成相应代码,只需要修改相应功能代码即可。
① 普通按钮
普通按钮如图14所示,拖到编辑区双击可以设置。主要设置参数如图15所示,从上到下依次是按钮背景颜色、按钮文本大小、按钮文本颜色、按钮文本内容、按钮标签。其中按钮标签是和程序对应起来的,每一个按钮标签对应一个函数。
本次设计主要需要三个普通按钮,分别为加载网络(Tag:pushbutton1)、识别(Tag:pushbutton2)和清空(Tag:pushbutton3)。
对应的程序如下:
%加载网络
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(~, ~, ~)
% hObject handle to pushbutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global network %训练的神经网络模型
[filename,filepath]=uigetfile({'*.mat'},'选择网络'); %打开文件夹,选择文件
if(isequal(filename,0)||isequal(filepath,0)) %如果取消就返回
return;
end
load([filepath,filename], 'trainedNetwork_1'); %加载神经网络
network = trainedNetwork_1;
%识别
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(~, ~, handles)
% hObject handle to pushbutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global network %训练的神经网络模型
h = getframe; %获得一帧坐标区图片
img = rgb2gray(h.cdata); %转灰度图
I = 255 - img; %转成黑色背景,白色字(同mnist数据集)
img = imresize(I,[28,28]); %修改图片大小(同mnist数据集)
[label,probs] = classify(network, img); %识别图片
result = num2str(100*max(probs),3); %图片相似度
res = strcat(result,"%");
set(handles.edit1,'string',res) %文本框中显示数字(edit1)
set(handles.edit2,'string',label) %文本框中显示相似度(edit2)
%清空
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(~, ~, handles)
% hObject handle to pushbutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
cla(handles.axes1); %清空坐标区
set(handles.edit1,'string','') %清空文本框1
set(handles.edit2,'string','') %清空文本框2
② 文本框
文本框如图17所示,其设置与按钮类似。程序不需要修改。
③ 静态文本
静态文本是提示和说明作用,在文本框右边。
④ 坐标区
坐标区如图18所示。在本次设计中坐标区主要作为手写板使用,其代码如下。
%手写板
function figure1_WindowButtonDownFcn(~, ~, handles)
global ButtonDown pos1
if strcmp(get(gcf, 'SelectionType'), 'normal')
ButtonDown = 1;
pos1 = get(handles.axes1, 'CurrentPoint');
end
function figure1_WindowButtonMotionFcn(~, ~, handles)
global ButtonDown pos1
if(ButtonDown == 1)
pos = get(handles.axes1,'CurrentPoint');
line([pos1(1,1) pos(1,1)], [pos1(1,2) pos(1,2)], 'Color','Black','LineStyle','-', 'LineWidth',8,'Marker','.', 'MarkerSize',20);
pos1 = pos;
end
function figure1_WindowButtonUpFcn(~, ~, ~)
global ButtonDown
ButtonDown = 0;
function axes1_ButtonDownFcn(~, ~, ~)
设计后的GUI如图19所示,其完整代码见最后。
https://pan.baidu.com/s/1Aabg7tNu6t4aAXnb1W-4MQ
提取码:vaa9
function varargout = Netgui(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Netgui_OpeningFcn, ...
'gui_OutputFcn', @Netgui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function Netgui_OpeningFcn(hObject, ~, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = Netgui_OutputFcn(~, ~, handles)
varargout{1} = handles.output;
%加载网络
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(~, ~, ~)
% hObject handle to pushbutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global network
[filename,filepath]=uigetfile({'*.mat'},'选择网络');
if(isequal(filename,0)||isequal(filepath,0))
return;
end
load([filepath,filename], 'trainedNetwork_1');
network = trainedNetwork_1;
%识别
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(~, ~, handles)
% hObject handle to pushbutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global network
h = getframe;
img = rgb2gray(h.cdata);
I = 255 - img;
img = imresize(I,[28,28]);
[label,probs] = classify(network, img);
result = num2str(100*max(probs),3);
res = strcat(result,"%");
set(handles.edit1,'string',res)
set(handles.edit2,'string',label)
%清空
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(~, ~, handles)
% hObject handle to pushbutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
cla(handles.axes1);
set(handles.edit1,'string','')
set(handles.edit2,'string','')
%输出框1
function edit1_Callback(~, ~, ~)
function edit1_CreateFcn(hObject, ~, ~)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%输出框2
function edit2_Callback(~, ~, ~)
function edit2_CreateFcn(hObject, ~, ~)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%手写板
function figure1_WindowButtonDownFcn(~, ~, handles)
global ButtonDown pos1
if strcmp(get(gcf, 'SelectionType'), 'normal')
ButtonDown = 1;
pos1 = get(handles.axes1, 'CurrentPoint');
end
function figure1_WindowButtonMotionFcn(~, ~, handles)
global ButtonDown pos1
if(ButtonDown == 1)
pos = get(handles.axes1,'CurrentPoint');
line([pos1(1,1) pos(1,1)], [pos1(1,2) pos(1,2)], 'Color','Black','LineStyle','-', 'LineWidth',8,'Marker','.', 'MarkerSize',20);
pos1 = pos;
end
function figure1_WindowButtonUpFcn(~, ~, ~)
global ButtonDown
ButtonDown = 0;
function axes1_ButtonDownFcn(~, ~, ~)