質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

751閲覧

keras 複数入力でinput_4 to have 3 dimensions,のエラーが出て対処方法がわからない

yk3125

総合スコア91

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/04/08 04:21

カラー画像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)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

画像サイズ64x64 の2種類による複数入力ができました。
出力は6パターンです。

python

1import tensorflow as tf 2import keras 3import numpy as np 4from PIL import Image 5import os, glob, random 6from keras.preprocessing import image 7from keras.preprocessing.image import load_img, img_to_array 8 9photo_width = 64 10photo_height2 = 64 11x1 = [] # 画像 12x2 = [] # 画像 13y = [] # ラベル 14outfile1 = "C:/Product/pato_ai/tensorflow/learning4/val.npz" 15 16def glob_images(path1, path2, label): 17 # pathフォルダ内の画像を読む 18 files = glob.glob("C:/Product/pato_ai/tensorflow/learning4/" + path1 + "/*.jpg") 19 20 for i,f in enumerate(files): 21 img = image.load_img(f, target_size=(photo_height2, photo_width)) 22 data1 = img_to_array(img) 23 data1 = data1 / 255 24 data1 = data1.reshape(-1,photo_height2, photo_width, 3) 25 x1.append(data1) # 画像 26 27 files = glob.glob("C:/Product/pato_ai/tensorflow/learning4/" + path2 + "/*.jpg") 28 for i,f in enumerate(files): 29 img = image.load_img(f, target_size=(photo_height2, photo_width)) 30 data1 = img_to_array(img) 31 data1 = data1 / 255 32 data1 = data1.reshape(-1,photo_height2, photo_width, 3) 33 x2.append(data1) # 画像 34 y.append(label) # ラベル 35 36def make_dataset(): 37 global x1, x2 38 global y 39 x1 = [] 40 x2 = [] 41 y = [] 42 43 glob_images("DBpic1A/0" , "DBpic1B/0" , 0) 44 glob_images("DBpic1A/2" , "DBpic1B/2" , 2) 45 glob_images("DBpic1A/3" , "DBpic1B/3" , 3) 46 glob_images("DBpic1A/4" , "DBpic1B/4" , 4) 47 glob_images("DBpic1A/5" , "DBpic1B/5" , 5) 48 np.savez(outfile1, x1=x1, x2=x2, y=y) 49 print("saved:" + outfile1) 50

python

