From 0e8223fafa25dca2bf4d39d63e72d834b2f8429d Mon Sep 17 00:00:00 2001 From: Zachary Ferguson Date: Sun, 27 Oct 2024 11:02:01 -0400 Subject: [PATCH] Replace .A with .toarray() --- seam_erasure/mask.py | 8 ++++---- seam_erasure/seam_erasure.py | 17 ++++++++++------- seam_erasure/texture.py | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/seam_erasure/mask.py b/seam_erasure/mask.py index a58af98..04cadcb 100644 --- a/seam_erasure/mask.py +++ b/seam_erasure/mask.py @@ -67,7 +67,7 @@ def mask_seam(mesh, seam_edges, width, height, seam_pixels=None): vals = numpy.full(len(seam_pixels), True, dtype=bool) coords = numpy.array(list(seam_pixels)) mask = scipy.sparse.coo_matrix((vals, (coords[:, 1], coords[:, 0])), - shape=(height, width)).A.astype(bool) + shape=(height, width)).toarray().astype(bool) return ~mask @@ -119,14 +119,14 @@ def mask_inside_seam(mesh, seam_edges, width, height): inbox = pts[inidx] # Only test seam pixels inside the bounding_box - if(inbox.shape[0] > 0): + if (inbox.shape[0] > 0): mask[inbox[:, 1], inbox[:, 0]] |= points_in_triangle(face, inbox) # Mask is False if pixels inside (this needs to be inverted). vals = numpy.full(len(seam_pixels), True, dtype=bool) coords = numpy.array(list(seam_pixels)) full = scipy.sparse.coo_matrix((vals, (coords[:, 1], coords[:, 0])), - shape=mask.shape).A + shape=mask.shape).toarray() mask = full ^ mask return ~mask @@ -156,7 +156,7 @@ def mask_inside_faces(mesh, width, height, init_mask=None): # This mask should be small enough for a dense matrix mask = init_mask - if(mask is None): + if (mask is None): mask = numpy.zeros((height, width), dtype=bool) disable_pbar = logging.getLogger().getEffectiveLevel() > logging.INFO diff --git a/seam_erasure/seam_erasure.py b/seam_erasure/seam_erasure.py index 1b41678..77b8269 100644 --- a/seam_erasure/seam_erasure.py +++ b/seam_erasure/seam_erasure.py @@ -62,8 +62,10 @@ def get_name(method): def display_quadratic_energy(coeffs, x0, x, name): """Compute the energy of a solution given the coefficents.""" logging.debug("{} Before After".format(name)) - E0 = x0.T.dot(coeffs.Q.dot(x0)) + 2.0 * x0.T.dot(coeffs.L.A) + coeffs.C.A - E = x.T.dot(coeffs.Q.dot(x)) + 2.0 * x.T.dot(coeffs.L.A) + coeffs.C.A + E0 = x0.T.dot(coeffs.Q.dot(x0)) + 2.0 * \ + x0.T.dot(coeffs.L.toarray()) + coeffs.C.toarray() + E = x.T.dot(coeffs.Q.dot(x)) + 2.0 * \ + x.T.dot(coeffs.L.toarray()) + coeffs.C.toarray() depth = (x.shape + (1,))[1] for i in range(depth): logging.debug("%d %g %g" % (i, E0[i] if depth < 2 else E0[i, i], @@ -198,17 +200,18 @@ def erase_seam(mesh, texture, sv_method=SeamValueMethod.NONE, do_global=False): dot_process.start() # Use iterative solver for large textures - if(quad.nnz >= 2e6): + if (quad.nnz >= 2e6): logging.info( "Using iterative solver for large system (nnz={})".format(quad.nnz)) textureVec = texture.reshape(N, -1) solution = numpy.empty(textureVec.shape) for j in range(textureVec.shape[1]): logging.info("Solving channel {}".format(j)) + def callback(xk): return logging.debug("||Qx - l||={:.3e}".format( - numpy.linalg.norm(quad.dot(xk) + lin[:, j].A.flatten()))) + numpy.linalg.norm(quad.dot(xk) + lin[:, j].toarray().flatten()))) solution[:, j], _ = scipy.sparse.linalg.cg( - quad, (-lin[:, j]).A, x0=textureVec[:, j], + quad, (-lin[:, j]).toarray(), x0=textureVec[:, j], tol=1 / 255., atol=1 / 255., callback=callback) # Use direct solver for smaller textures else: @@ -219,7 +222,7 @@ def callback(xk): return logging.debug("||Qx - l||={:.3e}".format( quad = quad.tocoo() system = cvxopt.spmatrix(quad.data, numpy.array(quad.row, dtype=int), numpy.array(quad.col, dtype=int)) - rhs = cvxopt.matrix(-lin.A) + rhs = cvxopt.matrix(-lin.toarray()) cvxopt.cholmod.linsolve(system, rhs) solution = numpy.array(rhs) except Exception as e: @@ -233,5 +236,5 @@ def callback(xk): return logging.debug("||Qx - l||={:.3e}".format( display_energies(energies, texture.reshape(N, -1), solution) if scipy.sparse.issparse(solution): - return solution.A + return solution.toarray() return solution diff --git a/seam_erasure/texture.py b/seam_erasure/texture.py index fc3f034..2781c8b 100644 --- a/seam_erasure/texture.py +++ b/seam_erasure/texture.py @@ -38,7 +38,7 @@ def save_texture_channels(data, base_fname): def save_float_mat_as_boolean(M, fname, tolerance=1e-8): """ Save a floating point matrix as a binary image for > tolerance. """ assert len(M.shape) == 2 # Needs to be a 2-dimensional matrix - tmp = 255 * (abs(M.A) > tolerance).astype("uint8") + tmp = 255 * (abs(M.toarray()) > tolerance).astype("uint8") tmp = tmp.reshape(tmp.shape[0], tmp.shape[1], 1).repeat(3, axis=2) # TODO: Use optional mode parameter to set to L of 1 Image.fromarray(tmp).save(fname)