前提・実現したいこと
cnnを使用して加速度から行動を分類するプログラムを作成しています.
解決しようといろいろ調べてみましたがわかりません.
どなたかわかる方がいましたらご教授お願いします.
発生している問題・エラーメッセージ
ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=5, found ndim=2. Full shape received: (None, 28)
該当のソースコード
python
1from tabnanny import verbose 2import numpy as np 3from numpy.fft import fftn 4import pandas as pd 5import collections 6from sklearn.model_selection import train_test_split 7import tensorflow as tf 8from tensorflow.keras.models import Sequential 9from tensorflow.keras.layers import Dense, Activation, Conv3D,LeakyReLU,Flatten, Dropout 10from tensorflow.keras.utils import to_categorical 11from keras.utils import np_utils 12from tensorflow.keras.optimizers import Adam 13 14#csvファイルの読み込み 15df = pd.read_csv('./data.csv',header = 0,dtype={'time':'float','accx':'float','accy':'float','accz':'float','label':'float'},encoding="UTF-8") 16 17#使わない列を削除 18df = df.drop(df.loc[:,['rotax', 'rotay', 'rotaz']],axis = 1) 19print(df.head()) 20 21df['accc'] = np.sqrt(df['accx']**2 + df['accy']**2 + df['accz']**2) 22print(df.head()) 23 24labels = df['label'].unique() 25headers = ['accx','accy','accz','accc'] 26fft_batch_size = 32 27 28def get_best_size(v,fft_batch_size): 29 still = True 30 for best_size in reversed(range(0,v.size)): 31 if best_size % fft_batch_size == 0: 32 return best_size 33 34data = [] 35target = [] 36 37for label in labels: 38 v = np.array(df[df['label']==label][headers].values) 39 v = v[0:get_best_size(v.T[0],fft_batch_size)] 40 41 features = [] 42 43 for i , header in enumerate(headers): 44 sensor_data = v.T[i].reshape((int( v.T[i].size/fft_batch_size),fft_batch_size)) 45 features.append(sensor_data.mean(axis = 1).tolist()) 46 features.append(sensor_data.max(axis = 1).tolist()) 47 features.append(sensor_data.min(axis = 1).tolist()) 48 features.append(sensor_data.std(axis = 1).tolist()) 49 features.append(sensor_data.var(axis = 1).tolist()) 50 51 F = np.fft.fft(sensor_data) 52 F_abs = np.abs(F) / fft_batch_size * 2. 53 F_abs.T[0] = F_abs.T[0] / 2. 54 F_data = (F_abs.T[:int(sensor_data[0].size/2)+1]).T 55 features.append(F_data.argmax(axis=1).tolist()) 56 features.append(F_data.max(axis=1).tolist()) 57 58 if i == 0: 59 for lll in list(range(0, sensor_data.max(axis=1).size) ): 60 target.append(label) 61 62 data.extend(np.array(features).T.tolist()) 63 64data = np.array(data) 65target = np.array(target) 66 67target = np_utils.to_categorical(target) 68 69x_train,x_test,y_train,y_test = train_test_split(data,target,test_size=0.3,random_state=3) 70 71num_classes = 4 #出力層 72 73#モデル 74model = Sequential() 75model.add(Conv3D(32, kernel_size=(5, 5, 5),input_shape=(64, 64, 64, 1), data_format='channels_last')) 76model.add(LeakyReLU(0.2)) 77for _ in range(2): 78 model.add(Conv3D(32, kernel_size=(5, 5, 5), data_format='channels_last')) 79 model.add(LeakyReLU(0.2)) 80model.add(Flatten()) 81model.add(Dense(256)) 82model.add(LeakyReLU(0.2)) 83model.add(Dropout(0.5)) 84model.add(Dense(num_classes)) 85model.add(Activation('softmax')) 86model.summary() 87 88# モデルのコンパイル 89model.compile(loss='categorical_crossentropy', optimizer=Adam(), metrics=['accuracy']) 90 91# モデルの学習 92history = model.fit(x_train, y_train, batch_size=32, epochs=50, validation_data=(x_test, y_test), verbose=1) 93 94train_score = model.evaluate(x_train,y_train) 95test_score = model.evaluate(x_test,y_test) 96print('Train loss',train_score[0]) 97print('Train accuracy',train_score[1]) 98print('Test loss:', test_score[0]) 99print('Test accuracy:', test_score[1]) 100
データの中身(一部)
x,y_train42個,42個とx,y_test18個,18個のデータです.
x_train [[ 5.04390949e-01 2.82111752e+00 -5.90021090e-01 ... 2.59547117e-01 0.00000000e+00 9.78655016e+00] [ 9.53502453e-03 1.34599070e+00 -1.89471101e+00 ... 9.78987226e-01 0.00000000e+00 9.73595706e+00] [ 1.37212890e-01 1.20518105e+00 -9.83268767e-01 ... 3.93301413e-01 0.00000000e+00 9.78349855e+00] ... [ 3.73374090e-01 1.31965421e+00 -1.34150079e+00 ... 9.23270503e-01 0.00000000e+00 9.89672438e+00] [ 9.91152646e-01 1.51178849e+00 2.04255183e-01 ... 1.78536129e-01 0.00000000e+00 1.01428930e+01] [ 9.12770779e-01 1.96324551e+00 -6.52419220e-02 ... 5.50420941e-01 0.00000000e+00 9.84790721e+00]] y_test [[0. 0. 0. 0. 1.] [0. 0. 1. 0. 0.] [0. 0. 0. 0. 1.] [0. 1. 0. 0. 0.] [0. 0. 0. 1. 0.] [0. 0. 1. 0. 0.] [0. 1. 0. 0. 0.] [0. 0. 1. 0. 0.] [0. 1. 0. 0. 0.] [0. 0. 0. 1. 0.] [0. 0. 1. 0. 0.] [0. 0. 0. 1. 0.] [0. 0. 0. 1. 0.] [0. 1. 0. 0. 0.] [0. 0. 0. 1. 0.] [0. 1. 0. 0. 0.] [0. 0. 0. 0. 1.] [0. 0. 0. 0. 1.]]
回答1件