这里是keypoint的label的处理和损失计算
1.Matcher
返回matches (Tensor[int64]):
其中N[i]在gt中[0,M-1]中有匹配返回N张量,,或者预测i不能匹配,返回负值。可以根据是张量还是负值简单判断是否匹配
该类为每个预测的“element”(例如,框)分配ground-truth元素。 每个预测元素将具有正好零或一个匹配;
每个ground-truth元素可以被分配给零个或多个预测元素。
匹配基于MxN match_quality_matrix,其表征每个(ground-truth, predicted)对的匹配程度。
例如,如果元素是框,则矩阵可以包含框IoU重叠值。
匹配器返回大小为N的张量,其包含与预测n匹配的ground-truth元素m的索引。 如果没有匹配项,则返回负值。
2.BalancedPositiveNegativeSampler 用于平衡正负样本比例的 一般是1:3,这里是1:4
记录下参数:
1)MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 512 是每张图片选择的element数
RoI minibatch size *per image* (number of regions of interest [ROIs])
每个训练的minibatch的总RoIs=TRAIN.BATCH_SIZE_PER_IM * TRAIN.IMS_PER_BATCH
TRAIN.IMS_PER_BATCH=2 #E.g., 1 gpu 512*2*1=1024
TRAIN.IMS_PER_BATCH=images per batch*GPU一般为 IMS_PER_BATCH=2*num_gpu
那其实TRAIN.IMS_PER_BATCH是设定的(一般一个gpu对应2),gpu是已知的,来但反向计算images per batch=2
2)MODEL.ROI_HEADS.POSITIVE_FRACTION = 0.25 每个batch中正样本/elements的比例
RoI minibatch的目标分数高于0.25标记为前景(即class> 0)
3.loss_evaluator = KeypointRCNNLossComputation(matcher, fg_bg_sampler, resolution)
import torch
from torch.nn import functional as F
from maskrcnn_benchmark.modeling.matcher import Matcher
from maskrcnn_benchmark.modeling.balanced_positive_negative_sampler import (
BalancedPositiveNegativeSampler,
)
from maskrcnn_benchmark.structures.boxlist_ops import boxlist_iou
from maskrcnn_benchmark.modeling.utils import cat
from maskrcnn_benchmark.layers import smooth_l1_loss
from maskrcnn_benchmark.structures.boxlist_ops import cat_boxlist
from maskrcnn_benchmark.structures.keypoint import keypoints_to_heat_map
def project_keypoints_to_heatmap(keypoints, proposals, discretization_size):
proposals = p