0
$\begingroup$

I am using the BreakHis database. More specifically, I am trying to classify the 400X images. The sizes of the images are $700x460x3$.

Here are the details of the dataset. Also, here is the code for the classification:

from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator()
train_it = datagen.flow_from_directory( 'C:/Users/ahmed.zaalouk/Downloads/train' , class_mode= 'categorical' , batch_size=32,color_mode='rgb')
val_it = datagen.flow_from_directory( 'C:/Users/ahmed.zaalouk/Downloads/validation' , class_mode= 'categorical' , batch_size=32,color_mode='rgb')
test_it = datagen.flow_from_directory( 'C:/Users/ahmed.zaalouk/Downloads/test' , class_mode= 'categorical' , batch_size=32,color_mode='rgb')
from keras.regularizers import l2
from keras.models import Sequential
from keras.layers import Add, Conv2D, MaxPooling2D, Dropout, Flatten, Dense, BatchNormalization, Activation
from tensorflow.keras import activations
# Creating the model
CNN_model = Sequential()
# The First Block
CNN_model.add(Conv2D(32, kernel_size=3,kernel_initializer='he_uniform', kernel_regularizer=l2(0.0005), padding='same', input_shape=(700, 460,3)))
CNN_model.add(Activation(activations.relu))
CNN_model.add(BatchNormalization())
CNN_model.add(MaxPooling2D(2, 2))


# The Second Block
CNN_model.add(Conv2D(32, kernel_size=3, kernel_initializer='he_uniform', kernel_regularizer=l2(0.0005), padding='same'))
CNN_model.add(Activation(activations.relu))
CNN_model.add(BatchNormalization())
CNN_model.add(MaxPooling2D(2, 2))
from keras.optimizers import Adam, SGD
from keras.engine.training import Model 
from keras import backend as K, regularizers
from keras import losses
CNN_model.add(Flatten()) 
# Layer 1
CNN_model.add(Dense(512)) # 512 units
# Layer 2
CNN_model.add(Dense(512, activation='relu')) # 512 units
CNN_model.add(Dropout(0.5))
# Layer 3
CNN_model.add(Dense(8, activation='softmax')) 
CNN_model.compile(optimizer="Adam", loss = 'categorical_crossentropy', metrics = ['acc'])
CNN_model.fit_generator(train_it, steps_per_epoch=19, validation_data=val_it, validation_steps=5)

Here is the model summary :

Model: "sequential_11"
_________________________________________________________________
Layer (type)                 Output Shape              Param    
=================================================================
conv2d_29 (Conv2D)           (None, 700, 460, 32)      896       
_________________________________________________________________
activation_27 (Activation)   (None, 700, 460, 32)      0         
_________________________________________________________________
batch_normalization_27 (Batc (None, 700, 460, 32)      128       
_________________________________________________________________
max_pooling2d_27 (MaxPooling (None, 350, 230, 32)      0         
_________________________________________________________________
conv2d_30 (Conv2D)           (None, 350, 230, 32)      9248      
_________________________________________________________________
activation_28 (Activation)   (None, 350, 230, 32)      0         
_________________________________________________________________
batch_normalization_28 (Batc (None, 350, 230, 32)      128       
_________________________________________________________________
max_pooling2d_28 (MaxPooling (None, 175, 115, 32)      0         
_________________________________________________________________
flatten_8 (Flatten)          (None, 644000)            0         
_________________________________________________________________
dense_24 (Dense)             (None, 512)               329728512 
_________________________________________________________________
dense_25 (Dense)             (None, 512)               262656    
_________________________________________________________________
dropout_8 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_26 (Dense)             (None, 8)                 4104      
=================================================================
Total params: 330,005,672
Trainable params: 330,005,544
Non-trainable params: 128
_________________________________________________________________
None

I am getting this error and I don't know how to fix it :

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-80-7fdd4a4a32e1> in <module>
----> 1 CNN_model.fit_generator(train_it, steps_per_epoch=19, validation_data=val_it, validation_steps=5)

~\Anaconda3\lib\site-packages\keras\engine\training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
   1916                   'will be removed in a future version. '
   1917                   'Please use `Model.fit`, which supports generators.')
-> 1918     return self.fit(
   1919         generator,
   1920         steps_per_epoch=steps_per_epoch,

~\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
   1156                 _r=1):
   1157               callbacks.on_train_batch_begin(step)
-> 1158               tmp_logs = self.train_function(iterator)
   1159               if data_handler.should_sync:
   1160                 context.async_wait()

~\Anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in __call__(self, *args, **kwds)
    887 
    888       with OptionalXlaContext(self._jit_compile):
--> 889         result = self._call(*args, **kwds)
    890 
    891       new_tracing_count = self.experimental_get_tracing_count()

~\Anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
    948         # Lifting succeeded, so variables are initialized and we can run the
    949         # stateless function.
--> 950         return self._stateless_fn(*args, **kwds)
    951     else:
    952       _, _, _, filtered_flat_args = \

~\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py in __call__(self, *args, **kwargs)
   3021       (graph_function,
   3022        filtered_flat_args) = self._maybe_define_function(args, kwargs)
-> 3023     return graph_function._call_flat(
   3024         filtered_flat_args, captured_inputs=graph_function.captured_inputs)  # pylint: disable=protected-access
   3025 

~\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
   1958         and executing_eagerly):
   1959       # No tape is watching; skip to running the function.
-> 1960       return self._build_call_outputs(self._inference_function.call(
   1961           ctx, args, cancellation_manager=cancellation_manager))
   1962     forward_backward = self._select_forward_and_backward_functions(

~\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py in call(self, ctx, args, cancellation_manager)
    589       with _InterpolateFunctionError(self):
    590         if cancellation_manager is None:
--> 591           outputs = execute.execute(
    592               str(self.signature.name),
    593               num_outputs=self._num_outputs,

~\Anaconda3\lib\site-packages\tensorflow\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     57   try:
     58     ctx.ensure_initialized()
---> 59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
     60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:

InvalidArgumentError:  Input to reshape is a tensor with 4194304 values, but the requested shape requires a multiple of 644000
     [[node sequential_11/flatten_8/Reshape (defined at C:\Users\ahmed.zaalouk\Anaconda3\lib\site-packages\keras\layers\core.py:672) ]] [Op:__inference_train_function_12909]

Errors may have originated from an input operation.
Input Source operations connected to node sequential_11/flatten_8/Reshape:
 sequential_11/max_pooling2d_28/MaxPool (defined at C:\Users\ahmed.zaalouk\Anaconda3\lib\site-packages\keras\layers\pooling.py:355) 
 sequential_11/flatten_8/Const (defined at C:\Users\ahmed.zaalouk\Anaconda3\lib\site-packages\keras\layers\core.py:667)

Function call stack:
train_function

Edit : The number of images in the training set is 1275 The number of images in the validation set is 365

$\endgroup$

1 Answer 1

1
$\begingroup$

Default target size in flow_from_directory is 256 * 256 (height * width). So your data is resized to a dimension 256 * 256 while reading and you specified input_shape=(700, 460,3) in the layer

ImageDataGenerator.flow_from_directory(
    directory,
    **target_size=(256, 256)**,
    color_mode="rgb",
    classes=None,
    class_mode="categorical",
    batch_size=32,
    shuffle=True,
    seed=None,
    save_to_dir=None,
    save_prefix="",
    save_format="png",
    follow_links=False,
    subset=None,
    interpolation="nearest",
)
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.