Fuzzy C-Means and Principle Component Analysis Approaching to Images (Numbers 0 ans 6) Recognition
2016-03-28 12:39
501 查看
由于这学期上了Fuzzy Logic Control这门课程,所以希望借着博客的东风来记录自己的学习历程,并且加深自己的理解。入门新手,如有错误,请您指正。
This is a image classification problem to which I ask you to use fuzzy clustering function fuzzy c-mean.The attached data file contains an input array X2 with 1200 16x16 digitized images of handwritten 0 and 6. The true labels of those images are given
in the array y2. The convention used is -1 for digit 0, and 1 for digit 6. Your task is to investigate the use of fuzzy clustering to classify the input images as 0 or 6. You should aim at minimizing the classification error. The classification error is the
difference between your fuzzy classification and the true class given in the y2 array.
分析:每张图片的分辨率是16*16灰度图像,所以每张图像的特征有256个,所以此时对于1200个样本来说,256个特征确实是太多了。但是出于测试的目标,我测试了一下大概正确区分的几率为78.2%左右。在第二个程序中我加入了Principle Component Analysis法去提取出most significant features,然后以此为数据代替原来的每张图用256个特征表示的数据,利用fcm去识别图像数字内容。
Solution 1:
%FCM applications in images clustering \
close all;
clear all;
clc;
load('digits_0_6');
%-- plot 20 images in X2
figure(1);
for i=1:20
subplot(5,4,i); imshow(X2(:,:,i));
end
for i=1:size(X2,3)
X2_Reshape(i,:)=reshape(X2(:,:,i).',1,256);
end
%Specify fuzzy partition matrix exponents.
M = [1.1 2.0 3.0 4.0];
for i = 1:4
% Cluster the data.
options = [M(i) NaN NaN 0];
clu_n = 2;
[centers,U] = fcm(X2_Reshape,clu_n,options);
% Classify the data points.
maxU = max(U);
index1 = find(U(1,:) == maxU);
index2 = find(U(2,:) == maxU);
% Find data points with lower maximum membership values.
%index3 = find(maxU < 0.6);
% Calculate the average maximum membership value.
averageMax = mean(maxU);
len_1 = length(index1);
len_2 = length(index2);
if find(index2(:,:)==4)
for j =1:len_1
estimated_y2(index1(j)) =-1;
end
for j =1:len_2
estimated_y2(index2(j)) =1;
end
elseif find(index1(:,:)==4)
for j =1:len_1
estimated_y2(index1(j)) =1;
end
for j =1:len_2
estimated_y2(index2(j)) = -1;
end
end
%-- calculate the clustering accuracy
right = 0;
for k=1:length(y2)
if estimated_y2(1,k) == y2(1,k)
right = right +1;
end
end
%-- maximum accuracy is about 95.6% just by fcm approaching to this problem
%-- maybe because too many features 256 for just 1200 samples,so I try
%-- to reduce image features by pca in another solution, the accuracy is up to 96%
fprintf('When M = %d, Accuracy = %.3f%%, averageMax is %.3f;\n', i,right/12,averageMax);
end
在此程序中显示了前二十个图像,输出结果是:
When M = 1, Accuracy = 76.333%, averageMax is 0.965;
When M = 2, Accuracy = 77.500%, averageMax is 0.666;
When M = 3, Accuracy = 78.167%, averageMax is 0.500;
When M = 4, Accuracy = 78.167%, averageMax is 0.500;
Solution 2:
%-- pca and fcm method to indentify number 0 and 6
%-- pca : extract the project of the images
%-- fcm : clusterinf the principle features of image
close all;
clear all;
clc;
load('digits_0_6');
%-- plot 20 images in X2
figure(1);
for i=1:20
subplot(5,4,i); imshow(X2(:,:,i));
end
%adopting pca method to extract principal features in images
for i=1:size(X2,3)
%the number of the most significant principal components that are taken into account.
m =5;
image = X2(:,:,i);
[l,N]=size(image);
% Subtracting the mean
mean_vec=mean(image')';
X_zero=image-mean_vec*ones(1,N);
% Computing the covariance matrix and its eigenvalues/eigenvectors
R=cov(X_zero');
[V,D]=eig(R);
eigenval=diag(D);
[eigenval,ind]=sort(eigenval,1,'descend');
eigenvec=V(:,ind);
explain=eigenval/sum(eigenval);
% Keeping the first m eigenvaules/eigenvectors
eigenval=eigenval(1:m);
eigenvec=eigenvec(:,1:m);
% Computing the transformation matrix
A=eigenvec(:,1:m)';
% Computing the transformed data set
Y=A*image;
%-- images projection for 5 significant principal components
X2_Projection(i,:) = reshape(Y',1,80);
end
%Specify fuzzy partition matrix exponents.
M = [1.1 2.0 3.0 4.0];
for i = 1:4
% Cluster the data.
options = [M(i) NaN NaN 0];
clu_n = 2;
[centers,U] = fcm(X2_Projection,clu_n,options);
% Classify the data points.
maxU = max(U);
index1 = find(U(1,:) == maxU);
index2 = find(U(2,:) == maxU);
% Find data points with lower maximum membership values.
index3 = find(maxU < 0.6);
% Calculate the average maximum membership value.
averageMax = mean(maxU);
len_1 = length(index1);
len_2 = length(index2);
if find(index2(:,:)==4)
for j =1:len_1
estimated_y2(index1(j)) =-1;
end
for j =1:len_2
estimated_y2(index2(j)) =1;
end
elseif find(index1(:,:)==4)
for j =1:len_1
estimated_y2(index1(j)) =1;
end
for j =1:len_2
estimated_y2(index2(j)) = -1;
end
end
%-- calculate the clustering accuracy
right = 0;
for k=1:length(y2)
if estimated_y2(1,k) == y2(1,k)
right = right +1;
end
end
%-- maximum accuracy is about 95.6% by pca and fcm approaching to this problem
fprintf('When M = %d, Accuracy = %.3f%%, averageMax is %.3f;\n', i,right/12,averageMax);
end
输出结果是:
When M = 1, Accuracy = 95.667%, averageMax is 0.978;
When M = 2, Accuracy = 92.167%, averageMax is 0.571;
When M = 3, Accuracy = 91.833%, averageMax is 0.500;
When M = 4, Accuracy = 91.833%, averageMax is 0.500;
可以看出,实验结果有明显的提升,但并不一定是最优答案,希望有新的朋友继续探索。
我想把数据传上去呢,可是没发现怎么upload啊。
This is a image classification problem to which I ask you to use fuzzy clustering function fuzzy c-mean.The attached data file contains an input array X2 with 1200 16x16 digitized images of handwritten 0 and 6. The true labels of those images are given
in the array y2. The convention used is -1 for digit 0, and 1 for digit 6. Your task is to investigate the use of fuzzy clustering to classify the input images as 0 or 6. You should aim at minimizing the classification error. The classification error is the
difference between your fuzzy classification and the true class given in the y2 array.
分析:每张图片的分辨率是16*16灰度图像,所以每张图像的特征有256个,所以此时对于1200个样本来说,256个特征确实是太多了。但是出于测试的目标,我测试了一下大概正确区分的几率为78.2%左右。在第二个程序中我加入了Principle Component Analysis法去提取出most significant features,然后以此为数据代替原来的每张图用256个特征表示的数据,利用fcm去识别图像数字内容。
Solution 1:
%FCM applications in images clustering \
close all;
clear all;
clc;
load('digits_0_6');
%-- plot 20 images in X2
figure(1);
for i=1:20
subplot(5,4,i); imshow(X2(:,:,i));
end
for i=1:size(X2,3)
X2_Reshape(i,:)=reshape(X2(:,:,i).',1,256);
end
%Specify fuzzy partition matrix exponents.
M = [1.1 2.0 3.0 4.0];
for i = 1:4
% Cluster the data.
options = [M(i) NaN NaN 0];
clu_n = 2;
[centers,U] = fcm(X2_Reshape,clu_n,options);
% Classify the data points.
maxU = max(U);
index1 = find(U(1,:) == maxU);
index2 = find(U(2,:) == maxU);
% Find data points with lower maximum membership values.
%index3 = find(maxU < 0.6);
% Calculate the average maximum membership value.
averageMax = mean(maxU);
len_1 = length(index1);
len_2 = length(index2);
if find(index2(:,:)==4)
for j =1:len_1
estimated_y2(index1(j)) =-1;
end
for j =1:len_2
estimated_y2(index2(j)) =1;
end
elseif find(index1(:,:)==4)
for j =1:len_1
estimated_y2(index1(j)) =1;
end
for j =1:len_2
estimated_y2(index2(j)) = -1;
end
end
%-- calculate the clustering accuracy
right = 0;
for k=1:length(y2)
if estimated_y2(1,k) == y2(1,k)
right = right +1;
end
end
%-- maximum accuracy is about 95.6% just by fcm approaching to this problem
%-- maybe because too many features 256 for just 1200 samples,so I try
%-- to reduce image features by pca in another solution, the accuracy is up to 96%
fprintf('When M = %d, Accuracy = %.3f%%, averageMax is %.3f;\n', i,right/12,averageMax);
end
在此程序中显示了前二十个图像,输出结果是:
When M = 1, Accuracy = 76.333%, averageMax is 0.965;
When M = 2, Accuracy = 77.500%, averageMax is 0.666;
When M = 3, Accuracy = 78.167%, averageMax is 0.500;
When M = 4, Accuracy = 78.167%, averageMax is 0.500;
Solution 2:
%-- pca and fcm method to indentify number 0 and 6
%-- pca : extract the project of the images
%-- fcm : clusterinf the principle features of image
close all;
clear all;
clc;
load('digits_0_6');
%-- plot 20 images in X2
figure(1);
for i=1:20
subplot(5,4,i); imshow(X2(:,:,i));
end
%adopting pca method to extract principal features in images
for i=1:size(X2,3)
%the number of the most significant principal components that are taken into account.
m =5;
image = X2(:,:,i);
[l,N]=size(image);
% Subtracting the mean
mean_vec=mean(image')';
X_zero=image-mean_vec*ones(1,N);
% Computing the covariance matrix and its eigenvalues/eigenvectors
R=cov(X_zero');
[V,D]=eig(R);
eigenval=diag(D);
[eigenval,ind]=sort(eigenval,1,'descend');
eigenvec=V(:,ind);
explain=eigenval/sum(eigenval);
% Keeping the first m eigenvaules/eigenvectors
eigenval=eigenval(1:m);
eigenvec=eigenvec(:,1:m);
% Computing the transformation matrix
A=eigenvec(:,1:m)';
% Computing the transformed data set
Y=A*image;
%-- images projection for 5 significant principal components
X2_Projection(i,:) = reshape(Y',1,80);
end
%Specify fuzzy partition matrix exponents.
M = [1.1 2.0 3.0 4.0];
for i = 1:4
% Cluster the data.
options = [M(i) NaN NaN 0];
clu_n = 2;
[centers,U] = fcm(X2_Projection,clu_n,options);
% Classify the data points.
maxU = max(U);
index1 = find(U(1,:) == maxU);
index2 = find(U(2,:) == maxU);
% Find data points with lower maximum membership values.
index3 = find(maxU < 0.6);
% Calculate the average maximum membership value.
averageMax = mean(maxU);
len_1 = length(index1);
len_2 = length(index2);
if find(index2(:,:)==4)
for j =1:len_1
estimated_y2(index1(j)) =-1;
end
for j =1:len_2
estimated_y2(index2(j)) =1;
end
elseif find(index1(:,:)==4)
for j =1:len_1
estimated_y2(index1(j)) =1;
end
for j =1:len_2
estimated_y2(index2(j)) = -1;
end
end
%-- calculate the clustering accuracy
right = 0;
for k=1:length(y2)
if estimated_y2(1,k) == y2(1,k)
right = right +1;
end
end
%-- maximum accuracy is about 95.6% by pca and fcm approaching to this problem
fprintf('When M = %d, Accuracy = %.3f%%, averageMax is %.3f;\n', i,right/12,averageMax);
end
输出结果是:
When M = 1, Accuracy = 95.667%, averageMax is 0.978;
When M = 2, Accuracy = 92.167%, averageMax is 0.571;
When M = 3, Accuracy = 91.833%, averageMax is 0.500;
When M = 4, Accuracy = 91.833%, averageMax is 0.500;
可以看出,实验结果有明显的提升,但并不一定是最优答案,希望有新的朋友继续探索。
我想把数据传上去呢,可是没发现怎么upload啊。
相关文章推荐
- 行为触发之Android自动化测试instrumentation(一)
- Android应用程序退出时
- Android activity的生命周期
- Android adb 打开mediascanner
- Android内存泄漏的各种原因详解
- android内存管理机制(一)
- 从手机相册,相机选取照片
- Android内存管理机制详解
- Unity3D手游开发日记(6) - 适合移动平台的水深处理
- 避免Android内存泄露
- android 权限管理的使用
- Android内存泄露案例分析
- hdu1452 Happy 2004(规律+因子和+积性函数)
- 一个常见的android内存泄露 问题
- android 中在activity弹出一个对话框,并不会执行onpause生命周期
- Android 项目的代码混淆
- Android内存泄露原因分析与处理方案
- androidStudio引入自定义属性
- Android studio 百度地图开发(1)配置工程、显示地图
- Android开发资料:Android启动优化解析