0

I am getting errors when training my CNN model which is for checking what a person is telling in sign language. I am working with keras, tensorflow. This is my code:

import tensorflow as tf  ;importing libraries
from tensorflow.keras import datasets,layers,models
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
; Data preprocessing
train_datagen = ImageDataGenerator(rescale = 1./255,
                                  shear_range = 0.2,
                                  zoom_range = 0.2,
                                  horizontal_flip = True)
training_set = train_datagen.flow_from_directory('split__data/Train',
                                                target_size = (64, 64),
                                                batch_size = 32,
                                                class_mode = 'categorical')
test_datagen = ImageDataGenerator(rescale = 1./255)
test_set = test_datagen.flow_from_directory('split__data/Test',
                                           target_size = (64, 64),
                                           batch_size = 32,
                                           class_mode = 'categorical')

; Building the model
cnn = tf.keras.models.Sequential()
cnn.add(tf.keras.layers.Conv2D(filters = 16,kernel_size = 3,activation = 'relu',input_shape = [64,64,3]))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))

cnn.add(tf.keras.layers.Conv2D(filters = 32,kernel_size = 3,activation = 'relu',input_shape = [64,64,3]))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))

cnn.add(tf.keras.layers.Conv2D(filters = 64,kernel_size = 3,activation = 'relu',input_shape = [64,64,3]))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))
cnn.add(tf.keras.layers.Flatten())
cnn.add(tf.keras.layers.Dense(units=500,activation='relu'))
cnn.add(tf.keras.layers.Dense(units=1,activation='softmax'))

; compiling and training
cnn.compile(optimizer = 'adam' , loss= 'categorical_crossentropy',metrics = ['accuracy'])
;the next line is giving me error
cnn.fit(x = training_set,validation_data = test_set,batch_size = 32,epochs = 10)

This is the error. I am not able to get from where these errors are being generated. I have tried changing batch size, image height and width but nothing happens. I am getting same error.

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-15-b67ba9813850> in <module>
----> 1 cnn.fit(x = training_set,validation_data = test_set,batch_size = 32,epochs = 10)

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\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)
   1181                 _r=1):
   1182               callbacks.on_train_batch_begin(step)
-> 1183               tmp_logs = self.train_function(iterator)
   1184               if data_handler.should_sync:
   1185                 context.async_wait()

C:\ProgramData\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()

C:\ProgramData\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 = \

C:\ProgramData\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 

C:\ProgramData\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(

C:\ProgramData\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,

C:\ProgramData\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:  In[0] mismatch In[1] shape: 35 vs. 1: [32,35] [500,1] 0 0
     [[node gradient_tape/sequential/dense_1/MatMul (defined at <ipython-input-15-b67ba9813850>:1) ]] [Op:__inference_train_function_847]

Function call stack:
train_function

can someone help?

1
  • 1
    How do you expect every single convolutional layer in the model to have input shape [64, 64,3]? Commented Nov 11, 2021 at 20:11

2 Answers 2

1

How many classes are in the dataset? You have the code

cnn.add(tf.keras.layers.Dense(units=1,activation='softmax'))

This would indicate you are doing binary classification which I expect is not what you want. Try this after your generator code

classes=list(training_set.class_indices.keys())
class_count=len (classes)  # this integer is the number of nodes you need in your models final layer

change the last layer in your model to

cnn.add(tf.keras.layers.Dense(units=class_count,activation='softmax'))
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Gerry P. Actually I am a beginner and I was trying to convert Binary model to categorical model by rearranging code. Can you tell me how to get a prediction for single image. Actually when I try to do it, it is giving correct output for only one character. Also if you could suggest a good resource for CNN model building, it would be great.
see my second answer
1

to predict a single image- first read in the image. If you trained your model on RGB images then

import matplotlib.pyplot as plt
import cv2
classes=list(train_gen.class_indices.keys())
img_path = r' full path to the image'
img=plt.imread(img_path)
img=cv2.resize(img, (64.64))
img=img/255
img=np.expand_dims(img, axis=0)
prediction=model.predict( img, verbose=1)
index=argmax(prediction)
predicted_class = classes(index)

I don't bother building my own CNN, instead I use transfer learning with the EfficientNetB3 model. Note EfficientNet expects pixels in the range 0 to 255 so do not scale the pixels. Code below works well for most applications

base_model=tf.keras.applications.EfficientNetB3(include_top=False, weights="imagenet",input_shape=img_shape, pooling='max') 
x=base_model.output
x=keras.layers.BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001 )(x)
x = Dense(256, kernel_regularizer = regularizers.l2(l = 0.016),activity_regularizer=regularizers.l1(0.006),
                bias_regularizer=regularizers.l1(0.006) ,activation='relu')(x)
x=Dropout(rate=.45, seed=123)(x)        
output=Dense(class_count, activation='softmax')(x)
model=Model(inputs=base_model.input, outputs=output)
model.compile(Adamax(learning_rate=.001), loss='categorical_crossentropy', metrics=['accuracy']) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.