Iammuratc / satellitepy

Fine-grained object detection in satellite images
MIT License
2 stars 0 forks source link

How to train a model with obboxes ? #194

Closed bobbender closed 5 months ago

bobbender commented 5 months ago

I can train a model with the following command using only the task "coarse-class":

python3 tools/train/bbavector.py \
--train-image-folder /satellitepy/data/small_1024/train/images/  \
--train-label-folder /satellitepy/data/small_1024/train/annfiles/ \
--train-label-format dota \
--valid-image-folder /satellitepy/data/small_1024/val/images/ \
--valid-label-folder  /satellitepy/data/small_1024/val/annfiles/ \
--valid-label-format dota \
--tasks coarse-class \
--target-task coarse-class \
--input-h 1024 \
--input-w 1024 \
--out-folder /mount_dir/training/ \
--num-epoch 1

However as far as I understand this does NOT include obboxes (?)

So a training tried to start a training with obboxes, by adding the task "obboxes", with the following command:

python3 tools/train/bbavector.py \
--train-image-folder /satellitepy/data/small_1024/train/images/  \
--train-label-folder /satellitepy/data/small_1024/train/annfiles/ \
--train-label-format dota \
--valid-image-folder /satellitepy/data/small_1024/val/images/ \
--valid-label-folder  /satellitepy/data/small_1024/val/annfiles/ \
--valid-label-format dota \
--tasks coarse-class obboxes \
--input-h 1024 \
--input-w 1024 \
--out-folder /mount_dir/training/ \
--num-epoch 1

But I get the following error:

Traceback (most recent call last):
  File "tools/train/bbavector.py", line 162, in <module>
    train_bbavector(args)
  File "tools/train/bbavector.py", line 157, in train_bbavector
    ctrbox_obj.train_network()
  File "/satellitepy/satellitepy/models/bbavector/train_model.py", line 105, in train_network
    train_loss = self.run_train(
  File "/satellitepy/satellitepy/models/bbavector/train_model.py", line 153, in run_train
    loss_dict = criterion(pr_decs, data_dict, self.target_task)
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/satellitepy/satellitepy/models/bbavector/loss.py", line 187, in forward
    loss_dict[task] = loss_fnc(
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/satellitepy/satellitepy/models/bbavector/loss.py", line 112, in forward
    if len(nan_mask.shape) > 2:
AttributeError: 'bool' object has no attribute 'shape'

My training data consists of png image patches the size 1024x1024 and corresponding dota annotation files that look like this, e.g.

626 273 629 270 618 260 615 263 coarse-class 0

So what am I missing here, how can I train a model with the task "obboxes" ? Furthermore, regarding the tasks, whats the difference between the parameters "--tasks" and "--target-task"

All the best and thx for any replies

LNdr28 commented 5 months ago

The model has to be trained with bounding boxes, it was an oversight from our side to allow training without. I don't expect the model that was trained without bboxes to work for testing. The issue that training is not working was the result of a recent code cleanup, it should be working now. The command you used should be working as is, because coarse-class is the default value for target-task. To specify it explicitly, use:

python3 tools/train/bbavector.py \
--train-image-folder /satellitepy/data/small_1024/train/images/  \
--train-label-folder /satellitepy/data/small_1024/train/annfiles/ \
--train-label-format dota \
--valid-image-folder /satellitepy/data/small_1024/val/images/ \
--valid-label-folder  /satellitepy/data/small_1024/val/annfiles/ \
--valid-label-format dota \
--tasks coarse-class obboxes \
--target-task coarse-class \
--input-h 1024 \
--input-w 1024 \
--out-folder /mount_dir/training/ \
--num-epoch 1

I will be done with such fixes for all functions soon, but right now there are a few things that are not working.

The difference between tasks and target-tasks is only important when training with multiple tasks. target-task is used to detect instances and determine bbox parameters, for the remaining tasks we make predictions for those detected objects. It is also used for thresholding based on confidence scores and non maximum suppression. 'target-task' always has to be a classification task.

bobbender commented 5 months ago

Hey thanks for the example call and the code improvements it the training works now.