カラー画像192x88 と文字列:6文字 2種類の入力で 単出力のAI学習です。
"aaaa"が表示されているのでmodel.fit()で失敗しているのですが
ValueError: Error when checking input: expected input_4 to have 3 dimensions, but got array with shape (2628, 1)
のエラーで、modelでは3次元で定義されているのにデータ2次元できたと言う事のようですが
どのように直したらいいかわかりません。
分かる方がいたら教えてください。
python
1from keras.models import Model, Sequential 2from keras.layers import Input, concatenate, Activation, Dense, Dropout, Flatten, SimpleRNN, Reshape 3from keras.preprocessing import image 4import numpy as np 5from keras.preprocessing.image import ImageDataGenerator 6from keras.layers import Conv2D, MaxPooling2D 7import keras 8 9num_classes = 6 10photo_width = 192 11photo_height = 88 12data_size = photo_height * photo_width * 3 13in_shape = (photo_height, photo_width, 3) 14infile1 = "C:/Product/pato_ai/tensorflow/learning3/val.npz" 15infile2 = "C:/Product/pato_ai/tensorflow/learning3/train.npz" 16outfile1 = "C:/Product/pato_ai/tensorflow/learning3/model/road_model.h5" 17outfile2 = "C:/Product/pato_ai/tensorflow/learning3/model/road_weights" 18outfile3 = "C:/Product/pato_ai/tensorflow/learning3/model/road_keka.csv" 19val_image = [] 20val_text = [] 21 22def main(): 23 global val_image 24 data = np.load(infile1) 25 val_image = data["input_image"] 26 val_text = data["input_text"] 27 val_label = data["input_label"] 28 29 data = np.load(infile2) 30 tst_image = data["input_image"] 31 tst_text = data["input_text"] 32 tst_label = data["input_label"] 33 34 print(val_text) 35 36 # ラベルデータをOne-Hot形式に変換 37 val_label = keras.utils.to_categorical(val_label, num_classes) 38 tst_label = keras.utils.to_categorical(tst_label, num_classes) 39 40 model = train(val_image, val_text, val_label, tst_image, tst_text, tst_label) 41 model_eval(model, x_test, y_test) 42 43 44def train(val_image, val_text, val_label, tst_image, tst_text, tst_label): 45 46 input_image = Input(shape=(photo_height, photo_width, 3)) 47 input_text = Input(shape=(6,1)) 48 49 # image input 50 model_image = Conv2D(32, (3, 3), activation='relu')(input_image) 51 model_image = Conv2D(32, (3, 3), activation='relu')(model_image) 52 model_image = MaxPooling2D(pool_size=(2, 2))(model_image) 53 model_image = Dropout(0.25)(model_image) 54 55 model_image = Conv2D(32, (3, 3), activation='relu')(model_image) 56 model_image = Conv2D(32, (3, 3), activation='relu')(model_image) 57 model_image = MaxPooling2D(pool_size=(2, 2))(model_image) 58 59 model_image = Flatten()(model_image) 60 model_image = Dense(255, activation='relu')(model_image) 61 model_image = Dense(10, activation='softmax')(model_image) 62 model_image = Model(inputs=input_image, outputs=model_image) 63 64 # text input 65 model_text = SimpleRNN(1, return_sequences=True)(input_text) 66 model_text = Dense(1, activation="linear")(model_text) 67 model_text = Flatten()(model_text) 68 model_text = Model(inputs=input_text, outputs=model_text) 69 70 # Image and text combined 71 combined = concatenate([model_image.output, model_text.output], axis=1) 72 final = Dense(32, activation="relu")(combined) 73 final = Dense(4, activation="sigmoid")(final) 74 75 model = Model(inputs=[model_image.input, model_text.input], outputs=final) 76 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc']) 77 model.save(outfile1) 78 79 80 i = 0 81 while i < 1: 82 print("aaaa") 83 hist = model.fit([val_image, val_text], val_label, 84 batch_size=32, epochs=30, verbose=1, validation_data=([tst_image, tst_text], tst_label)) 85 print("bbbb") 86 model.save_weights(outfile2+str(i)+".h5") 87 print("ccccc") 88 score = model.evaluate([tst_image, tst_text], tst_label) 89 print(" (loss)=", score[0]) 90 print("正解率(accuracy)=", score[1]) 91 f = open(outfile3,'a') 92 f.write(str(i)+" (loss)="+str(score[0])+"正解率(accuracy)=,"+str(score[1])+'\n') 93 f.close() 94 i = i + 1 95 96 97 hist = model.fit(x_train, y_train, batch_size=32, epochs=100, verbose=1, validation_data=(x_test, y_test)) 98 model.save_weights(outfile2 + ".h5") 99 return model 100 101def model_eval(model, x, y): 102 score = model.evaluate(x, y) 103 print(" (loss)=", score[0]) 104 print("正解率(accuracy)=", score[1]) 105 106if __name__ == "__main__": 107 main() 108 109
runfile('C:/Product/pato_ai/tensorflow/learning3/train03.py', wdir='C:/Product/pato_ai/tensorflow/learning3') ['111018' '111018' '111018' ..., '122008' '122008' '122008'] aaaa Traceback (most recent call last): File "<ipython-input-2-4062c011fabe>", line 1, in <module> runfile('C:/Product/pato_ai/tensorflow/learning3/train03.py', wdir='C:/Product/pato_ai/tensorflow/learning3') File "C:\Users\Arail\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile execfile(filename, namespace) File "C:\Users\Arail\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Product/pato_ai/tensorflow/learning3/train03.py", line 161, in <module> main() File "C:/Product/pato_ai/tensorflow/learning3/train03.py", line 50, in main model = train(val_image, val_text, val_label, tst_image, tst_text, tst_label) File "C:/Product/pato_ai/tensorflow/learning3/train03.py", line 138, in train batch_size=32, epochs=30, verbose=1, validation_data=([tst_image, tst_text], tst_label)) File "C:\Users\Arail\Anaconda3\lib\site-packages\keras\engine\training.py", line 1593, in fit batch_size=batch_size) File "C:\Users\Arail\Anaconda3\lib\site-packages\keras\engine\training.py", line 1426, in _standardize_user_data exception_prefix='input') File "C:\Users\Arail\Anaconda3\lib\site-packages\keras\engine\training.py", line 110, in _standardize_input_data 'with shape ' + str(data_shape)) ValueError: Error when checking input: expected input_4 to have 3 dimensions, but got array with shape (2628, 1)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。