Image Recognition 6: Synthesis


This experiment is a comprehensive content, including the following experimental content and results.

①The experimental content includes :
for the 1001 Abnormal Image Dataset database, the image data extraction and size adjustment are completed, and the five sets of training sets and test sets are randomly divided into five sets of training sets and test sets by using the set-out method according to the ratio of 3:2; the five sets of training sets and test
sets Four algorithms of PCA, LPP, LLE, and ISOMAP are used for feature extraction, and SVM classifier, KNN classifier, and decision tree are used to classify features; classification experiments include parameter change analysis
experiments and result visualization experiments.
②Experimental results include :
1. Images before and after resizing;
2. Classification accuracy change curves of the four feature extraction algorithms when the classifier parameters are changed;
3. The optimal parameters are given, and the four feature extraction algorithms are given in use Classification accuracy under the classifier with optimal parameters;
4. After determining the optimal parameters, visually display the six types of images for the classification results;

1. Image data extraction

In order to improve the reusability of the program, some parameters of the program are set at the front.
insert image description here
Read all image paths under the specified folder path through two layers of for loop. The first layer reads all subfolder information under a single folder, including the subfolder path sub_path, all image names under the subfolder image_name, and the number of images under the subfolder img_num. The second layer obtains the image path by traversing all image names image_name under the subfolder. Finally, read the image information according to the image path.
After reading the image information into a matrix through imread, convert the colored RGB image to a grayscale image through the rgb2gray function. Since the image size may be inconsistent, it is necessary to use the imresize function to adjust to a uniform size. This article uses the [32, 32] format for storage. Then pull the 32*32 data into 1 line, which is convenient for subsequent data processing.
Finally, since the first layer loops under different subfolders, it corresponds to different labels. Therefore, the label can be assigned a value according to the i of the cycle count, 1:Aeroplane 2:Boat 3:Car 4:Chair 5:Motorbike 6:Sofa, and store the value corresponding to the image information phi in the phi_label array.
insert image description here

2. Feature extraction

In order to improve the robustness of the algorithm and make it easier to re-run after the code reports an error, a link of saving and re-reading is designed. The meaning of this operation is to separate the image data reading block from the following steps such as feature extraction.
insert image description here
First set the parameter to 50, and then use PCA principal component analysis, LPP local preservation projection, ISOMAP equidistant feature map, LLE local linear embedding to sequentially extract the features of the resize data, the purpose is to reduce the data dimension, facilitate calculation and visualization, and its deeper Its significance lies in the extraction and synthesis of effective information and the rejection of useless information. After feature extraction, save them in dealX{1,1}, dealX{1,2}, dealX{1,3}, dealX{1,4} in turn.

PCA is often used to reduce the dimensionality of a dataset while maintaining the features of the dataset that contribute the most to the variance.
insert image description here

While LPP can reduce the spatial dimension, it can better maintain the internal fixed local structure. Among them, there is a judgment condition in the LPP function. If the condition is not met, an error will be reported and the operation will stop. Since this judgment statement has no real effect, it can be commented out.

insert image description here

ISOMAP adopts the reason of "partial Euclidean space properties", so that the distance between two points is approximately equal to the sum of the lengths of the lines connecting multiple adjacent points. In this way, the multi-dimensional space is "unfolded" into a lower-dimensional space.
Since some original data distribution characteristics do not conform to the ISOMAP and LLE processing specifications, some processed data are missing, so the corresponding labels need to be updated. Here, find the subscript corresponding to the data through conn_comp, and then update dealY by reading the corresponding label.

insert image description here

LLE can learn local linear low-dimensional manifolds of any dimension. The algorithm comes down to sparse matrix eigendecomposition, which has relatively small computational complexity and is easy to implement. But the manifold learned by the algorithm can only be unclosed, and the sample set is dense and uniform. The algorithm is sensitive to the selection of the number of nearest neighbor samples, and different numbers of nearest neighbors have a great influence on the final dimensionality reduction results.
Warning during LLE operation:
insert image description here
add a dot before the operator in line 83, and the warning is resolved, but the data dimension after lle extracts features does not change, indicating that the change of data dimension has nothing to do with it.
insert image description here