1import tensorflow as tf 2import keras 3import numpy as np 4from PIL import Image 5import os, glob, random 6from keras.preprocessing.image import load_img, img_to_array 7 8photo_width = 64 9photo_height2 = 64 10x1 = [] # 画像 11x2 = [] # 画像 12y = [] # ラベル 13outfile1 = "C:/Product/pato_ai/tensorflow/learning4/train.npz" 14 15def glob_images(path1, path2, label): 16 17 # pathフォルダ内の画像を読む 18 files = glob.glob("C:/Product/pato_ai/tensorflow/learning4/" + path1 + "/*.jpg") 19 20 for i,f in enumerate(files): 21 img = image.load_img(f, target_size=(photo_height2, photo_width)) 22 data1 = img_to_array(img) 23 data1 = data1 / 255 24 data1 = data1.reshape(-1,photo_height2, photo_width, 3) 25 x1.append(data1) # 画像 26 27 files = glob.glob("C:/Product/pato_ai/tensorflow/learning4/" + path2 + "/*.jpg") 28 for i,f in enumerate(files): 29 img = image.load_img(f, target_size=(photo_height2, photo_width)) 30 data1 = img_to_array(img) 31 data1 = data1 / 255 32 data1 = data1.reshape(-1,photo_height2, photo_width, 3) 33 x2.append(data1) # 画像 34 y.append(label) # ラベル 35 36def make_dataset(): 37 global x1, x2 38 global y 39 x1 = [] 40 x2 = [] 41 y = [] 42 43 glob_images("DBpic2A/0", "DBpic2B/0", 0) 44 glob_images("DBpic2A/2", "DBpic2B/2", 2) 45 glob_images("DBpic2A/3", "DBpic2B/3", 3) 46 glob_images("DBpic2A/4", "DBpic2B/4", 4) 47 glob_images("DBpic2A/5", "DBpic2B/5", 5) 48 np.savez(outfile1, x1=x1, x2=x2, y=y) 49 print("saved:" + outfile1) 50 51# データセットの作成 52make_dataset()

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 8from keras.layers import Add 9from keras.layers import Merge 10 11num_classes = 6 12photo_width = 64 13photo_height2 = 64 14infile1 = "C:/Product/pato_ai/tensorflow/learning4/val.npz" 15infile2 = "C:/Product/pato_ai/tensorflow/learning4/train.npz" 16outfile1 = "C:/Product/pato_ai/tensorflow/learning4/model/road_model.h5" 17outfile2 = "C:/Product/pato_ai/tensorflow/learning4/model/road_weights" 18outfile3 = "C:/Product/pato_ai/tensorflow/learning4/model/road_keka.csv" 19val_x1 = [] 20val_x2 = [] 21 22def main(): 23 global val_image 24 data = np.load(infile1) 25 val_x1 = data["x1"] 26 val_x2 = data["x2"] 27 val_y = data["y"] 28 29 data = np.load(infile2) 30 tst_x1 = data["x1"] 31 tst_x2 = data["x2"] 32 tst_y = data["y"] 33 34 35 # ラベルデータをOne-Hot形式に変換 36 val_y = keras.utils.to_categorical(val_y, num_classes) 37 tst_y = keras.utils.to_categorical(tst_y, num_classes) 38 val_x1 = val_x1.reshape(-1, photo_height2, photo_width,3) 39 val_x2 = val_x2.reshape(-1, photo_height2, photo_width,3) 40 tst_x1 = tst_x1.reshape(-1, photo_height2, photo_width,3) 41 tst_x2 = tst_x2.reshape(-1, photo_height2, photo_width,3) 42 model = train(val_x1, val_x2, val_y, tst_x1, tst_x2, tst_y) 43 44 45def train(val_x1, val_x2, val_y, tst_x1, tst_x2, tst_y): 46 47 left_branch = Sequential() 48 left_branch.add(Dense(units = 64, input_shape =(photo_height2, photo_width, 3))) 49 50 right_branch = Sequential() 51 right_branch.add(Dense(units = 64, input_shape =(photo_height2, photo_width, 3))) 52 53 merged = Merge([left_branch, right_branch], mode='concat') 54 final_model = Sequential() 55 final_model.add(merged) 56 final_model.add(Flatten()) 57 final_model.add(Dense(6, activation='softmax')) 58 59 final_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) 60 final_model.summary() 61 62 i = 0 63 while i < 3: 64 final_model.fit([val_x1, val_x2], val_y, batch_size=32, epochs=30, validation_split=0.1,) 65 final_model.save_weights(outfile2+str(i)+".h5") 66 score = final_model.evaluate([tst_x1, tst_x2], tst_y) 67 print(" (loss)=", score[0]) 68 print("正解率(accuracy)=", score[1]) 69 f = open(outfile3,'a') 70 f.write(str(i)+" (loss)="+str(score[0])+"正解率(accuracy)=,"+str(score[1])+'\n') 71 f.close() 72 i = i + 1 73 return final_model 74 75if __name__ == "__main__": 76 main() 77

投稿2021/04/21 01:05

yk3125

総合スコア91

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

合ってるか分かりませんが,質問者様が仰っている通り,期待される入力の次元が3次元であるのに対し,実際に入力された値は2次元となっていると思われます.
入力が3次元の可能性として考えられるのが画像入力時だと思われるので,入力する値が2次元になっているのではないでしょうか.

python

1print(val_image.shape) 2print(tst_image.shape)

投稿2021/04/09 03:37

HelloQ

総合スコア81

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yk3125

2021/04/09 13:16

model = の前にprint()を追加してみた print(val_image.shape) print(tst_image.shape) print(val_text.shape) print(tst_text.shape) model = train(val_image, val_text, val_label, tst_image, tst_text, tst_label) 結果 (2628, 88, 192, 3) (2160, 88, 192, 3) (2628,) (2160,) と表示されました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問