From db5cb3571896516002d5a0bc3379f019ace082a0 Mon Sep 17 00:00:00 2001 From: Hunor Laczko Date: Thu, 9 Feb 2023 12:43:03 +0000 Subject: [PATCH] generate curvatures --- custom_data/train_bins_25.npy | Bin 0 -> 184 bytes data/curvatures.py | 145 ++++++++++++++++++++++------------ data/preprocessing.py | 2 +- 3 files changed, 96 insertions(+), 51 deletions(-) create mode 100644 custom_data/train_bins_25.npy diff --git a/custom_data/train_bins_25.npy b/custom_data/train_bins_25.npy new file mode 100644 index 0000000000000000000000000000000000000000..c5c2a3ca4b2fe39509d8521e0830ef7e8894fdde GIT binary patch literal 184 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlV+i=qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= zXCxM+0{I%|I+{8PwF(pfE(Qh$1?k;0_5o=aSYSFo${xsZu+=ID(Fy0T^nhpvk6R1D K;@1MM*aHBDL?~eZ literal 0 HcmV?d00001 diff --git a/data/curvatures.py b/data/curvatures.py index e97d1b0..1f289fe 100644 --- a/data/curvatures.py +++ b/data/curvatures.py @@ -88,13 +88,13 @@ def k_neighbours(k): class Data: def __init__(self): - self.dist = None + self.k_neighbors = None self.mask = None self.faces = None class Cache: - def __init__(self, folder, neighbourhood, frames): + def __init__(self, folder, neighbourhood, frames, include_neighbours=True): self.cache = dict() for frame in tqdm.tqdm(frames): @@ -107,7 +107,14 @@ class Cache: self.cache[id] = Data() mask = read_garment_mask(folder, id, garment) #np.load(os.path.join(folder, 'mesh_mask', f'{id}.npy')).astype(bool) - k_neighbors = np.argsort(dist[mask, :][:, mask], axis=-1)[:, :neighbourhood] #cloud.get_neighbors(k=neighbourhood) + + if include_neighbours: + k_neighbors = np.argsort(dist[mask, :][:, mask], axis=-1)[:, :neighbourhood] #cloud.get_neighbors(k=neighbourhood) + self.cache[id].k_neighbors = k_neighbors + G = tm.graph.vertex_adjacency_graph(m) + for i in range(v.shape[0]): + for k in range(25): + assert nx.has_path(G, i, k_neighbors[i, k]), f'No path between {i} and {k_neighbors[i, k]}' _, faces = _maskFaces(np.zeros((14475,3)), F, mask) #np.load(os.path.join(folder, 'faces', f'{id}.npy')) v = read_posed_garment_frame(folder, id, garment, frame_nr)[mask,:] @@ -117,13 +124,8 @@ class Cache: outliers_n_neighbours = get_closest_n_points(v, outliers, 2) outliers_faces = np.concatenate((outliers[:, np.newaxis], outliers_n_neighbours), axis=-1) faces = np.concatenate((faces, outliers_faces), axis=0) + m = tm.Trimesh(v, faces, process=False, validate=False) - G = tm.graph.vertex_adjacency_graph(m) - for i in range(v.shape[0]): - for k in range(25): - assert nx.has_path(G, i, k_neighbors[i, k]), f'No path between {i} and {k_neighbors[i, k]}' - - self.cache[id].k_neighbors = k_neighbors self.cache[id].mask = mask self.cache[id].faces = faces @@ -153,9 +155,10 @@ def generate_curvatures(folder, neighbourhood, frames): # ids = os.listdir(folder)[int(args.start):int(args.end)] # frames = frames[int(args.start):int(args.end)] - # os.makedirs(os.path.join(folder, f'mesh_curvature_raw_{neighbourhood}'), exist_ok=True) + os.makedirs(os.path.join('/'.join(folder.split('/')[:-1]), 'backup', f'mesh_curvature_raw'), exist_ok=True) cache = Cache(folder, neighbourhood, frames) + filters = tb.Filters(complevel=9, complib='blosc2') for frame in tqdm.tqdm(frames): @@ -181,13 +184,16 @@ def generate_curvatures(folder, neighbourhood, frames): out = np.zeros((14475), dtype=np.float32) out[mask] = curvature - # np.savez_compressed(os.path.join(folder, f'mesh_curvature_raw_{neighbourhood}', f'{id}_{frame_nr}.npz'), mesh_curvature_raw=out) + + np.savez_compressed(os.path.join('/'.join(folder.split('/')[:-1]), 'backup', f'mesh_curvature_raw', f'{id}_{frame_nr}_{garment}.npz'), mesh_curvature_raw=out) group = h5file.get_node(f'/{id}/{frame_nr}/{garment}') if f'curvature_raw' in group: array = h5file.get_node(f'/{id}/{frame_nr}/{garment}/curvature_raw') array[:] = out else: - h5file.create_array(group, "curvature_raw", out) + carray = h5file.create_carray(group, "curvature_raw", atom=tb.Float32Atom(), shape=out.shape, filters=filters) + carray[:] = out + return None @@ -202,50 +208,71 @@ def get_bins(values, nr_of_bins): def generate_curvature_labels(folder, bins, curvatures, frames, neighbourhood): + cache = Cache(folder, neighbourhood, frames, include_neighbours=False) - os.makedirs(os.path.join(folder, f'mesh_curvature_{neighbourhood}_2'), exist_ok=True) + os.makedirs(os.path.join('/'.join(folder.split('/')[:-1]), 'backup', f'curvature_mesh_global'), exist_ok=True) + os.makedirs(os.path.join('/'.join(folder.split('/')[:-1]), 'backup', f'curvature_mesh_garment'), exist_ok=True) labels = np.linspace(0, bins.shape[0] - 2, bins.shape[0] - 1) + filters = tb.Filters(complevel=9, complib='blosc2') for i, frame in enumerate(tqdm.tqdm(frames)): - # id = frame.split('_')[0] - # mask = np.load(os.path.join(folder, 'mesh_mask', f'{id}.npy')) - # print(i) - curvature = curvatures[i] + id = frame.split()[0] + frame_nr = int(frame.split()[1]) + garment = frame.split()[2] - frame_labels = np.zeros((curvature.shape[0]), dtype=np.float32) + curvature = curvatures[i][cache[id].mask] + + frame_labels = np.zeros((curvature.shape[0]), dtype=np.uint8) for j in range(bins.shape[0] - 2): frame_labels[(curvature >= bins[j]) * (curvature < bins[j+1])] = labels[j] frame_labels[curvature >= bins[-2]] = labels[-1] + out = np.zeros((14475), dtype=np.uint8) + out[cache[id].mask] = frame_labels + 1 - np.savez_compressed(os.path.join(folder, f'mesh_curvature_{neighbourhood}_2', f'{frame}.npz'), mesh_curvature=frame_labels) + group = h5file.get_node(f'/{id}/{frame_nr}/{garment}') + if f'curvature_mesh_global' in group: + array = h5file.get_node(f'/{id}/{frame_nr}/{garment}/curvature_mesh_global') + array[:] = out + else: + carray = h5file.create_carray(group, "curvature_mesh_global", atom=tb.UInt8Atom(), shape=out.shape, filters=filters) + carray[:] = out + + np.savez_compressed(os.path.join('/'.join(folder.split('/')[:-1]), 'backup', f'curvature_mesh_global', f'{id}_{frame_nr}_{garment}.npz'), mesh_curvature_raw=out) # writePC2(os.path.join(garment_dir, "curvature_label_" + str(nr_of_labels) + "_" + str(neighbourhood) + ".pc16"), unmasked_label, True) + # redo it with local bins + bins = get_bins([curvature], nr_of_labels) + frame_labels = np.zeros((curvature.shape[0]), dtype=np.uint8) + for j in range(bins.shape[0] - 2): + frame_labels[(curvature >= bins[j]) * (curvature < bins[j+1])] = labels[j] + frame_labels[curvature >= bins[-2]] = labels[-1] + out = np.zeros((14475), dtype=np.uint8) + out[cache[id].mask] = frame_labels + 1 -def visualize_curvature_labels(folder): - ids = os.listdir(folder)#[:10] + group = h5file.get_node(f'/{id}/{frame_nr}/{garment}') + if f'curvature_mesh_garment' in group: + array = h5file.get_node(f'/{id}/{frame_nr}/{garment}/curvature_mesh_garment') + array[:] = out + else: + carray = h5file.create_carray(group, "curvature_mesh_garment", atom=tb.UInt8Atom(), shape=out.shape, filters=filters) + carray[:] = out - for id in tqdm.tqdm(ids): - garment_dir = os.path.join(folder, id) + np.savez_compressed(os.path.join('/'.join(folder.split('/')[:-1]), 'backup', f'curvature_mesh_garment', f'{id}_{frame_nr}_{garment}.npz'), mesh_curvature_raw=out) + - if not os.path.isdir(garment_dir): - continue +def visualize_curvature_labels(folder, frames): + cache = Cache(folder, 0, frames, include_neighbours=False) - garments = [f.replace('.pc16','') for f in os.listdir(garment_dir) if f.find('_') == -1 and f != 'info.mat' and not f.endswith('.obj')] - # assert len (garments) == 1 - if len (garments) != 1: - continue - garment = garments[0] - - frame = 10 + for frame in tqdm.tqdm(frames): + id = frame.split()[0] + frame_nr = int(frame.split()[1]) + garment = frame.split()[2] - bodies = readPC2Frame(os.path.join(folder, id, 'Body_posed.pc16'), frame, True) - offsetss = readPC2Frame(os.path.join(folder, id, garment + '_registered.pc16'), frame, True) - # curvature_labels = readPC2Frame(os.path.join(folder, id, 'curvature_label_6_25.pc16'), frame, True)[:, 0] - curvature_labels = np.load(os.path.join(folder, id, 'curvature_label_6_25.npy'), allow_pickle=True)[frame, :] - mask = np.squeeze(sio.loadmat(os.path.join(garment_dir, garment + '_mask.mat'))['mask'].astype(bool)) + mask = cache[id].mask + faces = cache[id].faces + verts = read_posed_garment_frame(folder, id, garment, frame_nr)[mask, :] + curv = h5file.get_node(f'/{id}/{frame_nr}/{garment}/curvature_mesh_global').read()[mask] - verts = offsetss - verts, faces = _maskFaces(verts, F, mask) # m = tm.Trimesh(verts, faces, process=False, validate=False) # outliers = np.array([len(v) for v in m.vertex_neighbors]) == 0 @@ -254,12 +281,12 @@ def visualize_curvature_labels(folder): # outliers_faces = np.concatenate((outliers[:, np.newaxis], outliers_n_neighbours), axis=-1) # faces = np.concatenate((faces, outliers_faces), axis=0) - colors = np.array([cm.color('jet', 1 - i) for i in np.linspace(0, 1, int(curvature_labels.max()+1))]) - colors_binned = np.zeros((curvature_labels.shape[0], 3)) - for i in range(int(curvature_labels.max())+1): - print(np.count_nonzero(curvature_labels == i)) - colors_binned[curvature_labels == i, :] = colors[i] - colors_binned = colors_binned[mask, :] + colors = np.array([cm.color('jet', 1 - i) for i in np.linspace(0, 1, int(curv.max()))]) + colors_binned = np.zeros((curv.shape[0], 3)) + for i in range(1, int(curv.max())+1): + # print(np.count_nonzero(curv == i)) + colors_binned[curv == i, :] = colors[i-1] + # colors_binned = colors_binned[mask, :] # m = tm.Trimesh(verts, faces, process=False, validate=False, vertex_colors=colors_binned) # m.show() @@ -291,11 +318,25 @@ def visualize_curvature_labels(folder): def read_raw_curvatures(folder, frames, neighbourhood): curvatures_all = [] for frame in tqdm.tqdm(frames): - curv = np.load(os.path.join(folder, f'mesh_curvature_raw_{neighbourhood}', f'{frame}.npz'))['mesh_curvature_raw'] + id = frame.split()[0] + frame_nr = int(frame.split()[1]) + garment = frame.split()[2] + curv = h5file.get_node(f'/{id}/{frame_nr}/{garment}/curvature_raw').read() curvatures_all.append(curv) return curvatures_all +def read_raw_curvatures_2(folder, frames, neighbourhood): + curvatures_all = [] + for frame in tqdm.tqdm(frames): + id = frame.split()[0] + frame_nr = int(frame.split()[1]) + garment = frame.split()[2] + # curv = np.load(os.path.join(folder, f'mesh_curvature_raw_{neighbourhood}', f'{frame}.npz'))['mesh_curvature_raw'] + curv = np.load(os.path.join('/'.join(folder.split('/')[:-1]), 'backup', f'mesh_curvature_raw', f'{id}_{frame_nr}_{garment}.npz'))['mesh_curvature_raw'] + curvatures_all.append(curv) + + return curvatures_all if __name__ == '__main__': parser = argparse.ArgumentParser() @@ -339,14 +380,18 @@ if __name__ == '__main__': type = folder.split('/')[-1] h5file = tb.open_file(os.path.join(root, f'{type}_extras.h5'), mode='a') + start = time.perf_counter() - generate_curvatures(folder, neighbourhood, frames) + # generate_curvatures(folder, neighbourhood, frames) # curvatures = read_raw_curvatures(folder, frames, neighbourhood) # bins = get_bins(curvatures, nr_of_labels) - # np.save(f'custom_data/train_bins_skirt_{neighbourhood}.npy', bins) - # bins = np.load(f'custom_data/train_bins_skirt_{neighbourhood}.npy') + # np.save(f'custom_data/{type}_bins{skirt_modifier}_{neighbourhood}.npy', bins) + # print(bins) + # bins = np.load(f'custom_data/{type}_bins{skirt_modifier}_{neighbourhood}.npy') # generate_curvature_labels(folder, bins, curvatures, frames, neighbourhood) - # visualize_curvature_labels(folder) + visualize_curvature_labels(folder, frames) + + print(time.perf_counter() - start) h5file.close() diff --git a/data/preprocessing.py b/data/preprocessing.py index 41e011c..7b6069d 100644 --- a/data/preprocessing.py +++ b/data/preprocessing.py @@ -119,7 +119,7 @@ def create_extras_file(folder): lines = [line.strip() for line in lines] frames = lines - for frame in tqdm(frames[:1000]): + for frame in tqdm(frames[:3000]): # add a new empty group to the root group for each frame id, frame_nr, garment = frame.split() if not id in h5file.root: