Iklan

Latest Post


Adaptive Neuro-Fuzzy Inference System (ANFIS)

Bhumi Literasi
Jumat, 13 November 2020, November 13, 2020 WIB Last Updated 2023-03-24T02:13:05Z

Adaptive Neuro-Fuzzy Inference System (ANFIS) merupakan jaringan syaraf adaptif yang berbasis pada sistem kesimpulan fuzzy (Fuzzy Inference System). Dengan menggunakan metode pembelajaran hybrid, ANFIS dapat memetakan nilai masukan menuju nilai keluaran berdasarkan pada pengetahuan yang dilatihkan dalam bentuk aturan fuzzy.

Berikut merupakan contoh aplikasi pemrograman MATLAB untuk mengklasifikasi citra daun ke dalam 4 buah kelas (A, B, C, dan D) menggunakan algoritma ANFIS. Pada contoh ini digunakan 40 citra daun yang terdiri dari 10 citra pada masing-masing kelas. Citra tersebut dibagi menjadi dua bagian yaitu sebanyak 28 citra untuk data pelatihan dan 12 citra untuk data pengujian. Contoh citra daun yang digunakan ditunjukkan pada gambar di bawah ini:

Langkah-langkah pemrograman-nya adalah sebagai berikut:
1. Mempersiapkan citra latih untuk proses pelatihan

2. Melakukan pelatihan algoritma ANFIS untuk mengklasifikasikan citra daun. Ciri yang digunakan untuk membedakan antara kelas daun yang satu dengan yang lain adalah ciri morfologi dan ciri tekstur. Ciri morfologi yang digunakan yaitu metric dan eccentricity. Sedangkan ciri tekstur yang digunakan yaitu contrast, correlation, energy, dan homogeneity. Keenam ciri tersebut digunakan sebagai masukan dalam algoritma ANFIS untuk mengklasifikasikan citra daun ke dalam 4 buah kelas. Koding program pelatihan adalah sebagai berikut:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
%
% Website: https://rizalmutaqin.com/
% Email  :
 
clc; clear; close all; warning off all;
 
% menetapkan nama folder pelatihan
image_folder = 'data latih';
% membaca nama file yang berekstensi .tif
filenames = dir(fullfile(image_folder, '*.tif'));
% membaca jumlah file
total_images = numel(filenames);
% menginisialisasi variabel feature
feature = zeros(total_images,6);
% untuk semua file yang dibaca
for n = 1:total_images
    % menggabungkan lokasi folder dengan nama file
    full_name = fullfile(image_folder, filenames(n).name);
    % membaca file citra
    I = imread(full_name);
    % melakukan segmentasi citra menggunakan metode otsu thresholding
    Img = imbinarize(I);    % untuk matlab dengan versi sebelum tahun 2016 gunakan perintah >> Img = im2bw(I,graythresh(I));
    % melakukan komplemen citra
    Img = imcomplement(Img);
    % operasi morfologi filling holes untuk menutup lubang pada objek
    Img = imfill(Img,'holes');
    % operasi morfologi area opening untuk menghilangkan noise
    Img = bwareaopen(Img,1000);
    % ekstraksi ciri morfologi
    stats = regionprops(Img,'All');
    area = stats.Area;
    perimeter = stats.Perimeter;
    metric = 4*pi*area/(perimeter^2);
    eccentricity = stats.Eccentricity;
    % ekstraksi ciri tekstur
    GLCM = graycomatrix(I,'Offset',[0 1; -1 1; -1 0; -1 -1]);
    stats = graycoprops(GLCM,{'contrast','correlation','energy','homogeneity'});
    contrast = mean(stats.Contrast);
    correlation = mean(stats.Correlation);
    energy = mean(stats.Energy);
    homogeneity = mean(stats.Homogeneity);
    % mengisi variabel feature dengan nilai hasil ekstraksi ciri
    feature(n,1) = metric;
    feature(n,2) = eccentricity;
    feature(n,3) = contrast;
    feature(n,4) = correlation;
    feature(n,5) = energy;
    feature(n,6) = homogeneity;
end
 
% menetapkan target pelatihan
target = zeros(total_images,1);
target(1:7,:) = 1;      % Daun A
target(8:14,:) = 2;     % Daun B
target(15:21,:) = 3;    % Daun C
target(22:28,:) = 4;    % Daun D
% menyusun data pelatihan
trnData = [feature,target];
% menetapkan parameter pelatihan anfis
numMFs = 2;
mfType = 'trapmf';
error_goal = 1e-6;
epoch = 20;
trnOpt(1) = epoch;
trnOpt(2) = error_goal;
% membangun arsitektur anfis
fismat = genfis1(trnData,numMFs,mfType);
% pelatihan anfis
[trnfismat,rmse] = anfis(trnData, fismat, trnOpt);
% plot membership function
[x,mf] = plotmf(trnfismat,'input',1);
subplot(2,3,1), plot(x,mf)
xlabel('input 1 (trapmf)')
[x,mf] = plotmf(trnfismat,'input',2);
subplot(2,3,2), plot(x,mf)
xlabel('input 2 (trapmf)')
[x,mf] = plotmf(trnfismat,'input',3);
subplot(2,3,3), plot(x,mf)
xlabel('input 3 (trapmf)')
[x,mf] = plotmf(trnfismat,'input',4);
subplot(2,3,4), plot(x,mf)
xlabel('input 4 (trapmf)')
[x,mf] = plotmf(trnfismat,'input',5);
subplot(2,3,5), plot(x,mf)
xlabel('input 5 (trapmf)')
[x,mf] = plotmf(trnfismat,'input',6);
subplot(2,3,6), plot(x,mf)
xlabel('input 6 (trapmf)')
% menyimpan variabel trnfismat hasil pelatihan anfis
writefis(trnfismat,'trnfismat');
% membaca nilai keluaran hasil pelatihan
output = round(evalfis(trnData(:,1:6), trnfismat));
% menghitung akurasi pelatihan
error = numel(find(output~=target));
akurasi_pelatihan = (numel(output)-error)/(numel(output))*100

