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

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

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

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

Python 3.x

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

Q&A

解決済

1回答

271閲覧

機械学習を実装中で画像の形状が合いませんどのようにすればよいですか?

SaitoHiroaki

総合スコア15

Keras

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

Python 3.x

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

0グッド

0クリップ

投稿2018/10/11 10:31

python

1import os 2import numpy as np 3from keras.preprocessing.image import ImageDataGenerator 4 5base_dir = base_dir = (r"C:\Users\HN4-00012\Documents\kosen fike\bunnkasai\loads15_data_small") 6 7train_dir = os.path.join(base_dir,'train') 8validation = os.path.join(base_dir,'validation') 9test_dir = os.path.join(base_dir,'test') 10 11datagen = ImageDataGenerator(rescale=1./255) 12batch_size = 20 13 14def extract_features(directory, sample_count): 15 features = np.zeros(shape=(sample_count, 4, 4, 512)) 16 labels = np.zeros(shape=(sample_count)) 17 generator = datagen.flow_from_directory(directory, 18 target_size=(150,150), 19 batch_size=batch_size, 20 class_mode='binary') 21 i=0 22 for inputs_batch, labels_batch in generator: 23 features_batch = conv_base.predict(inputs_batch) 24 features[i * batch_size : (i + 1) * batch_size] = features_batch 25 labels[i * batch_size : (i + 1) * batch_size] = labels_batch 26 i += 1 27 if i * batch_size >= sample_count: 28 break 29 30 return features, labels 31 32 33 34train_features, train_labels = extract_features(train_dir, 2000) 35validation_features, validation_labels = extract_features(validation_dir, 36 1000) 37test_features, test_labels = extract_features(test_dir, 1000) 38 39train_features = np.reshape(train_features, (2000, 4 * 4 * 512)) 40validation_features = np.reshape(validation_features, (1000, 4* 4 * 512)) 41test_features = np.reshape(test_features, (1000, 4 * 4 * 512)) 42 43

エラーが
Found 847 images belonging to 3 classes.


ValueError Traceback (most recent call last)
<ipython-input-8-d96131adac5a> in <module>()
32
33
---> 34 train_features, train_labels = extract_features(train_dir, 2000)
35 validation_features, validation_labels = extract_features(validation_dir,
36 1000)

<ipython-input-8-d96131adac5a> in extract_features(directory, sample_count)
22 for inputs_batch, labels_batch in generator:
23 features_batch = conv_base.predict(inputs_batch)
---> 24 features[i * batch_size : (i + 1) * batch_size] = features_batch
25 labels[i * batch_size : (i + 1) * batch_size] = labels_batch
26 i += 1

ValueError: could not broadcast input array from shape (7,4,4,512) into shape (20,4,4,512)

です
解決方法を教えてください!

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

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

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

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

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

guest

回答1

0

ベストアンサー

以下のようにしてみてはどうでしょうか。

python

1import os 2import numpy as np 3from keras.preprocessing.image import ImageDataGenerator 4 5base_dir = (r'C:\Users\HN4-00012\Documents\kosen fike\bunnkasai\loads15_data_small') 6train_dir = os.path.join(base_dir,'train') 7validation_dir = os.path.join(base_dir,'validation') 8test_dir = os.path.join(base_dir,'test') 9 10datagen = ImageDataGenerator(rescale=1./255) 11 12def extract_features(dirpath): 13 features = [] 14 labels = [] 15 generator = datagen.flow_from_directory( 16 dirpath, target_size=(150, 150), batch_size=32, class_mode='categorical') 17 18 num_steps = len(generator) 19 for step, (img_batch, label_batch) in enumerate(generator, 1): 20 print('step: {}, img_batch: {}, label_batch: {}'.format( 21 step, img_batch.shape, label_batch.shape)) 22 23 feat_batch = conv_base.predict(img_batch) 24 features.extend(feat_batch) 25 labels.extend(label_batch) 26 27 if step >= num_steps: 28 break 29 30 features = np.array(features) 31 features = features.reshape(len(features), -1) 32 labels = np.array(labels).astype(int) 33 return features, labels 34 35train_features, train_labels = extract_features(train_dir) 36validation_features, validation_labels = extract_features(validation_dir) 37test_features, test_labels = extract_features(test_dir)
  • datagen.flow_from_directory() で3クラス以上の場合は class_mode='categorical' を指定します。
  • ステップ数は num_steps = len(generator) で取得できるので、if step >= num_steps で break すればよいです。

投稿2018/10/11 10:38

編集2018/10/11 11:37
tiitoi

総合スコア21956

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

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

SaitoHiroaki

2018/10/11 10:48

1次元に潰す以前に同じエラーが出てしまいます。 なぜ、何でしょうか?
tiitoi

2018/10/11 11:38

回答を修正しました。 おそらく出力の形状と、np.zeros で確保した配列の形状があっていないのではないでしょうか? 予め確保しなくてもリストに詰めておいて、最後に numpy 配列にすればいいかと思います。
SaitoHiroaki

2018/10/12 23:05

実行してみたら出来ました! ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問