smail |
۰۴-۱۶-۱۳۹۴ ۰۴:۱۵ قبل از ظهر |
سوال در مورد کد نوشته شده در برنامه متلب(کد شمارش تعداد انگشتان باز یک دست)
سلام
برنامه زیر کد شمارش تعداد انگشتان باز یک دست با استفاده از شبکه عصبی پرسپترون است
كد:
%% initializeing the program
clc;
clear all;
close all;
%%
n=16;
l0=imread('left0.jpg');
l0=rgb2gray(l0);
l0=im2bw(l0,0.5);
%%
l1=imread('left1.jpg');
l1=rgb2gray(l1);
l1=im2bw(l1,0.5);
%%
l2=imread('left2.jpg');
l2=rgb2gray(l2);
l2=im2bw(l2,0.5);
%%
l3=imread('left3.jpg');
l3=rgb2gray(l3);
l3=im2bw(l3,0.5);
%%
l4=imread('left4.jpg');
l4=rgb2gray(l4);
l4=im2bw(l4,0.5);
%%
l5=imread('left9.jpg');
l5=rgb2gray(l5);
l5=im2bw(l5,0.5);
%%
l44=imread('left8.jpg');
l44=rgb2gray(l44);
l44=im2bw(l44,0.5);
%%
l33=imread('left7.jpg');
l33=rgb2gray(l33);
l33=im2bw(l33,0.5);
%%
r0=imread('right0.jpg');
r0=rgb2gray(r0);
r0=im2bw(r0,0.5);
%%
r1=imread('right1.jpg');
r1=rgb2gray(r1);
r1=im2bw(r1,0.5);
%%
r2=imread('right2.jpg');
r2=rgb2gray(r2);
r2=im2bw(r2,0.5);
%%
r3=imread('right3.jpg');
r3=rgb2gray(r3);
r3=im2bw(r3,0.5);
%%
r4=imread('right4.jpg');
r4=rgb2gray(r4);
r4=im2bw(r4,0.5);
%%
r5=imread('right9.jpg');
r5=rgb2gray(r5);
r5=im2bw(r5,0.5);
%%
r11=imread('right5.jpg');
r11=rgb2gray(r11);
r11=im2bw(r11,0.5);
%%
r22=imread('right6.jpg');
r22=rgb2gray(r22);
r22=im2bw(r22,0.5);
%%
r33=imread('right7.jpg');
r33=rgb2gray(r33);
r33=im2bw(r33,0.5);
%%
r44=imread('right8.jpg');
r44=rgb2gray(r44);
r44=im2bw(r44,0.5);
subplot(2,6,1);
imshow(l0);
subplot(2,6,2);
imshow(l1);
subplot(2,6,3);
imshow(l2);
subplot(2,6,4);
imshow(l3);
subplot(2,6,5);
imshow(l4);
subplot(2,6,6);
imshow(l5);
subplot(2,6,7);
imshow(r0);
subplot(2,6,8);
imshow(r1);
subplot(2,6,9);
imshow(r2);
subplot(2,6,10);
imshow(r3);
subplot(2,6,11);
imshow(r4);
subplot(2,6,12);
imshow(r5);
figure;
% x[n,10000]
x=[reshape(l0,1,[]);reshape(l1,1,[]);reshape(l2,1,[]);reshape(l3,1,[]);reshape(l4,1,[]);reshape(l5,1,[]);
reshape(r0,1,[]);reshape(r1,1,[]);reshape(r2,1,[]);reshape(r3,1,[]);reshape(r4,1,[]);reshape(r5,1,[]);
reshape(r11,1,[]);reshape(r22,1,[]);reshape(r33,1,[]);reshape(r44,1,[]);
];
% t[n,6]
t=[1 -1 -1 -1 -1 -1;
-1 1 -1 -1 -1 -1;
-1 -1 1 -1 -1 -1;
-1 -1 -1 1 -1 -1;
-1 -1 -1 -1 1 -1;
-1 -1 -1 -1 -1 1;
1 -1 -1 -1 -1 -1;
-1 1 -1 -1 -1 -1;
-1 -1 1 -1 -1 -1;
-1 -1 -1 1 -1 -1;
-1 -1 -1 -1 1 -1;
-1 -1 -1 -1 -1 1;
-1 1 -1 -1 -1 -1;
-1 -1 1 -1 -1 -1;
-1 -1 -1 1 -1 -1;
-1 -1 -1 -1 1 -1;
];
%%
b=zeros(1,6);
b_old=zeros(1,6);
y_in=zeros(1,6);
y=zeros(1,6);
w=zeros(10000,6);
w_old=zeros(10000,6);
s=0;
epoch=0;
%%
while s==0
epoch=epoch+1;
for i=1:n
for k=1:6
y_in(k)=0;
for j=1 : 10000
y_in(k)=y_in(k)+ w(j,k)*x(i,j);
end
y_in(k)=y_in(k)+b(k);
if(y_in(k)>0)
y(k)=1;
elseif (y_in(k)<0)
y(k)=-1;
else
y(k)=0;
end
if(t(i,k)~=y(k))
for z=1:10000
w(z,k)=w(z,k)+x(i,z)*t(i,k);
end
b(k)=b(k)+t(i,k);
end
end
end
if ((w_old==w))
s=1;
else
w_old=w;
b_old=b;
end
end
%%
[fn,path]=uigetfile('*.jpg','select your hand to count its finger');
test=imread([path,fn]);
test=imresize(test,[100 100]);
test=rgb2gray(test);
test=im2bw(test,0.5);
test=reshape(test,1,[]);
% test
y=0;
%r=randi(20)+1
result=zeros(1,6);
for k=1:6
for i=1:10000
y=y+test(i)*w(i,k);
end
y=y+b(k);
if(y>=0)
result(k)=1;
else
result(k)=-1;
end
y=0;
end
disp(['Your Output array is : ']);
disp(result);
if result== [ 1 -1 -1 -1 -1 -1]
disp('<moch maste>');
imshow (l0);
elseif result== [ -1 1 -1 -1 -1 -1]
disp('1 finger');
imshow(l1);
elseif result==[-1 -1 1 -1 -1 -1]
disp('2 finger');
imshow(l2);
elseif result==[ -1 -1 -1 1 -1 -1]
disp('3 finger');
imshow(l3)
elseif result== [ -1 -1 -1 -1 1 -1]
disp('4 finger');
imshow(l4);
elseif result==[ -1 -1 -1 -1 -1 1 ]
disp('5 finger');
imshow(l9)
else
disp('Im sorry');
end
میشه توضیح بیشتری بدید قسمتهای زیر از کد بالایی دقیقا چکار می کند.
مقادیر اولیه شبکه عصبی شامل y , b , w
كد:
b=zeros(1,6);
b_old=zeros(1,6);
y_in=zeros(1,6);
y=zeros(1,6);
w=zeros(10000,6);
w_old=zeros(10000,6);
s=0;
epoch=0;
میشه توضیح بدین این حلقه دقیقا چطور کار می کند؟
در این حلقه هر کدام از تصاویر به شبکه عصبی آموزش داده می شود تازمانی که کامل یاد بگیرد
كد:
while s==0
epoch=epoch+1;
for i=1:n
for k=1:6
y_in(k)=0;
for j=1 : 10000
y_in(k)=y_in(k)+ w(j,k)*x(i,j);
end
y_in(k)=y_in(k)+b(k);
if(y_in(k)>0)
y(k)=1;
elseif (y_in(k)<0)
y(k)=-1;
else
y(k)=0;
end
if(t(i,k)~=y(k))
for z=1:10000
w(z,k)=w(z,k)+x(i,z)*t(i,k);
end
b(k)=b(k)+t(i,k);
end
end
end
if ((w_old==w))
s=1;
else
w_old=w;
b_old=b;
end
end
|