From db8b77a3b8255cfa9b46d8d915fa5493f939e382 Mon Sep 17 00:00:00 2001 From: Hunor Laczko Date: Sun, 2 Jul 2023 01:08:17 +0200 Subject: [PATCH] remove unused parts --- custom_utils/custom_utils.py | 148 +---------------------------------- 1 file changed, 1 insertion(+), 147 deletions(-) diff --git a/custom_utils/custom_utils.py b/custom_utils/custom_utils.py index d9b256d..58bb990 100644 --- a/custom_utils/custom_utils.py +++ b/custom_utils/custom_utils.py @@ -9,9 +9,6 @@ from plotly.subplots import make_subplots from torchvision.transforms import Resize from torchvision.transforms.functional import InterpolationMode -# from tensorflow_graphics.geometry.convolution.graph_pooling import pool, unpool -# from tensorflow_graphics.geometry.convolution.graph_convolution import edge_convolution_template - def m2uv_array(m2uv, is_smpl_orig=False): result = np.zeros((14475 if not is_smpl_orig else 3638, 2), dtype=np.int32) @@ -74,35 +71,6 @@ class UV2Mesh(nn.Module): return x - -class UV2MeshLabel(): - def __init__(self, res=256, is_skirt=False, is_smpl_orig=False): - super(UV2MeshLabel, self).__init__() - self.res = res - uv_map = np.load('custom_utils/stretched_uv_map_' + ('skirt' if is_skirt else '3') + ('_orig' if is_smpl_orig else '') + '.npy') - self.uv_map_discreet = np.floor(uv_map * self.res).astype(np.int32) - m2uv = np.load('custom_utils/m2uv' + ('_skirt' if is_skirt else '') + ('_orig' if is_smpl_orig else '') + '.npy', allow_pickle=True).item() - m2uv = m2uv_array(m2uv, is_smpl_orig=is_smpl_orig) - self.uv_pixels = self.uv_map_discreet[m2uv] - - # input = channels_first - # @tf.function - def uv_to_mesh(self, uv): - batch_size = uv.shape[0] - - x1_ind = tf.tile(self.uv_pixels[:, 0, :][tf.newaxis, :, :], [batch_size, 1, 1]) - x2_ind = tf.tile(self.uv_pixels[:, 1, :][tf.newaxis, :, :], [batch_size, 1, 1]) - - x1 = tf.gather_nd(uv, x1_ind, batch_dims=1) - x2 = tf.gather_nd(uv, x2_ind, batch_dims=1) - - mesh = (x1 + x2) / 2 - return mesh - - def __call__(self, uv): - return self.uv_to_mesh(uv) - - def _maskFaces(V, F, mask, return_unfixed=False): V_mask = mask F_mask = np.take(V_mask, F.reshape(-1), axis=0) @@ -119,20 +87,6 @@ def _maskFaces(V, F, mask, return_unfixed=False): return V_new, F_new -# def _maskFaces_tf(V, F, mask): -# V_mask = mask -# F_mask = tf.gather(V_mask, F.reshape(-1), axis=0) -# F_mask = F_mask.reshape((-1, 3)).sum(axis=1) == 3 -# F_mask = tf.reduce_sum(tf.reshape(F_mask, (-1, 3)), axis=1) == 3 - -# V_ind = tf.zeros(V_mask.shape) -# V_ind[V_mask] = tf.range(V_mask.sum()) -# F_new = tf.gather(V_ind, F[F_mask, :].reshape(-1), axis=0).reshape((-1, 3)) -# V_new = V[V_mask, :] - -# return V_new, F_new - - def maskFacesBatch(V, F, mask): Vs = [] Fs = [] @@ -272,25 +226,6 @@ def plot_3d_collision(gt, collision_1, collision_2, collision_faces, collision_c return fig -def batch_to_dict(batch, signature): - result = dict() - for i, key in enumerate(signature.keys()): - result[key] = batch[i] - return result - -conditions = ['pose_shape', 'uv_static', 'uv_mask', 'uv_shaped_body'] -def nest_conditions(batch): - batch['conditions'] = {d: batch.pop(d) for d in conditions} - return batch - - -def sparse_to_tensor(L): - idx = np.mat([L.row, L.col]).transpose().astype(np.float32) - data = L.data.astype(dtype=np.float32) - shape = L.shape - return tf.SparseTensor(idx, data, shape) - - def get_summary_str(model): # from torchview import draw_graph @@ -304,6 +239,7 @@ def get_summary_str(model): return [f'all_params: {total_params}, trainable: {trainable_params}'] + def get_summary_html(model): lines = get_summary_str(model) html = '' @@ -312,88 +248,6 @@ def get_summary_html(model): return html -def repeat_laplacian(L, times): - idx = np.repeat(np.mat([L.row, L.col]), repeats=times, axis=1) - idx_offset = np.linspace(0, L.shape[0] * (times-1), times).astype(np.int32) - idx_offset = np.repeat(idx_offset, L.row.shape[0], axis=0) - idx += idx_offset[np.newaxis,:] - data = np.repeat(L.data, repeats=times, axis=0) - shape = (L.shape[0] * times, L.shape[1] * times) - return sp.sparse.coo_matrix((data, (np.squeeze(np.asarray(idx[0,:])), np.squeeze(np.asarray(idx[1,:])))), shape) - - -def merge_verts(Vs, batchDim): - V = tf.concat(tf.unstack(Vs, num=batchDim), axis=0) - return V - - -class GraphConvolution(nn.Module): - def __init__(self, shape, activation=None, name='gcn', L=None, batchSize=None, normalization=None): - super(GraphConvolution, self).__init__() - self.shape = shape - self.w = self.add_weight(name=name + '/weight', shape=(2, *shape), initializer=tf.initializers.glorot_normal, trainable=True) - self.b = self.add_weight(name=name + '/bias', shape=shape[-1], initializer=tf.initializers.zeros, trainable=True) - self.activation = activation or (lambda x: x) - self.normalization = normalization - self.batchSize = batchSize - self.indices = tf.cast(tf.linspace(0, self.batchSize * L.shape[1], self.batchSize+1), dtype=tf.int32) - self.L = repeat_laplacian(L, self.batchSize) - - def gather(self): - return [self.w, self.b] - - def _single(self, X): - X0 = tf.einsum('ab,bc->ac', X, self.w[0]) # Node - X1 = tf.einsum('ab,bc->ac', X, self.w[1]) # Neigh - # Neigh conv - X1 = edge_convolution_template( - X1, sparse_to_tensor(self.L), sizes=None, - edge_function=lambda x, y: y, - reduction='weighted', - edge_function_kwargs={} - ) - X = X0 + X1 + self.b - return X - - def call(self, X, training): - X = merge_verts(X, self.batchSize) - X = self._single(X) - X = tf.stack([X[self.indices[i-1]:self.indices[i]] for i in range(1, len(self.indices))]) - X = self.activation(X) - if self.normalization: - X = self.normalization(X, training=training) - return X - - -def replace_arrays(string): - out = string[:1] - i = 0 - while i < len(string) and string[i+1:].find('[') != -1: - start = string[i+1:].find('[') - end = string[i+1+start:].find(']') - out += string[i+1:i+1+start] - array = string[i+1+start:i+1+start+end+1].replace('\n', '') - out += array - i = i + 1 + start + end - - out += string[i+1:] - - return out - - -def group_inputs_for_levels(batch, config): - levels_input = [] - for res in config['pyramid']['resolutions']: - res_modifier = '_' + str(res) if res != 512 else '' - if config['use_skinning']: - uv_orig = batch['uv_unposed' + res_modifier] - else: - uv_orig = batch['uv_posed' + res_modifier] - levels_input.append([uv_orig, batch['uv_static' + res_modifier], batch['uv_body_posed' + res_modifier], batch['uv_curvature' + res_modifier], batch['uv_mask' + res_modifier], batch['uv_mask']]) - conds = [batch['uv_static'], batch['uv_body_posed'], batch['uv_curvature']] - return levels_input, conds - - class Accumulator(): def __init__(self): self.dict = dict()