예제 #1
0
 def convert_norm_to_float(net):
     '''
     BatchNorm layers to have parameters in single precision.
     Find all layers and convert them back to float. This can't
     be done with built in .apply as that function will apply
     fn to all modules, parameters, and buffers. Thus we wouldn't
     be able to guard the float conversion based on the module type.
     '''
     if isinstance(net, torch.nn.modules.batchnorm._BatchNorm):
         net.float()
     for child in net.children():
         VoxelNet.convert_norm_to_float(child)
     return net
예제 #2
0
def build(model_cfg: second_pb2.VoxelNet, voxel_generator,
          target_assigner) -> VoxelNet:
    """build second pytorch instance.
    """
    if not isinstance(model_cfg, second_pb2.VoxelNet):
        raise ValueError('model_cfg not of type ' 'second_pb2.VoxelNet.')
    vfe_num_filters = list(model_cfg.voxel_feature_extractor.num_filters)
    vfe_with_distance = model_cfg.voxel_feature_extractor.with_distance
    grid_size = voxel_generator.grid_size
    dense_shape = [1] + grid_size[::-1].tolist() + [vfe_num_filters[-1]]
    num_class = model_cfg.num_class

    num_input_features = model_cfg.num_point_features
    if model_cfg.without_reflectivity:
        num_input_features = 3
    loss_norm_type_dict = {
        0: LossNormType.NormByNumExamples,
        1: LossNormType.NormByNumPositives,
        2: LossNormType.NormByNumPosNeg,
    }
    loss_norm_type = loss_norm_type_dict[model_cfg.loss_norm_type]

    losses = losses_builder.build(model_cfg.loss)
    encode_rad_error_by_sin = model_cfg.encode_rad_error_by_sin
    cls_loss_ftor, loc_loss_ftor, cls_weight, loc_weight, _ = losses
    pos_cls_weight = model_cfg.pos_class_weight
    neg_cls_weight = model_cfg.neg_class_weight
    direction_loss_weight = model_cfg.direction_loss_weight

    net = VoxelNet(
        dense_shape,
        num_class=num_class,
        vfe_class_name=model_cfg.voxel_feature_extractor.module_class_name,
        vfe_num_filters=vfe_num_filters,
        middle_class_name=model_cfg.middle_feature_extractor.module_class_name,
        middle_num_filters_d1=list(
            model_cfg.middle_feature_extractor.num_filters_down1),
        middle_num_filters_d2=list(
            model_cfg.middle_feature_extractor.num_filters_down2),
        rpn_class_name=model_cfg.rpn.module_class_name,
        rpn_layer_nums=list(model_cfg.rpn.layer_nums),
        rpn_layer_strides=list(model_cfg.rpn.layer_strides),
        rpn_num_filters=list(model_cfg.rpn.num_filters),
        rpn_upsample_strides=list(model_cfg.rpn.upsample_strides),
        rpn_num_upsample_filters=list(model_cfg.rpn.num_upsample_filters),
        use_norm=True,
        use_rotate_nms=model_cfg.use_rotate_nms,
        multiclass_nms=model_cfg.use_multi_class_nms,
        nms_score_threshold=model_cfg.nms_score_threshold,
        nms_pre_max_size=model_cfg.nms_pre_max_size,
        nms_post_max_size=model_cfg.nms_post_max_size,
        nms_iou_threshold=model_cfg.nms_iou_threshold,
        use_sigmoid_score=model_cfg.use_sigmoid_score,
        encode_background_as_zeros=model_cfg.encode_background_as_zeros,
        use_direction_classifier=model_cfg.use_direction_classifier,
        use_bev=model_cfg.use_bev,
        num_input_features=num_input_features,
        num_groups=model_cfg.rpn.num_groups,
        use_groupnorm=model_cfg.rpn.use_groupnorm,
        with_distance=vfe_with_distance,
        cls_loss_weight=cls_weight,
        loc_loss_weight=loc_weight,
        pos_cls_weight=pos_cls_weight,
        neg_cls_weight=neg_cls_weight,
        direction_loss_weight=direction_loss_weight,
        loss_norm_type=loss_norm_type,
        encode_rad_error_by_sin=encode_rad_error_by_sin,
        loc_loss_ftor=loc_loss_ftor,
        cls_loss_ftor=cls_loss_ftor,
        target_assigner=target_assigner,
        voxel_size=voxel_generator.voxel_size,
        pc_range=voxel_generator.point_cloud_range)
    return net
