機械学習のカラー写真分類でHOGをインポートし局所特徴量を抽出して、クラスタリングを行いたいのですがクラスタリングを行うときにエラーがでてしまって躓いています。
使用するカラー写真はkeras.datasetsのcifar10.load_data()で訓練用とテスト用データがそれぞれ5万と1万個ずつはいっています。ラベルの数は10個です。
python
1#データの前処理 2n_samples = np.arange(10000) 3(X_train, labels_train),(X_test, labels_test) = cifar10.load_data() 4labels_train=labels_train.reshape(-1) 5labels_test=labels_test.reshape(-1) 6 7labels_train=labels_train[n_samples] #処理を軽くするためデータを1万個にする 8X_train=X_train[n_samples] 9 10#HOGの実行 11from skimage.color import rgb2gray#カラーからモノクロへ変換 12from skimage.feature import hog 13 14def get_descriptors(data): 15 orientations = 9 16 pixels_per_cell = (4,4) #写真を小領域に分割 17 cells_per_block = (3,3)#グリッドで局所特徴量を抽出 18 feature_vector = hog(rgb2gray(data), orientations, pixels_per_cell, cells_per_block) 19 return feature_vector.reshape(-1,np.multiply(*cells_per_block)* orientations) 20 21for data in X_train: 22 data_descriptors=get_descriptors(data) 23 24 25data_descriptors = np.array(data_descriptors) 26 27#クラスタリングでエラーが発生 28from sklearn.cluster import MiniBatchKMeans 29np.random.seed(0) 30 31codebook_size = 1000 32 33descriptors = np.vstack(data_descriptors[X_test]) 34indices = np.random.choice(np.arange(len(descriptors)), size=500000, replace=False) 35kmeans = MiniBatchKMeans(n_clusters=codebook_size, n_init=10, random_state=0) 36kmeans.fit(descriptors[indices].astype(float)) 37del descriptors, indicesIndexError: index 59 is out of bounds for axis 0 with size 36
IndexError Traceback (most recent call last)
<ipython-input-12-46d604a03021> in <module>()
5 codebook_size = 1000
6
----> 7 descriptors = np.vstack(data_descriptors[X_test])
8 indices = np.random.choice(np.arange(len(descriptors)), size=500000, replace=False)
9 kmeans = MiniBatchKMeans(n_clusters=codebook_size, n_init=10, random_state=0)
IndexError: index 158 is out of bounds for axis 0 with size 36