教師なし学習,クラスタリングについて
Show older comments
以下のサイトを参考にクラスタリングを行いたいと考えています。
サイトを参考に,使用するプログラムコードは以下の通りです。
clear;clc;close all
% unzip the zip file of MearchData
unzip('MerchData.zip');
% import a pre-trained network called darknet19
net=darknet19;
% load the images into the image data store called imds
imds = imageDatastore('MerchData','IncludeSubfolders',true,'LabelSource','foldernames');
% use augmented image datastore for image augmentation
augImds=augmentedImageDatastore(net.Layers(1, 1).InputSize(1:2),imds);
% randomly extract image index to display some images
idx=randperm(numel(imds.Files),20);
% use readByIndex function to read images from the autmented datastore
imgEx=readByIndex(augImds,idx);
% to show the tiled images
figure;montage(imgEx.input);title('example of the dataset')
% Gather label information from the image datastore
Labels=imds.Labels;
% count the number of images
numClass=numel(countcats(Labels));
% feature extraction with the pre-trained network
feature=squeeze(activations(net,augImds,'avg1'));
figure;
% conduct a principal component analysis for the dimension reduction
A=pca(feature,"Centered",true);
subplot(1,2,1)
gscatter(A(:,1),A(:,2),Labels)
subplot(1,2,2)
% perform t-sne for the dimension reduction
T=tsne(feature');
gscatter(T(:,1),T(:,2),Labels)
% perform k-means algorithm
% please note that as the result is dependent on the initial point in the algorithm, the
% result would not be same
C=kmeans(feature',numClass,"Start","plus");
% confirm the number of images in the largest group
[~,Frequency] = mode(C);
sz=net.Layers(1, 1).InputSize(1:2);
% prepare a matrix to show the clustering result
I=zeros(sz(1)*numClass,sz(2)*Frequency,3,'uint8');
% loop over the class to display images assigned to the group
for i=1:numClass
% read the images assigned to the group
% use the function "find" to find out the index of the i-th group image
ithGroup=readByIndex(augImds,find(C==i));
% tile the images extracted above
I((i-1)*sz(1)+1:i*sz(1),1:sz(2)*numel(find(C==i)),:)=cat(2,ithGroup.input{:});
end
figure;imshow(I);title('result of the image clustering using k-means after feature extraction with darknet19')
このプログラムコードは,デフォルトで,既に用意してある画像データがあり,実行を押すと,そのデータをクラスタリングしていますが,
自分が用意した画像をこのプログラムコードを使ってクラスタリングするには,どうしたらよいでしょうか。
MerchDataがデフォルトのデータの画像フォルダだと思ったので,そこを自分が用意した画像フォルダの名前にしてもエラーが出てしまいます。
他に変更するところがあったら教えていただけると幸いです。
よろしくお願いいたします。
4 Comments
michio
on 24 Sep 2020
エラーの内容もあると問題点のヒントになりますので、エラーメッセージの記載もお願いいたします。
MerchData を確認すると、以下のようにクラス別に画像を分けて保存しています。上記コードをそのまま動かすにはその辺も同じように構成する必要があるかと思いますのでご注意ください。

Kaneko
on 24 Sep 2020
michio
on 24 Sep 2020
> 関数 UNZIP はファイル ''data_picture.zip'' を検出できませんでした。
とのメッセージが出ていますが、まず最初の
unzip('data_picture.zip');
の部分がうまくいっていないようです。ファイル(data_picture.zip)はカレントフォルダにありますか?
Kaneko
on 24 Sep 2020
Accepted Answer
More Answers (0)
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!