前提・実現したいこと
PythonでCNNモデルを用い音声感情分類を行うシステムを作っています。
以下のエラーメッセージが発生しました。xとyのサイズを揃えないといけないと思うのですが、
どう修正すれば良いか調べてみても分からなかったため、どなたかご教授いただけませんでしょうか?
発生している問題・エラーメッセージ
(637, 2913, 40, 1) (762, 4) (450, 2913, 40, 1) (450, 4) Traceback (most recent call last): File "cnn_model.py", line 329, in <module> main() File "cnn_model.py", line 286, in main callbacks=[valid_metrics, chkPoint] File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler raise e.with_traceback(filtered_tb) from None File "/usr/local/lib/python3.7/dist-packages/keras/engine/data_adapter.py", line 1657, in _check_data_cardinality raise ValueError(msg) ValueError: Data cardinality is ambiguous: x sizes: 637 y sizes: 762 Make sure all arrays contain the same number of samples.
該当のソースコード
Python
1f_train_path = os.getcwd() + '/feature/Session' + fold + '/train/' 2 3def hard_label(path): 4 label=[] 5 f_list = list(ii for ii in sorted(glob.glob(path))) 6 for file_path in f_list: 7 with open(file_path, 'r') as file: 8 for line in file: 9 line_sp = line.replace('Happiness', '0') 10 line_sp = line_sp.replace('Anger', '1') 11 line_sp = line_sp.replace('Neutral', '2') 12 line_sp = line_sp.replace('Sadness', '3') 13 line_sp = line_sp.replace('\n', '') 14 line_sp = line_sp.split(',') 15 if (len(line_sp)==10): 16 if (line_sp[9] == '0') or (line_sp[9] == '1') or (line_sp[9] == '2') or (line_sp[9] == '3'): 17 label.append(line_sp[9]) 18 print(line_sp[0]) 19 20 file.close() 21 label = np.array(label) 22 label = label.astype('int16') 23 #print(label) 24 return label 25 26def load_data(path): 27 f_list = list(sorted(glob.glob(path + '*.npy'))) 28 X = np.zeros((len(f_list), slen, f_dim), dtype='float32') 29 30 for fname, ii in zip(f_list, range(len(f_list))): 31 tmp = np.load(fname) 32 padd = np.zeros((slen-len(tmp), f_dim)) 33 X[ii] = np.vstack((tmp, padd)) 34 return X.reshape(len(X), slen, f_dim, 1) 35 36print(x_train.shape, y_train.shape) 37 print(x_test.shape, y_test.shape) 38 39 # Trains the model for a fixed number of epochs (iterations on a dataset) 40 model_history = model.fit(x=x_train, 41 y=y_train, 42 batch_size=batch_size, 43 epochs=epochs, 44 #class_weight=class_weight, 45 verbose=0, 46 validation_data=(x_test, y_test), 47 callbacks=[valid_metrics, chkPoint] 48 ) 49 50def main(): 51 x_train = load_data(f_train_path)
補足情報(FW/ツールのバージョンなど)
Google Colabratory
Python 3.6.5
回答1件
あなたの回答
tips
プレビュー