def test_homography_warper_gradcheck(self): # generate input data batch_size = 1 height, width = 16, 32 # small patch, otherwise the test takes forever eye_size = 3 # identity 3x3 # create checkerboard board = utils.create_checkerboard(height, width, 4) patch_src = torch.from_numpy(board).view( 1, 1, height, width).expand(batch_size, 1, height, width) patch_src = utils.tensor_to_gradcheck_var(patch_src) # to var # create base homography dst_homo_src = utils.create_eye_batch(batch_size, eye_size) dst_homo_src = utils.tensor_to_gradcheck_var( dst_homo_src, requires_grad=False) # to var # instantiate warper warper = tgm.HomographyWarper(height, width) # evaluate function gradient res = gradcheck(warper, (patch_src, dst_homo_src,), raise_exception=True) self.assertTrue(res) # evaluate function gradient res = gradcheck( tgm.homography_warp, (patch_src, dst_homo_src, (height, width)), raise_exception=True) self.assertTrue(res)
def test_depth_warper(self): # generate input data batch_size = 1 height, width = 8, 8 cx, cy = width / 2, height / 2 fx, fy = 1., 1. rx, ry, rz = 0., 0., 0. tx, ty, tz = 0., 0., 0. offset = 1. # we will apply a 1unit offset to `i` camera pinhole_ref = utils.create_pinhole(fx, fy, cx, cy, height, width, rx, ry, rx, tx, ty, tz) pinhole_ref = pinhole_ref.expand(batch_size, -1) pinhole_i = utils.create_pinhole(fx, fy, cx, cy, height, width, rx, ry, rx, tx + offset, ty + offset, tz) pinhole_i = pinhole_i.expand(batch_size, -1) # create checkerboard board = utils.create_checkerboard(height, width, 4) patch_i = torch.from_numpy(board).view(1, 1, height, width).expand( batch_size, 1, height, width) # instantiate warper and compute relative homographies warper = tgm.DepthWarper(pinhole_i) warper.compute_homographies(pinhole_ref, scale=torch.ones(batch_size, 1)) # generate synthetic inverse depth inv_depth_ref = torch.ones(batch_size, 1, height, width) # warpd source patch by depth patch_ref = warper(inv_depth_ref, patch_i) # compute error res = utils.check_equal_torch( patch_ref[..., :int(height - offset), :int(width - offset)], patch_i[..., int(offset):, int(offset):]) self.assertTrue(res) # test functional patch_ref_functional = tgm.depth_warp(pinhole_i, pinhole_ref, inv_depth_ref, patch_i) res = utils.check_equal_torch(patch_ref, patch_ref_functional) self.assertTrue(res)
def test_local_homography_warper(self): # generate input data batch_size = 1 height, width = 16, 32 eye_size = 3 # identity 3x3 # create checkerboard board = utils.create_checkerboard(height, width, 4) patch_src = torch.from_numpy(board).view( 1, 1, height, width).expand(batch_size, 1, height, width) # create local homography dst_homo_src = utils.create_eye_batch(batch_size, eye_size) dst_homo_src = dst_homo_src.view(batch_size, -1).unsqueeze(1) dst_homo_src = dst_homo_src.repeat(1, height * width, 1).view( 1, height, width, 3, 3) # NxHxWx3x3 # warp reference patch patch_src_to_i = tgm.homography_warp( patch_src, dst_homo_src, (height, width))
def test_homography_warper(self, batch_size, device_type): # generate input data height, width = 128, 64 eye_size = 3 # identity 3x3 device = torch.device(device_type) # create checkerboard board = utils.create_checkerboard(height, width, 4) patch_src = torch.from_numpy(board).view(1, 1, height, width).expand( batch_size, 1, height, width) patch_src = patch_src.to(device) # create base homography dst_homo_src = utils.create_eye_batch(batch_size, eye_size).to(device) # instantiate warper warper = tgm.HomographyWarper(height, width) for i in range(self.num_tests): # generate homography noise homo_delta = torch.zeros_like(dst_homo_src) homo_delta[:, -1, -1] = 0.0 dst_homo_src_i = dst_homo_src + homo_delta # transform the points from dst to ref patch_dst = warper(patch_src, dst_homo_src_i) patch_dst_to_src = warper(patch_dst, torch.inverse(dst_homo_src_i)) # projected should be equal as initial error = utils.compute_patch_error(patch_dst, patch_dst_to_src, height, width) assert error.item() < self.threshold # check functional api patch_dst_to_src_functional = tgm.homography_warp( patch_dst, torch.inverse(dst_homo_src_i), (height, width)) assert utils.check_equal_torch(patch_dst_to_src, patch_dst_to_src_functional)
""" A function that max pools an image with size kernel size. Assume that the stride is equal to the kernel size, and that the kernel size is even. Args: im: [np.array of shape [H, W, 3]] kernel_size: integer Returns: im: [np.array of shape [H/kernel_size, W/kernel_size, 3]]. """ stride = kernel_size ### START YOUR CODE HERE ### (You can change anything inside this block) return new_im ### END YOUR CODE HERE ### if __name__ == "__main__": # DO NOT CHANGE im = skimage.data.chelsea() im = utils.uint8_to_float(im) max_pooled_image = MaxPool2d(im, 4) utils.save_im("chelsea.png", im) utils.save_im("chelsea_maxpooled.png", max_pooled_image) im = utils.create_checkerboard() im = utils.uint8_to_float(im) utils.save_im("checkerboard.png", im) max_pooled_image = MaxPool2d(im, 2) utils.save_im("checkerboard_maxpooled.png", max_pooled_image)