|
| 1 | +import warnings |
| 2 | +from transformers import DeformableDetrForObjectDetection, DeformableDetrImageProcessor, logging |
| 3 | +from deepforest.model import Model |
| 4 | +from torch import nn |
| 5 | + |
| 6 | +# Suppress huge amounts of unnecessary warnings from transformers. |
| 7 | +logging.set_verbosity_error() |
| 8 | + |
| 9 | + |
| 10 | +class DeformableDetrWrapper(nn.Module): |
| 11 | + """This class wraps a transformers DeformableDetrForObjectDetection model |
| 12 | + so that input pre- and post-processing happens transparently.""" |
| 13 | + |
| 14 | + def __init__(self, config, name, revision): |
| 15 | + """Initialize a DeformableDetrForObjectDetection model. |
| 16 | +
|
| 17 | + We assume that the provided name applies to both model and |
| 18 | + processor. By default this function creates a model with MS-COCO |
| 19 | + initialized weights, but can be overridden if needed. |
| 20 | + """ |
| 21 | + super().__init__() |
| 22 | + self.config = config |
| 23 | + |
| 24 | + # This suppresses a bunch of messages which are specific to DETR, |
| 25 | + # but do not impact model function. |
| 26 | + with warnings.catch_warnings(): |
| 27 | + warnings.simplefilter("ignore", category=UserWarning) |
| 28 | + |
| 29 | + self.net = DeformableDetrForObjectDetection.from_pretrained( |
| 30 | + name, |
| 31 | + revision=revision, |
| 32 | + num_labels=self.config.num_classes, |
| 33 | + ignore_mismatched_sizes=True) |
| 34 | + self.processor = DeformableDetrImageProcessor.from_pretrained( |
| 35 | + name, revision=revision) |
| 36 | + |
| 37 | + def _prepare_targets(self, targets): |
| 38 | + |
| 39 | + if not isinstance(targets, list): |
| 40 | + targets = [targets] |
| 41 | + |
| 42 | + coco_targets = [] |
| 43 | + |
| 44 | + for target in targets: |
| 45 | + coco_targets.append({ |
| 46 | + "image_id": |
| 47 | + 0, |
| 48 | + "annotations": [{ |
| 49 | + "id": i, |
| 50 | + "image_id": i, |
| 51 | + "category_id": label, |
| 52 | + "bbox": box.tolist(), |
| 53 | + "area": (box[3] - box[1]) * (box[2] - box[0]), |
| 54 | + "iscrowd": 0, |
| 55 | + } for i, (label, box) in enumerate(zip(target["labels"], target["boxes"])) |
| 56 | + ] |
| 57 | + }) |
| 58 | + |
| 59 | + return coco_targets |
| 60 | + |
| 61 | + def forward(self, images, targets=None, prepare_targets=True): |
| 62 | + """AutoModelForObjectDetection forward pass. If targets are provided |
| 63 | + the function returns a loss dictionary, otherwise it returns processed |
| 64 | + predictions. For details, see the transformers documentation for |
| 65 | + "post_process_object_detection". |
| 66 | +
|
| 67 | + Returns: |
| 68 | + predictions: list of dictionaries with "score", "boxes" and "labels", or |
| 69 | + a loss dict for training. |
| 70 | + """ |
| 71 | + |
| 72 | + if targets and prepare_targets: |
| 73 | + targets = self._prepare_targets(targets) |
| 74 | + |
| 75 | + encoded_inputs = self.processor.preprocess(images=images, |
| 76 | + annotations=targets, |
| 77 | + return_tensors="pt", |
| 78 | + do_rescale=False) |
| 79 | + |
| 80 | + preds = self.net(**encoded_inputs) |
| 81 | + |
| 82 | + if targets is None: |
| 83 | + return self.processor.post_process_object_detection( |
| 84 | + preds, |
| 85 | + threshold=self.config.score_thresh, |
| 86 | + target_sizes=[i.shape[-2:] for i in images] |
| 87 | + if isinstance(images, list) else [images.shape[-2:]]) |
| 88 | + else: |
| 89 | + return preds.loss_dict |
| 90 | + |
| 91 | + |
| 92 | +class Model(Model): |
| 93 | + |
| 94 | + def __init__(self, config, **kwargs): |
| 95 | + """ |
| 96 | + Args: |
| 97 | + """ |
| 98 | + super().__init__(config) |
| 99 | + |
| 100 | + def create_model(self, name="SenseTime/deformable-detr", revision="main"): |
| 101 | + """Create a Deformable DETR model from pretrained weights. |
| 102 | +
|
| 103 | + The number of classes set via config and will override the |
| 104 | + downloaded checkpoint. The default weights will load a model |
| 105 | + trained on MS-COCO that should fine-tune well on other tasks. |
| 106 | + """ |
| 107 | + return DeformableDetrWrapper(self.config, name, revision) |
0 commit comments