def test_jacobian_plowrank(): for get_task in nonlinear_tasks: loader, lc, parameters, model, function, n_output = get_task() generator = Jacobian(layer_collection=lc, model=model, function=function, n_output=n_output) PMat_lowrank = PMatLowRank(generator=generator, examples=loader) dw = random_pvector(lc, device=device) dw = dw / dw.norm() dense_tensor = PMat_lowrank.get_dense_tensor() # Test get_diag check_tensors(torch.diag(dense_tensor), PMat_lowrank.get_diag(), eps=1e-4) # Test frobenius frob_PMat = PMat_lowrank.frobenius_norm() frob_direct = (dense_tensor**2).sum()**.5 check_ratio(frob_direct, frob_PMat) # Test trace trace_PMat = PMat_lowrank.trace() trace_direct = torch.trace(dense_tensor) check_ratio(trace_PMat, trace_direct) # Test mv mv_direct = torch.mv(dense_tensor, dw.get_flat_representation()) mv = PMat_lowrank.mv(dw) check_tensors(mv_direct, mv.get_flat_representation()) # Test vTMV check_ratio(torch.dot(mv_direct, dw.get_flat_representation()), PMat_lowrank.vTMv(dw)) # Test solve # We will try to recover mv, which is in the span of the # low rank matrix regul = 1e-3 mmv = PMat_lowrank.mv(mv) mv_using_inv = PMat_lowrank.solve(mmv, regul=regul) check_tensors(mv.get_flat_representation(), mv_using_inv.get_flat_representation(), eps=1e-2) # Test inv TODO # Test add, sub, rmul check_tensors(1.23 * PMat_lowrank.get_dense_tensor(), (1.23 * PMat_lowrank).get_dense_tensor())
def classify(directory, label, samples, responses, v): images = os.listdir(path="./training_data/" + directory) for i in images: actual = str("./training_data/" + directory + i) print(actual) im = cv2.imread(actual) img = utils.preprocess(im) pre_processed = img.copy() ################# Now finding Contours ################### i, contours, h = cv2.findContours(image=img, mode=cv2.RETR_LIST, method=cv2.CHAIN_APPROX_SIMPLE) keys = [i for i in range(48, 58)] for cnt in contours: area = cv2.contourArea(cnt) if utils.check_rectangle(area): if v: print("AREA: %s" % (area)) [x, y, w, h] = cv2.boundingRect(cnt) if utils.check_ratio(x, y, w, h): cv2.rectangle(pre_processed, (x, y), (x + w, y + h), (0, 0, 255), 2) responses.append(label) sample = utils.roismall(pre_processed, x, y, w, h) samples = np.append(samples, sample, 0) if v: print("Rec: %s" % directory[:1]) elif v: print("Wrong Ration %s" % utils.ratio(x, y, w, h)) elif area > 5000 and area < 200000: if v: print("UNRECOGNIZED area: %s" % (area)) return samples, responses
def test_jacobian_plowrank(): for get_task in nonlinear_tasks: loader, lc, parameters, model, function, n_output = get_task() model.train() generator = Jacobian(layer_collection=lc, model=model, loader=loader, function=function, n_output=n_output) PMat_lowrank = PMatLowRank(generator) dw = random_pvector(lc, device=device) dense_tensor = PMat_lowrank.get_dense_tensor() # Test get_diag check_tensors(torch.diag(dense_tensor), PMat_lowrank.get_diag(), eps=1e-4) # Test frobenius frob_PMat = PMat_lowrank.frobenius_norm() frob_direct = (dense_tensor**2).sum()**.5 check_ratio(frob_direct, frob_PMat) # Test trace trace_PMat = PMat_lowrank.trace() trace_direct = torch.trace(dense_tensor) check_ratio(trace_PMat, trace_direct) # Test mv mv_direct = torch.mv(dense_tensor, dw.get_flat_representation()) check_tensors(mv_direct, PMat_lowrank.mv(dw).get_flat_representation()) # Test vTMV check_ratio(torch.dot(mv_direct, dw.get_flat_representation()), PMat_lowrank.vTMv(dw)) # Test solve TODO # Test inv TODO # Test add, sub, rmul check_tensors(1.23 * PMat_lowrank.get_dense_tensor(), (1.23 * PMat_lowrank).get_dense_tensor())
def test_jacobian_pblockdiag(): for get_task in nonlinear_tasks: loader, lc, parameters, model, function, n_output = get_task() model.train() generator = Jacobian(layer_collection=lc, model=model, loader=loader, function=function, n_output=n_output) PMat_blockdiag = PMatBlockDiag(generator) dw = random_pvector(lc, device=device) dense_tensor = PMat_blockdiag.get_dense_tensor() # Test get_diag check_tensors(torch.diag(dense_tensor), PMat_blockdiag.get_diag()) # Test frobenius frob_PMat = PMat_blockdiag.frobenius_norm() frob_direct = (dense_tensor**2).sum()**.5 check_ratio(frob_direct, frob_PMat) # Test trace trace_PMat = PMat_blockdiag.trace() trace_direct = torch.trace(dense_tensor) check_ratio(trace_PMat, trace_direct) # Test mv mv_direct = torch.mv(dense_tensor, dw.get_flat_representation()) check_tensors(mv_direct, PMat_blockdiag.mv(dw).get_flat_representation()) # Test vTMV check_ratio(torch.dot(mv_direct, dw.get_flat_representation()), PMat_blockdiag.vTMv(dw)) # Test solve regul = 1e-3 Mv_regul = torch.mv(dense_tensor + regul * torch.eye(PMat_blockdiag.size(0), device=device), dw.get_flat_representation()) Mv_regul = PVector(layer_collection=lc, vector_repr=Mv_regul) dw_using_inv = PMat_blockdiag.solve(Mv_regul, regul=regul) check_tensors(dw.get_flat_representation(), dw_using_inv.get_flat_representation(), eps=5e-3) # Test inv PMat_inv = PMat_blockdiag.inverse(regul=regul) check_tensors(dw.get_flat_representation(), PMat_inv.mv(PMat_blockdiag.mv(dw) + regul * dw) .get_flat_representation(), eps=5e-3) # Test add, sub, rmul loader, lc, parameters, model, function, n_output = get_task() model.train() generator = Jacobian(layer_collection=lc, model=model, loader=loader, function=function, n_output=n_output) PMat_blockdiag2 = PMatBlockDiag(generator) check_tensors(PMat_blockdiag.get_dense_tensor() + PMat_blockdiag2.get_dense_tensor(), (PMat_blockdiag + PMat_blockdiag2) .get_dense_tensor()) check_tensors(PMat_blockdiag.get_dense_tensor() - PMat_blockdiag2.get_dense_tensor(), (PMat_blockdiag - PMat_blockdiag2) .get_dense_tensor()) check_tensors(1.23 * PMat_blockdiag.get_dense_tensor(), (1.23 * PMat_blockdiag).get_dense_tensor())
def test_jacobian_pdiag_vs_pdense(): for get_task in nonlinear_tasks: loader, lc, parameters, model, function, n_output = get_task() model.train() generator = Jacobian(layer_collection=lc, model=model, loader=loader, function=function, n_output=n_output) PMat_diag = PMatDiag(generator) PMat_dense = PMatDense(generator) dw = random_pvector(lc, device=device) # Test get_dense_tensor matrix_diag = PMat_diag.get_dense_tensor() matrix_dense = PMat_dense.get_dense_tensor() check_tensors(torch.diag(matrix_diag), torch.diag(matrix_dense)) assert torch.norm(matrix_diag - torch.diag(torch.diag(matrix_diag))) < 1e-5 # Test trace check_ratio(torch.trace(matrix_diag), PMat_diag.trace()) # Test frobenius check_ratio(torch.norm(matrix_diag), PMat_diag.frobenius_norm()) # Test mv mv_direct = torch.mv(matrix_diag, dw.get_flat_representation()) mv_PMat_diag = PMat_diag.mv(dw) check_tensors(mv_direct, mv_PMat_diag.get_flat_representation()) # Test vTMv vTMv_direct = torch.dot(mv_direct, dw.get_flat_representation()) vTMv_PMat_diag = PMat_diag.vTMv(dw) check_ratio(vTMv_direct, vTMv_PMat_diag) # Test inverse regul = 1e-3 PMat_diag_inverse = PMat_diag.inverse(regul) prod = torch.mm(matrix_diag + regul * torch.eye(lc.numel(), device=device), PMat_diag_inverse.get_dense_tensor()) check_tensors(torch.eye(lc.numel(), device=device), prod) # Test solve regul = 1e-3 Mv_regul = torch.mv(matrix_diag + regul * torch.eye(PMat_diag.size(0), device=device), dw.get_flat_representation()) Mv_regul = PVector(layer_collection=lc, vector_repr=Mv_regul) dw_using_inv = PMat_diag.solve(Mv_regul, regul=regul) check_tensors(dw.get_flat_representation(), dw_using_inv.get_flat_representation(), eps=5e-3) # Test get_diag diag_direct = torch.diag(matrix_diag) diag_PMat_diag = PMat_diag.get_diag() check_tensors(diag_direct, diag_PMat_diag) # Test add, sub, rmul loader, lc, parameters, model, function, n_output = get_task() model.train() generator = Jacobian(layer_collection=lc, model=model, loader=loader, function=function, n_output=n_output) PMat_diag2 = PMatDiag(generator) check_tensors(PMat_diag.get_dense_tensor() + PMat_diag2.get_dense_tensor(), (PMat_diag + PMat_diag2).get_dense_tensor()) check_tensors(PMat_diag.get_dense_tensor() - PMat_diag2.get_dense_tensor(), (PMat_diag - PMat_diag2).get_dense_tensor()) check_tensors(1.23 * PMat_diag.get_dense_tensor(), (1.23 * PMat_diag).get_dense_tensor())
def test_jacobian_pdense(): for get_task in nonlinear_tasks: for centering in [True, False]: loader, lc, parameters, model, function, n_output = get_task() model.train() generator = Jacobian(layer_collection=lc, model=model, loader=loader, function=function, n_output=n_output, centering=centering) PMat_dense = PMatDense(generator) dw = random_pvector(lc, device=device) # Test get_diag check_tensors(torch.diag(PMat_dense.get_dense_tensor()), PMat_dense.get_diag()) # Test frobenius frob_PMat = PMat_dense.frobenius_norm() frob_direct = (PMat_dense.get_dense_tensor()**2).sum()**.5 check_ratio(frob_direct, frob_PMat) # Test trace trace_PMat = PMat_dense.trace() trace_direct = torch.trace(PMat_dense.get_dense_tensor()) check_ratio(trace_PMat, trace_direct) # Test solve # NB: regul is high since the matrix is not full rank regul = 1e-3 Mv_regul = torch.mv(PMat_dense.get_dense_tensor() + regul * torch.eye(PMat_dense.size(0), device=device), dw.get_flat_representation()) Mv_regul = PVector(layer_collection=lc, vector_repr=Mv_regul) dw_using_inv = PMat_dense.solve(Mv_regul, regul=regul) check_tensors(dw.get_flat_representation(), dw_using_inv.get_flat_representation(), eps=5e-3) # Test inv PMat_inv = PMat_dense.inverse(regul=regul) check_tensors(dw.get_flat_representation(), PMat_inv.mv(PMat_dense.mv(dw) + regul * dw) .get_flat_representation(), eps=5e-3) # Test add, sub, rmul loader, lc, parameters, model, function, n_output = get_task() model.train() generator = Jacobian(layer_collection=lc, model=model, loader=loader, function=function, n_output=n_output, centering=centering) PMat_dense2 = PMatDense(generator) check_tensors(PMat_dense.get_dense_tensor() + PMat_dense2.get_dense_tensor(), (PMat_dense + PMat_dense2).get_dense_tensor()) check_tensors(PMat_dense.get_dense_tensor() - PMat_dense2.get_dense_tensor(), (PMat_dense - PMat_dense2).get_dense_tensor()) check_tensors(1.23 * PMat_dense.get_dense_tensor(), (1.23 * PMat_dense).get_dense_tensor())