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)
です
解決方法を教えてください!
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/11 10:48
2018/10/11 11:38
2018/10/12 23:05