예제 #3
0
def build(model_cfg: second_pb2.VoxelNet, fv_generator, #voxel_generator,
          save_path=None, RGB_embedding=False) -> VoxelNet:
    """build second pytorch instance.
    """
    if not isinstance(model_cfg, second_pb2.VoxelNet):
        raise ValueError('model_cfg not of type ' 'second_pb2.VoxelNet.')
    vfe_num_filters = list(model_cfg.voxel_feature_extractor.num_filters)
    vfe_with_distance = model_cfg.voxel_feature_extractor.with_distance
    # grid_size = voxel_generator.grid_size
    fv_dim = fv_generator.fv_dim
    dense_shape = [1] + fv_dim[::-1].tolist() + [vfe_num_filters[-1]]
    num_class = model_cfg.num_class

    num_input_features = model_cfg.num_point_features
    if model_cfg.without_reflectivity:
        num_input_features = 3
    loss_norm_type_dict = {
        0: LossNormType.NormByNumExamples,
        1: LossNormType.NormByNumPositives,
        2: LossNormType.NormByNumPosNeg,
    }
    loss_norm_type = loss_norm_type_dict[model_cfg.loss_norm_type]

    losses = losses_builder.build(model_cfg.loss)
    encode_rad_error_by_sin = model_cfg.encode_rad_error_by_sin
    cls_loss_ftor, loc_loss_ftor, cls_weight, loc_weight, _ = losses
    pos_cls_weight = model_cfg.pos_class_weight
    neg_cls_weight = model_cfg.neg_class_weight
    direction_loss_weight = model_cfg.direction_loss_weight

    net = VoxelNet(
        dense_shape,
        num_class=num_class,
        # vfe_class_name=model_cfg.voxel_feature_extractor.module_class_name,
        # vfe_num_filters=vfe_num_filters,
        # middle_class_name=model_cfg.middle_feature_extractor.module_class_name,
        # middle_num_filters_d1=list(
        #     model_cfg.middle_feature_extractor.num_filters_down1),
        # middle_num_filters_d2=list(
        #     model_cfg.middle_feature_extractor.num_filters_down2),
        # rpn_class_name=model_cfg.rpn.module_class_name,
        # rpn_layer_nums=list(model_cfg.rpn.layer_nums),
        # rpn_layer_strides=list(model_cfg.rpn.layer_strides),
        # rpn_num_filters=list(model_cfg.rpn.num_filters),
        # rpn_upsample_strides=list(model_cfg.rpn.upsample_strides),
        # rpn_num_upsample_filters=list(model_cfg.rpn.num_upsample_filters),
        # use_norm=True,
        # use_rotate_nms=model_cfg.use_rotate_nms,
        # multiclass_nms=model_cfg.use_multi_class_nms,
        # nms_score_threshold=model_cfg.nms_score_threshold,
        # nms_pre_max_size=model_cfg.nms_pre_max_size,
        # nms_post_max_size=model_cfg.nms_post_max_size,
        # nms_iou_threshold=model_cfg.nms_iou_threshold,
        use_sigmoid_score=model_cfg.use_sigmoid_score,
        encode_background_as_zeros=model_cfg.encode_background_as_zeros,
        # use_direction_classifier=model_cfg.use_direction_classifier,
        # use_bev=model_cfg.use_bev,
        # num_input_features=num_input_features,
        # num_groups=model_cfg.rpn.num_groups,
        # use_groupnorm=model_cfg.rpn.use_groupnorm,
        # with_distance=vfe_with_distance,
        # cls_loss_weight=cls_weight,
        # loc_loss_weight=loc_weight,
        # pos_cls_weight=pos_cls_weight,
        # neg_cls_weight=neg_cls_weight,
        # direction_loss_weight=direction_loss_weight,
        # loss_norm_type=loss_norm_type,
        # encode_rad_error_by_sin=encode_rad_error_by_sin,
        # loc_loss_ftor=loc_loss_ftor,
        # cls_loss_ftor=cls_loss_ftor,
        # voxel_size=voxel_generator.voxel_size,
        # pc_range=voxel_generator.point_cloud_range,
        # grid_size=fv_generator.grid_size,
        # cartesian_coord_range=fv_generator.cartesian_coord_range,
        save_path=save_path,
        RGB_embedding=RGB_embedding
    )
    return net
