ResizeShortestEdge augmentation makes boxes incorrect #3613
Answered
by
Shuntw6096
Shuntw6096
asked this question in
Q&A
-
Hi, I want to customize my data mapper with Albumentations, but when I trained faster rcnn with visualize_training I found GT annotation(box) are incorrect. And I checked box before and after ResizeShortestEdge, it seems that ResizeShortestEdge does not transform boxes, but I use default dataset mapper, the annotation on tensorboard (visualize_training) are correct, is there any function to solve this problem? class mymapper:
def __init__(self, cfg):
self.min_size = cfg.INPUT.MIN_SIZE_TRAIN
self.max_size = cfg.INPUT.MAX_SIZE_TRAIN
self.sample_style = cfg.INPUT.MIN_SIZE_TRAIN_SAMPLING
def __call__(self, dataset_dict):
from detectron2.structures import BoxMode
from pathlib import Path
dataset_dict = copy.deepcopy(dataset_dict) # it will be modified by code below
# can use other ways to read image
image = utils.read_image(dataset_dict["file_name"], format="BGR")
# See "Data Augmentation" tutorial for details usage
transform = A.Compose([
A.RandomScale(scale_limit=(-0.9, 1), p=1), #LargeScaleJitter from scale of 0.1 to 2
A.PadIfNeeded(512, 832, border_mode=0), #constant 0 border
A.RandomCrop(512, 832),
A.HorizontalFlip(p=0.5),
], bbox_params=A.BboxParams(format="pascal_voc", )
)
anns = [ann['bbox'] + [ann['category_id']] for ann in dataset_dict['annotations']]
tr_output = transform(image=image, bboxes=anns)
image = tr_output['image']
height, width, _ =image.shape
anns = tr_output['bboxes']
# visualize(image, [ann[:-1] for ann in anns], dataset_dict["file_name"])
anns = [{'category_id':ann[-1], 'bbox':ann[:-1], 'bbox_mode':BoxMode.XYXY_ABS} for ann in anns]
auginput = T.AugInput(image)
transform = T.ResizeShortestEdge(self.min_size, self.max_size, self.sample_style)(auginput)
image = torch.from_numpy(auginput.image.transpose(2, 0, 1))
annos = [
utils.transform_instance_annotations(annotation, [transform], image.shape[1:])
for annotation in dataset_dict.pop("annotations")
]
ins = utils.annotations_to_instances(annos, image.shape[1:])
from detectron2.utils.visualizer import Visualizer
img = utils.convert_image_to_rgb(image.permute(1, 2, 0), 'BGR')
v_gt = Visualizer(img, None)
v_gt = v_gt.overlay_instances(boxes=ins.gt_boxes)
v_gt.save(str(OUTPUT_DIR/'imgs'/Path(dataset_dict["file_name"]).name))
return {
"file_name":dataset_dict["file_name"],
"image_id": dataset_dict["image_id"],
"height": height,
"width": width,
"image": image,
"instances": utils.annotations_to_instances(annos, image.shape[1:])
}
class Trainer(DefaultTrainer):
"""
We use the "DefaultTrainer" which contains pre-defined default logic for
standard training workflow. They may not work for you, especially if you
are working on a new research project. In that case you can write your
own training loop. You can use "tools/plain_train_net.py" as an example.
"""
@classmethod
def build_evaluator(cls, cfg, dataset_name, output_folder=None):
return build_evaluator(cfg, dataset_name, output_folder)
@classmethod
def test_with_TTA(cls, cfg, model):
logger = logging.getLogger("detectron2.trainer")
# In the end of training, run an evaluation with TTA
# Only support some R-CNN models.
logger.info("Running inference with test-time augmentation ...")
model = GeneralizedRCNNWithTTA(cfg, model)
evaluators = [
cls.build_evaluator(
cfg, name, output_folder=os.path.join(cfg.OUTPUT_DIR, "inference_TTA")
)
for name in cfg.DATASETS.TEST
]
res = cls.test(cfg, model, evaluators)
res = OrderedDict({k + "_TTA": v for k, v in res.items()})
return res
@classmethod
def build_train_loader(cls, cfg):
"""
Returns:
iterable
It now calls :func:`detectron2.data.build_detection_train_loader`.
Overwrite it if you'd like a different data loader.
"""
return build_detection_train_loader(cfg, mapper=mymapper(cfg)) Thanks. |
Beta Was this translation helpful? Give feedback.
Answered by
Shuntw6096
Oct 23, 2021
Replies: 1 comment
-
annos = [
utils.transform_instance_annotations(annotation, [transform], image.shape[1:])
for annotation in dataset_dict.pop("annotations")
] to annos = [
utils.transform_instance_annotations(annotation, [transform], image.shape[1:])
for annotation in anns
] The code is copied from doc, I forget to check variable, and it works. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Shuntw6096
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to
The code is copied from doc, I forget to check variable, and it works.