コード例 #1
0
def prepare_model():
    # input image channels: BGR--3, gray--1
    config_dict['num_input_channels'] = 3

    classifiation_loss = FocalLoss(use_sigmoid=True,
                                   gamma=2.0,
                                   alpha=0.25,
                                   reduction='mean',
                                   loss_weight=1.0)
    regression_loss = torch.nn.SmoothL1Loss(reduction='mean')

    # number of classes
    config_dict['num_classes'] = 1
    config_dict[
        'backbone_init_param_file_path'] = None  # if no pretrained weights, set to None
    lfd_backbone = LFDResNet(
        block_mode='fast',  # affect block type
        stem_mode='fast',  # affect stem type
        body_mode='fast',  # affect body architecture
        input_channels=config_dict['num_input_channels'],
        stem_channels=64,
        body_architecture=None,
        body_channels=None,
        out_indices=((0, 3), (1, 1), (2, 1), (3, 0), (4, 0)),
        frozen_stages=-1,
        activation_cfg=dict(type='ReLU', inplace=True),
        norm_cfg=dict(type='BatchNorm2d'),
        init_with_weight_file=config_dict['backbone_init_param_file_path'],
        norm_eval=False)

    lfd_neck = SimpleNeck(
        num_neck_channels=128,
        num_input_channels_list=lfd_backbone.num_output_channels_list,
        num_input_strides_list=lfd_backbone.num_output_strides_list,
        norm_cfg=dict(type='BatchNorm2d'),
        activation_cfg=dict(type='ReLU', inplace=True))

    lfd_head = LFDHead(
        num_classes=config_dict['num_classes'],
        num_heads=len(lfd_neck.num_output_strides_list),
        num_input_channels=128,
        num_head_channels=128,
        num_conv_layers=2,
        activation_cfg=dict(type='ReLU', inplace=True),
        norm_cfg=dict(type='BatchNorm2d'),
        share_head_flag=False,
        merge_path_flag=False,
        classification_loss_type=type(classifiation_loss).__name__)

    config_dict['model'] = LFD(
        backbone=lfd_backbone,
        neck=lfd_neck,
        head=lfd_head,
        num_classes=config_dict['num_classes'],
        regression_ranges=((0, 16), (16, 32), (32, 64), (64, 128), (128, 256)),
        gray_range_factors=(0.9, 1.1),
        point_strides=lfd_neck.num_output_strides_list,
        classification_loss_func=classifiation_loss,
        regression_loss_func=regression_loss,
        distance_to_bbox_mode='exp',
        classification_threshold=0.05,
        nms_threshold=0.5,
        pre_nms_bbox_limit=1000,
        post_nms_bbox_limit=100,
    )

    # init param weights file
    # when set, the executor will init the whole net using this file
    config_dict['weight_path'] = None

    # resume training path
    # when set, the 'weight_path' will be ignored. The executor will init the whole net and training parameters using this file
    config_dict['resume_path'] = None

    # evaluator
    # the evaluator should match the dataset
    config_dict['evaluator'] = None
コード例 #2
0
def prepare_model():
    # input image channels: BGR--3, gray--1
    config_dict['num_input_channels'] = 3

    classification_loss = CrossEntropyLoss(reduction='mean', loss_weight=1.0)

    regression_loss = IoULoss(eps=1e-6, reduction='mean', loss_weight=1.0)

    # number of classes
    config_dict['num_classes'] = 1
    config_dict[
        'backbone_init_param_file_path'] = None  # if no pretrained weights, set to None
    lfd_backbone = LFDResNet(
        block_mode='faster',  # affect block type
        stem_mode='faster',  # affect stem type
        body_mode=None,  # affect body architecture
        input_channels=config_dict['num_input_channels'],
        stem_channels=64,
        body_architecture=[4, 2, 2, 3],
        body_channels=[64, 64, 64, 128],
        out_indices=((0, 3), (1, 1), (2, 1), (3, 0), (3, 2)),
        frozen_stages=-1,
        activation_cfg=dict(type='ReLU', inplace=True),
        norm_cfg=dict(type='BatchNorm2d'),
        init_with_weight_file=config_dict['backbone_init_param_file_path'],
        norm_eval=False)

    lfd_neck = SimpleNeck(
        num_neck_channels=128,
        num_input_channels_list=lfd_backbone.num_output_channels_list,
        num_input_strides_list=lfd_backbone.num_output_strides_list,
        norm_cfg=dict(type='BatchNorm2d'),
        activation_cfg=dict(type='ReLU', inplace=True))

    lfd_head = LFDHead(
        num_classes=config_dict['num_classes'],
        num_heads=len(lfd_neck.num_output_strides_list),
        num_input_channels=128,
        num_head_channels=128,
        num_conv_layers=2,
        activation_cfg=dict(type='ReLU', inplace=True),
        norm_cfg=dict(type='GroupNorm', num_groups=16),
        share_head_flag=True,
        merge_path_flag=True,
        classification_loss_type=type(classification_loss).__name__,
        regression_loss_type=type(regression_loss).__name__)
    config_dict['detection_scales'] = ((4, 20), (20, 40), (40, 80), (80, 160),
                                       (160, 320))
    config_dict['model'] = LFD(
        backbone=lfd_backbone,
        neck=lfd_neck,
        head=lfd_head,
        num_classes=config_dict['num_classes'],
        regression_ranges=config_dict['detection_scales'],
        gray_range_factors=(0.9, 1.1),
        point_strides=lfd_neck.num_output_strides_list,
        classification_loss_func=classification_loss,
        regression_loss_func=regression_loss,
        distance_to_bbox_mode='sigmoid')

    # init param weights file
    # when set, the executor will init the whole net using this file
    config_dict['weight_path'] = None

    # resume training path
    # when set, the 'weight_path' will be ignored. The executor will init the whole net and training parameters using this file
    config_dict['resume_path'] = None

    # evaluator
    # the evaluator should match the dataset
    config_dict['evaluator'] = None