After consulting the data, it is found that LLE has strict requirements on the manifold distribution characteristics of the data. For example, it cannot be a closed manifold, it cannot be a sparse data set, it cannot be a data set with uneven distribution, etc., which limits its application.

3. Divide the data set and classify

Again, data storage and reading are performed first.
insert image description here
Then, read the image data and the corresponding labels after the four kinds of feature extraction through the for loop, and then perform five second divisions respectively.

First, get a scrambled sequence a through the randperm function, which contains (1, n), where n is the number of rows of the matrix after feature extraction. Since the 3:2 division required by the title is prone to imperfect division, the round function is used to round the number of divisions. Then the two sequences of train_num and test_num respectively take the 3:2 of the disordered sequence, Xtrain and Ytest take the number of columns corresponding to the original matrix according to the values ​​of the two sequences of train_num and test_num, and finally save the results of each division in the corresponding The training set and test set are randomly divided into training samples Xtrain and test samples Ytest, and the label Xlabel of the training sample and the label label of the test set are given at the same time.
insert image description here

After the division is completed, traindata and trainlabel are the training set data set and label respectively. testdata and testlabel are the test set data set and label respectively. Among them, the rows of data are samples, and the columns are features. The row of label is the sample.
insert image description here

3.1 Decision tree

Then use the decision tree for classification, and dt_acc stores the accuracy of the results of the five second divisions of the four methods.
insert image description here

Before calling a function, you need to look at the input and output size and parameters required by the function.
C4_5 input requirements: the rows of data are features, and the columns are samples . The column of label is the sample . The last two parameters, one is to prevent overfitting parameters (5 is selected here), and the other parameter is used to determine whether the variable is discrete or continuous (default is 10). Output: 1 row m columns, the column is the test sample .

insert image description here

Plot the accuracy of a decision tree classifier as a function of parameters.
Data is routinely accessed first.

insert image description here

Plotting the variation in accuracy for four methods of a decision tree classifier.
insert image description here

The print classification accuracy is the average of the 5 sets of training and test sets.
insert image description here

The five decision tree accuracy rates of PCA, lpp, isomap, and lle are 0.260558, 0.219920, 0.333333, and 0.250847 respectively.

3.2 SVM

SVM input requirements: The rows of data are samples . The column of label is the sample .
Use SVM for classification, and explore the classification experiment under the change of SVM classifier parameters, and select the optimal parameters and give the optimal accuracy. Mainly consider the impact of the parameters '-s' svm type and '-t' loss function type on the classification accuracy, and the -s and -t parameters are cycled from 1 to 4.
And in the outermost loop, calculate the average accuracy after five second divisions under different parameters, and save the data of 25 parameters in the four data dimensionality reduction methods into the svm_accs matrix (4*25).
insert image description here
Draw the image of the accuracy change under each parameter of the svm classifier, the method is similar to the decision tree.
It was found that the accuracy did not change after the parameters were changed. After checking one by one, it was found that there was a problem with the parameters of the model.
insert image description here
Try string combination, change the original ["-s ", (s-1)+" -t ", (t-1)] to ["-s "+num2str(s-1)+" -t " +num2str(t-1)].
insert image description here

Draw the image after solving.
insert image description here

It is found that the -st parameter has a great influence on the accuracy of SVM, and the -s type uses the SVM setting type (default 0 - C-SVC), that is, classification. The correct selection of the kernel function depends on the characteristics of the actual problem that generates the classification problem, because different actual problems have different measures of similarity, the kernel function can be regarded as a process of feature extraction, and choosing the correct kernel function can help improve the classification accuracy Rate.
Find the optimal parameters of svm.
insert image description here

3.3 knn

Use knn to classify it, and the parameter k takes 1 to 25. And calculate the mean value of the accuracy after five second divisions, and save the data of 25 parameters of the four data dimensionality reduction methods into the knn_acc matrix.
insert image description here
Draw the image of the accuracy change under each parameter of the knn classifier, the method is similar to the decision tree.
insert image description here
Find the optimal parameters of knn.
insert image description here

4. Visualization

