教師なし学習,クラスタリングについて

21 visualizzazioni (ultimi 30 giorni)
Kaneko
Kaneko il 24 Set 2020
Commentato: Kenta il 25 Set 2020
以下のサイトを参考にクラスタリングを行いたいと考えています。
サイトを参考に,使用するプログラムコードは以下の通りです。
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 Commenti
michio
michio il 24 Set 2020
> 関数 UNZIP はファイル ''data_picture.zip'' を検出できませんでした。
とのメッセージが出ていますが、まず最初の
unzip('data_picture.zip');
の部分がうまくいっていないようです。ファイル(data_picture.zip)はカレントフォルダにありますか?
Kaneko
Kaneko il 24 Set 2020
ありがとうございます。
data_pitureをzip型式にしたら
unzip('data_picture.zip');
の部分はうまくいきました。
しかし,次に以下のようなエラーが出ました。
エラー: SeriesNetwork/activations (line 790)
activations 用の入力イメージのサイズは、[256 256 3] 以上でなければなりません。
エラー: clustering (line 23)
feature=squeeze(activations(net,augImds,'avg1'));
使用している入力イメージは[28 28 1]なのでこのようなエラーが出たのだと考えてます。
入力イメージが[28 28 1]でもできるようにするにはどこかを,
この入力イメージのサイズに合わせなければいけないのでしょうか。
何度もすみません。よろしくお願いいたします。

Accedi per commentare.

Risposta accettata

michio
michio il 25 Set 2020
画像サイズは
augImds=augmentedImageDatastore(net.Layers(1, 1).InputSize(1:2),imds);
で darknet19 の入力層に合わせて変更されるようなコードになっています。詳細はこちら:https://jp.mathworks.com/help/deeplearning/ref/augmentedimagedatastore.html
エラーの原因はチャネル数かな?と推測しています。darknet19 は 256x256x3 ということで例えば RGB 画像入力が想定されていますが、使用されている画像は 28x28x1 ということでグレースケール画像ですね。
なので以下を試してみてください。色の前処理用のオプション 'ColorPreprocessing' を 'gray2rgb' に設定しています。
augImds=augmentedImageDatastore(net.Layers(1, 1).InputSize(1:2),imds,'ColorPreprocessing','gray2rgb');
augmentedImageDatastore が何をする関数かは是非
を確認してみてください。
  2 Commenti
Kaneko
Kaneko il 25 Set 2020
色の前処理オプションを設定したら,できました。
色々と教えていただき,ありがとうございました。
Kenta
Kenta il 25 Set 2020
こんにちは、私のファイルを実行していただきありがとうございます。28×28ですと、darknetを特徴抽出器として使うのではなく、cifar10などを28×28にリサイズして、グレースケールにて学習し、それを特徴抽出器として使うなどもよさそうですね。256×256のネットワークでもある程度よい特徴抽出はできるとは思いますが。

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!