Model ZOO Services Image Classification Training fails with 2 classes
When training a standard model (e.g. `mobilenetv2_a035`, `pretrained: True`) for image classification on a custom dataset with 2 classes, training crashes at the first epoch:
ValueError: Arguments `target` and `output` must have the same rank (ndim).
Received: target.shape=(None,), output.shape=(None, 2)
I’m working with: Windows, Python 3.12, TensorFlow/Keras 3
My minimal user_config.yaml to reproduce the error:
model:
model_name: mobilenetv2_a035
input_shape: (224, 224, 3)
pretrained: True
dataset:
class_names: [class_a, class_b]
training:
dropout: 0.3
My analysis is the following:
1. `get_mobilenetv2()` (`image_classification/tf/src/models/mobilenetv2.py`) correctly builds a binary head when
`num_classes == 2`: `Dense(1, activation="sigmoid")`.
2. `api.get_model()` saves the freshly built model and sets `cfg.model.model_path`.
3. Because `model_path` is now set, `ICTrainer.prepare()` (`tf/src/training/ic_trainer.py`) calls `change_model_number_of_classes(self.model, self.num_classes)`.
4. `change_model_number_of_classes()` (`tf/src/utils/models_mgt.py`) uses `output_shape = num_classes` (= 2). Since the current output has 1 unit, it replaces the head with `Dense(2, sigmoid)`.
5. `get_loss()` returns `BinaryCrossentropy` for 2 classes, and the dataset labels are integers with shape `(None,)`, so they don't match the `(None, 2)` output.
Is this a bug?
Thank you
