前提・実現したいこと
以下のコードを実行した結果、エラーが発生しました。
次元に関するエラーなのは分かるのですが、解決方法がわかりません。
ご教授お願いします。
発生している問題・エラーメッセージ
Traceback (most recent call last): File "sleepingmodel7.py", line 49, in <module> x = layers.Conv2D(32, (3, 3), activation='relu')(bottleneck) File "/opt/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py", line 446, in __call__ self.assert_input_compatibility(inputs) File "/opt/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py", line 342, in assert_input_compatibility str(K.ndim(x))) ValueError: Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=2
該当のソースコード
python
1from keras import layers 2from keras import models 3from keras import optimizers 4from keras.layers import GlobalAveragePooling2D 5import numpy as np 6import pandas as pd 7 8import os 9for dirname, _, filenames in os.walk('/kaggle/input'): 10 for filename in filenames: 11 print(os.path.join(dirname, filename)) 12 13import glob 14import matplotlib.pyplot as plt 15import seaborn as sns 16from PIL import Image 17from efficientnet.keras import EfficientNetB7 18from sklearn.model_selection import train_test_split 19from keras.utils import to_categorical 20from keras.layers import Input, Dense, Reshape 21from keras.models import Model 22from keras.callbacks import ModelCheckpoint 23 24from keras.preprocessing.image import ImageDataGenerator 25 26train_dir = 'dataset/train' 27validation_dir = 'dataset/validation' 28 29# rescaled by 1./255 30train_datagen = ImageDataGenerator( 31 rescale=1./255# 32) 33 34inputs = Input(shape=(100, 100, 3)) 35efficient_net = EfficientNetB7(weights='noisy-student',include_top=False,input_tensor = inputs,pooling='max') 36 37for layer in efficient_net.layers: 38 layer.trainable = True 39 40bottleneck=efficient_net.output 41print(bottleneck.shape) 42x = layers.Conv2D(32, (3, 3), activation='relu')(bottleneck) 43x = layers.MaxPooling2D((2, 2))(x) 44x = layers.Conv2D(64, (3, 3), activation='relu')(x) 45x = layers.MaxPooling2D((2, 2))(x) 46x = layers.Conv2D(128, (3, 3), activation='relu')(x) 47x = layers.MaxPooling2D((2, 2))(x) 48x = layers.Conv2D(128, (3, 3), activation='relu')(x) 49x = layers.MaxPooling2D((2, 2))(x) 50x = GlobalAveragePooling2D()(x) 51x = layers.Dense(32, activation='relu')(x) 52eye_output = layers.Dense(1, activation='sigmoid')(x) 53 54model = Model(inputs=inputs, outputs=eye_output) 55 56model.summary() 57 58model.compile(loss='binary_crossentropy',optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc']) 59test_datagen = ImageDataGenerator(rescale=1./255) 60 61train_generator = train_datagen.flow_from_directory( 62 train_dir, 63 target_size=(100, 100), 64 batch_size=80, 65 class_mode='binary') 66 67validation_generator = test_datagen.flow_from_directory( 68 validation_dir, 69 target_size=(100, 100), 70 batch_size=50, 71 class_mode='binary') 72 73history = model.fit_generator( 74 train_generator, 75 steps_per_epoch=30, 76 epochs=50, 77 validation_data=validation_generator, 78 validation_steps=6) 79 80model.save('sleeping1') 81 82from keras.models import load_model 83from keras.preprocessing.image import ImageDataGenerator 84model = load_model('sleeping1') 85 86test_dir = 'dataset/test' 87test_datagen = ImageDataGenerator(rescale=1./255) 88test_generator = test_datagen.flow_from_directory( 89 test_dir, 90 target_size=(100,100), 91 batch_size=50, 92 class_mode='binary') 93 94test_loss, test_acc = model.evaluate_generator(test_generator, steps=12) 95print('test loss:', test_loss) 96print('test acc:', test_acc) 97
再現してみるとエラーは以下のように出ますが、Full shape以降の表示は出ていませんか?
ValueError: Input 0 of layer conv2d is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 2560]
単純に読むと、bottleneckの形式がエラーが出たところの入力形式の期待と合っていないことだと思います。bottleneckの形式は期待通りなのでしょうか?
full shape以降の記述はありませんでした。
conv2dは4次元を期待していて、bottleneckは2次元になっているため期待通りではありません。
あなたの回答
tips
プレビュー