The reading and division are basically the same as before. You only need to set no_dims to 2, that is, reduce the dimension to two dimensions for easy visualization, and run and save the dataset after changing the dimension. Then read the data set directly, and divide the training set and test set. i is the data processing method, j is the test point after all dimensionality reduction, and k is the label of the test point (used to give color).
insert image description here
Results are visualized.
insert image description here
Here first take the random numbers of the three primary colors. The code theory for taking random colors is to generate 3*6 points, 6 different colors and 3 points for each color, but since 'color', [range, range, range] has the same color, it cannot be seen Label results of the test set after dimensionality reduction. Try to force him to get a different color through the randperm function.
Then visualize the decision tree, KNN, and SVM. First set the window so that they can be displayed at the same time, and then give the points with the same label the same color. Finally, pay attention to the i in the title. There are many variables here and it is easy to confuse.
insert image description here

5. Results display

1. Display the images before and after resize, including the original color image, grayscale image, and grayscale image after resize.

insert image description here
insert image description here

2. Draw two graphs to show the accuracy changes of the four feature extraction algorithms when the parameters of the SVM and KNN classifiers are changed.

insert image description here

3. After determining the optimal parameters, use the three classifiers under the optimal parameters for classification, and show the classification accuracy of the four algorithms under the classifier (the average accuracy of the five sets of data).

         PCA  	    LPP 	ISOMAP       LLE
决策树	0.2605	   0.2199	0.3333	    0.2508
SVM	    0.1584	   0.1544	0.4128	    0.1739
KNN	    0.3344	   0.1792	0.4298	    0.3466

It is found that the accuracy of SVM classifiers is generally not high. Check how SVM can get good results: You can ① normalize the data (simple scaling). ②Number of samples>>Number of features: If you want to use a linear model, you can use liblinear and use the -s 2 parameter. ③svmtrain can only handle binary classification problems, so when multi-classification needs to be changed to 1v1, and then vote to decide. Among them, ③ is very reasonable, and you can try it further during the winter vacation.

4. After determining the optimal parameters, visualize the distribution of six types of images for the classification results. Three classifiers are used for three graphs, and each graph shows the visualization results of four algorithms (the classification results of a set of data are taken).

4.1 Decision tree

insert image description here

4.2 KNN

insert image description here

4.3 SVM

insert image description here

6. Source code

%% resize 1001 Abnormal Image Dataset数据库中的图像
%% 设置参数
clc;clear;close all;
% warning('off') %关掉警告
base_path = 'D:\Desktop\大三上\机器学习\机器学习考查实验\1001 Abnormal Image Dataset';
path = string();
subpath = dir( base_path );
%% 读取指定路径单文件夹下,all文件夹内的all图像
all_imgnum=0;
for i = 1:length(subpath)-2 % 读取单文件夹下,all文件夹% 1,2分别是.和..% 先获取第一个子文件路径,然后获取第二个子文件路径
    sub_path = fullfile(base_path, subpath(i+2).name);% disp(sub_path); % D:\Desktop\大三上\机器学习\机器学习考查实验\1001 Abnormal Image Dataset\Aeroplane
    image_name = dir(sub_path); % 获取文件夹里的所有图像信息% disp(image_name(3).name); % 1.jpg
    img_num=length(image_name)-2; % 文件夹里图像个数% disp(img_num); % 100 114 110 109 85 108
    all_imgnum=img_num+all_imgnum;% disp(all_imgnum); % 100 214 324 433 518 626
    %% 获取图片数据
    for j = 1:img_num % 获取子文件夹下图片的路径
        % fullfile函数利用文件各部分信息创建并合成完整文件名
        img_path = fullfile(sub_path, image_name(j+2).name); % 子文件夹+图片名称
        read_img = imread(img_path); % 读图
        if(ndims(read_img)==3)
            read_img = rgb2gray(read_img);  % RGB图像转灰度图像
        end
        image = double(imresize(read_img, [32,32]));  % 图片统一大小,指定长宽[32,32]
        phi(all_imgnum-img_num+j,:)=double(reshape(image,1,[])); % 存放每个图片data于数组phi_cell,一行存放一个图像
    end
    %% 存放图片label于矩阵phi_label % 定义标签,1:Aeroplane 2:Boat 3:Car 4:Chair 5:Motorbike 6:Sofa
    phi_label(1,all_imgnum-img_num+1:all_imgnum)=i*ones(1,img_num);% 行拼接每个标签给对应的图片个数拼成一行