Pada proses pelatihan tersebut digunakan dua buah membership function pada masing-masing input dengan tipe trapezoidal-shaped membership function (trapmf) yang ditunjukkkan pada gambar berikut:

Hasil keluaran dari proses pelatihan ditunjukkan pada tabel di bawah ini:

Pada tabel di atas tampak bahwa seluruh citra diklasifikasikan sesuai dengan target kelas yang sebenarnya. Sehingga diperoleh akurasi hasil proses pelatihan adalah sebesar 28/28*100 = 100%

3. Mempersiapkan citra uji untuk proses pengujian

4. Melakukan pengujian algoritma ANFIS menggunakan fuzzy inference system yang dihasilkan dari proses pelatihan. Koding program untuk proses pengujian adalah:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
%
% Website: https://rizalmutaqin.com/
% Email  :
 
clc; clear; close all; warning off all;
 
% membaca variabel variabel trnfismat hasil pelatihan anfis
trnfismat = readfis('trnfismat');
% menetapkan nama folder pengujian
image_folder = 'data uji';
% membaca nama file yang berekstensi .tif
filenames = dir(fullfile(image_folder, '*.tif'));
% membaca jumlah file
total_images = numel(filenames);
% menginisialisasi variabel feature
feature = zeros(total_images,6);
% untuk semua file yang dibaca
for n = 1:total_images
    % menggabungkan lokasi folder dengan nama file
    full_name = fullfile(image_folder, filenames(n).name);
    % membaca file citra
    I = imread(full_name);
    % melakukan segmentasi citra menggunakan metode otsu thresholding
    Img = imbinarize(I);    % untuk matlab dengan versi sebelum tahun 2016 gunakan perintah >> Img = im2bw(I,graythresh(I));
    % melakukan komplemen citra
    Img = imcomplement(Img);
    % operasi morfologi filling holes untuk menutup lubang pada objek
    Img = imfill(Img,'holes');
    % operasi morfologi area opening untuk menghilangkan noise
    Img = bwareaopen(Img,1000);
    % ekstraksi ciri morfologi
    stats = regionprops(Img,'All');
    area = stats.Area;
    perimeter = stats.Perimeter;
    metric = 4*pi*area/(perimeter^2);
    eccentricity = stats.Eccentricity;
    % ekstraksi ciri tekstur
    GLCM = graycomatrix(I,'Offset',[0 1; -1 1; -1 0; -1 -1]);
    stats = graycoprops(GLCM,{'contrast','correlation','energy','homogeneity'});
    contrast = mean(stats.Contrast);
    correlation = mean(stats.Correlation);
    energy = mean(stats.Energy);
    homogeneity = mean(stats.Homogeneity);
    % mengisi variabel feature dengan nilai hasil ekstraksi ciri
    feature(n,1) = metric;
    feature(n,2) = eccentricity;
    feature(n,3) = contrast;
    feature(n,4) = correlation;
    feature(n,5) = energy;
    feature(n,6) = homogeneity;
end
 
% menetapkan target pengujian
target = zeros(total_images,1);
target(1:3,:) = 1;
target(4:6,:) = 2;
target(7:9,:) = 3;
target(10:12,:) = 4;
% menyusun data pengujian
testData = feature;
% membaca nilai keluaran hasil pengujian
output = round(evalfis(testData, trnfismat));
% menghitung akurasi pengujian
error = numel(find(output~=target));
akurasi_pengujian = (numel(output)-error)/(numel(output))*100

Hasil yang diperoleh pada proses pengujian ditunjukkan pada tabel berikut:

Berdasarkan tabel di atas, dari 12 citra yang diujikan, terdapat tiga citra yang diklasifikasikan tidak sesuai dengan target kelas daun sebenarnya. Sehingga akurasi yang dihasilkan pada proses pengujian adalah sebesar 9/12*100 = 75%. Hasil ini menunjukkan bahwa algoritma ANFIS cukup baik digunakan untuk mengklasifikasikan daun berdasarkan ciri morfologi dan ciri tekstur.

5. Membuat tampilan Graphical User Interface (GUI)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
%
% Website: https://rizalmutaqin.com//
% Email  :
 
