ChunML / ssd-tf2

A super clean implementation of SSD (Single Shot MultiBox Detector) made possible by Tensorflow 2.0
MIT License
115 stars 54 forks source link

Shape mismatch after the config was changed #8

Closed wang-tf closed 4 years ago

wang-tf commented 4 years ago

Thanks for your work! While after I changed the default box format to:

SSD800:
  ratios: [[2], [2, 2], [2, 2], [2, 2], [2], [2], [2]]
  scales: [0.002, 0.018, 0.034, 0.05, 0.066, 0.082, 0.1, 1.05]
  fm_sizes: [100, 50, 25, 13, 11, 9, 6]
  image_size: 800

The error was raised like:

   train.py:46 train_step  *
        conf_loss, loc_loss = criterion(confs, locs, gt_confs, gt_locs)
    /data/wangtf/Projects/ssd-tf2/losses.py:59 __call__  *
        temp_loss = cross_entropy(gt_confs, confs)
    /opt/miniconda3/envs/tf2.0/lib/python3.7/site-packages/tensorflow_core/python/keras/losses.py:126 __call__
        losses = self.call(y_true, y_pred)
    /opt/miniconda3/envs/tf2.0/lib/python3.7/site-packages/tensorflow_core/python/keras/losses.py:221 call
        return self.fn(y_true, y_pred, **self._fn_kwargs)
    /opt/miniconda3/envs/tf2.0/lib/python3.7/site-packages/tensorflow_core/python/keras/losses.py:978 sparse_categorical_crossentropy
        y_true, y_pred, from_logits=from_logits, axis=axis)
    /opt/miniconda3/envs/tf2.0/lib/python3.7/site-packages/tensorflow_core/python/keras/backend.py:4546 sparse_categorical_crossentropy
        labels=target, logits=output)
    /opt/miniconda3/envs/tf2.0/lib/python3.7/site-packages/tensorflow_core/python/ops/nn_ops.py:3477 sparse_softmax_cross_entropy_with_logits_v2
        labels=labels, logits=logits, name=name)
    /opt/miniconda3/envs/tf2.0/lib/python3.7/site-packages/tensorflow_core/python/ops/nn_ops.py:3393 sparse_softmax_cross_entropy_with_logits
        logits.get_shape()))

    ValueError: Shape mismatch: The shape of labels (received (364296,)) should equal the shape of logits except for the last dimension (received (6, 60716)).

It likey occured when using tf.keras.losses.SparseCategoricalCrossentropy. And my input tensors' shape were printed like 'gt_confs: (6, 60716), confs: (6, 60716, 1)' Could you help me to find my error or give me some advices?

ChunML commented 4 years ago

Hmm, that error occurred because you are supposed to use Binary loss instead (since the shape is (batch, n_box, 1) class is either 0 or 1 => binary). The thing is, you can't just have 1 class in object detection. You will always end up with n_class + 1 (the background is one class itself).

To be short, check n_class setting first. You are probably setting it to zero.

wang-tf commented 4 years ago

Yes, you are right. The problem is the NUM_CLASS setting. It need including background.