Team Ai
Apppublic

Arulkumar03/Wheat_HEAD_Detection_Counting_ComputerVision_Model

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
write-models.md91 linesDownload Raw Back to tutorials
1# Write Models2 3If you are trying to do something completely new, you may wish to implement4a model entirely from scratch. However, in many situations you may5be interested in modifying or extending some components of an existing model.6Therefore, we also provide mechanisms that let users override the7behavior of certain internal components of standard models.8 9 10## Register New Components11 12For common concepts that users often want to customize, such as "backbone feature extractor", "box head",13we provide a registration mechanism for users to inject custom implementation that14will be immediately available to use in config files.15 16For example, to add a new backbone, import this code in your code:17```python18from detectron2.modeling import BACKBONE_REGISTRY, Backbone, ShapeSpec19 20@BACKBONE_REGISTRY.register()21class ToyBackbone(Backbone):22  def __init__(self, cfg, input_shape):23    super().__init__()24    # create your own backbone25    self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=16, padding=3)26 27  def forward(self, image):28    return {"conv1": self.conv1(image)}29 30  def output_shape(self):31    return {"conv1": ShapeSpec(channels=64, stride=16)}32```33 34In this code, we implement a new backbone following the interface of the35[Backbone](../modules/modeling.html#detectron2.modeling.Backbone) class,36and register it into the [BACKBONE_REGISTRY](../modules/modeling.html#detectron2.modeling.BACKBONE_REGISTRY)37which requires subclasses of `Backbone`.38After importing this code, detectron2 can link the name of the class to its implementation. Therefore you can write the following code:39 40```python41cfg = ...   # read a config42cfg.MODEL.BACKBONE.NAME = 'ToyBackbone'   # or set it in the config file43model = build_model(cfg)  # it will find `ToyBackbone` defined above44```45 46As another example, to add new abilities to the ROI heads in the Generalized R-CNN meta-architecture,47you can implement a new48[ROIHeads](../modules/modeling.html#detectron2.modeling.ROIHeads) subclass and put it in the `ROI_HEADS_REGISTRY`.49[DensePose](../../projects/DensePose)50and [MeshRCNN](https://github.com/facebookresearch/meshrcnn)51are two examples that implement new ROIHeads to perform new tasks.52And [projects/](../../projects/)53contains more examples that implement different architectures.54 55A complete list of registries can be found in [API documentation](../modules/modeling.html#model-registries).56You can register components in these registries to customize different parts of a model, or the57entire model.58 59## Construct Models with Explicit Arguments60 61Registry is a bridge to connect names in config files to the actual code.62They are meant to cover a few main components that users frequently need to replace.63However, the capability of a text-based config file is sometimes limited and64some deeper customization may be available only through writing code.65 66Most model components in detectron2 have a clear `__init__` interface that documents67what input arguments it needs. Calling them with custom arguments will give you a custom variant68of the model.69 70As an example, to use __custom loss function__ in the box head of a Faster R-CNN, we can do the following:71 721. Losses are currently computed in [FastRCNNOutputLayers](../modules/modeling.html#detectron2.modeling.FastRCNNOutputLayers).73   We need to implement a variant or a subclass of it, with custom loss functions, named  `MyRCNNOutput`.742. Call `StandardROIHeads` with `box_predictor=MyRCNNOutput()` argument instead of the builtin `FastRCNNOutputLayers`.75   If all other arguments should stay unchanged, this can be easily achieved by using the [configurable `__init__`](../modules/config.html#detectron2.config.configurable) mechanism:76 77   ```python78   roi_heads = StandardROIHeads(79     cfg, backbone.output_shape(),80     box_predictor=MyRCNNOutput(...)81   )82   ```833. (optional) If we want to enable this new model from a config file, registration is needed:84   ```python85   @ROI_HEADS_REGISTRY.register()86   class MyStandardROIHeads(StandardROIHeads):87     def __init__(self, cfg, input_shape):88       super().__init__(cfg, input_shape,89                        box_predictor=MyRCNNOutput(...))90   ```91