torch.meshgrid()函数解析

torch.meshgrid()函数解析:https://blog.csdn.net/weixin_39504171/article/details/106356977


def _create_grid_offsets(size, stride, offset, device):
	'''
	Argument:
	size:turple,value=(h,w),特征图大小
	stride:步伐,即原图缩小了stride倍,变成了(h,w)大小
	offset:偏移值,BorderDet的offset=0
	device:cuda() or cpu
	'''
    grid_height, grid_width = size
    shifts_start = offset * stride
    shifts_x = torch.arange(
        shifts_start, grid_width * stride + shifts_start, step=stride,
        dtype=torch.float32, device=device
    )
    shifts_y = torch.arange(
        shifts_start, grid_height * stride + shifts_start, step=stride,
        dtype=torch.float32, device=device
    )
    shift_y, shift_x = torch.meshgrid(shifts_y, shifts_x)
    shift_x = shift_x.reshape(-1)
    shift_y = shift_y.reshape(-1)
    return shift_x, shift_y
 def grid_shifts(self, grid_sizes, device):
        shifts_over_all_feature_maps = []
        for size, stride in zip(grid_sizes, self.strides):
            shift_x, shift_y = _create_grid_offsets(size, stride, self.offset, device)
            shifts = torch.stack((shift_x, shift_y), dim=1)#(h*w,2)

            shifts_over_all_feature_maps.append(shifts)

        return shifts_over_all_feature_maps#list(feature_num=特征图数量):每个元素是(h*w,2)

你可能感兴趣的:(pytorch,目标检测,python)