|
| 1 | + |
| 2 | +%% |
| 3 | +%clc; clear all; close all; |
| 4 | +clear;clc;close all |
| 5 | +tic; |
| 6 | +%location |
| 7 | + |
| 8 | +path_train='train.txt'; |
| 9 | +path_test = 'test.txt'; |
| 10 | +train_label = repmat(1,1,676) |
| 11 | +test_label = repmat(1,1,676) |
| 12 | +train_data = load(path_train); |
| 13 | +test_data = load(path_test); |
| 14 | +[data_row,data_clown]=size(train_data); |
| 15 | + |
| 16 | + |
| 17 | +%s1 = struct('train_data_1',train_data_1,'train_label_1',train_label_1,'test_data_1',test_data_1,'test_label_1',test_label_1) |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +%SOM network m*n |
| 22 | +m=10; |
| 23 | +n=10; |
| 24 | +%Neuron som_sum |
| 25 | +som_sum=m*n; |
| 26 | +%weight initialize |
| 27 | +w = rand(som_sum, data_clown); |
| 28 | +%Initialize learning rate |
| 29 | +learn0 = 0.6; |
| 30 | +learn_rate = learn0; |
| 31 | +%learning para |
| 32 | +learn_para=100; |
| 33 | +%Setting iteration |
| 34 | +iter = 50;%1000 500 |
| 35 | +%Neuron location |
| 36 | +[I,J] = ind2sub([m, n], 1:som_sum); |
| 37 | +%Neighbor Initialize |
| 38 | +neighbor0 =2; |
| 39 | +neighbor_redius = neighbor0; |
| 40 | +%Neighbor parameters |
| 41 | +neighbor_para = 1000/log(neighbor0); |
| 42 | + |
| 43 | +for t=1:iter |
| 44 | + % Scan all the sample points |
| 45 | + display(['Iteration: ' num2str(t)]); |
| 46 | + for j=1:data_row |
| 47 | + %Getting the winner neuron |
| 48 | + data_x = train_data(j,:); |
| 49 | + %Find the winner neuron |
| 50 | + [win_row, win_som_index]=min(dist(data_x,w')); |
| 51 | + %Find the winner neuron position |
| 52 | + [win_som_row,win_som_cloumn] = ind2sub([m, n],win_som_index); |
| 53 | + win_som=[win_som_row,win_som_cloumn]; |
| 54 | + %Calculate and update the winner neuron neighbourhood position |
| 55 | + %distance_som = sum(( ([I( : ), J( : )] - repmat(win_som, som_sum,1)) .^2) ,2); |
| 56 | + distance_som = exp( sum(( ([I( : ), J( : )] - repmat(win_som, som_sum,1)) .^2) ,2)/(-2*neighbor_redius*neighbor_redius)) ; |
| 57 | + %Update the weights |
| 58 | + for i = 1:som_sum |
| 59 | + % if distance_som(i)<neighbor_redius*neighbor_redius |
| 60 | + w(i,:) = w(i,:) + learn_rate.*distance_som(i).*( data_x - w(i,:)); |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + %Updateing the learning rate |
| 65 | + learn_rate = learn0 * exp(-t/learn_para); |
| 66 | + %Update the neighbor radius |
| 67 | + neighbor_redius = neighbor0*exp(-t/neighbor_para); |
| 68 | +end |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +%save as som_num |
| 73 | +som_num=cell(1,size(w,1)); |
| 74 | +for i=1:size(w,1) |
| 75 | + som_num{1,i}=[]; |
| 76 | +end |
| 77 | +%Every neuron data corresponding number |
| 78 | +for num=1:data_row |
| 79 | + [som_row,clown]= min(sum(( (w - repmat(train_data(num,:), som_sum,1)) .^2) ,2)); |
| 80 | + som_num{1,clown}= [som_num{1,clown},num]; |
| 81 | +end |
| 82 | + |
| 83 | +figure() |
| 84 | +for i=1:100 |
| 85 | +subplot(10,10,i); |
| 86 | +weight=reshape(w(i,:),26,26); |
| 87 | +imshow(double(weight)); |
| 88 | +end |
| 89 | + |
| 90 | +path1='som_num_1.mat'; |
| 91 | +save(path1,'som_num'); |
| 92 | +toc; |
| 93 | +%% |
| 94 | +number1 = load('p_dataset_26/Sample1/img002-00001.mat') |
| 95 | +number2 = load('p_dataset_26/Sample2/img003-00001.mat') |
| 96 | +number3 = load('p_dataset_26/Sample3/img004-00001.mat') |
| 97 | +numbera = load('p_dataset_26/SampleA/img011-00001.mat') |
| 98 | +numberb = load('p_dataset_26/SampleB/img012-00001.mat') |
| 99 | +numberc = load('p_dataset_26/SampleC/img013-00001.mat') |
| 100 | +%% |
| 101 | +fid = fopen('charact1.txt'); |
| 102 | +% read a char at a time, ignore linefeed and carriage return |
| 103 | +% and put them in a 64 X 64 matrix |
| 104 | +lf = char(10); % line feed character |
| 105 | +cr = char(13); % carriage return character |
| 106 | +A = fscanf(fid, [cr lf '%c'],[64,64]); |
| 107 | +% close the file handler |
| 108 | +fclose(fid); |
| 109 | +A = A'; % transpose since fscanf returns column vectors |
| 110 | +% convert letters A‐V to their corresponding values in 32 gray levels |
| 111 | +% literal A becomes number 10 and so on... |
| 112 | +A(isletter(A))= A(isletter(A)) - 55; |
| 113 | +%convert number literals 0‐9 to their corresponding values in 32 gray |
| 114 | +%levels. Numeric literal '0' becomes number 0 and so on... |
| 115 | +A(A >= '0' & A <= '9') = A(A >= '0' & A <= '9') - 48; |
| 116 | +A = uint8(A); |
| 117 | +M = A |
| 118 | + |
| 119 | +% Preprocessing for test image |
| 120 | +Add_1 = zeros(26,2) |
| 121 | +N_1_o = M(1:26,1:24) |
| 122 | +N_1 = cat(2,Add_1,N_1_o) |
| 123 | +N_1_ad = imadjust(N_1,stretchlim(N_1),[]) |
| 124 | +Nb1 = imcomplement(N_1_ad) |
| 125 | +N1 = reshape(Nb1,1,[]) |
| 126 | + |
| 127 | + |
| 128 | +N_2 = M(1:26, 22:47) |
| 129 | +% N_2(1:2,:)=[] |
| 130 | +% N_2(21:end,:)=[] |
| 131 | +N_2_ad = imadjust(N_2,stretchlim(N_2),[]) |
| 132 | +Nb2 = imcomplement(N_2_ad) |
| 133 | +N2 = reshape(Nb2,1,[]) |
| 134 | + |
| 135 | +Add_3 = zeros(26,4) |
| 136 | +N_3_o = M(1:26, 43:64) |
| 137 | +N_3 = cat(2,N_3_o,Add_3) |
| 138 | +N_3(1:5,:)=[] |
| 139 | +N_3(15:21,:)=[] |
| 140 | +N_3_up = repmat(N_3(5,:),7,1) |
| 141 | +N_3_low = repmat(N_3(9,:),7,1) |
| 142 | +N_3_new = zeros(26,26) |
| 143 | +N_3_new(1:4,:) = N_3(1:4,:) |
| 144 | +N_3_new(5:11,:) = N_3_up |
| 145 | +N_3_new(12:15,:) = N_3(6:9,:) |
| 146 | +N_3_new(16:22,:) =N_3_low |
| 147 | +N_3_new(23:26,:) = N_3(11:14,:) |
| 148 | +%N_3_ad = imadjust(N_new,stretchlim(N_new),[]) |
| 149 | +N_3_ad = imadjust(N_3_new,[0.01 0.02],[0 1]) |
| 150 | +Nb3 = imcomplement(N_3_ad) |
| 151 | +Nb3 = Nb3.*255 |
| 152 | +N3 = reshape(Nb3,1,[]) |
| 153 | + |
| 154 | +Add_a = zeros(26,2) |
| 155 | +N_A_o = M(38:63, 1:24) |
| 156 | +N_A = cat(2,Add_a,N_A_o) |
| 157 | +N_a_ad = imadjust(N_A,stretchlim(N_A),[]) |
| 158 | +Na = imcomplement(N_a_ad) |
| 159 | +NA = reshape(Na,1,[]) |
| 160 | + |
| 161 | + |
| 162 | +N_B = M(38:63, 20:45) |
| 163 | +N_B(1,:)=[] |
| 164 | +N_B(21:25,:)=[] |
| 165 | +N_B_up= repmat(N_B(4,:),5,1) |
| 166 | +N_B_low= repmat(N_B(11,:),5,1) |
| 167 | +N_B_new = zeros(26,26) |
| 168 | +N_B_new(1:3,:)=N_B(1:3,:) |
| 169 | +N_B_new(4:6,:)= N_B(4:6,:) |
| 170 | +N_B_new(7:11,:)= N_B_up |
| 171 | +N_B_new(12:15,:)=N_B(9:12,:) |
| 172 | +N_B_new(16:20,:)=N_B_low |
| 173 | +N_B_new(21:26,:)=N_B(15:20,:) |
| 174 | +N_b_ad = imadjust(N_B_new,[0.01 0.02],[0 1]) |
| 175 | +Nb = imcomplement(N_b_ad) |
| 176 | +NB = reshape(Nb,1,[]) |
| 177 | + |
| 178 | +Add_C= zeros(26,1) |
| 179 | +N_C = M(38:63, 40:64) |
| 180 | +N_C = cat(2,N_C,Add_C) |
| 181 | +N_C(1,:)=[] |
| 182 | +N_C(21:end,:)=[] |
| 183 | +N_C_mid = repmat(N_C(10,:),11,1) |
| 184 | +N_C_new= zeros(26,26) |
| 185 | +N_C_new(1:7,:)=N_C(1:7,:) |
| 186 | +N_C_new(8:18,:)=N_C_mid |
| 187 | +N_C_new(19:26,:)=N_C(13:20,:) |
| 188 | +N_C_new(:,19:23)=zeros(26,5) |
| 189 | +N_C_ad = imadjust(N_C_new,[0.01 0.02],[0 1]) |
| 190 | +Nc = imcomplement(N_C_ad) |
| 191 | +Nc = Nc.*255 |
| 192 | +NC = reshape(Nc,1,[]) |
| 193 | + |
| 194 | +n1 = load('train_1.txt') |
| 195 | +n2 = load('train_2.txt') |
| 196 | +n3 = load('train_3.txt') |
| 197 | +na = load('train_A.txt') |
| 198 | +nb = load('train_B.txt') |
| 199 | +nc = load('train_C.txt') |
| 200 | +n1_t = load('test_1.txt') |
| 201 | +n2_t = load('test_2.txt') |
| 202 | +n3_t = load('test_3.txt') |
| 203 | +na_t = load('test_A.txt') |
| 204 | +nb_t = load('test_B.txt') |
| 205 | +nc_t = load('test_C.txt') |
| 206 | +n = [n1;n2;n3;na;nb;nc] |
| 207 | +n_t = [n1_t; n2_t; n3_t; na_t; nb_t; nc_t] |
| 208 | +inputs = n'; |
| 209 | +dimension1 = 10; |
| 210 | +dimension2 = 10; |
| 211 | +net = selforgmap([dimension1 dimension2]); |
| 212 | +[net,tr] = train(net,inputs); |
| 213 | +view(net) |
| 214 | +y = net(inputs); |
| 215 | + |
| 216 | +%t=mapminmax(t); |
| 217 | +% sim( ) to do Network simulation |
| 218 | +% r=sim(net,t); |
| 219 | +% % |
| 220 | +% rr=vec2ind(r) |
| 221 | +% Find the topology structure of the network |
| 222 | +plotsomtop(net) |
| 223 | +% plot the distance plot |
| 224 | +plotsomnd(net) |
| 225 | +% plot the classifier result |
| 226 | +plotsomhits(net,inputs) |
| 227 | + |
| 228 | + |
| 229 | + |
| 230 | + |
0 commit comments