text
stringlengths
1
93.6k
curr_img_height = int(np.floor(img_height))
curr_img_width = int(np.floor(img_width))
# pixel coord
all_h_coords = np.arange(0, curr_img_height, 1)
all_w_coords = np.arange(0, curr_img_width, 1)
curr_pxl_coord = np.array(np.meshgrid(all_h_coords, all_w_coords, indexing='ij'))
coord_tensor = np.concatenate([curr_pxl_coord[1:2, :, :], curr_pxl_coord[:1, :, :]])
all_XY_feat = (torch.from_numpy(
np.tile(coord_tensor, (args.batch_size, 1, 1, 1)).astype(np.float32)).cuda())
return torch_spix_idx_tensor, all_XY_feat
#===================== pooling and upsampling feature ==========================================
def shift9pos(input, h_shift_unit=1, w_shift_unit=1):
# input should be padding as (c, 1+ height+1, 1+width+1)
input_pd = np.pad(input, ((h_shift_unit, h_shift_unit), (w_shift_unit, w_shift_unit)), mode='edge')
input_pd = np.expand_dims(input_pd, axis=0)
# assign to ...
top = input_pd[:, :-2 * h_shift_unit, w_shift_unit:-w_shift_unit]
bottom = input_pd[:, 2 * h_shift_unit:, w_shift_unit:-w_shift_unit]
left = input_pd[:, h_shift_unit:-h_shift_unit, :-2 * w_shift_unit]
right = input_pd[:, h_shift_unit:-h_shift_unit, 2 * w_shift_unit:]
center = input_pd[:,h_shift_unit:-h_shift_unit,w_shift_unit:-w_shift_unit]
bottom_right = input_pd[:, 2 * h_shift_unit:, 2 * w_shift_unit:]
bottom_left = input_pd[:, 2 * h_shift_unit:, :-2 * w_shift_unit]
top_right = input_pd[:, :-2 * h_shift_unit, 2 * w_shift_unit:]
top_left = input_pd[:, :-2 * h_shift_unit, :-2 * w_shift_unit]
shift_tensor = np.concatenate([ top_left, top, top_right,
left, center, right,
bottom_left, bottom, bottom_right], axis=0)
return shift_tensor
def poolfeat(input, prob, sp_h=2, sp_w=2):
def feat_prob_sum(feat_sum, prob_sum, shift_feat):
feat_sum += shift_feat[:, :-1, :, :]
prob_sum += shift_feat[:, -1:, :, :]
return feat_sum, prob_sum
b, _, h, w = input.shape
h_shift_unit = 1
w_shift_unit = 1
p2d = (w_shift_unit, w_shift_unit, h_shift_unit, h_shift_unit)
feat_ = torch.cat([input, torch.ones([b, 1, h, w]).cuda()], dim=1) # b* (n+1) *h*w
prob_feat = F.avg_pool2d(feat_ * prob.narrow(1, 0, 1), kernel_size=(sp_h, sp_w),stride=(sp_h, sp_w)) # b * (n+1) * h* w
send_to_top_left = F.pad(prob_feat, p2d, mode='constant', value=0)[:, :, 2 * h_shift_unit:, 2 * w_shift_unit:]
feat_sum = send_to_top_left[:, :-1, :, :].clone()
prob_sum = send_to_top_left[:, -1:, :, :].clone()
prob_feat = F.avg_pool2d(feat_ * prob.narrow(1, 1, 1), kernel_size=(sp_h, sp_w), stride=(sp_h, sp_w)) # b * (n+1) * h* w
top = F.pad(prob_feat, p2d, mode='constant', value=0)[:, :, 2 * h_shift_unit:, w_shift_unit:-w_shift_unit]
feat_sum, prob_sum = feat_prob_sum(feat_sum,prob_sum,top )
prob_feat = F.avg_pool2d(feat_ * prob.narrow(1, 2, 1), kernel_size=(sp_h, sp_w), stride=(sp_h, sp_w)) # b * (n+1) * h* w
top_right = F.pad(prob_feat, p2d, mode='constant', value=0)[:, :, 2 * h_shift_unit:, :-2 * w_shift_unit]
feat_sum, prob_sum = feat_prob_sum(feat_sum, prob_sum, top_right)
prob_feat = F.avg_pool2d(feat_ * prob.narrow(1, 3, 1), kernel_size=(sp_h, sp_w), stride=(sp_h, sp_w)) # b * (n+1) * h* w
left = F.pad(prob_feat, p2d, mode='constant', value=0)[:, :, h_shift_unit:-h_shift_unit, 2 * w_shift_unit:]
feat_sum, prob_sum = feat_prob_sum(feat_sum, prob_sum, left)
prob_feat = F.avg_pool2d(feat_ * prob.narrow(1, 4, 1), kernel_size=(sp_h, sp_w), stride=(sp_h, sp_w)) # b * (n+1) * h* w
center = F.pad(prob_feat, p2d, mode='constant', value=0)[:, :, h_shift_unit:-h_shift_unit, w_shift_unit:-w_shift_unit]
feat_sum, prob_sum = feat_prob_sum(feat_sum, prob_sum, center)
prob_feat = F.avg_pool2d(feat_ * prob.narrow(1, 5, 1), kernel_size=(sp_h, sp_w), stride=(sp_h, sp_w)) # b * (n+1) * h* w
right = F.pad(prob_feat, p2d, mode='constant', value=0)[:, :, h_shift_unit:-h_shift_unit, :-2 * w_shift_unit]
feat_sum, prob_sum = feat_prob_sum(feat_sum, prob_sum, right)
prob_feat = F.avg_pool2d(feat_ * prob.narrow(1, 6, 1), kernel_size=(sp_h, sp_w), stride=(sp_h, sp_w)) # b * (n+1) * h* w
bottom_left = F.pad(prob_feat, p2d, mode='constant', value=0)[:, :, :-2 * h_shift_unit, 2 * w_shift_unit:]
feat_sum, prob_sum = feat_prob_sum(feat_sum, prob_sum, bottom_left)
prob_feat = F.avg_pool2d(feat_ * prob.narrow(1, 7, 1), kernel_size=(sp_h, sp_w), stride=(sp_h, sp_w)) # b * (n+1) * h* w
bottom = F.pad(prob_feat, p2d, mode='constant', value=0)[:, :, :-2 * h_shift_unit, w_shift_unit:-w_shift_unit]
feat_sum, prob_sum = feat_prob_sum(feat_sum, prob_sum, bottom)
prob_feat = F.avg_pool2d(feat_ * prob.narrow(1, 8, 1), kernel_size=(sp_h, sp_w), stride=(sp_h, sp_w)) # b * (n+1) * h* w
bottom_right = F.pad(prob_feat, p2d, mode='constant', value=0)[:, :, :-2 * h_shift_unit, :-2 * w_shift_unit]
feat_sum, prob_sum = feat_prob_sum(feat_sum, prob_sum, bottom_right)
pooled_feat = feat_sum / (prob_sum + 1e-8)
return pooled_feat
def upfeat(input, prob, up_h=2, up_w=2):
# input b*n*H*W downsampled
# prob b*9*h*w
b, c, h, w = input.shape