├── 机器学习课程设计_20170311.pdf ├── matlab_cnn ├── classifier.mat ├── DeepLearningImageClassification.m ├── DeepLearningImageClassification.mlx └── predictCategory.m ├── 设计报告 ├── 设计报告 章程 2015010912010.pdf ├── 设计报告 章程 2015010912010.docx ├── DeepLearningImageClassification.pdf └── ImageCategoryClassificationTrain.pdf ├── matlab_bagofwords ├── classifier.mat ├── ImageCategoryClassificationTrain.m ├── ImageCategoryClassificationTrain.mlx └── predictCategory.m └── README.md /机器学习课程设计_20170311.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chengzhag/matlab_image_classification/HEAD/机器学习课程设计_20170311.pdf -------------------------------------------------------------------------------- /matlab_cnn/classifier.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chengzhag/matlab_image_classification/HEAD/matlab_cnn/classifier.mat -------------------------------------------------------------------------------- /设计报告/设计报告 章程 2015010912010.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chengzhag/matlab_image_classification/HEAD/设计报告/设计报告 章程 2015010912010.pdf -------------------------------------------------------------------------------- /matlab_bagofwords/classifier.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chengzhag/matlab_image_classification/HEAD/matlab_bagofwords/classifier.mat -------------------------------------------------------------------------------- /设计报告/设计报告 章程 2015010912010.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chengzhag/matlab_image_classification/HEAD/设计报告/设计报告 章程 2015010912010.docx -------------------------------------------------------------------------------- /设计报告/DeepLearningImageClassification.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chengzhag/matlab_image_classification/HEAD/设计报告/DeepLearningImageClassification.pdf -------------------------------------------------------------------------------- /设计报告/ImageCategoryClassificationTrain.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chengzhag/matlab_image_classification/HEAD/设计报告/ImageCategoryClassificationTrain.pdf -------------------------------------------------------------------------------- /matlab_cnn/DeepLearningImageClassification.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chengzhag/matlab_image_classification/HEAD/matlab_cnn/DeepLearningImageClassification.m -------------------------------------------------------------------------------- /matlab_cnn/DeepLearningImageClassification.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chengzhag/matlab_image_classification/HEAD/matlab_cnn/DeepLearningImageClassification.mlx -------------------------------------------------------------------------------- /matlab_bagofwords/ImageCategoryClassificationTrain.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chengzhag/matlab_image_classification/HEAD/matlab_bagofwords/ImageCategoryClassificationTrain.m -------------------------------------------------------------------------------- /matlab_bagofwords/ImageCategoryClassificationTrain.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chengzhag/matlab_image_classification/HEAD/matlab_bagofwords/ImageCategoryClassificationTrain.mlx -------------------------------------------------------------------------------- /matlab_bagofwords/predictCategory.m: -------------------------------------------------------------------------------- 1 | function [categories,scores]=predictCategory(im,categoryClassifier) 2 | if nargin==1 3 | categoryClassifier=load('classifier.mat'); 4 | categoryClassifier=categoryClassifier.categoryClassifier; 5 | end 6 | [labelIdx, scores] = predict(categoryClassifier, im); 7 | categories=categoryClassifier.Labels(labelIdx); 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # project1_image_classification 2 | 3 | 基于matlab和bag of words的图像分类, 4 | 目录中不包含数据集! 5 | 6 | ## 设计目标 7 | 8 | - 输入一幅图像,输出标签 9 | - 可以固定图片大小 10 | 11 | ## 实现 12 | 13 | - 数据集:caltech101 14 | 15 | ### matlab + bag of words 16 | 17 | >改自ImageCategoryClassificationTrainSample 18 | 19 | - bag of words:利用matlab中bagOfFeatures函数提取SURF特征并K-means聚类构造“词典” 20 | - svm:利用trainImageCategoryClassifier函数训练线性SVM分类器 21 | 22 | 测试结果: 23 | - 训练集正确率:97.91% 24 | - 测试集正确率:30.11% 25 | 26 | 27 | ### matlab + cnn + svm 28 | 29 | > 改自DeepLearningImageClassificationSample 30 | 31 | - cnn:利用预先训练好的AlexNet CNN网络获取特征向量,由于AlexNet已经针对ImageNet上的众多样本进行了训练,从其中抽取的特征向量对于一般图像具有较强的区分能力 32 | - svm:fitcecoc函数可以方便地训练基于SVM的多分类分类器 33 | 34 | 测试结果: 35 | - 训练集正确率:99.67% 36 | - 测试集正确率:77.95% 37 | 38 | ### 运行说明 39 | 40 | - 两个实验文件夹都包含xml、m文件 41 | - xml为matlab2016b的新功能,旧版本可使用m文件 42 | - 两个文件夹都有predictCategory.m文件,该函数输入参数为一幅任意图形,输出参数为类别字符串的元胞数组 43 | 44 | -------------------------------------------------------------------------------- /matlab_cnn/predictCategory.m: -------------------------------------------------------------------------------- 1 | function [categories,scores]=predictCategory(im,categoryClassifier,convnet) 2 | if nargin==1 3 | categoryClassifier=load('classifier.mat'); 4 | categoryClassifier=categoryClassifier.classifier; 5 | convnet = helperImportMatConvNet('imagenet-caffe-alex.mat'); 6 | elseif nargin==2 7 | convnet = helperImportMatConvNet('imagenet-caffe-alex.mat'); 8 | end 9 | img = readAndPreprocessImage(im); 10 | featureLayer = 'fc7'; 11 | imageFeatures = activations(convnet, img, featureLayer); 12 | [categories, scores] = predict(categoryClassifier, imageFeatures); 13 | end 14 | 15 | %% 16 | % Note that other CNN models will have different input size constraints, 17 | % and may require other pre-processing steps. 18 | function Iout = readAndPreprocessImage(I) 19 | % Some images may be grayscale. Replicate the image 3 times to 20 | % create an RGB image. 21 | if ismatrix(I) 22 | I = cat(3,I,I,I); 23 | end 24 | 25 | % Resize the image as required for the CNN. 26 | Iout = imresize(I, [227 227]); 27 | 28 | % Note that the aspect ratio is not preserved. In Caltech 101, the 29 | % object of interest is centered in the image and occupies a 30 | % majority of the image scene. Therefore, preserving the aspect 31 | % ratio is not critical. However, for other data sets, it may prove 32 | % beneficial to preserve the aspect ratio of the original image 33 | % when resizing. 34 | end --------------------------------------------------------------------------------