機械学習初心者です。
以前はOpenCVでSVMを使って分類していたのですが、今回sklearnで分類をしようと考えています
このようにコードを作ってみたのですが、最後のSVMの使用の際にエラーが発生します。
原因やコードの間違いを分かるかたがいらしたら教えてください。
以下が実際につくったコードです。
import glob import cv2 import numpy as np import time from tqdm import tqdm from sklearn.svm import SVC # SVM用 #import lightgbm as lgb from sklearn.metrics import confusion_matrix #混同行列 from sklearn.metrics import accuracy_score, precision_score #適合率 from sklearn.metrics import recall_score, f1_score #再現率,F1スコア from sklearn.metrics import make_scorer # 特徴量抽出器 def create_images_array(load_img_paths): imgs=[] #Hog特徴量パラメータ win_size = (64,64) block_size = (16,16) block_stride = (4,4) cell_size = (4,4) #binsは方向の数 bins = 9 #画像群の配列を生成 for load_img_path in tqdm(load_img_paths): #画像をロードし、グレースケール変換 #色反転、64*64にリサイズ、1次元配列に変換 img = cv2.imread(load_img_path) gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) gray = cv2.resize(gray,win_size) hog = cv2.HOGDescriptor(win_size, block_size, block_stride, cell_size, bins) img = hog.compute(gray) imgs.append(img) return np.array(imgs, np.float32) def main(): t1 = time.time() # 学習用の画像ファイルの格納先(FEのT1~T8,WのT1~T8) LOAD_TRAIN_IMG1S_PATH = './preprocess_images/case3/train/T1-FE/*' LOAD_TRAIN_IMG2S_PATH = './preprocess_images/case3/train/T2-FE/*' LOAD_TRAIN_IMG3S_PATH = './preprocess_images/case3/train/T3-FE/*' LOAD_TRAIN_IMG4S_PATH = './preprocess_images/case3/train/T4-FE/*' LOAD_TRAIN_IMG5S_PATH = './preprocess_images/case3/train/T5-FE/*' LOAD_TRAIN_IMG6S_PATH = './preprocess_images/case3/train/T6-FE/*' LOAD_TRAIN_IMG7S_PATH = './preprocess_images/case3/train/T7-FE/*' LOAD_TRAIN_IMG8S_PATH = './preprocess_images/case3/train/T8-FE/*' # 学習用の画像ファイルのパスを取得 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) # 学習用の画像ファイルをロードし特徴量抽出 imgs1 = create_images_array(load_img1_paths) imgs2 = create_images_array(load_img2_paths) imgs3 = create_images_array(load_img3_paths) imgs4 = create_images_array(load_img4_paths) imgs5 = create_images_array(load_img5_paths) imgs6 = create_images_array(load_img6_paths) imgs7 = create_images_array(load_img7_paths) imgs8 = create_images_array(load_img8_paths) X_train = np.r_[imgs1, imgs2, imgs3, imgs4, imgs5, imgs6, imgs7, imgs8] # 正解ラベルを生成imgs.番号でラベルを決める labels1 = np.full(len(load_img1_paths), 1, np.int32) labels2 = np.full(len(load_img2_paths), 2, np.int32) labels3 = np.full(len(load_img3_paths), 3, np.int32) labels4 = np.full(len(load_img4_paths), 4, np.int32) labels5 = np.full(len(load_img5_paths), 5, np.int32) labels6 = np.full(len(load_img6_paths), 6, np.int32) labels7 = np.full(len(load_img7_paths), 7, np.int32) labels8 = np.full(len(load_img8_paths), 8, np.int32) Y_train = np.array([np.r_[labels1, labels2, labels3, labels4, labels5, labels6, labels7, labels8]) # 学習モデルを検証(Test) # テスト用の画像ファイルの格納先 LOAD_TEST_IMG1S_PATH = './preprocess_images/case3/test/T1-FE/*' LOAD_TEST_IMG2S_PATH = './preprocess_images/case3/test/T2-FE/*' LOAD_TEST_IMG3S_PATH = './preprocess_images/case3/test/T3-FE/*' LOAD_TEST_IMG4S_PATH = './preprocess_images/case3/test/T4-FE/*' LOAD_TEST_IMG5S_PATH = './preprocess_images/case3/test/T5-FE/*' LOAD_TEST_IMG6S_PATH = './preprocess_images/case3/test/T6-FE/*' LOAD_TEST_IMG7S_PATH = './preprocess_images/case3/test/T7-FE/*' LOAD_TEST_IMG8S_PATH = './preprocess_images/case3/test/T8-FE/*' # テスト用の画像ファイルパスの取得 test_img1_paths = glob.glob(LOAD_TEST_IMG1S_PATH) test_img2_paths = glob.glob(LOAD_TEST_IMG2S_PATH) test_img3_paths = glob.glob(LOAD_TEST_IMG3S_PATH) test_img4_paths = glob.glob(LOAD_TEST_IMG4S_PATH) test_img5_paths = glob.glob(LOAD_TEST_IMG5S_PATH) test_img6_paths = glob.glob(LOAD_TEST_IMG6S_PATH) test_img7_paths = glob.glob(LOAD_TEST_IMG7S_PATH) test_img8_paths = glob.glob(LOAD_TEST_IMG8S_PATH) #テスト用の画像をロードし特徴量抽出 test_imgs1 = create_images_array(test_img1_paths) test_imgs2 = create_images_array(test_img2_paths) test_imgs3 = create_images_array(test_img3_paths) test_imgs4 = create_images_array(test_img4_paths) test_imgs5 = create_images_array(test_img5_paths) test_imgs6 = create_images_array(test_img6_paths) test_imgs7 = create_images_array(test_img7_paths) test_imgs8 = create_images_array(test_img8_paths) X_test = np.r_[test_imgs1, test_imgs2, test_imgs3, test_imgs4, test_imgs5, test_imgs6, test_imgs7, test_imgs8 ] # 正解ラベルを生成 test_labels1 = np.full(len(test_img1_paths), 1, np.int32) test_labels2 = np.full(len(test_img2_paths), 2, np.int32) test_labels3 = np.full(len(test_img3_paths), 3, np.int32) test_labels4 = np.full(len(test_img4_paths), 4, np.int32) test_labels5 = np.full(len(test_img5_paths), 5, np.int32) test_labels6 = np.full(len(test_img6_paths), 6, np.int32) test_labels7 = np.full(len(test_img7_paths), 7, np.int32) test_labels8 = np.full(len(test_img8_paths), 8, np.int32) Y_test = np.array([np.r_[test_labels1, test_labels2, test_labels3, test_labels4, test_labels5,test_labels6, test_labels7, test_labels8 ]]) model_SVM = SVC(random_state=0, kernel = "rbf", C = 1000 , gamma = 0.01 ) #学習モデル構築。引数に訓練データの特徴量と、それに対応したラベル model_SVM.fit(X_train,Y_train) #予測したクラスラベル Y_pred = model_SVM.predict(X_test) # .scoreで正解率を算出。 print("\nSVM") print("train score:",model_SVM.score(X_train,Y_train)) print("test score:",model_SVM.score(X_test,Y_test)) t2 = time.time() elapsed_time = t2-t1 print(f"経過時間:{elapsed_time}") if __name__ == '__main__': main()
以下がエラーコードです。
ValueError: Found array with dim 3. Estimator expected <= 2.
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。