question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImageDataGenerator fed to 3D CNN in keras

See original GitHub issue

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 train_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)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:55

github_iconTop GitHub Comments

6reactions
emadeldeen24commented, Sep 30, 2020

You can use my tweaked version of ImageDataGenerator class that fits this kind of problems.

It’s the same as the main Keras ImageDataGenerator but I added an option to take more than one image/frame on each iteration. This is by changing the parameter frames_per_step to specify the number of frames/images you want to include in each iteration.

So here’s how to use it:

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)
4reactions
dhuy228commented, Sep 27, 2019

@MarcelBeining hey mate, I just customised the Keras’ ImageDataGenerator for medical image analysis applications, i.e. 3D Dicom slices (10,32,32,32,1). It might help with your problem.

https://github.com/dhuy228/augmented-volumetric-image-generator/blob/master/generator.py

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keras ImageDataGenerator flow directory with 3D CNN data ...
I think the problem is the ImageDataGenerator . It only works for images, not for videos (I got the same error and at ......
Read more >
tf.keras.preprocessing.image.ImageDataGenerator - TensorFlow
Generate batches of tensor image data with real-time data augmentation.
Read more >
3D CNN with ImageDataGenerator in keras - Google Groups
Hey everyone, I am new in keras and python I am trying to use 3D CNN with keras, I did the following code...
Read more >
Keras ImageDataGenerator and Data Augmentation
In today's tutorial, you will learn how to use Keras' ImageDataGenerator class to perform data augmentation.
Read more >
Image Augmentation for Deep Learning with Keras
After you have created and configured your ImageDataGenerator, you must fit it on your data. This will calculate any statistics required to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found