end
%% 保存划分后的数据集
save('Xzong0.mat','phi'); % 存数据
save('Lzong0.mat','phi_label');% 存标签
% 2.使用PCA、LPP、LLE、ISOMAP先对resize后的数据进行特征提取,
% 然后使用SVM分类器、KNN分类器、决策树进行分类。(参考实验8、10、11、12)
% warning('off') %关掉警告
%% 读取数据集
load Xzong0.mat
load Lzong0.mat
%% 设置参数
no_dims = 50; % no_dims设置为50,或者根据特征值数目设置
%% PCA 主成分分析法
[mappedXpca1, mappingpca1] = pca(phi, no_dims);
dealX{
    
    1,1} = mappedXpca1;
dealY{
    
    1,1} = phi_label;
%% LPP 局部保留投影
% 降低空间维度的同时,能较好的保持内部固定的局部结构,
% 并且它对异化值(通常理解为错误的点,或者为污点)不敏感,与PCA相区别
% [mappedX, mapping] = lpp(X, no_dims, k, sigma, eig_impl)
% 错误使用 lpp (line 24) Number of samples should be higher than number of
% dimensions.样本数量应该大于维度数量。解决:注释掉
[mappedXlpp1, mappinglpp1] = lpp(phi, no_dims);
dealX{
    
    1,2} = mappedXlpp1;
dealY{
    
    1,2} = phi_label;
%% ISOMAP 等距特征映射
[mappedXisomap1, mappingisomap1,conn_comp1] = isomap(phi, no_dims,5);
dealX{
    
    1,3} = mappedXisomap1;
for j = conn_comp1
    isomaplable = phi_label(j);
end
dealY{
    
    1,3} = isomaplable;
%% LLE 局部线性嵌入
% 降维后矩阵大小变小,因此读出对应的标签并存储
[mappedXlle1, mappinglle1,conn_comp] = lle(phi, no_dims);
dealX{
    
    1,4} = mappedXlle1;
for j = conn_comp
    llelable = phi_label(j);
end
dealY{
    
    1,4} = llelable;
