title: How to train categorical CNN?
body: <p>I'm currently trying to train a model to do bird species recognization. This model will be later converted and hosted on Arduino nano 33 BLE near a place where birds come to eat.</p>
  <p>To train my model I've used kaggle API to use the dataset that contains 250 species divided into a train, validation, test set. The images are .jpg 224x224 RGB.
  To ease data labelling I used Keras preprocessing tool that allow me to label data based on their folder, this works perfectly.</p>
  <p>Here is the preprocessing :</p>
  <pre><code>
      from tensorflow.keras.preprocessing.image import ImageDataGenerator
      
      # All images will be augmented
      train_datagen = ImageDataGenerator(
            rescale=1./255,
            rotation_range=40,
            width_shift_range=0.2,
            height_shift_range=0.2,
            shear_range=0.2,
            zoom_range=0.2,
            horizontal_flip=True,
            fill_mode='nearest')
      
      # Flow training images in batches of 128 using train_datagen generator
      train_generator = train_datagen.flow_from_directory(
              '/content/train',  # This is the source directory for training images
              target_size=(224, 224),  # All images will be resized to 150x150
              batch_size=128,
              class_mode='binary',
              color_mode='rgb',
              save_format='jpg')
      
      validation_datagen = ImageDataGenerator(rescale=1/255)
      
      validation_generator = validation_datagen.flow_from_directory(
              '/content/valid',
              target_size=(224, 224),
              class_mode='categorical',
              color_mode='rgb',
              save_format='jpg')
  
  </code></pre>
  <p>Then I have created a keras model with convolution and maxpooling to process my data and then I've used 2 hidden layers to use softmax activation. Here is my model code :</p>
  <pre><code>
      import tensorflow as tf
      
      model = tf.keras.models.Sequential([
          tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(224, 224, 3)),
          tf.keras.layers.MaxPooling2D(2, 2),
          tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
          tf.keras.layers.MaxPooling2D(2,2),
          tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
          tf.keras.layers.MaxPooling2D(2,2),
          tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
          tf.keras.layers.MaxPooling2D(2,2),
          tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
          tf.keras.layers.MaxPooling2D(2,2),
          tf.keras.layers.Flatten(),
          tf.keras.layers.Dense(512, activation='relu'),
          tf.keras.layers.Dense(250, activation='softmax')
      ])
  
  </code></pre>
  <p>The error I'm facing is :</p>
  <pre><code>InvalidArgumentError                      Traceback (most recent call last)
  &lt;ipython-input-58-6a14ef1f8bcb&gt; in &lt;module&gt;()
        4       epochs=15,
        5       verbose=1,
  ----&gt; 6       validation_data=validation_generator)
  
  6 frames
  /usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
       58     ctx.ensure_initialized()
       59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
  ---&gt; 60                                         inputs, attrs, num_outputs)
       61   except core._NotOkStatusException as e:
       62     if name is not None:
  
  InvalidArgumentError:  Can not squeeze dim[1], expected a dimension of 1, got 250
       [[node Squeeze (defined at &lt;ipython-input-58-6a14ef1f8bcb&gt;:6) ]] [Op:__inference_test_function_3788]
  
  Function call stack:
  test_function
  </code></pre>
  <p>The repository of my project :
  <a href="https://github.com/BaptisteZloch/Birds-species-spotting" rel="nofollow noreferrer">https://github.com/BaptisteZloch/Birds-species-spotting</a></p>
  <p>I hope someone could help me to solve this problem !</p>
  <p>Regards,
  Baptiste ZLOCH</p>