title: proper tensorflow flow_from_dataframe usage with multi-output / multi-label model
body: <p>I'm getting a shape incompatibility at the point of trying to fit my model -- not sure if the issue is with my instantiation of the <code>DataIterator</code> or something in my model definition...</p>
  <p><a href="http://pastebin.com/Fs8UKuLR" rel="nofollow noreferrer">Pastebin of code to reproduce</a></p>
  <p><strong>NOTE: to use code sample above code with the below sample data, you must modify <code>directory=None</code> to be <code>directory=data_sample</code> and untar <code>data_sample.tgz</code> in the same directory you're running this code from</strong></p>
  <p>Run this to train a single prediction head age model:</p>
  <pre><code>python tf_train_copy.py --csv_file data_sample/dataframe.csv --count 100 --training_type age
  </code></pre>
  <p>Run this to train a two prediction  head age and gender model which fails because of the shape-related <code>ValueError</code> that this question is for:</p>
  <pre><code>python tf_train_copy.py --csv_file data_sample/dataframe.csv --count 100 --training_type age_gender
  </code></pre>
  <p><a href="https://drive.google.com/file/d/1lT32IVLUYebT_v7hvNgVnhMlAHOpqlJR/view?usp=sharing" rel="nofollow noreferrer">Small sample of 100 images and associated dataframe</a></p>
  <p>I have a dataframe that looks like this -- with binary <code>gender</code> data <code>{'m', 'f'}</code> and <code>age</code> data that has 3 values:</p>
  <pre><code>                                                 filename gender mask age_group
  218069  /home/lrm/data/VGGFace2_cropped/n005204_0182_0...      f    f  (26, 39)
  295315  /home/lrm/data/VGGFace2_cropped/n000162_0085_0...      m    f  (18, 25)
  176301  /home/lrm/data/VGGFace2_cropped/n005378_0359_0...      f    f  (26, 39)
  212662  /home/lrm/data/VGGFace2_cropped/n006412_0026_0...      m    f  (40, 55)
  327910  /home/lrm/data/VGGFace2_cropped/n005114_0240_0...      m    f  (18, 25)
  ...                                                   ...    ...  ...       ...
  283903  /home/lrm/data/VGGFace2_cropped/n002902_0158_0...      m    f  (26, 39)
  156748  /home/lrm/data/VGGFace2_cropped/n003909_0464_0...      m    f  (40, 55)
  89294   /home/lrm/data/VGGFace2_cropped/n000332_0195_0...      f    f  (18, 25)
  156880  /home/lrm/data/VGGFace2_cropped/n005892_0122_0...      f    f  (18, 25)
  304084  /home/lrm/data/VGGFace2_cropped/n002321_0114_0...      m    f  (18, 25)
  </code></pre>
  <p>and a model defined as such:</p>
  <pre><code>dense_age = tf.keras.layers.Dense(units=64, activation=tf.keras.layers.ReLU(6.0), name='dense_age')(base_model.output)
  dropout_age = tf.keras.layers.Dropout(rate=args.dropout)(dense_age)
  pred_age = tf.keras.layers.Dense(units=3, activation='softmax', name='pred_age')(dropout_age)
  
  dense_gender = tf.keras.layers.Dense(units=64, activation=tf.keras.layers.ReLU(6.0), name='dense_gender')(base_model.output)
  dropout_gender = tf.keras.layers.Dropout(rate=args.dropout)(dense_gender)
  # also tried units=2 for gender prediction
  pred_gender = tf.keras.layers.Dense(units=1, activation='softmax', name='pred_gender')(dropout_gender)
  
  model = tf.keras.models.Model(inputs=base_model.input, outputs=[pred_age, pred_gender])
  model.compile(
          optimizer=optimizer,
          loss=[tf.keras.losses.CategoricalCrossentropy(from_logits=True, label_smoothing=0.1), tf.keras.losses.CategoricalCrossentropy(from_logits=True, label_smoothing=0.1)],
          metrics=[tf.keras.metrics.Accuracy(name='age_accuracy'), tf.keras.metrics.Accuracy(name='gender_accuracy')])
  </code></pre>
  <p>this is how my <code>DataIterator</code> object is created:</p>
  <pre><code>train_generator = train_datagen.flow_from_dataframe(
          directory=None,
          dataframe=train_data,
          x_col='filename',
          y_col=['age_group', 'gender'],
          class_mode='multi_output',
          **dataflow_kwargs)
  </code></pre>
  <p>and I'm getting a <code>ValueError</code> saying there's a shape incompatibility:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;tf_train.py&quot;, line 385, in &lt;module&gt;
      validation_steps=validation_steps)
    File &quot;/home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py&quot;, line 324, in new_func
      return func(*args, **kwargs)
    File &quot;/home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py&quot;, line 1829, in fit_generator
      initial_epoch=initial_epoch)
    File &quot;/home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py&quot;, line 108, in _method_wrapper
      return method(self, *args, **kwargs)
    File &quot;/home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py&quot;, line 1098, in fit
      tmp_logs = train_function(iterator)
    File &quot;/home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py&quot;, line 780, in __call__
      result = self._call(*args, **kwds)
    File &quot;/home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py&quot;, line 823, in _call
      self._initialize(args, kwds, add_initializers_to=initializers)
    File &quot;/home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py&quot;, line 697, in _initialize
      *args, **kwds))
    File &quot;/home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/eager/function.py&quot;, line 2855, in _get_concrete_function_internal_garbage_collected
      graph_function, _, _ = self._maybe_define_function(args, kwargs)
    File &quot;/home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/eager/function.py&quot;, line 3213, in _maybe_define_function
      graph_function = self._create_graph_function(args, kwargs)
    File &quot;/home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/eager/function.py&quot;, line 3075, in _create_graph_function
      capture_by_value=self._capture_by_value),
    File &quot;/home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py&quot;, line 986, in func_graph_from_py_func
      func_outputs = python_func(*func_args, **func_kwargs)
    File &quot;/home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py&quot;, line 600, in wrapped_fn
      return weak_wrapped_fn().__wrapped__(*args, **kwds)
    File &quot;/home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py&quot;, line 973, in wrapper
      raise e.ag_error_metadata.to_exception(e)
  ValueError: in user code:
  
      /home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:806 train_function  *
          return step_function(self, iterator)
      /home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:796 step_function  **
          outputs = model.distribute_strategy.run(run_step, args=(data,))
      /home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
          return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
      /home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
          return self._call_for_each_replica(fn, args, kwargs)
      /home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
          return fn(*args, **kwargs)
      /home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:789 run_step  **
          outputs = model.train_step(data)
      /home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:749 train_step
          y, y_pred, sample_weight, regularization_losses=self.losses)
      /home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/keras/engine/compile_utils.py:204 __call__
          loss_value = loss_obj(y_t, y_p, sample_weight=sw)
      /home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/keras/losses.py:149 __call__
          losses = ag_call(y_true, y_pred)
      /home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/keras/losses.py:253 call  **
          return ag_fn(y_true, y_pred, **self._fn_kwargs)
      /home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py:201 wrapper
          return target(*args, **kwargs)
      /home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/keras/losses.py:1535 categorical_crossentropy
          return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)
      /home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py:201 wrapper
          return target(*args, **kwargs)
      /home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/keras/backend.py:4687 categorical_crossentropy
          target.shape.assert_is_compatible_with(output.shape)
      /home/lrm/.pyenv/versions/tf_training_231/lib/python3.7/site-packages/tensorflow/python/framework/tensor_shape.py:1134 assert_is_compatible_with
          raise ValueError(&quot;Shapes %s and %s are incompatible&quot; % (self, other))
  
      ValueError: Shapes (None, 1) and (None, 3) are incompatible
  </code></pre>