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

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

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

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

NumPy

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

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

Q&A

解決済

1回答

1988閲覧

keras モデルデータの読み込みエラー?

sezaki_H

総合スコア41

Keras

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

NumPy

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

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

0グッド

0クリップ

投稿2019/06/22 05:14

発生している問題・エラーメッセージ

男性と女性で分けているのですが、
女性の学習データを読み込もうとするとエラーが出ます。
女性のデータの数は29名分です。が、エラー文に含まれる30という数字は男性データの数です。

ValueError: Dimension 1 in both shapes must be equal, but are 29 and 30. Shapes are [200,29] and [200,30]. for 'Assign_14' (op: 'Assign') with input shapes: [200,29], [200,30].

該当のソースコード

汚くて申し訳ありません。

Python

1from keras.models import Sequential 2from keras.layers import Activation, Dense, Dropout, Conv2D, MaxPooling2D, Flatten 3from keras.utils.np_utils import to_categorical 4from keras.optimizers import Adam 5import numpy as np 6from PIL import Image 7import os 8 9# 学習用のデータを作る. 10image_list = [] 11image_list2 = [] 12label_list = [] 13label_list2 = [] 14 15 16 17# データが格納されたディレクトリ名 18basedir='./data/' 19# 訓練データのディレクトリ 20mendir=basedir + 'training/men/' 21womendir=basedir + 'training/women/' 22 23 24men_label_name = [] 25for md in os.listdir(mendir): 26 if md == '.DS_Store': 27 continue 28 men_label_name.append(md) 29 30print(men_label_name) 31 32 33women_label_name = [] 34for wd in os.listdir(womendir): 35 if wd == '.DS_Store': 36 continue 37 women_label_name.append(wd) 38 39print(women_label_name) 40 41 42 43label = 0 44# ./data/train 以下の画像を読み込む。 45for dir in os.listdir(mendir): 46 if dir == '.DS_Store': 47 continue 48 49 dir1 = mendir + dir 50 h=0 51 for ln in men_label_name: 52 if dir == men_label_name[h]: 53 label = h 54 55 h+=1 56 57 58 59 for file in os.listdir(dir1): 60 if file != '.DS_Store': 61 # 配列label_listに正解ラベルを追加 62 label_list.append(label) 63 filepath = dir1 + '/' + file 64 # 画像を100x100pixelに変換し、1要素が[R,G,B]3要素を含む配列の100x100の2次元配列として読み込む。 65 # [R,G,B]はそれぞれが0-255の配列。 66 image = np.array(Image.open(filepath).convert('RGB').resize((100, 100))) 67 # 出来上がった配列をimage_listに追加。 68 image_list.append(image / 255.) 69 70# kerasに渡すためにnumpy配列に変換。 71X = np.array(image_list) 72 73# ラベルの配列を1と0からなるラベル配列に変更 74# 0 -> [1,0,0], 1 -> [0,1,0] という感じ。 75Y = to_categorical(label_list) 76 77 78 79#early_stopping=EarlyStopping(monitor='loss',patience=10, verbose=1) 80print("men_learn") 81# モデルを生成してニューラルネットを構築 82model = Sequential() 83model.add(Conv2D(64, (3, 3), input_shape=(100, 100, 3), padding='same', activation='relu')) 84model.add(Conv2D(32, (3, 3), padding='same', activation='relu'))#畳み込み 85model.add(MaxPooling2D(pool_size=(2, 2))) 86model.add(Flatten()) 87model.add(Dense(200, activation='relu')) 88model.add(Dropout(0.2)) 89model.add(Dense(30, activation='softmax')) 90 91model.summary() 92 93# モデルをコンパイル 94model.compile(loss="categorical_crossentropy", optimizer=Adam(), metrics=["accuracy"]) 95# 学習を実行。10%はテストに使用。 96#model.fit(X, Y, epochs=1000, batch_size=25, callbacks=[early_stopping]) 97model.fit(X, Y, epochs=1, batch_size=100)#batch_sizeは100でもいい 98 99 100json_string = model.to_json() 101open('ir_first_model_men.json', 'w').write(json_string) # ネットワーク構造をJSON形式で保存 102model.save_weights('ir_first_weights_men.h5') 103 104print("wp") 105 106 107 108# 重みをバイナリ形式で保存 109label = 0 110# ./data/train 以下の画像を読み込む。 111for dir in os.listdir(womendir): 112 if dir == '.DS_Store': 113 continue 114 115 dir1 = womendir + dir 116 h=0 117 for ln in women_label_name: 118 if dir == women_label_name[h]: 119 label = h 120 121 h+=1 122 123 124 125 for file in os.listdir(dir1): 126 if file != '.DS_Store': 127 # 配列label_listに正解ラベルを追加 128 label_list2.append(label) 129 filepath = dir1 + '/' + file 130 # 画像を100x100pixelに変換し、1要素が[R,G,B]3要素を含む配列の100x100の2次元配列として読み込む。 131 # [R,G,B]はそれぞれが0-255の配列。 132 image2 = np.array(Image.open(filepath).convert('RGB').resize((100, 100))) 133 # 出来上がった配列をimage_listに追加。 134 image_list2.append(image2 / 255.) 135 136# kerasに渡すためにnumpy配列に変換。 137X = np.array(image_list2) 138 139# ラベルの配列を1と0からなるラベル配列に変更 140# 0 -> [1,0,0], 1 -> [0,1,0] という感じ。 141Y = to_categorical(label_list2) 142 143 144 145#early_stopping=EarlyStopping(monitor='loss',patience=10, verbose=1) 146print("women_learn") 147# モデルを生成してニューラルネットを構築 148model2 = Sequential() 149model2.add(Conv2D(64, (3, 3), input_shape=(100, 100, 3), padding='same', activation='relu')) 150model2.add(Conv2D(32, (3, 3), padding='same', activation='relu'))#畳み込み 151model2.add(MaxPooling2D(pool_size=(2, 2))) 152model2.add(Flatten()) 153model2.add(Dense(200, activation='relu')) 154model2.add(Dropout(0.2)) 155model2.add(Dense(29, activation='softmax')) 156 157model2.summary() 158 159# モデルをコンパイル 160model2.compile(loss="categorical_crossentropy", optimizer=Adam(), metrics=["accuracy"]) 161# 学習を実行。10%はテストに使用。 162#model.fit(X, Y, epochs=1000, batch_size=25, callbacks=[early_stopping]) 163model2.fit(X, Y, epochs=1, batch_size=100)#batch_sizeは100でもいい 164 165 166json_string2 = model2.to_json() 167open('ir_first_model_women.json', 'w').write(json_string2) # ネットワーク構造をJSON形式で保存 168model.save_weights('ir_first_weights_women.h5') 169 170 171 172 173 174 175print('finish!') 176

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

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

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

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

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

guest

回答1

0

ベストアンサー

最後にセーブするモデルはmodel2ではありませんか?

json_string2 = model2.to_json() open('ir_first_model_women.json', 'w').write(json_string2) # ネットワーク構造をJSON形式で保存 #修正前 model.save_weights('ir_first_weights_women.h5') #修正後 model2.save_weights('ir_first_weights_women.h5')

投稿2019/06/24 13:27

amahara_waya

総合スコア1029

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

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

sezaki_H

2019/06/27 11:56

単純なミスでした(汗) ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問