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

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

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

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

Python

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

Q&A

0回答

2499閲覧

tensorflow複数入力のモデルを作りたい

yk3125

総合スコア91

Keras

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

Python

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

0グッド

0クリップ

投稿2020/04/30 07:12

AI判定で複数入力の判定プログラムを作りたいのですが、ネット上では同じようなパターンが見つけれず、見よう見まねで作っています。
やりたいことは画像と数値3種類の複数入力で出力が0~5を出力するものです
入力1:画像
入力2:数値(パラメータ1,2,3)
出力:6種類 0~5

プログラムを実行すると
ValueError: Input 0 is incompatible with layer conv2d_11: expected ndim=4, found ndim=2
が出ました。
モデルの x = Conv2D(32, (3, 3), padding='same',activation='relu', input_shape=in_shape)(input1)
あたりが悪いのですが、どう修正していいかわかりません。
分かる方がいれば、教えてください。

import tensorflow as tf import keras from keras.layers import Activation, Dropout, Dense, Input, concatenate from keras.models import Model from keras.layers import Conv2D, MaxPooling2D from keras.utils.np_utils import to_categorical import numpy as np from IPython.core.display import display from PIL import Image import glob photo_size = 152 in_shape = (phto_size, phto_size, 3) pic_size = photo_size * photo_size * 3 in_size2 = 3 # 入力条件2 3種類  nb_classes = 6 # 出力 判定6種類 focs_size = 1333 gain_size = 160 time_size = 24 x_val1 = [] # 画像 x_val2 = [] # 入力条件2 y_val = [] # 出力 ラベル x_train1 = [] # 画像 x_train2 = [] # 入力条件2 y_train = [] # 出力 ラベル outfile1 = "C:/Product/PI_hantei/tensorflow4C/learning7B/ai_model.h5" outfile2 = "C:/Product/PI_hantei/tensorflow4C/learning7B/ai_weights.h5" def make_model(): # MLPモデル構造を定義 input1 = Input(shape=(pic_size,)) input2 = Input(shape=(in_size2,)) # 入力1から結合前まで x = Conv2D(32, (3, 3), padding='same',activation='relu', input_shape=in_shape)(input1) x = Activation('relu')(x) x = Conv2D(32, (3, 3))(x) x = Activation('relu')(x) x = MaxPooling2D(pool_size=(2, 2))(x) x = Dropout(0.25)(x) x = Model(inputs=input1, outputs=x) # 入力2から結合前まで y = Dense(128, activation="relu")(input2) y = Model(inputs=input2, outputs=y) # 結合 combined = concatenate([x.output, y.output]) # 密結合 z = Dense(128, activation="relu")(combined) z = Dense(nb_classes, activation="softmax")(z) # モデル定義とコンパイル model = Model(inputs=[x.input, y.input], outputs=z) #model.add(Dropout(0.2)) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) display(model.summary()) model.save(outfile1) print("saved:" + outfile1) return model def glob_images(pt, path, label): files = glob.glob("C:/Product/PI_hantei/DBpic/" + path + "/*.jpg") #print(files) # 画像ファイル数分ループ for i, f in enumerate(files): buf = f.split('\') fname = buf[len(buf)-1] tmf = fname[7:9]+":"+fname[9:11]+":"+fname[11:13] tm = int(fname[7:9]) b1 = fname.split('_') FOCS = int(b1[1]) GAIN = int(b1[2][0:3]) print('fname:{0}, time:{1}({4}) FOC:{2} GAIN:{3}'.format(fname, tmf, FOCS, GAIN, tm)) img = Image.open(f) # 画像ファイルを読む img = img.convert("RGB") data1 = np.asarray(img) # 画像をnumpy形式にする data1 = data1 / 256 data1 = data1.reshape(photo_size, photo_size, 3) # データを正規化する(0-1の間にする) FOCS_sng = FOCS / focs_size GAIN_sng = GAIN / gain_size tm_sng = tm / time_size if pt == 1: x_val1.append(data1) # 画像 x_val2.append(np.array([FOCS_sng, GAIN_sng, tm_sng])) y_val.append(label) # ラベル else: x_train1.append(data1) x_train2.append(np.array([FOCS_sng, GAIN_sng, tm_sng])) y_train.append(label) # ラベル model = make_model() glob_images(1, "2020-02-03/pic1" , 0) glob_images(1, "2020-02-03/pic2" , 1) glob_images(1, "2020-02-03/pic3" , 2) # 学習します x_val1 = np.reshape(x_val1, (-1, pic_size)) y_val = to_categorical(y_val, nb_classes) hist = model.fit([x_val1, np.array(x_val2)], y_val, batch_size=50, epochs=20) model.save_weights(outfile2) print("saved:" + outfile2) glob_images(2, "2020-02-20D/pic1" , 0) glob_images(2, "2020-02-20D/pic2" , 1) glob_images(2, "2020-02-20D/pic3" , 2) # 形式を変換 x_train1 = np.reshape(x_train1, (-1, pic_size)) y_train = to_categorical(y_train, nb_classes) # 正解率を調べる --- (*5) score = model.evaluate([x_train1, np.array(x_train2)], y_train, verbose=1) print("正解率=", score[1], "ロス=", score[0])

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問