Introduction to Convolutional Neural Network (CNN

For what is convolutional neural network (CNN), please refer to the materials for learning. If you are a beginner, I recommend the deep learning course of Li Hongyi from Taiwan. I’m not going to give you the links, but you can see them all over the web.

Although it is worth mentioning that CNN has an unstoppable momentum in image processing field, but it’s not only used in image processing field, known as alphaGo chess also can be treated by CNN’s structure, because the game has similarities with the image, so, CNN give us is a kind of idea to deal with problems, Some scholars have summed up three properties of the problems that can be solved by CNN:

Locality For an image, it is necessary to detect the features in the image to determine the category of the image. Usually, these features are not determined by the whole image, but by some local areas. For example, if a bird’s beak is detected in a certain part of a picture, it can be determined that there is a bird in the picture.

Homogeneity For different pictures, they have the same features, which will appear in different positions of the picture, that is to say, the same detection mode can be used to detect the same features of different pictures, but these features are in different positions of the picture, but the operation of feature detection is almost the same. For example, in different pictures, although the beak is in different positions, we can use the same pattern to detect it.

Invariance For an image, if we sample it down, the properties of the image remain roughly the same.

Verification code recognition principle Matlab image reading processing, remove noise points and shallow points, binarization, the image into a 0/1 matrix, so complete the pretreatment. Then the image should be cut to take the small picture position of each number and scale it to the same size for further processing by the neural network. Finally, the image is transformed into a format that can be recognized by the neural network, such as BP network, which is transformed into a row vector, and deep convolutional network, which is transformed into a matrix.

Ii. Source code

function varargout = interface(varargin)
% INTERFACE MATLAB code for interface.fig
%      INTERFACE, by itself, creates a new INTERFACE or raises the existing
%      singleton*.
%
%      H = INTERFACE returns the handle to a new INTERFACE or the handle to
%      the existing singleton*.
%
%      INTERFACE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in INTERFACE.M with the given input arguments.
%
%      INTERFACE('Property'.'Value',...). creates anew INTERFACE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before interface_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to interface_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one % instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
 
% Edit the above text to modify the response to help interface
global iteration;
global trainnet;
global recognet;
global testnet;
% Last Modified by GUIDE v2. 5 21-Jun- 2018. 12:51:57
 
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @interface_OpeningFcn, ...
                   'gui_OutputFcn',  @interface_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
% End initialization code - DO NOT EDIT
 
 
% --- Executes just before interface is made visible.
function interface_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to interface (see VARARGIN)
global iteration;
global trainnet;
global recognet;
global testnet;
 
iteration = 10;
trainnet = 'cnn_net_500';
 
 
cnnnetmat = dir(fullfile('*.mat'));
 
for i = 1:length(cnnnetmat)
    str{i} = cnnnetmat(i).name;
end
set(handles.popupmenu2,'String',str);
set(handles.popupmenu1,'String',str);
recognet = cnnnetmat(1).name;
testnet =  cnnnetmat(1).name;
% Choose default command line output for interface
handles.output = hObject;
 
% Update handles structure
guidata(hObject, handles);
 
% UIWAIT makes interface wait for user response (see UIRESUME)
% uiwait(handles.figure1);
 
 
% --- Outputs from this function are returned to the command line.
function varargout = interface_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
% Get default command line output from handles structure
varargout{1} = handles.output;
 
 
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global testnet;
[ err_rate ] = test_cnn( testnet );
set(handles.text3,'String',[num2str(err_rate*100) The '%']);
 
 
function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double
global iteration;
iteration = str2double(get(hObject,'String'));
 
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0.'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor'.'white');
end
 
 
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global iteration;
global trainnet;
train_cnn( iteration );
trainnetname = strcat(trainnet,'.mat');
dos(['rename' 32 'cnn_net.mat' 32 trainnetname]);
 
cnnnetmat = dir(fullfile('*.mat'));
 
for i = 1:length(cnnnetmat)
    str{i} = cnnnetmat(i).name;
end
set(handles.popupmenu2,'String',str);
set(handles.popupmenu1,'String',str);
 
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global tr_dir;
tr_dir = uigetdir({},'Select folder'); 
picture = dir(fullfile(tr_dir,'*. BMP, *. PNG'));
tr_dir = strcat(tr_dir,'\'); for i = 1:length(picture) str{i} = picture(i).name; end set(handles.listbox1,'String',str);
 
 
 
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1
global recognet;
global tr_dir;
    contents = cellstr(get(hObject,'String'));
    fn = contents{get(hObject,'Value')};
    fn = [tr_dir fn];
    [ result ] = recog_cnn( imread(fn) , recognet );
    set(handles.text6,'String',result);
    axes(handles.axes1);
    imshow(imread(fn));
 
 
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 
% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0.'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor'.'white');
end
 
 
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global recognet;
    [fn,impathname,~]=uigetfile('*.bmp'.'Select picture');
    fn=[impathname fn]; 
    [ result ] = recog_cnn( imread(fn) , recognet );
    set(handles.text6,'String',result);
    axes(handles.axes1);
    imshow(imread(fn));
 
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1
global testnet;
contents = cellstr(get(hObject,'String'));
testnet =  contents{get(hObject,'Value')};
 
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 
% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0.'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor'.'white');
end
 
 
 
function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double
global trainnet;
trainnet = get(hObject,'String');
 
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0.'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor'.'white');
end
 
 
% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2
global recognet;
contents = cellstr(get(hObject,'String'));
recognet =  contents{get(hObject,'Value')};
 
% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 
% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0.'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor'.'white');
end
Copy the code

3. Operation results

Fourth, note

Version: 2014 a