text
stringlengths
1
93.6k
yss_np = Yss[0].cpu().data.numpy()
gt_ref_map = yss_np # (B, 1, h, w)
# For every gt patch from the gt_ref_map
for b in range(0, gt_ref_map.shape[0]):
y_idx, x_idx = np.where(gt_ref_map[b][0] > 0)
num_heads = y_idx.shape[0]
if num_heads > 1:
distances = (x_idx - x_idx[np.newaxis, :].T) ** 2 + (y_idx - y_idx[np.newaxis, :].T) ** 2
min_distances = np.sqrt(np.partition(distances, 1, axis=1)[:, 1])
min_distances = np.minimum(min_distances, np.inf) ##? WHY INF???
box_inds = np.digitize(min_distances, BOX_SIZE_BINS_NPY, False)
box_inds = np.maximum(box_inds - 1, 0) # to make zero based indexing
elif num_heads == 1:
box_inds = np.array([BOX_SIZE_BINS_NPY.shape[0] - 1])
else:
box_inds = np.array([])
assert(np.all(box_inds < BOX_SIZE_BINS_NPY.shape[0]))
scale_inds = np.digitize(box_inds, SCALE_BINS_ON_BOX_SIZE_BINS, False)
# Assign the w_maps
check_sum = 0
for i, (yss, w_map) in enumerate(zip(Yss, Yss_out)):
scale_sel_inds = (scale_inds == i)
check_sum += np.sum(scale_sel_inds)
if scale_sel_inds.shape[0] > 0:
# find box index in the scale
sel_box_inds = box_inds[scale_sel_inds]
scale_box_inds = sel_box_inds % 3
heads_y = y_idx[scale_sel_inds] // PRED_DOWNSCALE_FACTORS[3-i]
heads_x = x_idx[scale_sel_inds] // PRED_DOWNSCALE_FACTORS[3-i]
Yss_out[i][b, scale_box_inds, heads_y, heads_x] = BOX_SIZE_BINS_NPY[sel_box_inds]
Yss_out[i][b, 3, heads_y, heads_x] = 0
assert(check_sum == torch.sum(Yss[0][b]).item() == len(y_idx))
Yss_out = [torch.cuda.FloatTensor(w_map) for w_map in Yss_out]
check_sum = 0
for yss_out in Yss_out:
yss_out_argmax, _ = torch.max(yss_out[:, 0:3], dim=1)
yss_out_argmax = (yss_out_argmax>0).type(torch.cuda.FloatTensor)
check_sum += torch.sum(yss_out_argmax).item()
yss = (Yss[0]>0).type(torch.cuda.FloatTensor)
assert(torch.sum(yss) == check_sum)
return Yss_out
'''
This function upsamples given tensor by a factor but make sures there is no repetition
of values. Basically when upsampling by a factor of 2, there are 3 new places created. This fn.
instead of repeating the values, marks them 1.
Caveat : this function currently supports upsample by factor=2 only. For power of 2, use it
multiple times. This doesn't support factors other than powers of 2
Input - input (torch tensor) - A binary map denoting where the head is present. (Bx4xHxW)
factor (int) - factor by which you need to upsample
Output - output (torch tensor) - Upsampled and non-repeated output (Bx4xH'xW')
H' - upsampled height
W' - upsampled width
'''
def upsample_single(self, input_, factor=2):
channels = input_.size(1)
indices = torch.nonzero(input_)
indices_up = indices.clone()
# Corner case!
if indices_up.size(0) == 0:
return torch.zeros(input_.size(0),input_.size(1), input_.size(2)*factor, input_.size(3)*factor).cuda()
indices_up[:, 2] *= factor
indices_up[:, 3] *= factor
output = torch.zeros(input_.size(0),input_.size(1), input_.size(2)*factor, input_.size(3)*factor).cuda()
output[indices_up[:, 0], indices_up[:, 1], indices_up[:, 2], indices_up[:, 3]] = input_[indices[:, 0], indices[:, 1], indices[:, 2], indices[:, 3]]
output[indices_up[:, 0], channels-1, indices_up[:, 2]+1, indices_up[:, 3]] = 1.0
output[indices_up[:, 0], channels-1, indices_up[:, 2], indices_up[:, 3]+1] = 1.0
output[indices_up[:, 0], channels-1, indices_up[:, 2]+1, indices_up[:, 3]+1] = 1.0
output_check = nn.functional.max_pool2d(output, kernel_size=2)
return output
'''
This function implements the GWTA loss in which it
divides the pred and gt into grids and calculates
loss on each grid and returns the maximum of the losses.
input : pred (torch.cuda.FloatTensor) - Bx4xHxW - prediction from the network
gt (torch.cuda.FloatTensor) - BxHxW - Ground truth points
criterion - criterion to take the loss between pred and gt
grid_factor (int) - the image would be divided in 2^grid_factor number of patches for takeing WTA loss
output : max_loss (torch.FloatTensor) - Maximum of the grid losses
'''
def gwta_loss(self, pred, gt, criterion, grid_factor=2):