This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate C-IL for instance segmentation (#19)
* Update CHANGELOG.md Signed-off-by: Songki Choi <[email protected]> * Add instance-segmentation features EfficientnetB2B_maskrcnn * add cross_focal_loss * add resnet50 ins-seg * add grad_clip for instance seg Co-authored-by: Songki Choi <[email protected]>
- Loading branch information
1 parent
21d3a6c
commit c2e826f
Showing
16 changed files
with
755 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Copyright (C) 2022 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
import functools | ||
from mmdet.models.builder import DETECTORS | ||
from mmdet.models.detectors.mask_rcnn import MaskRCNN | ||
from .sam_detector_mixin import SAMDetectorMixin | ||
from .l2sp_detector_mixin import L2SPDetectorMixin | ||
from mpa.modules.utils.task_adapt import map_class_names | ||
from mpa.utils.logger import get_logger | ||
|
||
logger = get_logger() | ||
|
||
|
||
@DETECTORS.register_module() | ||
class CustomMaskRCNN(SAMDetectorMixin, L2SPDetectorMixin, MaskRCNN): | ||
def __init__(self, *args, task_adapt=None, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
# Hook for class-sensitive weight loading | ||
if task_adapt: | ||
self._register_load_state_dict_pre_hook( | ||
functools.partial( | ||
self.load_state_dict_pre_hook, | ||
self, # model | ||
task_adapt['dst_classes'], # model_classes | ||
task_adapt['src_classes'] # chkpt_classes | ||
) | ||
) | ||
|
||
@staticmethod | ||
def load_state_dict_pre_hook(model, model_classes, chkpt_classes, chkpt_dict, prefix, *args, **kwargs): | ||
"""Modify input state_dict according to class name matching before weight loading | ||
""" | ||
logger.info(f'----------------- CustomMaskRCNN.load_state_dict_pre_hook() called w/ prefix: {prefix}') | ||
|
||
# Dst to src mapping index | ||
model_dict = model.state_dict() | ||
model_classes = list(model_classes) | ||
chkpt_classes = list(chkpt_classes) | ||
model2chkpt = map_class_names(model_classes, chkpt_classes) | ||
logger.info(f'{chkpt_classes} -> {model_classes} ({model2chkpt})') | ||
|
||
# List of class-relevant params & their row-stride | ||
param_strides = { | ||
'roi_head.bbox_head.fc_cls.weight': 1, | ||
'roi_head.bbox_head.fc_cls.bias': 1, | ||
'roi_head.bbox_head.fc_reg.weight': 4, # 4 rows (bbox) for each class | ||
'roi_head.bbox_head.fc_reg.bias': 4, | ||
} | ||
|
||
for model_name, stride in param_strides.items(): | ||
chkpt_name = prefix + model_name | ||
if model_name not in model_dict or chkpt_name not in chkpt_dict: | ||
logger.info(f'Skipping weight copy: {chkpt_name}') | ||
continue | ||
|
||
# Mix weights | ||
model_param = model_dict[model_name].clone() | ||
chkpt_param = chkpt_dict[chkpt_name] | ||
for m, c in enumerate(model2chkpt): | ||
if c >= 0: | ||
# Copying only matched weight rows | ||
model_param[(m) * stride:(m + 1) * stride].copy_( | ||
chkpt_param[(c) * stride:(c + 1) * stride]) | ||
if model_param.shape[0] > len(model_classes * stride): # BG class | ||
c = len(chkpt_classes) | ||
m = len(model_classes) | ||
model_param[(m) * stride:(m + 1) * stride].copy_( | ||
chkpt_param[(c) * stride:(c + 1) * stride]) | ||
|
||
# Replace checkpoint weight by mixed weights | ||
chkpt_dict[chkpt_name] = model_param |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.