MCPcopy Create free account
hub / github.com/apple/ml-pointersect / uniformly_sample

Method uniformly_sample

pointersect/inference/structures.py:1924–1976  ·  view source on GitHub ↗

Uniformly sample more cameras from the current ones. Currently, we do not support gradient (though nothing stops it theoretically). Returns: new camera: (b, num_samples)

(self, num_samples: int)

Source from the content-addressed store, hash-verified

1922
1923 @torch.no_grad()
1924 def uniformly_sample(self, num_samples: int) -> 'Camera':
1925 """
1926 Uniformly sample more cameras from the current ones.
1927 Currently, we do not support gradient (though nothing stops it theoretically).
1928
1929 Returns:
1930 new camera: (b, num_samples)
1931 """
1932 length = self.H_c2w.size(1)
1933
1934 idxs = np.linspace(0, 1 - 1e-8, num_samples) * (length - 1)
1935 self_H_c2w = self.H_c2w.detach().cpu().numpy() # (b, q, 4, 4)
1936 self_intrinsic = self.intrinsic.detach().cpu().numpy() # (b, q, 3, 3)
1937
1938 all_H_c2ws = []
1939 all_intrinsics = []
1940 for b in range(self.H_c2w.size(0)):
1941 H_c2ws = []
1942 intrinsics = []
1943
1944 for i in range(len(idxs)):
1945 idx = idxs[i]
1946 idx_from = math.floor(idx)
1947 idx_to = idx_from + 1
1948 t = idx - idx_from
1949 H_c2w = rigid_motion.interp_homegeneous_matrices(
1950 t=t,
1951 H0=self_H_c2w[b, idx_from],
1952 H1=self_H_c2w[b, idx_to],
1953 )
1954 H_c2w = torch.from_numpy(H_c2w)
1955 if torch.norm(H_c2w, p=2, dim=-2).any() <= 1e-6 or \
1956 torch.logical_not(torch.norm(H_c2w, p=2, dim=-2).isfinite()).any():
1957 print('oh no!')
1958 H_c2ws.append(H_c2w)
1959
1960 intrinsic = (1 - t) * self_intrinsic[b, idx_from] + t * self_intrinsic[b, idx_to]
1961 intrinsics.append(torch.from_numpy(intrinsic))
1962
1963 H_c2ws = torch.stack(H_c2ws, dim=0)
1964 intrinsics = torch.stack(intrinsics, dim=0)
1965 all_H_c2ws.append(H_c2ws)
1966 all_intrinsics.append(intrinsics)
1967
1968 all_H_c2ws = torch.stack(all_H_c2ws, dim=0)
1969 all_intrinsics = torch.stack(all_intrinsics, dim=0)
1970
1971 return Camera(
1972 H_c2w=all_H_c2ws.to(device=self.H_c2w.device, dtype=self.H_c2w.dtype),
1973 intrinsic=all_intrinsics.to(device=self.intrinsic.device, dtype=self.intrinsic.dtype),
1974 width_px=self.width_px,
1975 height_px=self.height_px,
1976 )
1977
1978 def get_camera_frames(
1979 self,

Callers 1

__init__Method · 0.95

Calls 4

CameraClass · 0.85
sizeMethod · 0.80
detachMethod · 0.45
toMethod · 0.45

Tested by

no test coverage detected