title: How can I solve InvalidArgumentError: Graph execution error?
body: <p>I am trying to train a deep learning model by using CNN and the transfer learning method. I used VGG16 with ImageNet as a pretrained model. My aim is to implement facial emotion recognition in Google Colab, but I got an error while fitting the model. It gives the following error after the first epoch is completed.</p>
  <pre><code>Epoch 1/50
  224/224 [==============================] - ETA: 0s - loss: 1.8204 - accuracy: 0.2337
  ---------------------------------------------------------------------------
  InvalidArgumentError                      Traceback (most recent call last)
  &lt;ipython-input-26-abee8cf0777d&gt; in &lt;module&gt;()
       5                 epochs=epochs,
       6                 validation_data=validation_generator,
  ----&gt; 7                 validation_steps=num_test_imgs//batch_size)
  
  1 frames
  /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
  53     ctx.ensure_initialized()
      54     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
  ---&gt; 55                                         inputs, attrs, num_outputs)
      56   except core._NotOkStatusException as e:
      57     if name is not None:
  
  InvalidArgumentError: Graph execution error:
  
  Detected at node 'sequential_1/vgg16/block1_conv1/Conv2D' defined at (most recent call last):
     File &quot;/usr/lib/python3.7/runpy.py&quot;, line 193, in _run_module_as_main
       &quot;__main__&quot;, mod_spec)
     File &quot;/usr/lib/python3.7/runpy.py&quot;, line 85, in _run_code
       exec(code, run_globals)
     File &quot;/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py&quot;, line 16, in &lt;module&gt;
       app.launch_new_instance()
     File &quot;/usr/local/lib/python3.7/dist-packages/traitlets/config/application.py&quot;, line 846, in launch_instance
       app.start()
     File &quot;/usr/local/lib/python3.7/dist-packages/ipykernel/kernelapp.py&quot;, line 499, in start
       self.io_loop.start()
     File &quot;/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py&quot;, line 132, in start
       self.asyncio_loop.run_forever()
     File &quot;/usr/lib/python3.7/asyncio/base_events.py&quot;, line 541, in run_forever
       self._run_once()
     File &quot;/usr/lib/python3.7/asyncio/base_events.py&quot;, line 1786, in _run_once
       handle._run()
     File &quot;/usr/lib/python3.7/asyncio/events.py&quot;, line 88, in _run
       self._context.run(self._callback, *self._args)
     File &quot;/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py&quot;, line 122, in _handle_events
       handler_func(fileobj, events)
     File &quot;/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py&quot;, line 300, in null_wrapper
       return fn(*args, **kwargs)
     File &quot;/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py&quot;, line 452, in _handle_events
       self._handle_recv()
     File &quot;/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py&quot;, line 481, in _handle_recv
       self._run_callback(callback, msg)
     File &quot;/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py&quot;, line 431, in _run_callback
       callback(*args, **kwargs)
     File &quot;/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py&quot;, line 300, in null_wrapper
       return fn(*args, **kwargs)
     File &quot;/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py&quot;, line 283, in dispatcher
       return self.dispatch_shell(stream, msg)
     File &quot;/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py&quot;, line 233, in dispatch_shell
       handler(stream, idents, msg)
     File &quot;/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py&quot;, line 399, in execute_request
       user_expressions, allow_stdin)
     File &quot;/usr/local/lib/python3.7/dist-packages/ipykernel/ipkernel.py&quot;, line 208, in do_execute
       res = shell.run_cell(code, store_history=store_history, silent=silent)
     File &quot;/usr/local/lib/python3.7/dist-packages/ipykernel/zmqshell.py&quot;, line 537, in run_cell
       return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
     File &quot;/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py&quot;, line 2718, in run_cell
       interactivity=interactivity, compiler=compiler, result=result)
     File &quot;/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py&quot;, line 2822, in run_ast_nodes
       if self.run_code(code, result):
     File &quot;/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py&quot;, line 2882, in run_code
       exec(code_obj, self.user_global_ns, self.user_ns)
     File &quot;&lt;ipython-input-26-abee8cf0777d&gt;&quot;, line 7, in &lt;module&gt;
       validation_steps=num_test_imgs//batch_size)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py&quot;, line 64, in error_handler
       return fn(*args, **kwargs)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/engine/training.py&quot;, line 1431, in fit
       _use_cached_eval_dataset=True)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py&quot;, line 64, in error_handler
       return fn(*args, **kwargs)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/engine/training.py&quot;, line 1716, in evaluate
       tmp_logs = self.test_function(iterator)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/engine/training.py&quot;, line 1525, in test_function
       return step_function(self, iterator)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/engine/training.py&quot;, line 1514, in step_function
       outputs = model.distribute_strategy.run(run_step, args=(data,))
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/engine/training.py&quot;, line 1507, in run_step
       outputs = model.test_step(data)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/engine/training.py&quot;, line 1471, in test_step
       y_pred = self(x, training=False)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py&quot;, line 64, in error_handler
       return fn(*args, **kwargs)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py&quot;, line 1096, in __call__
       outputs = call_fn(inputs, *args, **kwargs)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py&quot;, line 92, in error_handler
       return fn(*args, **kwargs)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/engine/sequential.py&quot;, line 374, in call
       return super(Sequential, self).call(inputs, training=training, mask=mask)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py&quot;, line 452, in call
       inputs, training=training, mask=mask)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py&quot;, line 589, in _run_internal_graph
       outputs = node.layer(*args, **kwargs)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py&quot;, line 64, in error_handler
       return fn(*args, **kwargs)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py&quot;, line 1096, in __call__
       outputs = call_fn(inputs, *args, **kwargs)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py&quot;, line 92, in error_handler
       return fn(*args, **kwargs)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py&quot;, line 452, in call
       inputs, training=training, mask=mask)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py&quot;, line 589, in _run_internal_graph
       outputs = node.layer(*args, **kwargs)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py&quot;, line 64, in error_handler
       return fn(*args, **kwargs)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py&quot;, line 1096, in __call__
       outputs = call_fn(inputs, *args, **kwargs)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py&quot;, line 92, in error_handler
       return fn(*args, **kwargs)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/layers/convolutional.py&quot;, line 248, in call
       outputs = self.convolution_op(inputs, self.kernel)
     File &quot;/usr/local/lib/python3.7/dist-packages/keras/layers/convolutional.py&quot;, line 240, in convolution_op
       name=self.__class__.__name__)
  Node: 'sequential_1/vgg16/block1_conv1/Conv2D'
  input depth must be evenly divisible by filter depth: 1 vs 3
      [[{{node sequential_1/vgg16/block1_conv1/Conv2D}}]] [Op:__inference_test_function_3544]
  
  </code></pre>
  <p>I have train and test folders, and there are 7 emotion folders in both, so because of this, I set the output layer as 7.</p>
  <pre><code>model.add(Dense(7, activation='softmax'))
  </code></pre>
  <p>And here is the whole code:</p>
  <pre><code>train_datagen = ImageDataGenerator(
      rescale=1/255,
      rotation_range=30,
      shear_range=0.3,
      zoom_range=0.3,
      horizontal_flip=True,
      fill_mode='nearest')
  validation_datagen = ImageDataGenerator(rescale=1./255)
  </code></pre>
  <pre><code>train_generator = train_datagen.flow_from_directory(
      train_data_dir,
      color_mode='rgb',
      target_size=(IMG_HEIGHT, IMG_WIDTH),
      batch_size=batch_size,
      class_mode='categorical',
      shuffle=True)
  </code></pre>
  <pre><code>validation_generator = validation_datagen.flow_from_directory(
      validation_data_dir,
      color_mode='grayscale',
      target_size=(IMG_HEIGHT, IMG_WIDTH),
      batch_size=batch_size,
      class_mode='categorical',
      shuffle=True)
  </code></pre>
  <pre><code>class_labels=['Angry','Disgust', 'Fear', 'Happy','Neutral','Sad','Surprise']
  </code></pre>
  <pre><code>from keras.applications.vgg16 import VGG16 
  #from keras.applications import VGG16
  #Load the VGG model
  base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
  base_model.summary()
  </code></pre>
  <pre><code>for layer in base_model.layers[:-4]:
      layer.trainable = False
   
  #Check the trainable status of the individual layers
  for layer in base_model.layers:
      print(layer, layer.trainable)
  
  </code></pre>
  <pre><code>model = Sequential()
  
  #Adding the vgg convolutional base model
  model.add(base_model)
   
  model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(224,224,3),padding='same'))
  
  model.add(Conv2D(64, kernel_size=(3, 3), activation='relu',padding='same'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.1))
  
  model.add(Conv2D(128, kernel_size=(3, 3), activation='relu',padding='same'))
  model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
  model.add(Dropout(0.1))
  
  model.add(Conv2D(256, kernel_size=(3, 3), activation='relu',padding='same'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.1))
  
  model.add(Flatten())
  model.add(Dense(512, activation='relu'))
  model.add(Dropout(0.2))
  
  model.add(Dense(7, activation='softmax'))
  
  model.compile(optimizer = 'adam', loss='categorical_crossentropy', metrics=['accuracy'])
  print(model.summary())
  </code></pre>
  <pre><code>epochs=50
  #train_generator=np.array(train_generator)
  history=model.fit(train_generator,
                  steps_per_epoch=num_train_imgs//batch_size,
                  epochs=epochs,
                  validation_data=validation_generator,
                  validation_steps=num_test_imgs//batch_size)
  </code></pre>
  <p>I could not understand where the error came from. How can I solve this?</p>