%% 存数据集
save('dealX0zong.mat','dealX'); % 存数据
save('dealY0zong.mat','dealY'); % 存数据
%% 然后使用SVM分类器、KNN分类器、决策树进行分类。(参考实验8、10、11、12)
%% 读取数据集
clc;clear;close all;
load dealX0zong.mat
load dealY0zong.mat
sizei=size(dealX);
for i = 1:sizei(2)         % 这个4指的是对于每一个数据处理方法
    X = dealX{
    
    1,i};
    Y = dealY{
    
    1,i};
    Y = Y' ;
    for j = 1:5
        fprintf("loading %d %d\n", i, j);
       %% 划分数据集
        e=round(length(X(:,1))*3/5); 
        num=randperm(length(X)); %打乱列序列 
        train_num=num(1:e); %取打乱序列的前60%
        test_num=num(e+1:end); %取打乱序列的后40% %end直到数组结尾
        % 划分data和label
        traindata=X(train_num,:); % 留出法的训练集
        trainlabel=Y(train_num,:); % 训练集标签
        testdata=X(test_num,:); % 留出法的测试集
        testlable=Y(test_num,:); % 测试集标签
       %% 实验八的决策树,精度矩阵dt_acc(4*5)
        [test_targets]= C4_5(traindata', trainlabel', testdata', 5,10);
        temp_count = 0;
        for k = 1:length(testlable)
            if(testlable(k) == test_targets(k))
                temp_count = temp_count + 1;
            end
        end
        dt_acc(i,j) = temp_count/length(testlable);
       %% 实验十的svm,精度矩阵svm_acc(5*25),外层循环处理后为4*25
       count = 1;
%        svm_arg = ["-t 0 -g 0.8","-t 1 -g 0.1","-t 2 -g 3.2","-t 2 -g 6.4","-t 2 -g 12.8"];
%        for h = 1:length(svm_arg)
%                % 训练不同的参数设置
%                model1 = svmtrain(trainlabel',traindata,svm_arg(h));
%                [predicted_label, accuracy_mse,decision_values_prob_estimates]=svmpredict(testlable, testdata, model1);
%                % 获取不同参数下的分类精度
%                svm_acc222(j,count)=accuracy_mse(1)/100;
%                count = count + 1;
%        end
      %% 实验十的svm,精度矩阵svm_acc(5*25),外层循环处理后为4*25
       for s = 1:5
           for t = 1:5
               xticks{count} = sprintf('%s%d%s%s%d','-s ',s-1,',','-t ',t-1); % 将不同的参数设置保存
               % 不同的参数设置用来训练
               model = svmtrain(trainlabel,traindata,["-s "+num2str(s-1)+" -t "+num2str(t-1)]);
               [predicted_label, accuracy_mse,decision_values_prob_estimates]=svmpredict(testlable, testdata, model);
               % 获取不同参数下的分类精度
               svm_acc(j,count)=accuracy_mse(1)/100;
               count = count + 1;
           end
       end
       %% 实验十的knn,精度矩阵knn_acc1(5*25),外层循环处理后为4*25
        for k = 1:25
            [Class] = cvKnn(testdata', traindata', trainlabel',k);
            count = 0;
            for n = 1:length(Class)
                if(Class(n)==testlable(n))
                    count = count + 1;
                end
            end
            knn_acc1(j, k) = count / length(testlable);
        end
    end
    for k2 = 1:length(knn_acc1(1,:))
        knn_acc(i, k2) = mean(knn_acc1(:,k2));
    end
    % 在外层循环求svm的精度矩阵svm_acc的平均值(1*25),得到四种方法的五次二划分平均值矩阵(4*25)
    for k = 1:length(svm_acc(1,:))
        svm_accs(i, k) = mean(svm_acc(:,k));
    end
%     svm_accs111(1,i) = mean(acc);
%     for k = 1:length(svm_acc(1,:))
%         svm_accs333(i, k) = mean(svm_acc222(:,k));
%     end
end
%% 存数据集
save('dt_acc.mat','dt_acc'); % 存数据
save('svm_acc.mat','svm_acc'); % 存数据
save('knn_acc.mat','knn_acc'); % 存数据
save('svm_accs.mat','svm_accs'); % 存数据
%% 3.选取出最优参数、给出最优精度。(参考实验11、12)
%% 读取数据集
load dt_acc.mat
load svm_acc.mat
load knn_acc.mat
load svm_accs.mat
%% 图1 决策树的精度画图,4*5的矩阵,每一列是每一个降维
figure(1);%subplot(2,2,1)
x = [1:length(dt_acc(1,:))];
color = ['r','b','g','y','w','k'];
for i = 1:length(dt_acc(:,1))
    plot(x, dt_acc(i,:), color(i),'linewidth',5);
    hold on;
end
legend('PCA','lpp','isomap','lle');
title('四种方法的五次划分决策树准确率');
xlabel('次数');
ylabel('准确率');
set(gca,'fontsize',30);
%% 5. 分类精度为5组训练集和测试集上的平均值
fprintf('PCA,lpp,isomap,lle的5次决策树准确率分别为')
for i = 1:length(dt_acc)-1
    fprintf('%f,',mean(dt_acc(i,:)))
end
fprintf('svm最佳准确率为')
% disp(svm_accs111);
%% 图2 svm的精度画图,4*25的矩阵,每一列是不同的参数
figure(2);%subplot(2,2,2)
for i = 1:4
    x = [1:25];
    plot(x,svm_accs(i,:),color(i),'LineWidth',2);
    hold on;
end
axis([-inf,inf,0 1]);
xlabel('-s和-t参数变化组合');
ylabel('准确率');
title('svm 各参数下的准确率');
% 设置横坐标
set(gca,'XTickLabel',xticks,'XTickLabelRotation',80,'XTick',[1:1:25],'fontsize',30);
legend('PCA','lpp','isomap','lle');
%% 图3 knn的精度画图,4*25的矩阵,每一列是不同的参数
figure(3);%subplot(2,2,4)
for i = 1:4
    x = [1:25];
    plot(x,knn_acc(i,:),color(i),'LineWidth',2);
    hold on;
end
axis([-inf,inf,0 1]);
xlabel('k参数');
ylabel('准确率');
title('k 调参后的准确率');
% 设置横坐标
set(gca,'XTick',[1:25],'fontsize',30);
legend('PCA','lpp','isomap','lle');
%% 3. 进行SVM、KNN分类器参数变化下的分类实验,并选取出最优参数、给出最优精度。(参考实验11、12)
method = ['PCA', 'LPP','ISO','LLE'];
%% 寻找KNN最优参数
for i = 1:length(knn_acc(:,1))
   [m, index] = max(knn_acc(i,:));
   fprintf("降维方法 %c%c%c 对应的KNN最优参数:%d  ",method((3*i-2):3*i),index);
   fprintf("分类精度:%f\n", m);
end
%% 寻找SVM最优参数
for i = 1:length(svm_accs(:,1))
    [m, index] = max(svm_accs(i,:));
    fprintf("降维方法 %c%c%c 对应的SVM最优参数:%c%c%c%c%c%c%c%c%c",method((3*i-2):3*i),xticks(index));
   fprintf("分类精度:%f\n", m);
end
%% 寻找SVM最优参数
for i = 1:length(svm_accs(:,1))
    [m, index] = max(svm_accs(i,:));
    fprintf("降维方法 %c%c%c 对应的SVM最优参数:-s 21,-t 3",method((3*i-2):3*i));
   fprintf("分类精度:%f\n", m);
end
%% 4. 使用最优参数进行分类,对六类图像的分类结果进行可视化。(参考实验7)
load dealX01zong.mat
load dealY01zong.mat
sizei=size(dealX);
for i = 1:sizei(2)         % 这个4指的是对于每一个数据处理方法
    X = dealX{
    
    1,i};
    Y = dealY{
    
    1,i};
    Y = Y';
    a = randperm(length(X));
    train_num = a( 1,1:round(length(a)*3/5 ) );
    test_num  = a( 1,round( length(a)*3/5):end );
    Xtrain = double(X(train_num,:));
    Xlabel = double(Y(train_num,:));
    Ytest  = double(X(test_num,:));
    Ylabel  = double(Y(test_num,:));
    %% 绘图点的颜色取三原色随机数
    c=randperm(1000,6)/1000; % 取6个[0,1]之间不重复的随机数
    d=randperm(1000,6)/1000;
    e=randperm(1000,6)/1000;
    method = ['PCA', 'LPP','ISO','LLE'];
    %% 决策树分类可视化
    figure(i+4)
    [test_targets]= C4_5(Xtrain', Xlabel, Ytest', 5,10);
    for j = 1:length(test_targets)
        % 根据六种标签绘图
        k =  test_targets(j);
        plot(Ytest(j,1),Ytest(j,2),'*','MarkerFaceColor',[c(k),d(k),e(k)]);
        title([mat2str(method((3*i-2):3*i)),'方法下决策树分类可视化']);
        hold on; 
    end
    %% KNN分类可视化
    figure(i+8)
    g=[13,21,23,3];
    [Class] = cvKnn(Ytest', Xtrain', Xlabel',g(i));
    for j = 1:length(Class)
        % 根据六种标签绘图
        k =  Class(j);
        plot(Ytest(j,1),Ytest(j,2),'*','MarkerFaceColor',[c(k),d(k),e(k)]) 
        hold on;
    end
    title([mat2str(method((3*i-2):3*i)),'方法下,k=',g(i),'时的KNN分类图']);
    %% SVM分类可视化
    figure(i+12)
    model = svmtrain(Xlabel,Xtrain,["-s 1 -t 3"]);
    [predicted_label, accuracy_mse,decision_values_prob_estimates]=svmpredict(Ylabel, Ytest, model);
    for j = 1:length(predicted_label)
        % 根据六种标签绘图
        k =  predicted_label(j);
        plot(Ytest(j,1),Ytest(j,2),'*','MarkerFaceColor',[c(k),d(k),e(k)]) 
        hold on;
    end
    title([mat2str(method((3*i-2):3*i)),'方法下s=0,t=4的SVM分类图']);
end

Guess you like

Origin blog.csdn.net/wtyuong/article/details/122629127