function varargout = Klasifikasi_daun_anfis(varargin)
% KLASIFIKASI_DAUN_ANFIS MATLAB code for Klasifikasi_daun_anfis.fig
%      KLASIFIKASI_DAUN_ANFIS, by itself, creates a new KLASIFIKASI_DAUN_ANFIS or raises the existing
%      singleton*.
%
%      H = KLASIFIKASI_DAUN_ANFIS returns the handle to a new KLASIFIKASI_DAUN_ANFIS or the handle to
%      the existing singleton*.
%
%      KLASIFIKASI_DAUN_ANFIS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in KLASIFIKASI_DAUN_ANFIS.M with the given input arguments.
%
%      KLASIFIKASI_DAUN_ANFIS('Property','Value',...) creates a new KLASIFIKASI_DAUN_ANFIS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Klasifikasi_daun_anfis_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Klasifikasi_daun_anfis_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 Klasifikasi_daun_anfis
 
% Last Modified by GUIDE v2.5 01-Aug-2019 09:51:53
 
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @Klasifikasi_daun_anfis_OpeningFcn, ...
    'gui_OutputFcn',  @Klasifikasi_daun_anfis_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 Klasifikasi_daun_anfis is made visible.
function Klasifikasi_daun_anfis_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 Klasifikasi_daun_anfis (see VARARGIN)
 
% Choose default command line output for Klasifikasi_daun_anfis
handles.output = hObject;
 
% Update handles structure
guidata(hObject, handles);
movegui(hObject,'center');
warning off all;
 
% UIWAIT makes Klasifikasi_daun_anfis wait for user response (see UIRESUME)
% uiwait(handles.figure1);
 
 
% --- Outputs from this function are returned to the command line.
function varargout = Klasifikasi_daun_anfis_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 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)
 
% menampilkan menu browse file
[nama_file,nama_path] = uigetfile({'*.tif'});
% jika ada file yang dipilih maka akan menjalankan perintah di bawah ini
if ~isequal(nama_file,0)
    % membaca file citra
    I = imread(fullfile(nama_path,nama_file));
    % menampilkan citra pada axes
    axes(handles.axes1)
    imshow(I)
    title('Citra Asli');
    % menampilkan nama file citra pada edit text
    set(handles.edit1,'String',nama_file)
    % menyimpan variabel I pada lokasi handles agar dapat dipanggil oleh
    % pushbutton yang lain
    handles.I = I;
    guidata(hObject,handles)
else
    % jika tidak ada file yang dipilih maka akan kembali
    return
end
 
 
% --- 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)
 
% memanggil variabel I yang ada di lokasi handles
I = handles.I;
% melakukan segmentasi citra menggunakan metode otsu thresholding
Img = imbinarize(I);    % untuk matlab dengan versi sebelum tahun 2016 gunakan perintah >> Img = im2bw(I,graythresh(I));
% melakukan komplemen citra
Img = imcomplement(Img);
% operasi morfologi filling holes untuk menutup lubang pada objek
Img = imfill(Img,'holes');
% operasi morfologi area opening untuk menghilangkan noise
Img = bwareaopen(Img,1000);
% ekstraksi ciri morfologi
stats = regionprops(Img,'All');
area = stats.Area;
perimeter = stats.Perimeter;
metric = 4*pi*area/(perimeter^2);
eccentricity = stats.Eccentricity;
% ekstraksi ciri tekstur
GLCM = graycomatrix(I,'Offset',[0 1; -1 1; -1 0; -1 -1]);
stats = graycoprops(GLCM,{'contrast','correlation','energy','homogeneity'});
contrast = mean(stats.Contrast);
correlation = mean(stats.Correlation);
energy = mean(stats.Energy);
homogeneity = mean(stats.Homogeneity);
% menyusun data pengujian
testData = [metric,eccentricity,contrast,correlation,energy,homogeneity];
% membaca variabel variabel trnfismat hasil pelatihan anfis
trnfismat = readfis('trnfismat.fis');
% membaca nilai keluaran hasil pengujian
output = round(evalfis(testData, trnfismat));
% menampilkan citra hasil segmentasi pada axes
axes(handles.axes2)
imshow(Img)
title('Citra Hasil Segmentasi');
% mengubah nilai keluaran menjadi kelas keluaran
if output == 1
    kelas = 'Daun A';
elseif output == 2
    kelas = 'Daun B';
elseif output == 3
    kelas = 'Daun C';
elseif output == 4
    kelas = 'Daun D';
end
% menampilkan kelas keluaran pada edit text
set(handles.edit2,'String',kelas)
 
% --- 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)
 
% mereset tampilan gui
axes(handles.axes1)
cla reset;
set(gca,'XTick',[]);
set(gca,'YTick',[]);
 
axes(handles.axes2)
cla reset;
set(gca,'XTick',[]);
set(gca,'YTick',[]);
 
set(handles.edit1,'String','');
 
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
 
 
% --- 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
 
 
 
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
 
 
% --- 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

>> Hasil klasifikasi untuk daun A

>> Hasil klasifikasi untuk daun B

>> Hasil klasifikasi untuk daun C

>> Hasil klasifikasi untuk daun D

Komentar

Tampilkan