def test_equivariance(self): hnet = HNet(5) inp = torch.randn(7, 1, 60, 60) inp_rot = rot90(inp) out = hnet(inp) out_rot = hnet(inp_rot) diff = (rot90(out) - out_rot).max().item() self.assertLess(diff, 1e-3)
def test_backward_jit(self): b, h, w = 5, 30, 30 repr = (2, 3) rnorm = RayleighNorm2d(repr).double() inp = torch.randn(2, b, sum(repr), h, w, dtype=torch.float64) rot = rot90(inp) base_fwd = rnorm(inp) rot_fwd = rnorm(rot) diff = (rot90(base_fwd) - rot_fwd).max().item() self.assertLess(diff, 1e-5)
def test_equivariance_eval(self): b, h, w, d = 5, 30, 30, 30 repr = (2, 3) bnorm = InstanceNorm3d(repr).double() inp = torch.randn(2, b, sum(repr), h, w, d, dtype=torch.float64) rot = rot90(inp) bnorm.eval() base_fwd = bnorm(inp) rot_fwd = bnorm(rot) diff = (rot90(base_fwd) - rot_fwd).max().item() self.assertLess(diff, 1e-5)
def test_equivariance(self): b, h, w = 5, 30, 30 repr = (2, 3) rnorm = RayleighNorm2d(repr).double() inp = torch.randn(2, b, sum(repr), h, w, dtype=torch.float64) rot = rot90(inp) # train mode rnorm.train() base_fwd = rnorm(inp) rot_fwd = rnorm(rot) diff = (rot90(base_fwd) - rot_fwd).max().item() self.assertLess(diff, 1e-5)
def test_backward_jit(self): b, h, w = 5, 30, 30 repr = (2, 3) gnorm = GroupNorm2d(repr).double() inp = torch.randn(2, b, sum(repr), h, w, dtype=torch.float64) rot = rot90(inp) # train mode gnorm.train() base_fwd = gnorm(inp) rot_fwd = gnorm(rot) diff = (rot90(base_fwd) - rot_fwd).max().item() self.assertLess(diff, 1e-5)
def test_rot90_fn(self): """ Test rotation function """ img = self._read_img('lena512color.tiff') img_rot = self._read_img('lena512color_rot90.tiff') self.assertTrue((rot90(img,1)-img_rot).sum() ==0)
def _test_equivariance(self, order): b, s, c1, c2, h, w = 5, 7, 5, 10, 30, 30 repr1 = [c1] repr2 = [0] * (order - 1) + [c2] conv1 = HConv2d(repr1, repr2, s).double() conv2 = HConv2d(repr2, repr1, s).double() inp = torch.randn(2, b, c1, h, w, dtype=torch.float64) rot = rot90(inp) base_fwd = conv2(conv1(inp)) rot_fwd = conv2(conv1(rot)) diff = (rot90(base_fwd) - rot_fwd).max().item() self.assertLess(diff, 1e-5)
def extract_images( file_chk, i_chk, j_chk, d_chk, file_scl, i_scl, j_scl, d_scl, rot, out_chk, out_scl, out_eye): # open checker image img_chk_bgr = cv2.imread(file_chk, cv2.IMREAD_COLOR) if img_chk_bgr is None: print('Failed to load image file:', file_chk) return # open sclera image img_scl_bgr = cv2.imread(file_scl, cv2.IMREAD_COLOR) if img_scl_bgr is None: print('Failed to load image file:', file_scl) return # compute area print('{0}'.format(j_scl)) print('{0}'.format(i_scl)) area_chk_bgr, chk_mask = utils.compute_area(img_chk_bgr, (j_chk, i_chk), (d_chk,)*3) area_scl_bgr, scl_mask = utils.compute_area(img_scl_bgr, (j_scl, i_scl), (d_scl,)*3) # correct wb wb_bgr_f = numpy.float32(cv2.mean(area_chk_bgr, mask=chk_mask)[:3]) img_scl_bgr_wb_f = img_scl_bgr.copy().astype(numpy.float32) img_scl_bgr_wb_f /= wb_bgr_f area_chk_bgr_wb_f = area_chk_bgr.copy().astype(numpy.float32) area_chk_bgr_wb_f /= wb_bgr_f area_scl_bgr_wb_f = area_scl_bgr.copy().astype(numpy.float32) area_scl_bgr_wb_f /= wb_bgr_f # crop and rotate chk_x0, chk_x1, chk_y0, chk_y1 = utils.crop_rect(chk_mask, CROP_K) img_chk = utils.rot90(area_chk_bgr_wb_f[chk_y0:chk_y1, chk_x0:chk_x1], rot) scl_x0, scl_x1, scl_y0, scl_y1 = utils.crop_rect(scl_mask, CROP_K) img_scl = utils.rot90(area_scl_bgr_wb_f[scl_y0:scl_y1, scl_x0:scl_x1], rot) img_eye = utils.rot90(img_scl_bgr_wb_f[scl_y0:scl_y1, scl_x0:scl_x1], rot) # write output images cv2.imwrite(out_scl+'.png', img_scl*WB_K_LDR) cv2.imwrite(out_scl+'.exr', img_scl*WB_K_HDR) cv2.imwrite(out_chk+'.png', img_chk*WB_K_LDR) cv2.imwrite(out_chk+'.exr', img_chk*WB_K_HDR) cv2.imwrite(out_eye+'.png', img_eye*WB_K_LDR) cv2.imwrite(out_eye+'.exr', img_eye*WB_K_HDR) print('{}, WB = {}'.format(out_scl, wb_bgr_f))
def test_equivariance_multi_stream(self): b, s, h, w, d = 3, 5, 20, 20, 20 rep1 = (2, ) rep2 = (1, 2, 3) cconv1 = HConv3d(rep1, rep2, s).double() cconv2 = HConv3d(rep2, rep1, s).double() inp = torch.randn(2, b, rep1[0], h, w, d, dtype=torch.float64) rot = rot90(inp) base_fwd = cconv2(cconv1(inp)) rot_fwd = cconv2(cconv1(rot)) diff = (rot90(base_fwd) - rot_fwd).max().item() self.assertLess(diff, 1e-3)
def test_equivariance_multi_stream_two_hops(self): b, r, h, w = 5, 7, 50, 50 rep1 = (2, ) rep2 = (1, 2, 3) rep3 = (4, 5, 6) rep4 = (2, ) cconv1 = HConv2d(rep1, rep2, r).double() cconv2 = HConv2d(rep2, rep3, r).double() cconv3 = HConv2d(rep3, rep4, r).double() inp = torch.randn(2, b, rep1[0], h, w, dtype=torch.float64) rot = rot90(inp) base_fwd = cconv3(cconv2(cconv1(inp))) rot_fwd = cconv3(cconv2(cconv1(rot))) diff = (rot90(base_fwd) - rot_fwd).max().item() self.assertLess(diff, 1e-3)
def _test_equivariance_transition(self, order): b, s, c1, c2, h, w = 5, 7, 5, 10, 32, 32 repr1 = [c1] repr2 = [0] * (order - 1) + [c2] conv1 = HConv2d(repr1, repr2, s, conv_kwargs={ 'stride': 2, }).double() # conv2 = HConv2d(repr2, repr1, s, conv_kwargs={ # 'stride': 2, # }).double() conv2 = HConv2dTranspose(repr2, repr1, s, conv_kwargs={ 'stride': 2, #'output_padding': 1, }).double() inp = torch.randn(2, b, c1, h, w, dtype=torch.float64) inp = expand_twice(inp) rot = rot90(inp) base_fwd = rot90(conv2(conv1(inp))) rot_fwd = conv2(conv1(rot)) # self.assertEqual(inp.shape, base_fwd.shape) diff = (base_fwd - rot_fwd) ax.set_aspect('equal') rot_fwd_np = rot_fwd.detach().numpy() base_fwd_np = base_fwd.detach().numpy() diff_np = rot_fwd_np - base_fwd_np ax.quiver(rot_fwd_np[0, 0, 0], base_fwd_np[0, 0, 0]) self.assertLess(diff.max().item(), 1e-5)
def _diff_rotation(self, order, plane): b, s, c1, c2, h, w, d = 3, 5, 3, 8, 20, 20, 20 repr1 = [c1] repr2 = [0] * (order - 1) + [c2] conv1 = HConv3d(repr1, repr2, s).double() conv2 = HConv3d(repr2, repr1, s).double() rotation = lambda t: rot90(t, plane=plane) inp = torch.randn(2, b, c1, h, w, d, dtype=torch.float64) rot = rotation(inp) base_fwd = conv2(conv1(inp)) rot_fwd = conv2(conv1(rot)) return (rotation(base_fwd) - rot_fwd).max().item()