|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +from tqdm import tqdm |
| 4 | +import argparse |
| 5 | + |
| 6 | +from scipy.ndimage import zoom |
| 7 | +from skimage.data import camera |
| 8 | +import numpy as np |
| 9 | +from scipy.spatial.distance import cdist |
| 10 | + |
| 11 | +def safemkdir(dirn): |
| 12 | + if not os.path.isdir(dirn): |
| 13 | + os.mkdir(dirn) |
| 14 | + |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +def duration_to_alignment(in_duration): |
| 18 | + total_len = np.sum(in_duration) |
| 19 | + num_chars = len(in_duration) |
| 20 | + |
| 21 | + attention = np.zeros(shape=(num_chars,total_len),dtype=np.float32) |
| 22 | + y_offset = 0 |
| 23 | + |
| 24 | + for duration_idx, duration_val in enumerate(in_duration): |
| 25 | + for y_val in range(0,duration_val): |
| 26 | + attention[duration_idx][y_offset + y_val] = 1.0 |
| 27 | + |
| 28 | + y_offset += duration_val |
| 29 | + |
| 30 | + return attention |
| 31 | + |
| 32 | + |
| 33 | +def rescale_alignment(in_alignment,in_targcharlen): |
| 34 | + current_x = in_alignment.shape[0] |
| 35 | + x_ratio = in_targcharlen / current_x |
| 36 | + pivot_points = [] |
| 37 | + |
| 38 | + zoomed = zoom(in_alignment,(x_ratio,1.0),mode="nearest") |
| 39 | + |
| 40 | + for x_v in range(0,zoomed.shape[0]): |
| 41 | + for y_v in range(0,zoomed.shape[1]): |
| 42 | + val = zoomed[x_v][y_v] |
| 43 | + if val < 0.5: |
| 44 | + val = 0.0 |
| 45 | + else: |
| 46 | + val = 1.0 |
| 47 | + pivot_points.append( (x_v,y_v) ) |
| 48 | + |
| 49 | + zoomed[x_v][y_v] = val |
| 50 | + |
| 51 | + |
| 52 | + if zoomed.shape[0] != in_targcharlen: |
| 53 | + print("Zooming didn't rshape well, explicitly reshaping") |
| 54 | + zoomed.resize((in_targcharlen,in_alignment.shape[1])) |
| 55 | + |
| 56 | + return zoomed, pivot_points |
| 57 | + |
| 58 | + |
| 59 | +def gather_dist(in_mtr,in_points): |
| 60 | + #initialize with known size for fast |
| 61 | + full_coords = [(0,0) for x in range(in_mtr.shape[0] * in_mtr.shape[1])] |
| 62 | + i = 0 |
| 63 | + for x in range(0, in_mtr.shape[0]): |
| 64 | + for y in range(0, in_mtr.shape[1]): |
| 65 | + full_coords[i] = (x,y) |
| 66 | + i += 1 |
| 67 | + |
| 68 | + return cdist(full_coords, in_points,"euclidean") |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +def create_guided(in_align,in_pvt,looseness): |
| 74 | + new_att = np.ones(in_align.shape,dtype=np.float32) |
| 75 | + # It is dramatically faster that we first gather all the points and calculate than do it manually |
| 76 | + # for each point in for loop |
| 77 | + dist_arr = gather_dist(in_align,in_pvt) |
| 78 | + # Scale looseness based on attention size. (addition works better than mul). Also divide by 100 |
| 79 | + # because having user input 3.35 is nicer |
| 80 | + real_loose = (looseness / 100) * (new_att.shape[0] + new_att.shape[1]) |
| 81 | + g_idx = 0 |
| 82 | + for x in range(0, new_att.shape[0]): |
| 83 | + for y in range(0, new_att.shape[1]): |
| 84 | + min_point_idx = dist_arr[g_idx].argmin() |
| 85 | + |
| 86 | + closest_pvt = in_pvt[min_point_idx] |
| 87 | + distance = dist_arr[g_idx][min_point_idx] / real_loose |
| 88 | + distance = np.power(distance,2) |
| 89 | + |
| 90 | + g_idx += 1 |
| 91 | + |
| 92 | + new_att[x,y] = distance |
| 93 | + |
| 94 | + return np.clip(new_att,0.0,1.0) |
| 95 | + |
| 96 | +def get_pivot_points(in_att): |
| 97 | + ret_points = [] |
| 98 | + for x in range(0, in_att.shape[0]): |
| 99 | + for y in range(0, in_att.shape[1]): |
| 100 | + if in_att[x,y] > 0.8: |
| 101 | + ret_points.append((x,y)) |
| 102 | + return ret_points |
| 103 | + |
| 104 | +def main(): |
| 105 | + parser = argparse.ArgumentParser(description="Postprocess durations to become alignments") |
| 106 | + parser.add_argument( |
| 107 | + "--dump-dir", |
| 108 | + default="dump", |
| 109 | + type=str, |
| 110 | + help="Path of dump directory", |
| 111 | + ) |
| 112 | + parser.add_argument( |
| 113 | + "--looseness", |
| 114 | + default=3.5, |
| 115 | + type=float, |
| 116 | + help="Looseness of the generated guided attention map. Lower values = tighter", |
| 117 | + ) |
| 118 | + args = parser.parse_args() |
| 119 | + dump_dir = args.dump_dir |
| 120 | + dump_sets = ["train","valid"] |
| 121 | + |
| 122 | + for d_set in dump_sets: |
| 123 | + full_fol = os.path.join(dump_dir,d_set) |
| 124 | + align_path = os.path.join(full_fol,"alignments") |
| 125 | + |
| 126 | + ids_path = os.path.join(full_fol,"ids") |
| 127 | + durations_path = os.path.join(full_fol,"durations") |
| 128 | + |
| 129 | + safemkdir(align_path) |
| 130 | + |
| 131 | + for duration_fn in tqdm(os.listdir(durations_path)): |
| 132 | + if not ".npy" in duration_fn: |
| 133 | + continue |
| 134 | + |
| 135 | + id_fn = duration_fn.replace("-durations","-ids") |
| 136 | + |
| 137 | + id_path = os.path.join(ids_path,id_fn) |
| 138 | + duration_path = os.path.join(durations_path,duration_fn) |
| 139 | + |
| 140 | + duration_arr = np.load(duration_path) |
| 141 | + id_arr = np.load(id_path) |
| 142 | + |
| 143 | + id_true_size = len(id_arr) |
| 144 | + |
| 145 | + align = duration_to_alignment(duration_arr) |
| 146 | + |
| 147 | + if align.shape[0] != id_true_size: |
| 148 | + align, points = rescale_alignment(align,id_true_size) |
| 149 | + else: |
| 150 | + points = get_pivot_points(align) |
| 151 | + |
| 152 | + if len(points) == 0: |
| 153 | + print("WARNING points are empty for",id_fn) |
| 154 | + |
| 155 | + align = create_guided(align,points,args.looseness) |
| 156 | + |
| 157 | + |
| 158 | + align_fn = id_fn.replace("-ids","-alignment") |
| 159 | + align_full_fn = os.path.join(align_path,align_fn) |
| 160 | + |
| 161 | + np.save(align_full_fn,align.astype("float32")) |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | +if __name__ == "__main__": |
| 168 | + main() |
| 169 | + |
0 commit comments