예제 #4
0
def build(model_cfg: second_pb2.VoxelNet,
          voxel_generator,
          target_assigner,
          measure_time=False) -> VoxelNet:
    """build second pytorch instance.
    """
    if not isinstance(model_cfg, second_pb2.VoxelNet):
        raise ValueError('model_cfg not of type ' 'second_pb2.VoxelNet.')
    vfe_num_filters = list(model_cfg.voxel_feature_extractor.num_filters)
    vfe_with_distance = model_cfg.voxel_feature_extractor.with_distance
    grid_size = voxel_generator.grid_size
    dense_shape = [1] + grid_size[::-1].tolist() + [vfe_num_filters[-1]]
    num_class = len(target_assigner.classes)
    class_cfg = model_cfg.target_assigner
    use_iou_param_partaa = class_cfg.use_iou_param_partaa

    num_input_features = model_cfg.num_point_features
    if model_cfg.without_reflectivity:
        num_input_features = 3
    loss_norm_type_dict = {
        0: LossNormType.NormByNumExamples,
        1: LossNormType.NormByNumPositives,
        2: LossNormType.NormByNumPosNeg,
    }
    loss_norm_type = loss_norm_type_dict[model_cfg.loss_norm_type]

    losses = losses_builder.build(model_cfg.loss)
    encode_rad_error_by_sin = model_cfg.encode_rad_error_by_sin
    cls_loss_ftor, loc_loss_ftor, iou_loss_ftor, \
        cls_weight, loc_weight, iou_weight, _ = losses
    pos_cls_weight = model_cfg.pos_class_weight
    neg_cls_weight = model_cfg.neg_class_weight
    direction_loss_weight = model_cfg.direction_loss_weight

    # if model_cfg.use_rpn:
    net = VoxelNet(
        dense_shape,
        num_class=num_class,
        vfe_class_name=model_cfg.voxel_feature_extractor.module_class_name,
        vfe_num_filters=vfe_num_filters,
        middle_class_name=model_cfg.middle_feature_extractor.module_class_name,
        middle_num_input_features=model_cfg.middle_feature_extractor.
        num_input_features,
        middle_num_filters_d1=list(
            model_cfg.middle_feature_extractor.num_filters_down1),
        middle_num_filters_d2=list(
            model_cfg.middle_feature_extractor.num_filters_down2),
        rpn_class_name=model_cfg.rpn.module_class_name,
        rpn_num_input_features=model_cfg.rpn.num_input_features,
        rpn_layer_nums=list(model_cfg.rpn.layer_nums),
        rpn_layer_strides=list(model_cfg.rpn.layer_strides),
        rpn_num_filters=list(model_cfg.rpn.num_filters),
        rpn_upsample_strides=list(model_cfg.rpn.upsample_strides),
        rpn_num_upsample_filters=list(model_cfg.rpn.num_upsample_filters),
        use_norm=True,
        use_voxel_classifier=model_cfg.use_aux_classifier,
        use_rotate_nms=model_cfg.use_rotate_nms,
        multiclass_nms=model_cfg.use_multi_class_nms,
        nms_score_threshold=model_cfg.nms_score_threshold,
        nms_pre_max_size=model_cfg.nms_pre_max_size,
        nms_post_max_size=model_cfg.nms_post_max_size,
        nms_iou_threshold=model_cfg.nms_iou_threshold,
        use_sigmoid_score=model_cfg.use_sigmoid_score,
        use_sparse_rpn=False,
        encode_background_as_zeros=model_cfg.encode_background_as_zeros,
        use_direction_classifier=model_cfg.use_direction_classifier,
        use_bev=model_cfg.use_bev,
        num_input_features=num_input_features,
        num_groups=model_cfg.rpn.num_groups,
        use_groupnorm=model_cfg.rpn.use_groupnorm,
        with_distance=vfe_with_distance,
        cls_loss_weight=cls_weight,
        loc_loss_weight=loc_weight,
        pos_cls_weight=pos_cls_weight,
        neg_cls_weight=neg_cls_weight,
        direction_loss_weight=direction_loss_weight,
        loss_norm_type=loss_norm_type,
        encode_rad_error_by_sin=encode_rad_error_by_sin,
        loc_loss_ftor=loc_loss_ftor,
        cls_loss_ftor=cls_loss_ftor,
        target_assigner=target_assigner,
        measure_time=measure_time,
        use_iou_branch=model_cfg.use_iou_branch,
        iou_dict=model_cfg.iou,
        use_iou_param_partaa=use_iou_param_partaa,
        iou_loss_weight=iou_weight,
        iou_loss_ftor=iou_loss_ftor)
    # elif model_cfg.use_rcnn:
    # net = VoxelNet_Second(
    #     dense_shape,
    #     num_class=num_class,
    #     vfe_class_name=model_cfg.voxel_feature_extractor.module_class_name,
    #     vfe_num_filters=vfe_num_filters,
    #     middle_class_name=model_cfg.middle_feature_extractor.module_class_name,
    #     middle_num_input_features=model_cfg.middle_feature_extractor.num_input_features,
    #     middle_num_filters_d1=list(
    #         model_cfg.middle_feature_extractor.num_filters_down1),
    #     middle_num_filters_d2=list(
    #         model_cfg.middle_feature_extractor.num_filters_down2),
    #     rpn_class_name=model_cfg.rpn.module_class_name,
    #     rpn_num_input_features=model_cfg.rpn.num_input_features,
    #     rpn_layer_nums=list(model_cfg.rpn.layer_nums),
    #     rpn_layer_strides=list(model_cfg.rpn.layer_strides),
    #     rpn_num_filters=list(model_cfg.rpn.num_filters),
    #     rpn_upsample_strides=list(model_cfg.rpn.upsample_strides),
    #     rpn_num_upsample_filters=list(model_cfg.rpn.num_upsample_filters),
    #     use_norm=True,
    #     use_voxel_classifier=model_cfg.use_aux_classifier,
    #     use_rotate_nms=model_cfg.use_rotate_nms,
    #     multiclass_nms=model_cfg.use_multi_class_nms,
    #     nms_score_threshold=model_cfg.nms_score_threshold,
    #     nms_pre_max_size=model_cfg.nms_pre_max_size,
    #     nms_post_max_size=model_cfg.nms_post_max_size,
    #     nms_iou_threshold=model_cfg.nms_iou_threshold,
    #     use_sigmoid_score=model_cfg.use_sigmoid_score,
    #     use_sparse_rpn=False,
    #     encode_background_as_zeros=model_cfg.encode_background_as_zeros,
    #     use_direction_classifier=model_cfg.use_direction_classifier,
    #     use_bev=model_cfg.use_bev,
    #     num_input_features=num_input_features,
    #     num_groups=model_cfg.rpn.num_groups,
    #     use_groupnorm=model_cfg.rpn.use_groupnorm,
    #     with_distance=vfe_with_distance,
    #     cls_loss_weight=cls_weight,
    #     loc_loss_weight=loc_weight,
    #     pos_cls_weight=pos_cls_weight,
    #     neg_cls_weight=neg_cls_weight,
    #     direction_loss_weight=direction_loss_weight,
    #     loss_norm_type=loss_norm_type,
    #     encode_rad_error_by_sin=encode_rad_error_by_sin,
    #     loc_loss_ftor=loc_loss_ftor,
    #     cls_loss_ftor=cls_loss_ftor,
    #     target_assigner=target_assigner,
    #     measure_time=measure_time,
    # )

    return net