2021年3月7日星期日

tensorflow.python.framework.errors_impl.InvalidArgumentError: Matrix size-incompatible: In[0]: [10,3], In[1]: [128,1]

I am new to TensorFlow and I am trying to make a project for my 5th semester. I am learning CNN and had this model for binary class classification problem in a course but when I try to use it for multi-class classification problem, it's giving me headache. I kinda found out that the problem is in the input shape I am giving but can't figure out how to solve this. I might be wrong too.

This is my code:

#Importing the libraries  import tensorflow as tf  import numpy as np  from keras.preprocessing.image import ImageDataGenerator  from keras.preprocessing import image  from keras.callbacks import EarlyStopping, ModelCheckpoint    #Preprocessing the training set  train_datagen = ImageDataGenerator(rescale = 1./255,                                     shear_range = 0.2,                                     zoom_range = 0.2,                                     horizontal_flip = True)  training_set = train_datagen.flow_from_directory('.../Dataset/Train_set',                                                   target_size = (128, 128),                                                   batch_size = 32,                                                   class_mode = 'categorical')    #Preprocessing the test set  test_datagen = ImageDataGenerator(rescale = 1./255)  test_set = test_datagen.flow_from_directory('.../Dataset/Test_set',                                              target_size = (128, 128),                                              batch_size = 32,                                              class_mode = 'categorical')    #Initializing the cnn  cnn = tf.keras.models.Sequential()    #CNN  cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu', input_shape=[128,128,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'))  cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))  cnn.add(tf.keras.layers.Flatten())  cnn.add(tf.keras.layers.Dense(units=128, activation='relu'))  cnn.add(tf.keras.layers.Dense(units=1, activation='softmax'))    #Compiling the CNN  cnn.compile(optimizer = 'sgd', loss = 'categorical_crossentropy', metrics = ['accuracy'])    #Callbacks  es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=200)  mc = ModelCheckpoint('Models_After_Using_Callback/best_model_demo.h5', monitor='val_accuracy', mode='max', verbose=1, save_best_only=True)    #Training the CNN and evaluating on the test set  cnn.fit(x = training_set, validation_data = test_set, epochs = 50, callbacks=[es, mc])    cnn.summary()  cnn.save("Trained Model/Trained_model_1_(128 by 128).h5")  

Here is the error i am getting:

Traceback (most recent call last):    File "D:/3-2_Project/Digital Attendence System/Face_Recognition_With_Video/Train_Data_Demo.py", line 45, in <module>      cnn.fit(x = training_set, validation_data = test_set, epochs = 50, callbacks=[es, mc])    File "D:\Softwares\Soft (Installed)\PYTHON 3.8\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1100, in fit      tmp_logs = self.train_function(iterator)    File "D:\Softwares\Soft (Installed)\PYTHON 3.8\lib\site-packages\tensorflow\python\eager\def_function.py", line 828, in __call__      result = self._call(*args, **kwds)    File "D:\Softwares\Soft (Installed)\PYTHON 3.8\lib\site-packages\tensorflow\python\eager\def_function.py", line 888, in _call      return self._stateless_fn(*args, **kwds)    File "D:\Softwares\Soft (Installed)\PYTHON 3.8\lib\site-packages\tensorflow\python\eager\function.py", line 2942, in __call__      return graph_function._call_flat(    File "D:\Softwares\Soft (Installed)\PYTHON 3.8\lib\site-packages\tensorflow\python\eager\function.py", line 1918, in _call_flat      return self._build_call_outputs(self._inference_function.call(    File "D:\Softwares\Soft (Installed)\PYTHON 3.8\lib\site-packages\tensorflow\python\eager\function.py", line 555, in call      outputs = execute.execute(    File "D:\Softwares\Soft (Installed)\PYTHON 3.8\lib\site-packages\tensorflow\python\eager\execute.py", line 59, in quick_execute      tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,  tensorflow.python.framework.errors_impl.InvalidArgumentError:  Matrix size-incompatible: In[0]: [10,3], In[1]: [128,1]       [[node gradient_tape/sequential/dense_1/MatMul (defined at /3-2_Project/Digital Attendence System/Face_Recognition_With_Video/Train_Data_Demo.py:45) ]] [Op:__inference_train_function_602]  

Can anyone tell me what's wrong with my code here?

https://stackoverflow.com/questions/66506900/tensorflow-python-framework-errors-impl-invalidargumenterror-matrix-size-incomp March 06, 2021 at 10:45PM

没有评论:

发表评论