def test_valid_mask(): from utils.utils import pltImshow batch_size = 1 mat_homographies = [sample_homography(3) for i in range(batch_size)] mat_H = np.stack(mat_homographies, axis=0) corner_img = np.array([(-1, -1), (-1, 1), (1, -1), (1, 1)]) # printCorners(corner_img, mat_H) # points = warp_points_np(corner_img, mat_homographies) mat_H = torch.tensor(mat_H, dtype=torch.float32) mat_H_inv = torch.stack([torch.inverse(mat_H[i, :, :]) for i in range(batch_size)]) from utils.utils import compute_valid_mask, labels2Dto3D device = 'cpu' shape = torch.tensor([240, 320]) for i in range(1): r = 3 mask_valid = compute_valid_mask(shape, inv_homography=mat_H_inv, device=device, erosion_radius=r) pltImshow(mask_valid[0,:,:]) cell_size = 8 mask_valid = labels2Dto3D(mask_valid.view(batch_size, 1, mask_valid.shape[1], mask_valid.shape[2]), cell_size=cell_size) mask_valid = torch.prod(mask_valid[:,:cell_size*cell_size,:,:], dim=1) pltImshow(mask_valid[0,:,:].cpu().numpy()) mask = {} mask.update({'homographies': mat_H, 'masks': mask_valid}) np.savez_compressed('h2.npz', **mask) print("finish testing valid mask")
def sample_homo(image): import tensorflow as tf from utils.homographies import sample_homography H = sample_homography(tf.shape(image)[:2]) with tf.Session(): H_ = H.eval() H_ = np.concatenate((H_, np.array([1])[:, np.newaxis]), axis=1) # warped_im = tf.contrib.image.transform(image, H, interpolation="BILINEAR") mat = np.reshape(H_, (3, 3)) # for i in range(batch): # np.stack() return mat
def test_sample_homography(): batch_size = 30 filename = '../configs/superpoint_coco_train.yaml' import yaml with open(filename, 'r') as f: config = yaml.load(f) test_tf = False test_corner_def = True if test_tf == True: from utils.homographies import sample_homography as sample_homography boundary = 1 # from utils.homographies import sample_homography_np as sample_homography # mat_homographies = matrix[np.newaxis, :,:] # mat_homographies = [sample_homography(tf.constant([boundary,boundary]), mat_homographies = [sample_homography(np.array([boundary,boundary]), **config['data']['warped_pair']['params']) for i in range(batch_size)] mat_homographies = np.stack(mat_homographies, axis=0) corner_img = np.array([[0., 0.], [0., boundary], [boundary, boundary], [boundary, 0.]]) printCorners(corner_img, mat_homographies) if test_corner_def: corner_img = np.array([(-1, -1), (-1, 1), (1, 1), (1, -1)]) from utils.homographies import sample_homography_np as sample_homography boundary = 2 mat_homographies = [sample_homography(np.array([boundary,boundary]), shift=-1, **config['data']['warped_pair']['params']) for i in range(batch_size)] mat_homographies = np.stack(mat_homographies, axis=0) printCorners(corner_img, mat_homographies) else: from utils.utils import sample_homography mat_homographies = [sample_homography(1) for i in range(batch_size)] # sess = tf.Session() # with sess.as_default(): # m = mat_homographies[0].eval() print("end")
def sample_homographies(batch_size=1, scale=10, device='cpu'): ## sample homography matrix # mat_H = [sample_homography(inv_scale=scale) for i in range(batch_size)] mat_H = [sample_homography(inv_scale=scale) for i in range(batch_size)] ##### debug # from utils.utils import sample_homo # mat_H = [sample_homo(image=np.zeros((1,1))) for i in range(batch_size)] # mat_H = [np.identity(3) for i in range(batch_size)] mat_H = np.stack(mat_H, axis=0) mat_H = torch.tensor(mat_H, dtype=torch.float32) mat_H = mat_H.to(device) mat_H_inv = torch.stack([torch.inverse(mat_H[i, :, :]) for i in range(batch_size)]) mat_H_inv = torch.tensor(mat_H_inv, dtype=torch.float32) mat_H_inv = mat_H_inv.to(device) return mat_H, mat_H_inv