```import numpy as np
import keras
from keras.optimizers import SGD
from keras.models import Sequential, Model
from keras.layers import Activation, Dense, Dropout, Flatten, Input, Merge, Conv3D, MaxPooling3D
import tensorflow as tf
import numpy as np
import keras
from keras.engine.training import Model
from keras.models import load_model,save_model,Model,Sequential
from keras.layers import Conv3D, MaxPooling3D
from keras.layers import Dense, Dropout, Activation, Flatten
from keras import backend as K
from keras.optimizers import SGD, RMSprop,Adam
from keras import metrics
from keras.callbacks import ModelCheckpoint
from keras.preprocessing.image import ImageDataGenerator
K.set_image_dim_ordering('tf')
IMG_SIZE_PX=50
SLICE_COUNT=20
nb_filters = [32, 64]
nb_pool = [2, 2]
nb_conv = [3,3]
batch_size = 1
nb_classes = 2
nb_epoch =3
model = Sequential()
model.add(Conv3D(nb_filters[0], kernel_dim1=nb_conv[0], kernel_dim2=nb_conv[0], kernel_dim3=nb_conv[0],
input_shape=(20, 50, 50,1), activation='relu'))
model.add(MaxPooling3D(pool_size=(nb_pool[0], nb_pool[0], nb_pool[0])))
model.add(Conv3D(nb_filters[1], kernel_dim1=nb_conv[0], kernel_dim2=nb_conv[0], kernel_dim3=nb_conv[0],activation='relu'))
model.add(MaxPooling3D(pool_size=(nb_pool[0], nb_pool[0], nb_pool[0])))
model.add(Dropout(0.8))
model.add(Flatten())
model.add(Dense(1024, init='normal', activation='relu'))
model.add(Dropout(0.8))
model.add(Dense(nb_classes,init='normal'))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',optimizer='adadelta',metrics=['accuracy'])
train_data=np.load('D:/muchdata-50-50-20.npy')
train=train_data[-10:]
test=train_data[-2:]
train1 = np.array([i[0] for i in train]).reshape(-1,20,50,50,1)
y_train = [i[1] for i in train]
testx=np.array([i[0] for i in test]).reshape(-1,20,50,50,1)
testy=[i[1] for i in test]
datagen = ImageDataGenerator(featurewise_center=True,featurewise_std_normalization=True,rotation_range=20,width_shift_range=0.2,height_shift_range=0.2,horizontal_flip=True)
datagen.fit(train1)
I'm getting the following error while running this code:
ValueError Traceback (most recent call last)
54 testy=[i[1] for i in test]
55 datagen = ImageDataGenerator(featurewise_center=True,featurewise_std_normalization=True,rotation_range=20,width_shift_range=0.2,height_shift_range=0.2,horizontal_flip=True)
---> 56 datagen.fit(train1)
C:Program FilesAnaconda3libsite-packageskeraspreprocessingimage.py in fit(self, x, augment, rounds, seed)
633 if x.ndim != 4:
634 raise ValueError('Input to .fit() should have rank 4. '
--> 635 'Got array with shape: ' + str(x.shape))
636 if x.shape[self.channel_axis] not in {1, 3, 4}:
637 raise ValueError(
ValueError: Input to .fit() should have rank 4. Got array with shape: (10, 20, 50, 50, 1)
I tried all possible ways but nothing is helping......so pls any1 help me solve this issue
the input to network must to be rank 4, that is [bathsize, height, width, channel] or something like this. but your input is rank 5. maybe this is the reason
thanks for your comment but for a 3d convolutional neural network the input tensor is 5d then how can i preprocess?
You have to write your own ImageDataGenerator. https://github.com/fchollet/keras/issues/2939
Start with making a copy of the class: https://github.com/fchollet/keras/blob/cebf2084ebb0a603383ceb6807653921796cd095/keras/preprocessing/image.py#L342
Then modify it to work with 3d data.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.
I do use ImageDataGenerator to get my training and testing data and then fed them into the netwrok with input_shape = (100, 100, 16, 1). still there is an error says expecting 5d input but found 4d. I know 3D CNN need 5d shape but how can I reshape the train_data whcih yields from ImageDataGenerator? wish to get your help .. thanks
https://gist.github.com/Emadeldeen-24/736c33ac2af0c00cc48810ad62e1f54a
Here is an imagedatagenerator for 5D input to Conv3D nets. Hope it helps.
from tweaked_ImageGenerator_v2 import ImageDataGenerator
datagen = ImageDataGenerator()
train_data=datagen.flow_from_directory('path/to/data', target_size=(x, y), batch_size=32, frames_per_step=4)
Most helpful comment
thanks for your comment but for a 3d convolutional neural network the input tensor is 5d then how can i preprocess?