Kerasを用いたCNN3によるcifar10の画像認識
Keras初心者です。
CNNを理解するためにKerasでモデルを作り、cifar10のデータを元にモデルの認識精度を出力しようとしています。
以下のプログラムに対するエラーの原因がわからずに困っております。
もしご存知の方いらしましたら、ご教授またはご指摘頂けると幸いです。
以下プログラム
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.models import load_model
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution3D, MaxPooling2D
import keras.optimizers
from keras.utils import np_utils
#highparameter
batch_size = 32
nb_classes = 10
nb_epoch = 80
data_augmentation = False
# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
model = Sequential()
#input: number of frames/depth: 3 , number of channels: 1 , width: 128, height: 128 (3, 1, 128, 128)
# 1st layer group
model.add(Convolution3D(nb_filter=64,kernel_dim1=3,
kernel_dim2=3,kernel_dim3=3,
activation='relu',
input_shape = X_train.shape[1:]))
model.add(Convolution3D(nb_filter=64,kernel_dim1=3,
kernel_dim2=3,kernel_dim3=3,
activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# 2st layer group
model.add(Convolution3D(nb_filter=128,kernel_dim1=3,
kernel_dim2=3,kernel_dim3=3,
activation='relu'))
model.add(Convolution3D(nb_filter=64,kernel_dim1=3,
kernel_dim2=3,kernel_dim3=3,
activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# 3st layer group
model.add(Convolution3D(nb_filter=256,kernel_dim1=3,
kernel_dim2=3,kernel_dim3=3,
activation='relu'))
model.add(Convolution3D(nb_filter=256,kernel_dim1=3,
kernel_dim2=3,kernel_dim3=3,
activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1024),activation('relu'))
model.add(Dense(512),activation('relu'))
model.add(Dense(num_classes))
model.summary()
model.compile(loss='categorical_crossentropy',
optimizer=RMSprop(),
metrics=['accuracy'])
history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=nb_epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
以下エラー
> ValueError Traceback (most recent call
> last) <ipython-input-6-787c050ba750> in <module>()
> 33 kernel_dim2=3,kernel_dim3=3,
> 34 activation='relu',
> ---> 35 input_shape = X_train.shape[1:]))
> 36 model.add(Convolution3D(nb_filter=64,kernel_dim1=3,
> 37 kernel_dim2=3,kernel_dim3=3,
>
> /usr/local/lib/python2.7/dist-packages/keras/models.pyc in add(self,
> layer)
> 420 # and create the node connecting the current layer
> 421 # to the input layer we just created.
> --> 422 layer(x)
> 423
> 424 if len(layer.inbound_nodes) != 1:
>
> /usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc in
> __call__(self, inputs, **kwargs)
> 509 # Raise exceptions in case the input is not compatible
> 510 # with the input_spec specified in the layer constructor.
> --> 511 self.assert_input_compatibility(inputs)
> 512
> 513 # Collect input shapes to build layer.
>
> /usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc in
> assert_input_compatibility(self, inputs)
> 411 self.name + ': expected ndim=' +
> 412 str(spec.ndim) + ', found ndim=' +
> --> 413 str(K.ndim(x)))
> 414 if spec.max_ndim is not None:
> 415 ndim = K.ndim(x)
>
> ValueError: Input 0 is incompatible with layer conv3d_6: expected
> ndim=5, found ndim=4