3D CNN with ImageDataGenerator in keras

934 views
Skip to first unread message

portli...@gmail.com

unread,
May 9, 2018, 12:25:26 PM5/9/18
to Keras-users
Hey everyone, I am new in keras and python I am trying to use 3D CNN with keras, I did the following code shown below and there is an error relates to the dimension of the input shape:

The error says expecting 5d input but found 4d!! I know that 3D CNN input shape should be 5d but how can I reshape the data to be 5d? please.

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, Conv3D, MaxPooling3D
from keras.layers.advanced_activations import LeakyReLU
from keras.optimizers import SGD, RMSprop
from keras.utils import np_utils, generic_utils
from keras.losses import categorical_crossentropy
from keras.optimizers import Adam
import os
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import cv2
from sklearn.cross_validation import train_test_split
from sklearn import cross_validation
from sklearn import preprocessing

datagen = ImageDataGenerator()
train_data=datagen.flow_from_directory('C:\\Users\\AA\\Data\\Training', target_size=(80, 100), color_mode='grayscale', classes=None, class_mode='categorical', batch_size=32, interpolation='nearest')
test_data=datagen.flow_from_directory('C:\\Users\\AA\\Data\\Testing', target_size=(80, 100), color_mode='grayscale', classes=None, class_mode='categorical', batch_size=32, interpolation='nearest')



    ins = (100, 100, 16, 1)
    model = Sequential()
    model.add(Conv3D(32, kernel_size=(3, 3, 3), input_shape=ins, padding=0))
    model.add(Activation('relu'))
    model.add(Conv3D(32, kernel_size=(3, 3, 3), padding=0))
    model.add(Activation('softmax'))
    model.add(MaxPooling3D(pool_size=(3, 3, 3), padding=0))
    model.add(Dropout(0.25))

    model.add(Conv3D(64, kernel_size=(3, 3, 3), padding=0))
    model.add(Activation('relu'))
    model.add(Conv3D(64, kernel_size=(3, 3, 3), padding=0))
    model.add(Activation('softmax'))
    model.add(MaxPooling3D(pool_size=(3, 3, 3), padding=0))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(512, activation='sigmoid'))
    model.add(Dropout(0.5))
    model.add(Dense(8, activation='softmax'))


 model.compile(loss=categorical_crossentropy, optimizer=Adam(), metrics=['accuracy'])    


   model.fit_generator(train_data,
        steps_per_epoch=2000,
        epochs=50,
        validation_data=test_data,
        validation_steps=800)

shasha...@gmail.com

unread,
May 9, 2018, 10:47:26 PM5/9/18
to Keras-users
Can you display the error ? 
Just wanted to know at which line it occured. My guess is that, it occured at the shape declaration variable (ins). 
Message has been deleted
Message has been deleted

portli...@gmail.com

unread,
May 10, 2018, 8:29:10 AM5/10/18
to Keras-users
Yes sure, the error is:

ValueError: Error when checking input: expected conv3d_1_input to have 5 dimensions, but got array with shape (32, 100, 100, 1)

Thanks a lot

portli...@gmail.com

unread,
May 10, 2018, 8:31:45 AM5/10/18
to Keras-users
BTW,  the target size is (100, 100) NOT as shown in the code 80, 100.




    ins = (100, 100, 16, <span style="color:rgb(125,39,39);font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-

mahmoud....@port.ac.uk

unread,
May 11, 2018, 11:03:55 AM5/11/18
to Keras-users
Did you see the error?

rolando pula

unread,
Apr 7, 2020, 11:17:45 PM4/7/20
to Keras-users
Hello,

Did you already solve this kind of problem? I also encountering this now. 

Noong Huwebes, Mayo 10, 2018 ng 12:25:26 AM UTC+8, si portli...@gmail.com ay sumulat:
Reply all
Reply to author
Forward
0 new messages