コード例 #3
0
def prepare_model():
    # input image channels: BGR--3, gray--1
    config_dict['num_input_channels'] = 3

    classification_loss = QualityFocalLoss(use_sigmoid=True,
                                           beta=2.0,
                                           reduction='mean',
                                           loss_weight=2.0)

    regression_loss = IoULoss(eps=1e-6, reduction='mean', loss_weight=1.0)

    # number of classes
    config_dict['num_classes'] = 1
    config_dict[
        'backbone_init_param_file_path'] = './pre-trained_backbones/lfd_resnet_TL_L_imagenet_20210707_234720/epoch_60.pth'
    lfd_backbone = LFDResNet(
        block_mode='faster',  # affect block type
        stem_mode='fast',  # affect stem type
        body_mode=None,  # affect body architecture
        input_channels=config_dict['num_input_channels'],
        stem_channels=64,
        body_architecture=[5, 3, 2, 2, 2],
        body_channels=[64, 64, 128, 128, 128],
        out_indices=((0, 4), (1, 2), (2, 1), (3, 1), (4, 1)),
        frozen_stages=-1,
        activation_cfg=dict(type='ReLU', inplace=True),
        norm_cfg=dict(type='BatchNorm2d'),
        init_with_weight_file=config_dict['backbone_init_param_file_path'],
        norm_eval=False)

    lfd_neck = SimpleNeck(
        num_neck_channels=128,
        num_input_channels_list=lfd_backbone.num_output_channels_list,
        num_input_strides_list=lfd_backbone.num_output_strides_list,
        norm_cfg=dict(type='BatchNorm2d'),
        activation_cfg=dict(type='ReLU', inplace=True))

    lfd_head = LFDHead(
        num_classes=config_dict['num_classes'],
        num_heads=len(lfd_neck.num_output_strides_list),
        num_input_channels=128,
        num_head_channels=128,
        num_conv_layers=2,
        activation_cfg=dict(type='ReLU', inplace=True),
        norm_cfg=None,  # dict(type='GroupNorm', num_groups=16),
        share_head_flag=True,
        merge_path_flag=True,
        classification_loss_type=type(classification_loss).__name__,
        regression_loss_type=type(regression_loss).__name__)
    config_dict['detection_scales'] = ((4, 32), (32, 64), (64, 128),
                                       (128, 256), (256, 512))
    config_dict['model'] = LFD(
        backbone=lfd_backbone,
        neck=lfd_neck,
        head=lfd_head,
        num_classes=config_dict['num_classes'],
        regression_ranges=config_dict['detection_scales'],
        gray_range_factors=(0.9, 1.1),
        range_assign_mode='dist',
        point_strides=lfd_neck.num_output_strides_list,
        classification_loss_func=classification_loss,
        regression_loss_func=regression_loss,
        distance_to_bbox_mode='sigmoid',
        enable_regression_weight=False,
        enable_classification_weight=False)

    # init param weights file
    # when set, the executor will init the whole net using this file
    config_dict['weight_path'] = None

    # resume training path
    # when set, the 'weight_path' will be ignored. The executor will init the whole net and training parameters using this file
    config_dict['resume_path'] = None