Cifar 10 Image Recognition by CNN3 Using Keras

Asked 2 years ago, Updated 2 years ago, 89 views

I'm a beginner at Keras.
In order to understand CNN, I am trying to create a model in Keras and output the recognition accuracy of the model based on cifar10 data.
I am having trouble knowing the cause of the error for the following programs.
If anyone knows, I would appreciate it if you could teach or point it out.

The following programs

 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 convolution 3D, MaxPooling 2D
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_trainshape:',X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

# convert class vectors to binary class metrics
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.validate(x_test,y_test,verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

Error below

>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 (Convolution 3D(nb_filter=64, kernel_dim1=3,) 
>37 kernel_dim2 = 3, kernel_dim3 =3,
> 
>/usr/local/lib/python 2.7/dist-packages/keras/models.picin 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 iflen(layer.inbound_nodes)!=1:
> 
>/usr/local/lib/python 2.7/dist-packages/keras/engine/topology.pycin
>__call__(self, inputs, **kwargs)
>509#Raise exceptions in case the input is not compatible
>510# with the input_spec specified in the layer constructor.
>-->511self.assert_input_compatibility (inputs)
>512 
>513#Collect input shapes to build layer.
> 
>/usr/local/lib/python 2.7/dist-packages/keras/engine/topology.pycin
>assert_input_compatibility (self, inputs)
>411 self.name+': expected ndim='+
>412str(spec.ndim)+', foundndim='+
>--> 413 str (K.ndim(x)))
>414 if spec.max_ndim is not None:
>415ndim=K.ndim(x)
> 
>ValueError: Input 0 is incompatible with layer conv3d_6:expected
>ndim=5, foundndim=4

deep-learning keras

2022-09-30 18:38

1 Answers

X_train.shape[1:] and depth seem to have been cut and handed over, but I think I need to also hand over depth.

https://keras.io/ja/layers/convolutional/ #conv3d

When using this layer as the first layer of the model, specify the input_shape keyword argument (integer tuples, no sample axes). For example, in a 10 frame 128x128 RGB image, input_shape=(3,10,128,128).

*In the first place, I think it's 2d, not 3d, but


2022-09-30 18:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.