(config_path, target_rate=None, update_freq=200, update_delta=0.01, num_tune_epoch=5)
get information of target assign to tune thresholds in anchor generator.
get information of target assign to tune thresholds in anchor generator.
get information of target assign to tune thresholds in anchor generator .
def helper_tune_target_assigner(config_path, target_rate=None, update_freq=200, update_delta=0.01, num_tune_epoch=5):
    """get information of target assign to tune thresholds in anchor generator.
    """    
    if isinstance(config_path, str):
        # directly provide a config object. this usually used
        # when you want to train with several different parameters in
        # one script.
        config = pipeline_pb2.TrainEvalPipelineConfig()
        with open(config_path, "r") as f:
            proto_str = f.read()
            text_format.Merge(proto_str, config)
    else:
        config = config_path
        proto_str = text_format.MessageToString(config, indent=2)

    input_cfg = config.train_input_reader
    eval_input_cfg = config.eval_input_reader
    model_cfg = config.model.second
    train_cfg = config.train_config

    net = build_network(model_cfg, False)
    # if train_cfg.enable_mixed_precision:
    #     net.half()
    #     net.metrics_to_float()
    #     net.convert_norm_to_float(net)
    target_assigner = net.target_assigner
    voxel_generator = net.voxel_generator
    dataset = input_reader_builder.build(
        input_cfg,
        model_cfg,
        training=True,
        voxel_generator=voxel_generator,
        target_assigner=target_assigner,
        multi_gpu=False)

    dataloader = torch.utils.data.DataLoader(
        dataset,
        batch_size=1,
        shuffle=False,
        num_workers=0,
        pin_memory=False,
        collate_fn=merge_second_batch,
        worker_init_fn=_worker_init_fn,
        drop_last=False)
    
    class_count = {}
    anchor_count = {}
    class_count_tune = {}
    anchor_count_tune = {}
    for c in target_assigner.classes:
        class_count[c] = 0
        anchor_count[c] = 0
        class_count_tune[c] = 0
        anchor_count_tune[c] = 0


    step = 0
    classes = target_assigner.classes
    if target_rate is None:
        num_tune_epoch = 0
    for epoch in range(num_tune_epoch):
        for example in dataloader:
            gt_names = example["gt_names"]
            for name in gt_names:
                class_count_tune[name] += 1
            
            labels = example['labels']
            for i in range(1, len(classes) + 1):
                anchor_count_tune[classes[i - 1]] += int(np.sum(labels == i))
            if target_rate is not None:
                for name, rate in target_rate.items():
                    if class_count_tune[name] > update_freq:
                        # calc rate
                        current_rate = anchor_count_tune[name] / class_count_tune[name]
                        if current_rate > rate:
                            target_assigner._anchor_generators[classes.index(name)].match_threshold += update_delta
                            target_assigner._anchor_generators[classes.index(name)].unmatch_threshold += update_delta
                        else:
                            target_assigner._anchor_generators[classes.index(name)].match_threshold -= update_delta
                            target_assigner._anchor_generators[classes.index(name)].unmatch_threshold -= update_delta
                        anchor_count_tune[name] = 0
                        class_count_tune[name] = 0
            step += 1
    for c in target_assigner.classes:
        class_count[c] = 0
        anchor_count[c] = 0
    total_voxel_gene_time = 0
    count = 0

    for example in dataloader:
        gt_names = example["gt_names"]
        total_voxel_gene_time += example["metrics"][0]["voxel_gene_time"]
        count += 1

        for name in gt_names:
            class_count[name] += 1
        
        labels = example['labels']
        for i in range(1, len(classes) + 1):
            anchor_count[classes[i - 1]] += int(np.sum(labels == i))
    print("avg voxel gene time", total_voxel_gene_time / count)

    print(json.dumps(class_count, indent=2))
    print(json.dumps(anchor_count, indent=2))
    if target_rate is not None:
        for ag in target_assigner._anchor_generators:
            if ag.class_name in target_rate:
                print(ag.class_name, ag.match_threshold, ag.unmatch_threshold)
def helper_tune_target_assigner ( config_path , target_rate = None , update_freq = 200 , update_delta = 0.01 , num_tune_epoch = 5 ) : if isinstance ( config_path , str ) : # directly provide a config object. this usually used # when you want to train with several different parameters in # one script. config = pipeline_pb2 . TrainEvalPipelineConfig ( ) with open ( config_path , "r" ) as f : proto_str = f . read ( ) text_format . Merge ( proto_str , config ) else : config = config_path proto_str = text_format . MessageToString ( config , indent = 2 ) input_cfg = config . train_input_reader eval_input_cfg = config . eval_input_reader model_cfg = config . model . second train_cfg = config . train_config net = build_network ( model_cfg , False ) # if train_cfg.enable_mixed_precision: #     net.half() #     net.metrics_to_float() #     net.convert_norm_to_float(net) target_assigner = net . target_assigner voxel_generator = net . voxel_generator dataset = input_reader_builder . build ( input_cfg , model_cfg , training = True , voxel_generator = voxel_generator , target_assigner = target_assigner , multi_gpu = False ) dataloader = torch . utils . data . DataLoader ( dataset , batch_size = 1 , shuffle = False , num_workers = 0 , pin_memory = False , collate_fn = merge_second_batch , worker_init_fn = _worker_init_fn , drop_last = False ) class_count = { } anchor_count = { } class_count_tune = { } anchor_count_tune = { } for c in target_assigner . classes : class_count [ c ] = 0 anchor_count [ c ] = 0 class_count_tune [ c ] = 0 anchor_count_tune [ c ] = 0 step = 0 classes = target_assigner . classes if target_rate is None : num_tune_epoch = 0 for epoch in range ( num_tune_epoch ) : for example in dataloader : gt_names = example [ "gt_names" ] for name in gt_names : class_count_tune [ name ] += 1 labels = example [ 'labels' ] for i in range ( 1 , len ( classes ) + 1 ) : anchor_count_tune [ classes [ i - 1 ] ] += int ( np . sum ( labels == i ) ) if target_rate is not None : for name , rate in target_rate . items ( ) : if class_count_tune [ name ] > update_freq : # calc rate current_rate = anchor_count_tune [ name ] / class_count_tune [ name ] if current_rate > rate : target_assigner . _anchor_generators [ classes . index ( name ) ] . match_threshold += update_delta target_assigner . _anchor_generators [ classes . index ( name ) ] . unmatch_threshold += update_delta else : target_assigner . _anchor_generators [ classes . index ( name ) ] . match_threshold -= update_delta target_assigner . _anchor_generators [ classes . index ( name ) ] . unmatch_threshold -= update_delta anchor_count_tune [ name ] = 0 class_count_tune [ name ] = 0 step += 1 for c in target_assigner . classes : class_count [ c ] = 0 anchor_count [ c ] = 0 total_voxel_gene_time = 0 count = 0 for example in dataloader : gt_names = example [ "gt_names" ] total_voxel_gene_time += example [ "metrics" ] [ 0 ] [ "voxel_gene_time" ] count += 1 for name in gt_names : class_count [ name ] += 1 labels = example [ 'labels' ] for i in range ( 1 , len ( classes ) + 1 ) : anchor_count [ classes [ i - 1 ] ] += int ( np . sum ( labels == i ) ) print ( "avg voxel gene time" , total_voxel_gene_time / count ) print ( json . dumps ( class_count , indent = 2 ) ) print ( json . dumps ( anchor_count , indent = 2 ) ) if target_rate is not None : for ag in target_assigner . _anchor_generators : if ag . class_name in target_rate : print ( ag . class_name , ag . match_threshold , ag . unmatch_threshold )