京東金融大數據競賽豬臉識別(9)- 識別方法之五
阿新 • • 發佈:2018-03-14
圖像識別 深度網絡 預訓練模型 這裏給出使用深度網絡中間層輸出結果作為圖像特征,並構建分類模型和對訓練數據進行識別的代碼。相關內容可參看Matlab圖像識別/檢索系列(7)-10行代碼完成深度學習網絡之取中間層數據作為特征。代碼如下:
clear trainPath = fullfile(pwd,‘image‘); trainData = imageDatastore(trainPath,... ‘IncludeSubfolders‘,true,‘LabelSource‘,‘foldernames‘); %對訓練數據集進行劃分 [trainingImages,testImages] = splitEachLabel(trainData,0.7,‘randomized‘); numTrainImages = numel(trainingImages.Labels); %設定要加載的預訓練模型 % net1 = googlenet; % net2 = vgg16; % net = vgg19; net = alexnet; layer = ‘fc7‘; %提取第7層網絡輸出數據 trainingFeatures = activations(net,trainingImages,layer); testFeatures = activations(net,testImages,layer); trainingLabels = trainingImages.Labels; testLabels = testImages.Labels; %訓練多分類模型 classifier = fitcecoc(trainingFeatures,trainingLabels, ‘FitPosterior‘,1); %預測數據的類別 predictedLabels = predict(classifier,testFeatures); [label,NegLoss,PBScore,Posterior] = predict(classifier,testFeatures); idx = [1 5 10 15]; figure for i = 1:numel(idx) subplot(2,2,i) I = readimage(testImages,idx(i)); label = predictedLabels(idx(i)); imshow(I); title(char(label)); end %計算準確率 accuracy = mean(predictedLabels == testLabels);
該方法在csv文件上傳後得分在1左右,排名約在前8%。這段代碼不是比賽用的完整代碼,不過用法類似。
京東金融大數據競賽豬臉識別(9)- 識別方法之五