Classify the type of football! Image Classification | Kaggle
%Code to train the model:
clc;
clear all;
close all;
warning off;
net=alexnet;
layers=net.Layers;
featureLayer='fc7';
imds=imageDatastore('Training','IncludeSubFolders',true,'LabelSource','foldernames');
trainingLabels=imds.Labels;
inputSize = net.Layers(1).InputSize;
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imds);
featuresTrain = activations(net,augimdsTrain,featureLayer,'OutputAs','rows');
classifier=fitcecoc(featuresTrain,trainingLabels);
save classifier classifier
%Code to test the model:
clc
clear all
close all
warning off
load classifier;
net=alexnet;
featureLayer='fc7';
[filename, pathname] = uigetfile('*.*', 'Pick a MATLAB
code file');
if isequal(filename,0) || isequal(pathname,0)
disp('User pressed
cancel')
else
filename=strcat(pathname,filename);
ab=imread(filename);
ab=imresize(ab,[227
227]);
featuresTrain =
activations(net,ab,featureLayer,'OutputAs','rows');
gs=predict(classifier,featuresTrain);
disp(char(gs));
end
function varargout = Football(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn',
@Football_OpeningFcn, ...
'gui_OutputFcn', @Football_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 Football is made visible.
function Football_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 Football (see VARARGIN)
% Choose default command line output for Football
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Football wait for user response (see
UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command
line.
function varargout = Football_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 Load_the_SVM_Classifier.
function Load_the_SVM_Classifier_Callback(hObject, eventdata, handles)
% hObject handle to
Load_the_SVM_Classifier (see GCBO)
% eventdata reserved
- to be defined in a future version of MATLAB
% handles structure
with handles and user data (see GUIDATA)
net=alexnet;
featureLayer='fc7';
load classifier;
handles.classifier = classifier;
handles.net = alexnet;
handles.featureLayer=featureLayer;
% Update handles structure
guidata(hObject, handles);
function Outcome_Callback(hObject, eventdata, handles)
% hObject handle to
Outcome (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 Outcome
as text
%
str2double(get(hObject,'String')) returns contents of Outcome as a
double
% --- Executes during object creation, after setting all
properties.
function Outcome_CreateFcn(hObject, eventdata, handles)
% hObject handle to
Outcome (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 Select_an_Image.
function Select_an_Image_Callback(hObject, eventdata, handles)
% hObject handle to
Select_an_Image (see GCBO)
% eventdata reserved
- to be defined in a future version of MATLAB
% handles structure
with handles and user data (see GUIDATA)
net=handles.net;
featureLayer=handles.featureLayer;
classifier=handles.classifier;
[filename, pathname] = uigetfile('*.*', 'Pick a MATLAB
code file');
filename=strcat(pathname,filename);
ab=imread(filename);
handles.axes1();
imshow(ab);
ab=imresize(ab,[227 227]);
featuresTrain = activations(net,ab,featureLayer,'OutputAs','rows');
gs=predict(classifier,featuresTrain);
bs=string(gs);
set(handles.Outcome,'String',bs);
No comments