画像の特徴量をだしてそれをt-SNEで二次元化してそれをマップにプロットしようと考えています。
Xが求めた特徴量を格納したもので、labelはXに対応したラベルを示しています。
マップのプロットのところでエラーが発生しまして以下にコードとエラー内容を示します。
分かる方がいらしたら教えてください。
最初に全体のコードです。
# -*- coding: utf-8 -* import glob import cv2 import numpy as np import time from tqdm import tqdm from skimage import feature,exposure from keras.models import model_from_json from keras.utils.vis_utils import plot_model from keras.models import Model import pandas as pd from matplotlib import pylab as plt from sklearn.manifold import TSNE from sklearn.preprocessing import StandardScaler from sklearn.preprocessing import MinMaxScaler # 特徴量抽出 def create_images_array(load_img_paths,model,layer_name): imgs=[] middle_layer_model = Model(inputs=model.input, outputs=model.get_layer(layer_name).output) for load_img_path in tqdm(load_img_paths): img = cv2.imread(load_img_path) #型を合わせる。255は正規化のため。 target = np.reshape(img, (1, img.shape[0], img.shape[1], img.shape[2])).astype('float') / 255.0 middle_output = middle_layer_model.predict(target) imgs.append(middle_output) return np.array(imgs, np.float32) def tSNE(X,seikika): #データの正規化 seikika.fit(X) X_pre_emb = seikika.transform(X) #tSNEの適用 tsne = TSNE(n_components=2, random_state=0) X_embedded = tsne.fit_transform(X_pre_emb) #tSNEで次元削減したデータの正規化 X_2d= seikika.fit_transform(X_embedded) print(X_2d.shape) #print(X_2d) return X_2d def main(): t1 = time.time() json_string = open('./preprocess/model.json').read() model = model_from_json(json_string) model.load_weights('./preprocess/weight.hdf5') layer_name = 'conv5_block3_out' # 学習用の画像ファイルの格納先(FEのT1~T8,WのT1~T8) LOAD_TRAIN_IMG1S_PATH = './preprocess_images_kears/case3/train/T1-FE/*' LOAD_TRAIN_IMG2S_PATH = './preprocess_images_keras/case3/train/T2-FE/*' LOAD_TRAIN_IMG3S_PATH = './preprocess_images_keras/case3/train/T3-FE/*' LOAD_TRAIN_IMG4S_PATH = './preprocess_images_keras/case3/train/T4-FE/*' LOAD_TRAIN_IMG5S_PATH = './preprocess_images_keras/case3/train/T5-FE/*' LOAD_TRAIN_IMG6S_PATH = './preprocess_images_keras/case3/train/T6-FE/*' LOAD_TRAIN_IMG7S_PATH = './preprocess_images_keras/case3/train/T7-FE/*' LOAD_TRAIN_IMG8S_PATH = './preprocess_images_keras/case3/train/T8-FE/*' LOAD_TRAIN_IMG9S_PATH = './preprocess_images_keras/case3/train/T1-W/*' LOAD_TRAIN_IMG10S_PATH = './preprocess_images_keras/case3/train/T2-W/*' LOAD_TRAIN_IMG11S_PATH = './preprocess_images_keras/case3/train/T3-W/*' LOAD_TRAIN_IMG12S_PATH = './preprocess_images_keras/case3/train/T4-W/*' LOAD_TRAIN_IMG13S_PATH = './preprocess_images_keras/case3/train/T5-W/*' LOAD_TRAIN_IMG14S_PATH = './preprocess_images_keras/case3/train/T6-W/*' LOAD_TRAIN_IMG15S_PATH = './preprocess_images_keras/case3/train/T7-W/*' LOAD_TRAIN_IMG16S_PATH = './preprocess_images_keras/case3/train/T8-W/*' # 学習用の画像ファイルのパスを取得 load_img1_paths = glob.glob(LOAD_TRAIN_IMG1S_PATH) load_img2_paths = glob.glob(LOAD_TRAIN_IMG2S_PATH) load_img3_paths = glob.glob(LOAD_TRAIN_IMG3S_PATH) load_img4_paths = glob.glob(LOAD_TRAIN_IMG4S_PATH) load_img5_paths = glob.glob(LOAD_TRAIN_IMG5S_PATH) load_img6_paths = glob.glob(LOAD_TRAIN_IMG6S_PATH) load_img7_paths = glob.glob(LOAD_TRAIN_IMG7S_PATH) load_img8_paths = glob.glob(LOAD_TRAIN_IMG8S_PATH) load_img9_paths = glob.glob(LOAD_TRAIN_IMG9S_PATH) load_img10_paths = glob.glob(LOAD_TRAIN_IMG10S_PATH) load_img11_paths = glob.glob(LOAD_TRAIN_IMG11S_PATH) load_img12_paths = glob.glob(LOAD_TRAIN_IMG12S_PATH) load_img13_paths = glob.glob(LOAD_TRAIN_IMG13S_PATH) load_img14_paths = glob.glob(LOAD_TRAIN_IMG14S_PATH) load_img15_paths = glob.glob(LOAD_TRAIN_IMG15S_PATH) load_img16_paths = glob.glob(LOAD_TRAIN_IMG16S_PATH) # 学習用の画像ファイルをロードし特徴量抽出 imgs1 = create_images_array(load_img1_paths,model,layer_name) imgs2 = create_images_array(load_img2_paths,model,layer_name) imgs3 = create_images_array(load_img3_paths,model,layer_name) imgs4 = create_images_array(load_img4_paths,model,layer_name) imgs5 = create_images_array(load_img5_paths,model,layer_name) imgs6 = create_images_array(load_img6_paths,model,layer_name) imgs7 = create_images_array(load_img7_paths,model,layer_name) imgs8 = create_images_array(load_img8_paths,model,layer_name) imgs9 = create_images_array(load_img9_paths,model,layer_name) imgs10 = create_images_array(load_img10_paths,model,layer_name) imgs11 = create_images_array(load_img11_paths,model,layer_name) imgs12 = create_images_array(load_img12_paths,model,layer_name) imgs13 = create_images_array(load_img13_paths,model,layer_name) imgs14 = create_images_array(load_img14_paths,model,layer_name) imgs15 = create_images_array(load_img15_paths,model,layer_name) imgs16 = create_images_array(load_img16_paths,model,layer_name) X = np.r_[imgs1, imgs2, imgs3, imgs4, imgs5, imgs6, imgs7, imgs8, imgs9, imgs10, imgs11, imgs12, imgs13, imgs14, imgs15, imgs16] # 正解ラベルを生成imgs.番号でラベルを決める #2種類分類 labels1 = np.full(len(load_img1_paths), 0, np.int32) labels2 = np.full(len(load_img2_paths), 0, np.int32) labels3 = np.full(len(load_img3_paths), 0, np.int32) labels4 = np.full(len(load_img4_paths), 0, np.int32) labels5 = np.full(len(load_img5_paths), 0, np.int32) labels6 = np.full(len(load_img6_paths), 0, np.int32) labels7 = np.full(len(load_img7_paths), 0, np.int32) labels8 = np.full(len(load_img8_paths), 0, np.int32) labels9 = np.full(len(load_img9_paths), 1, np.int32) labels10 = np.full(len(load_img10_paths), 1, np.int32) labels11 = np.full(len(load_img11_paths), 1, np.int32) labels12 = np.full(len(load_img12_paths), 1, np.int32) labels13 = np.full(len(load_img13_paths), 1, np.int32) labels14 = np.full(len(load_img14_paths), 1, np.int32) labels15 = np.full(len(load_img15_paths), 1, np.int32) labels16 = np.full(len(load_img16_paths), 1, np.int32) label = np.r_[labels1, labels2, labels3, labels4, labels5, labels6, labels7, labels8, labels9, labels10, labels11, labels12, labels13, labels14, labels15, labels16] #正規化 seikika = StandardScaler() #seikika = MinMaxScaler() X_embeddedsc = tSNE(X,seikika) #print(X_embeddedsc) # グラフを横長にする from matplotlib.pylab import rcParams rcParams['figure.figsize'] = 15, 6 import matplotlib as mpl mpl.rcParams['font.family'] = ['serif'] #8種類分類のプロット print("-----------------------------------------------------------") # 可視化の用意 color_list = ["blue", "red"] marker_list = ["o", "^"] label_list = ["FE", "W"] plt.figure(figsize=(7,7)) plt.title("feautures") #plt.tick_params(labelsize=16) plt.xlabel("t-SNE comp-1") plt.ylabel("t-SNE comp-2") for index in range(len(np.unique(label))): plt.scatter(X_embeddedsc[label==index,0], X_embeddedsc[label==index,1], edgecolor=color_list[index],color=color_list[index], marker=marker_list[index], label=label_list[index]) plt.legend(loc="upper right") plt.show() print("-----------------------------------------------------------\n\n") t2 = time.time() elapsed_time = (t2-t1)/3600 print(f"経過時間:{elapsed_time}") if __name__ == '__main__': main()
のように一通り簡単にコードを書いてみたのですが、次のエラーが発生しました。
Traceback (most recent call last): File "C:\Users\Desktop\tSNE_CNN.py", line 242, in <module> main() File "C:\Users\Desktop\tSNE_CNN.py", line 126, in main X = np.r_[imgs1, imgs2, imgs3, imgs4, imgs5, imgs6, imgs7, imgs8, imgs9, imgs10, imgs11, imgs12, imgs13, imgs14, imgs15, imgs16] File "C:\anaconda\lib\site-packages\numpy\lib\index_tricks.py", line 406, in __getitem__ res = self.concatenate(tuple(objs), axis=axis) File "<__array_function__ internals>", line 6, in concatenate ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 5 dimension(s)
コードのエラーを読み取るにコードの最後当たりの場所の以下のコードがエラーの原因と考えます
for index in range(len(np.unique(label))): plt.scatter(X_embeddedsc[label==index,0], X_embeddedsc[label==index,1], edgecolor=color_list[index],color=color_list[index], marker=marker_list[index], label=label_list[index]) plt.legend(loc="upper right")
そのコードの中でもの以下の書き方が分からず困っています。X_embeddedsにたいしてラベル付けされたものを引き出すにはどのようにコードを書けばいいでしょうか。
X_embeddedsc[label==index,0], X_embeddedsc[label==index,1]
回答1件
あなたの回答
tips
プレビュー