response
stringlengths
1
33.1k
instruction
stringlengths
22
582k
Create indexes a for dataloader to read for training. When users have a new task and their own data, they need to create similar indexes. The indexes contain meta information of "where to find the data for training".
def create_indexes(args): """Create indexes a for dataloader to read for training. When users have a new task and their own data, they need to create similar indexes. The indexes contain meta information of "where to find the data for training". """ # Arguments & parameters waveforms_hdf5_path = args.waveforms_hdf5_path indexes_hdf5_path = args.indexes_hdf5_path # Paths create_folder(os.path.dirname(indexes_hdf5_path)) with h5py.File(waveforms_hdf5_path, 'r') as hr: with h5py.File(indexes_hdf5_path, 'w') as hw: audios_num = len(hr['audio_name']) hw.create_dataset('audio_name', data=hr['audio_name'][:], dtype='S20') hw.create_dataset('target', data=hr['target'][:], dtype=np.bool) hw.create_dataset('hdf5_path', data=[waveforms_hdf5_path.encode()] * audios_num, dtype='S200') hw.create_dataset('index_in_hdf5', data=np.arange(audios_num), dtype=np.int32) print('Write to {}'.format(indexes_hdf5_path))
Combine all balanced and unbalanced indexes hdf5s to a single hdf5. This combined indexes hdf5 is used for training with full data (~20k balanced audio clips + ~1.9m unbalanced audio clips).
def combine_full_indexes(args): """Combine all balanced and unbalanced indexes hdf5s to a single hdf5. This combined indexes hdf5 is used for training with full data (~20k balanced audio clips + ~1.9m unbalanced audio clips). """ # Arguments & parameters indexes_hdf5s_dir = args.indexes_hdf5s_dir full_indexes_hdf5_path = args.full_indexes_hdf5_path classes_num = config.classes_num # Paths paths = get_sub_filepaths(indexes_hdf5s_dir) paths = [path for path in paths if ( 'train' in path and 'full_train' not in path and 'mini' not in path)] print('Total {} hdf5 to combine.'.format(len(paths))) with h5py.File(full_indexes_hdf5_path, 'w') as full_hf: full_hf.create_dataset( name='audio_name', shape=(0,), maxshape=(None,), dtype='S20') full_hf.create_dataset( name='target', shape=(0, classes_num), maxshape=(None, classes_num), dtype=np.bool) full_hf.create_dataset( name='hdf5_path', shape=(0,), maxshape=(None,), dtype='S200') full_hf.create_dataset( name='index_in_hdf5', shape=(0,), maxshape=(None,), dtype=np.int32) for path in paths: with h5py.File(path, 'r') as part_hf: print(path) n = len(full_hf['audio_name'][:]) new_n = n + len(part_hf['audio_name'][:]) full_hf['audio_name'].resize((new_n,)) full_hf['audio_name'][n : new_n] = part_hf['audio_name'][:] full_hf['target'].resize((new_n, classes_num)) full_hf['target'][n : new_n] = part_hf['target'][:] full_hf['hdf5_path'].resize((new_n,)) full_hf['hdf5_path'][n : new_n] = part_hf['hdf5_path'][:] full_hf['index_in_hdf5'].resize((new_n,)) full_hf['index_in_hdf5'][n : new_n] = part_hf['index_in_hdf5'][:] print('Write combined full hdf5 to {}'.format(full_indexes_hdf5_path))
Split unbalanced csv to part csvs. Each part csv contains up to 50000 ids.
def split_unbalanced_csv_to_partial_csvs(args): """Split unbalanced csv to part csvs. Each part csv contains up to 50000 ids. """ unbalanced_csv_path = args.unbalanced_csv unbalanced_partial_csvs_dir = args.unbalanced_partial_csvs_dir create_folder(unbalanced_partial_csvs_dir) with open(unbalanced_csv_path, 'r') as f: lines = f.readlines() lines = lines[3:] # Remove head info audios_num_per_file = 50000 files_num = int(np.ceil(len(lines) / float(audios_num_per_file))) for r in range(files_num): lines_per_file = lines[r * audios_num_per_file : (r + 1) * audios_num_per_file] out_csv_path = os.path.join(unbalanced_partial_csvs_dir, 'unbalanced_train_segments_part{:02d}.csv'.format(r)) with open(out_csv_path, 'w') as f: f.write('empty\n') f.write('empty\n') f.write('empty\n') for line in lines_per_file: f.write(line) print('Write out csv to {}'.format(out_csv_path))
Download videos and extract audio in wav format.
def download_wavs(args): """Download videos and extract audio in wav format. """ # Paths csv_path = args.csv_path audios_dir = args.audios_dir mini_data = args.mini_data if mini_data: logs_dir = '_logs/download_dataset/{}'.format(get_filename(csv_path)) else: logs_dir = '_logs/download_dataset_minidata/{}'.format(get_filename(csv_path)) create_folder(audios_dir) create_folder(logs_dir) create_logging(logs_dir, filemode='w') logging.info('Download log is saved to {}'.format(logs_dir)) # Read csv with open(csv_path, 'r') as f: lines = f.readlines() lines = lines[3:] # Remove csv head info if mini_data: lines = lines[0 : 10] # Download partial data for debug download_time = time.time() # Download for (n, line) in enumerate(lines): items = line.split(', ') audio_id = items[0] start_time = float(items[1]) end_time = float(items[2]) duration = end_time - start_time logging.info('{} {} start_time: {:.1f}, end_time: {:.1f}'.format( n, audio_id, start_time, end_time)) # Download full video of whatever format video_name = os.path.join(audios_dir, '_Y{}.%(ext)s'.format(audio_id)) os.system("youtube-dl --quiet -o '{}' -x https://www.youtube.com/watch?v={}"\ .format(video_name, audio_id)) video_paths = glob.glob(os.path.join(audios_dir, '_Y' + audio_id + '.*')) # If download successful if len(video_paths) > 0: video_path = video_paths[0] # Choose one video # Add 'Y' to the head because some video ids are started with '-' # which will cause problem audio_path = os.path.join(audios_dir, 'Y' + audio_id + '.wav') # Extract audio in wav format os.system("ffmpeg -loglevel panic -i {} -ac 1 -ar 32000 -ss {} -t 00:00:{} {} "\ .format(video_path, str(datetime.timedelta(seconds=start_time)), duration, audio_path)) # Remove downloaded video os.system("rm {}".format(video_path)) logging.info("Download and convert to {}".format(audio_path)) logging.info('Download finished! Time spent: {:.3f} s'.format( time.time() - download_time)) logging.info('Logs can be viewed in {}'.format(logs_dir))
Pack waveform and target of several audio clips to a single hdf5 file. This can speed up loading and training.
def pack_waveforms_to_hdf5(args): """Pack waveform and target of several audio clips to a single hdf5 file. This can speed up loading and training. """ # Arguments & parameters audios_dir = args.audios_dir csv_path = args.csv_path waveforms_hdf5_path = args.waveforms_hdf5_path mini_data = args.mini_data clip_samples = config.clip_samples classes_num = config.classes_num sample_rate = config.sample_rate id_to_ix = config.id_to_ix # Paths if mini_data: prefix = 'mini_' waveforms_hdf5_path += '.mini' else: prefix = '' create_folder(os.path.dirname(waveforms_hdf5_path)) logs_dir = '_logs/pack_waveforms_to_hdf5/{}{}'.format(prefix, get_filename(csv_path)) create_folder(logs_dir) create_logging(logs_dir, filemode='w') logging.info('Write logs to {}'.format(logs_dir)) # Read csv file meta_dict = read_metadata(csv_path, classes_num, id_to_ix) if mini_data: mini_num = 10 for key in meta_dict.keys(): meta_dict[key] = meta_dict[key][0 : mini_num] audios_num = len(meta_dict['audio_name']) # Pack waveform to hdf5 total_time = time.time() with h5py.File(waveforms_hdf5_path, 'w') as hf: hf.create_dataset('audio_name', shape=((audios_num,)), dtype='S20') hf.create_dataset('waveform', shape=((audios_num, clip_samples)), dtype=np.int16) hf.create_dataset('target', shape=((audios_num, classes_num)), dtype=np.bool) hf.attrs.create('sample_rate', data=sample_rate, dtype=np.int32) # Pack waveform & target of several audio clips to a single hdf5 file for n in range(audios_num): audio_path = os.path.join(audios_dir, meta_dict['audio_name'][n]) if os.path.isfile(audio_path): logging.info('{} {}'.format(n, audio_path)) (audio, _) = librosa.core.load(audio_path, sr=sample_rate, mono=True) audio = pad_or_truncate(audio, clip_samples) hf['audio_name'][n] = meta_dict['audio_name'][n].encode() hf['waveform'][n] = float32_to_int16(audio) hf['target'][n] = meta_dict['target'][n] else: logging.info('{} File does not exist! {}'.format(n, audio_path)) logging.info('Write to {}'.format(waveforms_hdf5_path)) logging.info('Pack hdf5 time: {:.3f}'.format(time.time() - total_time))
Read audio names from black list.
def read_black_list(black_list_csv): """Read audio names from black list. """ with open(black_list_csv, 'r') as fr: reader = csv.reader(fr) lines = list(reader) black_list_names = ['Y{}.wav'.format(line[0]) for line in lines] return black_list_names
Collate data. Args: list_data_dict, e.g., [{'audio_name': str, 'waveform': (clip_samples,), ...}, {'audio_name': str, 'waveform': (clip_samples,), ...}, ...] Returns: np_data_dict, dict, e.g., {'audio_name': (batch_size,), 'waveform': (batch_size, clip_samples), ...}
def collate_fn(list_data_dict): """Collate data. Args: list_data_dict, e.g., [{'audio_name': str, 'waveform': (clip_samples,), ...}, {'audio_name': str, 'waveform': (clip_samples,), ...}, ...] Returns: np_data_dict, dict, e.g., {'audio_name': (batch_size,), 'waveform': (batch_size, clip_samples), ...} """ np_data_dict = {} for key in list_data_dict[0].keys(): np_data_dict[key] = np.array([data_dict[key] for data_dict in list_data_dict]) return np_data_dict
E.g., 1234567 -> 1,234,567
def add_comma(integer): """E.g., 1234567 -> 1,234,567 """ integer = int(integer) if integer >= 1000: return str(integer // 1000) + ',' + str(integer % 1000) else: return str(integer)
Read metadata of AudioSet from a csv file. Args: csv_path: str Returns: meta_dict: {'audio_name': (audios_num,), 'target': (audios_num, classes_num)}
def read_metadata(csv_path, classes_num, id_to_ix): """Read metadata of AudioSet from a csv file. Args: csv_path: str Returns: meta_dict: {'audio_name': (audios_num,), 'target': (audios_num, classes_num)} """ with open(csv_path, 'r') as fr: lines = fr.readlines() lines = lines[3:] # Remove heads audios_num = len(lines) targets = np.zeros((audios_num, classes_num), dtype=np.bool) audio_names = [] for n, line in enumerate(lines): items = line.split(', ') """items: ['--4gqARaEJE', '0.000', '10.000', '"/m/068hy,/m/07q6cd_,/m/0bt9lr,/m/0jbk"\n']""" audio_name = 'Y{}.wav'.format(items[0]) # Audios are started with an extra 'Y' when downloading label_ids = items[3].split('"')[1].split(',') audio_names.append(audio_name) # Target for id in label_ids: ix = id_to_ix[id] targets[n, ix] = 1 meta_dict = {'audio_name': np.array(audio_names), 'target': targets} return meta_dict
Pad all audio to specific length.
def pad_or_truncate(x, audio_length): """Pad all audio to specific length.""" if len(x) <= audio_length: return np.concatenate((x, np.zeros(audio_length - len(x))), axis=0) else: return x[0 : audio_length]
Load checkpoint from a file or URI. Args: model (Module): Module to load checkpoint. filename (str): Accept local filepath, URL, ``torchvision://xxx``, ``open-mmlab://xxx``. Please refer to ``docs/model_zoo.md`` for details. map_location (str): Same as :func:`torch.load`. strict (bool): Whether to allow different params for the model and checkpoint. logger (:mod:`logging.Logger` or None): The logger for error message. revise_keys (list): A list of customized keywords to modify the state_dict in checkpoint. Each item is a (pattern, replacement) pair of the regular expression operations. Default: strip the prefix 'module.' by [(r'^module\.', '')]. Returns: dict or OrderedDict: The loaded checkpoint.
def load_checkpoint(model, filename, map_location=None, strict=False, logger=None, revise_keys=[(r'^module\.', '')]): """Load checkpoint from a file or URI. Args: model (Module): Module to load checkpoint. filename (str): Accept local filepath, URL, ``torchvision://xxx``, ``open-mmlab://xxx``. Please refer to ``docs/model_zoo.md`` for details. map_location (str): Same as :func:`torch.load`. strict (bool): Whether to allow different params for the model and checkpoint. logger (:mod:`logging.Logger` or None): The logger for error message. revise_keys (list): A list of customized keywords to modify the state_dict in checkpoint. Each item is a (pattern, replacement) pair of the regular expression operations. Default: strip the prefix 'module.' by [(r'^module\\.', '')]. Returns: dict or OrderedDict: The loaded checkpoint. """ checkpoint = _load_checkpoint(filename, map_location, logger) ''' new_proj = torch.nn.Conv2d(1, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1)) new_proj.weight = torch.nn.Parameter(torch.sum(checkpoint['patch_embed1.proj.weight'], dim=1).unsqueeze(1)) checkpoint['patch_embed1.proj.weight'] = new_proj.weight new_proj.weight = torch.nn.Parameter(torch.sum(checkpoint['patch_embed1.proj.weight'], dim=2).unsqueeze(2).repeat(1,1,3,1)) checkpoint['patch_embed1.proj.weight'] = new_proj.weight new_proj.weight = torch.nn.Parameter(torch.sum(checkpoint['patch_embed1.proj.weight'], dim=3).unsqueeze(3).repeat(1,1,1,3)) checkpoint['patch_embed1.proj.weight'] = new_proj.weight ''' new_proj = torch.nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(4, 4), padding=(2, 2)) new_proj.weight = torch.nn.Parameter(torch.sum(checkpoint['patch_embed1.proj.weight'], dim=1).unsqueeze(1)) checkpoint['patch_embed1.proj.weight'] = new_proj.weight # OrderedDict is a subclass of dict if not isinstance(checkpoint, dict): raise RuntimeError( f'No state_dict found in checkpoint file {filename}') # get state_dict from checkpoint if 'state_dict' in checkpoint: state_dict = checkpoint['state_dict'] else: state_dict = checkpoint # strip prefix of state_dict metadata = getattr(state_dict, '_metadata', OrderedDict()) for p, r in revise_keys: state_dict = OrderedDict( {re.sub(p, r, k): v for k, v in state_dict.items()}) state_dict = OrderedDict({k.replace('backbone.',''):v for k,v in state_dict.items()}) # Keep metadata in state_dict state_dict._metadata = metadata # load state_dict load_state_dict(model, state_dict, strict, logger) return checkpoint
Initialize a Linear or Convolutional layer.
def init_layer(layer): """Initialize a Linear or Convolutional layer. """ nn.init.xavier_uniform_(layer.weight) if hasattr(layer, 'bias'): if layer.bias is not None: layer.bias.data.fill_(0.)
Initialize a Batchnorm layer.
def init_bn(bn): """Initialize a Batchnorm layer. """ bn.bias.data.fill_(0.) bn.weight.data.fill_(1.)
parse_poolingfunction A heler function to parse any temporal pooling Pooling is done on dimension 1 :param poolingfunction_name: :param **kwargs:
def parse_poolingfunction(poolingfunction_name='mean', **kwargs): """parse_poolingfunction A heler function to parse any temporal pooling Pooling is done on dimension 1 :param poolingfunction_name: :param **kwargs: """ poolingfunction_name = poolingfunction_name.lower() if poolingfunction_name == 'mean': return MeanPool(pooldim=1) elif poolingfunction_name == 'max': return MaxPool(pooldim=1) elif poolingfunction_name == 'linear': return LinearSoftPool(pooldim=1) elif poolingfunction_name == 'expalpha': return AutoExpPool(outputdim=kwargs['outputdim'], pooldim=1) elif poolingfunction_name == 'soft': return SoftPool(pooldim=1) elif poolingfunction_name == 'auto': return AutoPool(outputdim=kwargs['outputdim']) elif poolingfunction_name == 'attention': return AttentionPool(inputdim=kwargs['inputdim'], outputdim=kwargs['outputdim'])
parse_config_or_kwargs :param config_file: Config file that has parameters, yaml format :param **kwargs: Other alternative parameters or overwrites for config
def parse_config_or_kwargs(config_file, **kwargs): """parse_config_or_kwargs :param config_file: Config file that has parameters, yaml format :param **kwargs: Other alternative parameters or overwrites for config """ with open(config_file) as con_read: yaml_config = yaml.load(con_read, Loader=yaml.FullLoader) arguments = dict(yaml_config, **kwargs) return arguments
Find contiguous regions from bool valued numpy.array. Copy of https://dcase-repo.github.io/dcase_util/_modules/dcase_util/data/decisions.html#DecisionEncoder Reason is: 1. This does not belong to a class necessarily 2. Import DecisionEncoder requires sndfile over some other imports..which causes some problems on clusters
def find_contiguous_regions(activity_array): # in this part, if you cannot understand the binary operation, I think you can write a O(n) complexity method """Find contiguous regions from bool valued numpy.array. Copy of https://dcase-repo.github.io/dcase_util/_modules/dcase_util/data/decisions.html#DecisionEncoder Reason is: 1. This does not belong to a class necessarily 2. Import DecisionEncoder requires sndfile over some other imports..which causes some problems on clusters """ change_indices = np.logical_xor(activity_array[1:], activity_array[:-1]).nonzero()[0] change_indices += 1 if activity_array[0]: # If the first element of activity_array is True add 0 at the beginning change_indices = np.r_[0, change_indices] if activity_array[-1]: # If the last element of activity_array is True, add the length of the array change_indices = np.r_[change_indices, activity_array.size] # print(change_indices.reshape((-1, 2))) # Reshape the result into two columns return change_indices.reshape((-1, 2))
split_train_cv :param data_frame: :type data_frame: pd.DataFrame :param frac: :type frac: float
def split_train_cv( data_frame: pd.DataFrame, frac: float = 0.9, y=None, # Only for stratified, computes necessary split **kwargs): """split_train_cv :param data_frame: :type data_frame: pd.DataFrame :param frac: :type frac: float """ if kwargs.get('mode', None) == 'urbansed': # Filenames are DATA_-1 DATA_-2 etc data_frame.loc[:, 'id'] = data_frame.groupby( data_frame['filename'].str.split('_').apply( lambda x: '_'.join(x[:-1]))).ngroup() sampler = np.random.permutation(data_frame['id'].nunique()) num_train = int(frac * len(sampler)) train_indexes = sampler[:num_train] cv_indexes = sampler[num_train:] train_data = data_frame[data_frame['id'].isin(train_indexes)] cv_data = data_frame[data_frame['id'].isin(cv_indexes)] del train_data['id'] del cv_data['id'] elif kwargs.get('mode', None) == 'stratified': # stratified --> 分层的 ? # Use statified sampling from skmultilearn.model_selection import iterative_train_test_split index_train, _, index_cv, _ = iterative_train_test_split( data_frame.index.values.reshape(-1, 1), y, test_size=1. - frac) train_data = data_frame[data_frame.index.isin(index_train.squeeze())] cv_data = data_frame[data_frame.index.isin(index_cv.squeeze())] # cv --> cross validation else: # Simply split train_test train_data = data_frame.sample(frac=frac, random_state=10) cv_data = data_frame[~data_frame.index.isin(train_data.index)] return train_data, cv_data
pprint_dict :param outputfun: function to use, defaults to sys.stdout :param in_dict: dict to print
def pprint_dict(in_dict, outputfun=sys.stdout.write, formatter='yaml'): # print yaml file """pprint_dict :param outputfun: function to use, defaults to sys.stdout :param in_dict: dict to print """ if formatter == 'yaml': format_fun = yaml.dump elif formatter == 'pretty': format_fun = pformat for line in format_fun(in_dict).split('\n'): outputfun(line)
encode_labels Encodes labels :param labels: pd.Series representing the raw labels e.g., Speech, Water :param encoder (optional): Encoder already fitted returns encoded labels (many hot) and the encoder
def train_labelencoder(labels: pd.Series, sparse=True): """encode_labels Encodes labels :param labels: pd.Series representing the raw labels e.g., Speech, Water :param encoder (optional): Encoder already fitted returns encoded labels (many hot) and the encoder """ assert isinstance(labels, pd.Series), "Labels need to be series" if isinstance(labels[0], six.string_types): # In case of using non processed strings, e.g., Vaccum, Speech label_array = labels.str.split(',').values.tolist() # split label according to ',' elif isinstance(labels[0], np.ndarray): # Encoder does not like to see numpy array label_array = [lab.tolist() for lab in labels] elif isinstance(labels[0], collections.Iterable): label_array = labels encoder = pre.MultiLabelBinarizer(sparse_output=sparse) encoder.fit(label_array) return encoder
encode_labels Encodes labels :param labels: pd.Series representing the raw labels e.g., Speech, Water :param encoder (optional): Encoder already fitted returns encoded labels (many hot) and the encoder
def encode_labels(labels: pd.Series, encoder=None, sparse=True): """encode_labels Encodes labels :param labels: pd.Series representing the raw labels e.g., Speech, Water :param encoder (optional): Encoder already fitted returns encoded labels (many hot) and the encoder """ assert isinstance(labels, pd.Series), "Labels need to be series" instance = labels.iloc[0] if isinstance(instance, six.string_types): # In case of using non processed strings, e.g., Vaccum, Speech label_array = labels.str.split(',').values.tolist() elif isinstance(instance, np.ndarray): # Encoder does not like to see numpy array label_array = [lab.tolist() for lab in labels] elif isinstance(instance, collections.Iterable): label_array = labels # get label_array, it is a list ,contain a lot of label, this label are string type if not encoder: encoder = pre.MultiLabelBinarizer(sparse_output=sparse) # if we encoder is None, we should init a encoder firstly. encoder.fit(label_array) labels_encoded = encoder.transform(label_array) # transform string to digit return labels_encoded, encoder
decode_with_timestamps Decodes the predicted label array (2d) into a list of [(Labelname, onset, offset), ...] :param encoder: Encoder during training :type encoder: pre.MultiLabelBinarizer :param labels: n-dim array :type labels: np.array
def decode_with_timestamps(events,labels: np.array): """decode_with_timestamps Decodes the predicted label array (2d) into a list of [(Labelname, onset, offset), ...] :param encoder: Encoder during training :type encoder: pre.MultiLabelBinarizer :param labels: n-dim array :type labels: np.array """ # print('events ',events) # print('labels ',labels.shape) #assert 1==2 if labels.ndim == 2: #print('...') return [_decode_with_timestamps(events[i],labels[i]) for i in range(labels.shape[0])] else: return _decode_with_timestamps(events,labels)
median_filter :param x: input prediction array of shape (B, T, C) or (B, T). Input is a sequence of probabilities 0 <= x <= 1 :param window_size: An integer to use :param threshold: Binary thresholding threshold
def median_filter(x, window_size, threshold=0.5): """median_filter :param x: input prediction array of shape (B, T, C) or (B, T). Input is a sequence of probabilities 0 <= x <= 1 :param window_size: An integer to use :param threshold: Binary thresholding threshold """ x = binarize(x, threshold=threshold) # transfer to 0 or 1 if x.ndim == 3: size = (1, window_size, 1) elif x.ndim == 2 and x.shape[0] == 1: # Assume input is class-specific median filtering # E.g, Batch x Time [1, 501] size = (1, window_size) elif x.ndim == 2 and x.shape[0] > 1: # Assume input is standard median pooling, class-independent # E.g., Time x Class [501, 10] size = (window_size, 1) return scipy.ndimage.median_filter(x, size=size)
double_threshold Helper function to calculate double threshold for n-dim arrays :param x: input array :param high_thres: high threshold value :param low_thres: Low threshold value :param n_connect: Distance of <= n clusters will be merged
def double_threshold(x, high_thres, low_thres, n_connect=1): """double_threshold Helper function to calculate double threshold for n-dim arrays :param x: input array :param high_thres: high threshold value :param low_thres: Low threshold value :param n_connect: Distance of <= n clusters will be merged """ assert x.ndim <= 3, "Whoops something went wrong with the input ({}), check if its <= 3 dims".format( x.shape) if x.ndim == 3: apply_dim = 1 elif x.ndim < 3: apply_dim = 0 # x is assumed to be 3d: (batch, time, dim) # Assumed to be 2d : (time, dim) # Assumed to be 1d : (time) # time axis is therefore at 1 for 3d and 0 for 2d ( return np.apply_along_axis(lambda x: _double_threshold( x, high_thres, low_thres, n_connect=n_connect), axis=apply_dim, arr=x)
_double_threshold Computes a double threshold over the input array :param x: input array, needs to be 1d :param high_thres: High threshold over the array :param low_thres: Low threshold over the array :param n_connect: Postprocessing, maximal distance between clusters to connect :param return_arr: By default this function returns the filtered indiced, but if return_arr = True it returns an array of tsame size as x filled with ones and zeros.
def _double_threshold(x, high_thres, low_thres, n_connect=1, return_arr=True): # in nature, double_threshold considers boundary question """_double_threshold Computes a double threshold over the input array :param x: input array, needs to be 1d :param high_thres: High threshold over the array :param low_thres: Low threshold over the array :param n_connect: Postprocessing, maximal distance between clusters to connect :param return_arr: By default this function returns the filtered indiced, but if return_arr = True it returns an array of tsame size as x filled with ones and zeros. """ assert x.ndim == 1, "Input needs to be 1d" high_locations = np.where(x > high_thres)[0] # return the index, where value is greater than high_thres locations = x > low_thres # return true of false encoded_pairs = find_contiguous_regions(locations) # print('encoded_pairs ',encoded_pairs) filtered_list = list( filter( lambda pair: ((pair[0] <= high_locations) & (high_locations <= pair[1])).any(), encoded_pairs)) # find encoded_pair where inclide a high_lacations #print('filtered_list ',filtered_list) filtered_list = connect_(filtered_list, n_connect) # if the distance of two pair is less than n_connect, we can merge them if return_arr: zero_one_arr = np.zeros_like(x, dtype=int) for sl in filtered_list: zero_one_arr[sl[0]:sl[1]] = 1 return zero_one_arr return filtered_list
connect_clusters_ Connects clustered predictions (0,1) in x with range n :param x: Input array. zero-one format :param n: Number of frames to skip until connection can be made
def connect_clusters_(x, n=1): """connect_clusters_ Connects clustered predictions (0,1) in x with range n :param x: Input array. zero-one format :param n: Number of frames to skip until connection can be made """ assert x.ndim == 1, "input needs to be 1d" reg = find_contiguous_regions(x) start_end = connect_(reg, n=n) zero_one_arr = np.zeros_like(x, dtype=int) for sl in start_end: zero_one_arr[sl[0]:sl[1]] = 1 return zero_one_arr
connect_ Connects two adjacent clusters if their distance is <= n :param pairs: Clusters of iterateables e.g., [(1,5),(7,10)] :param n: distance between two clusters
def connect_(pairs, n=1): """connect_ Connects two adjacent clusters if their distance is <= n :param pairs: Clusters of iterateables e.g., [(1,5),(7,10)] :param n: distance between two clusters """ if len(pairs) == 0: return [] start_, end_ = pairs[0] new_pairs = [] for i, (next_item, cur_item) in enumerate(zip(pairs[1:], pairs[0:])): end_ = next_item[1] if next_item[0] - cur_item[1] <= n: pass else: new_pairs.append((start_, cur_item[1])) start_ = next_item[0] new_pairs.append((start_, end_)) return new_pairs
Initialize a Linear or Convolutional layer.
def init_layer(layer): """Initialize a Linear or Convolutional layer. """ nn.init.xavier_uniform_(layer.weight) if hasattr(layer, 'bias'): if layer.bias is not None: layer.bias.data.fill_(0.)
Initialize a Batchnorm layer.
def init_bn(bn): """Initialize a Batchnorm layer. """ bn.bias.data.fill_(0.) bn.weight.data.fill_(1.)
parse_poolingfunction A heler function to parse any temporal pooling Pooling is done on dimension 1 :param poolingfunction_name: :param **kwargs:
def parse_poolingfunction(poolingfunction_name='mean', **kwargs): """parse_poolingfunction A heler function to parse any temporal pooling Pooling is done on dimension 1 :param poolingfunction_name: :param **kwargs: """ poolingfunction_name = poolingfunction_name.lower() if poolingfunction_name == 'mean': return MeanPool(pooldim=1) elif poolingfunction_name == 'linear': return LinearSoftPool(pooldim=1) elif poolingfunction_name == 'attention': return AttentionPool(inputdim=kwargs['inputdim'], outputdim=kwargs['outputdim'])
features: [N, T, ...] (assume the second dimension represents length) lens: [N,]
def mean_with_lens(features, lens): """ features: [N, T, ...] (assume the second dimension represents length) lens: [N,] """ lens = torch.as_tensor(lens) if max(lens) != features.size(1): max_length = features.size(1) mask = generate_length_mask(lens, max_length) else: mask = generate_length_mask(lens) mask = mask.to(features.device) # [N, T] while mask.ndim < features.ndim: mask = mask.unsqueeze(-1) feature_mean = features * mask feature_mean = feature_mean.sum(1) while lens.ndim < feature_mean.ndim: lens = lens.unsqueeze(1) feature_mean = feature_mean / lens.to(features.device) # feature_mean = features * mask.unsqueeze(-1) # feature_mean = feature_mean.sum(1) / lens.unsqueeze(1).to(features.device) return feature_mean
features: [N, T, ...] (assume the second dimension represents length) lens: [N,]
def max_with_lens(features, lens): """ features: [N, T, ...] (assume the second dimension represents length) lens: [N,] """ lens = torch.as_tensor(lens) mask = generate_length_mask(lens).to(features.device) # [N, T] feature_max = features.clone() feature_max[~mask] = float("-inf") feature_max, _ = feature_max.max(1) return feature_max
Build vocabulary from csv file with a given threshold to drop all counts < threshold Args: input_json(string): Preprossessed json file. Structure like this: { 'audios': [ { 'audio_id': 'xxx', 'captions': [ { 'caption': 'xxx', 'cap_id': 'xxx' } ] }, ... ] } threshold (int): Threshold to drop all words with counts < threshold keep_punctuation (bool): Includes or excludes punctuation. Returns: vocab (Vocab): Object with the processed vocabulary
def build_vocab(input_json: str, threshold: int, keep_punctuation: bool, host_address: str, character_level: bool = False, zh: bool = True ): """Build vocabulary from csv file with a given threshold to drop all counts < threshold Args: input_json(string): Preprossessed json file. Structure like this: { 'audios': [ { 'audio_id': 'xxx', 'captions': [ { 'caption': 'xxx', 'cap_id': 'xxx' } ] }, ... ] } threshold (int): Threshold to drop all words with counts < threshold keep_punctuation (bool): Includes or excludes punctuation. Returns: vocab (Vocab): Object with the processed vocabulary """ data = json.load(open(input_json, "r"))["audios"] counter = Counter() pretokenized = "tokens" in data[0]["captions"][0] if zh: from nltk.parse.corenlp import CoreNLPParser from zhon.hanzi import punctuation if not pretokenized: parser = CoreNLPParser(host_address) for audio_idx in tqdm(range(len(data)), leave=False, ascii=True): for cap_idx in range(len(data[audio_idx]["captions"])): if pretokenized: tokens = data[audio_idx]["captions"][cap_idx]["tokens"].split() else: caption = data[audio_idx]["captions"][cap_idx]["caption"] # Remove all punctuations if not keep_punctuation: caption = re.sub("[{}]".format(punctuation), "", caption) if character_level: tokens = list(caption) else: tokens = list(parser.tokenize(caption)) data[audio_idx]["captions"][cap_idx]["tokens"] = " ".join(tokens) counter.update(tokens) else: if pretokenized: for audio_idx in tqdm(range(len(data)), leave=False, ascii=True): for cap_idx in range(len(data[audio_idx]["captions"])): tokens = data[audio_idx]["captions"][cap_idx]["tokens"].split() counter.update(tokens) else: from pycocoevalcap.tokenizer.ptbtokenizer import PTBTokenizer captions = {} for audio_idx in range(len(data)): audio_id = data[audio_idx]["audio_id"] captions[audio_id] = [] for cap_idx in range(len(data[audio_idx]["captions"])): caption = data[audio_idx]["captions"][cap_idx]["caption"] captions[audio_id].append({ "audio_id": audio_id, "id": cap_idx, "caption": caption }) tokenizer = PTBTokenizer() captions = tokenizer.tokenize(captions) for audio_idx in tqdm(range(len(data)), leave=False, ascii=True): audio_id = data[audio_idx]["audio_id"] for cap_idx in range(len(data[audio_idx]["captions"])): tokens = captions[audio_id][cap_idx] data[audio_idx]["captions"][cap_idx]["tokens"] = tokens counter.update(tokens.split(" ")) if not pretokenized: json.dump({ "audios": data }, open(input_json, "w"), indent=4, ensure_ascii=not zh) words = [word for word, cnt in counter.items() if cnt >= threshold] # Create a vocab wrapper and add some special tokens. vocab = Vocabulary() vocab.add_word("<pad>") vocab.add_word("<start>") vocab.add_word("<end>") vocab.add_word("<unk>") # Add the words to the vocabulary. for word in words: vocab.add_word(word) return vocab
Build vocabulary from csv file with a given threshold to drop all counts < threshold Args: input_json(string): Preprossessed json file. Structure like this: { 'audios': [ { 'audio_id': 'xxx', 'captions': [ { 'caption': 'xxx', 'cap_id': 'xxx' } ] }, ... ] } threshold (int): Threshold to drop all words with counts < threshold keep_punctuation (bool): Includes or excludes punctuation. Returns: vocab (Vocab): Object with the processed vocabulary
def build_vocab(input_json: str, output_json: str, threshold: int, keep_punctuation: bool, character_level: bool = False, zh: bool = True ): """Build vocabulary from csv file with a given threshold to drop all counts < threshold Args: input_json(string): Preprossessed json file. Structure like this: { 'audios': [ { 'audio_id': 'xxx', 'captions': [ { 'caption': 'xxx', 'cap_id': 'xxx' } ] }, ... ] } threshold (int): Threshold to drop all words with counts < threshold keep_punctuation (bool): Includes or excludes punctuation. Returns: vocab (Vocab): Object with the processed vocabulary """ data = json.load(open(input_json, "r"))["audios"] counter = Counter() pretokenized = "tokens" in data[0]["captions"][0] if zh: from ltp import LTP from zhon.hanzi import punctuation if not pretokenized: parser = LTP("base") for audio_idx in tqdm(range(len(data)), leave=False, ascii=True): for cap_idx in range(len(data[audio_idx]["captions"])): if pretokenized: tokens = data[audio_idx]["captions"][cap_idx]["tokens"].split() else: caption = data[audio_idx]["captions"][cap_idx]["caption"] if character_level: tokens = list(caption) else: tokens, _ = parser.seg([caption]) tokens = tokens[0] # Remove all punctuations if not keep_punctuation: tokens = [token for token in tokens if token not in punctuation] data[audio_idx]["captions"][cap_idx]["tokens"] = " ".join(tokens) counter.update(tokens) else: if pretokenized: for audio_idx in tqdm(range(len(data)), leave=False, ascii=True): for cap_idx in range(len(data[audio_idx]["captions"])): tokens = data[audio_idx]["captions"][cap_idx]["tokens"].split() counter.update(tokens) else: from pycocoevalcap.tokenizer.ptbtokenizer import PTBTokenizer captions = {} for audio_idx in range(len(data)): audio_id = data[audio_idx]["audio_id"] captions[audio_id] = [] for cap_idx in range(len(data[audio_idx]["captions"])): caption = data[audio_idx]["captions"][cap_idx]["caption"] captions[audio_id].append({ "audio_id": audio_id, "id": cap_idx, "caption": caption }) tokenizer = PTBTokenizer() captions = tokenizer.tokenize(captions) for audio_idx in tqdm(range(len(data)), leave=False, ascii=True): audio_id = data[audio_idx]["audio_id"] for cap_idx in range(len(data[audio_idx]["captions"])): tokens = captions[audio_id][cap_idx] data[audio_idx]["captions"][cap_idx]["tokens"] = tokens counter.update(tokens.split(" ")) if not pretokenized: if output_json is None: output_json = input_json json.dump({ "audios": data }, open(output_json, "w"), indent=4, ensure_ascii=not zh) words = [word for word, cnt in counter.items() if cnt >= threshold] # Create a vocab wrapper and add some special tokens. vocab = Vocabulary() vocab.add_word("<pad>") vocab.add_word("<start>") vocab.add_word("<end>") vocab.add_word("<unk>") # Add the words to the vocabulary. for word in words: vocab.add_word(word) return vocab
Build vocabulary from csv file with a given threshold to drop all counts < threshold Args: input_json(string): Preprossessed json file. Structure like this: { 'audios': [ { 'audio_id': 'xxx', 'captions': [ { 'caption': 'xxx', 'cap_id': 'xxx' } ] }, ... ] } threshold (int): Threshold to drop all words with counts < threshold keep_punctuation (bool): Includes or excludes punctuation. Returns: vocab (Vocab): Object with the processed vocabulary
def build_vocab(input_json: str, output_json: str, threshold: int, keep_punctuation: bool, host_address: str, character_level: bool = False, retokenize: bool = True, zh: bool = True ): """Build vocabulary from csv file with a given threshold to drop all counts < threshold Args: input_json(string): Preprossessed json file. Structure like this: { 'audios': [ { 'audio_id': 'xxx', 'captions': [ { 'caption': 'xxx', 'cap_id': 'xxx' } ] }, ... ] } threshold (int): Threshold to drop all words with counts < threshold keep_punctuation (bool): Includes or excludes punctuation. Returns: vocab (Vocab): Object with the processed vocabulary """ data = json.load(open(input_json, "r"))["audios"] counter = Counter() if retokenize: pretokenized = False else: pretokenized = "tokens" in data[0]["captions"][0] if zh: from nltk.parse.corenlp import CoreNLPParser from zhon.hanzi import punctuation if not pretokenized: parser = CoreNLPParser(host_address) for audio_idx in tqdm(range(len(data)), leave=False, ascii=True): for cap_idx in range(len(data[audio_idx]["captions"])): if pretokenized: tokens = data[audio_idx]["captions"][cap_idx]["tokens"].split() else: caption = data[audio_idx]["captions"][cap_idx]["caption"] # Remove all punctuations if not keep_punctuation: caption = re.sub("[{}]".format(punctuation), "", caption) if character_level: tokens = list(caption) else: tokens = list(parser.tokenize(caption)) data[audio_idx]["captions"][cap_idx]["tokens"] = " ".join(tokens) counter.update(tokens) else: if pretokenized: for audio_idx in tqdm(range(len(data)), leave=False, ascii=True): for cap_idx in range(len(data[audio_idx]["captions"])): tokens = data[audio_idx]["captions"][cap_idx]["tokens"].split() counter.update(tokens) else: import spacy tokenizer = spacy.load("en_core_web_sm", disable=["parser", "ner"]) for audio_idx in tqdm(range(len(data)), leave=False, ascii=True): captions = data[audio_idx]["captions"] for cap_idx in range(len(captions)): caption = captions[cap_idx]["caption"] doc = tokenizer(caption) tokens = " ".join([str(token).lower() for token in doc]) data[audio_idx]["captions"][cap_idx]["tokens"] = tokens counter.update(tokens.split(" ")) if not pretokenized: if output_json is None: json.dump({ "audios": data }, open(input_json, "w"), indent=4, ensure_ascii=not zh) else: json.dump({ "audios": data }, open(output_json, "w"), indent=4, ensure_ascii=not zh) words = [word for word, cnt in counter.items() if cnt >= threshold] # Create a vocab wrapper and add some special tokens. vocab = Vocabulary() vocab.add_word("<pad>") vocab.add_word("<start>") vocab.add_word("<end>") vocab.add_word("<unk>") # Add the words to the vocabulary. for word in words: vocab.add_word(word) return vocab
Build vocabulary from csv file with a given threshold to drop all counts < threshold Args: input_json(string): Preprossessed json file. Structure like this: { 'audios': [ { 'audio_id': 'xxx', 'captions': [ { 'caption': 'xxx', 'cap_id': 'xxx' } ] }, ... ] } threshold (int): Threshold to drop all words with counts < threshold keep_punctuation (bool): Includes or excludes punctuation. Returns: vocab (Vocab): Object with the processed vocabulary
def tokenize_caption(input_json: str, keep_punctuation: bool = False, host_address: str = None, character_level: bool = False, zh: bool = True, output_json: str = None): """Build vocabulary from csv file with a given threshold to drop all counts < threshold Args: input_json(string): Preprossessed json file. Structure like this: { 'audios': [ { 'audio_id': 'xxx', 'captions': [ { 'caption': 'xxx', 'cap_id': 'xxx' } ] }, ... ] } threshold (int): Threshold to drop all words with counts < threshold keep_punctuation (bool): Includes or excludes punctuation. Returns: vocab (Vocab): Object with the processed vocabulary """ data = json.load(open(input_json, "r"))["audios"] if zh: from nltk.parse.corenlp import CoreNLPParser from zhon.hanzi import punctuation parser = CoreNLPParser(host_address) for audio_idx in tqdm(range(len(data)), leave=False, ascii=True): for cap_idx in range(len(data[audio_idx]["captions"])): caption = data[audio_idx]["captions"][cap_idx]["caption"] # Remove all punctuations if not keep_punctuation: caption = re.sub("[{}]".format(punctuation), "", caption) if character_level: tokens = list(caption) else: tokens = list(parser.tokenize(caption)) data[audio_idx]["captions"][cap_idx]["tokens"] = " ".join(tokens) else: from pycocoevalcap.tokenizer.ptbtokenizer import PTBTokenizer captions = {} for audio_idx in range(len(data)): audio_id = data[audio_idx]["audio_id"] captions[audio_id] = [] for cap_idx in range(len(data[audio_idx]["captions"])): caption = data[audio_idx]["captions"][cap_idx]["caption"] captions[audio_id].append({ "audio_id": audio_id, "id": cap_idx, "caption": caption }) tokenizer = PTBTokenizer() captions = tokenizer.tokenize(captions) for audio_idx in tqdm(range(len(data)), leave=False, ascii=True): audio_id = data[audio_idx]["audio_id"] for cap_idx in range(len(data[audio_idx]["captions"])): tokens = captions[audio_id][cap_idx] data[audio_idx]["captions"][cap_idx]["tokens"] = tokens if output_json: json.dump( { "audios": data }, open(output_json, "w"), indent=4, ensure_ascii=not zh) else: json.dump( { "audios": data }, open(input_json, "w"), indent=4, ensure_ascii=not zh)
pprint_dict :param outputfun: function to use, defaults to sys.stdout :param in_dict: dict to print
def pprint_dict(in_dict, outputfun=sys.stdout.write, formatter='yaml'): """pprint_dict :param outputfun: function to use, defaults to sys.stdout :param in_dict: dict to print """ if formatter == 'yaml': format_fun = yaml.dump elif formatter == 'pretty': format_fun = pformat for line in format_fun(in_dict).split('\n'): outputfun(line)
Ensures that segments without voice in the waveform remain no longer than a threshold determined by the VAD parameters in params.py. :param wav: the raw waveform as a numpy array of floats :param vad_max_silence_length: Maximum number of consecutive silent frames a segment can have. :return: the same waveform with silences trimmed away (length <= original wav length)
def trim_long_silences(path, sr=None, return_raw_wav=False, norm=True, vad_max_silence_length=12): """ Ensures that segments without voice in the waveform remain no longer than a threshold determined by the VAD parameters in params.py. :param wav: the raw waveform as a numpy array of floats :param vad_max_silence_length: Maximum number of consecutive silent frames a segment can have. :return: the same waveform with silences trimmed away (length <= original wav length) """ ## Voice Activation Detection # Window size of the VAD. Must be either 10, 20 or 30 milliseconds. # This sets the granularity of the VAD. Should not need to be changed. sampling_rate = 16000 wav_raw, sr = librosa.core.load(path, sr=sr) if norm: meter = pyln.Meter(sr) # create BS.1770 meter loudness = meter.integrated_loudness(wav_raw) wav_raw = pyln.normalize.loudness(wav_raw, loudness, -20.0) if np.abs(wav_raw).max() > 1.0: wav_raw = wav_raw / np.abs(wav_raw).max() wav = librosa.resample(wav_raw, sr, sampling_rate, res_type='kaiser_best') vad_window_length = 30 # In milliseconds # Number of frames to average together when performing the moving average smoothing. # The larger this value, the larger the VAD variations must be to not get smoothed out. vad_moving_average_width = 8 # Compute the voice detection window size samples_per_window = (vad_window_length * sampling_rate) // 1000 # Trim the end of the audio to have a multiple of the window size wav = wav[:len(wav) - (len(wav) % samples_per_window)] # Convert the float waveform to 16-bit mono PCM pcm_wave = struct.pack("%dh" % len(wav), *(np.round(wav * int16_max)).astype(np.int16)) # Perform voice activation detection voice_flags = [] vad = webrtcvad.Vad(mode=3) for window_start in range(0, len(wav), samples_per_window): window_end = window_start + samples_per_window voice_flags.append(vad.is_speech(pcm_wave[window_start * 2:window_end * 2], sample_rate=sampling_rate)) voice_flags = np.array(voice_flags) # Smooth the voice detection with a moving average def moving_average(array, width): array_padded = np.concatenate((np.zeros((width - 1) // 2), array, np.zeros(width // 2))) ret = np.cumsum(array_padded, dtype=float) ret[width:] = ret[width:] - ret[:-width] return ret[width - 1:] / width audio_mask = moving_average(voice_flags, vad_moving_average_width) audio_mask = np.round(audio_mask).astype(np.bool) # Dilate the voiced regions audio_mask = binary_dilation(audio_mask, np.ones(vad_max_silence_length + 1)) audio_mask = np.repeat(audio_mask, samples_per_window) audio_mask = resize(audio_mask, (len(wav_raw),)) > 0 if return_raw_wav: return wav_raw, audio_mask, sr return wav_raw[audio_mask], audio_mask, sr
:param wav_data: [T] :param mel: [T, 80] :param hparams: :return:
def get_pitch(wav_data, mel, hparams): """ :param wav_data: [T] :param mel: [T, 80] :param hparams: :return: """ time_step = hparams['hop_size'] / hparams['audio_sample_rate'] * 1000 f0_min = 80 f0_max = 750 if hparams['hop_size'] == 128: pad_size = 4 elif hparams['hop_size'] == 256: pad_size = 2 else: assert False f0 = parselmouth.Sound(wav_data, hparams['audio_sample_rate']).to_pitch_ac( time_step=time_step / 1000, voicing_threshold=0.6, pitch_floor=f0_min, pitch_ceiling=f0_max).selected_array['frequency'] lpad = pad_size * 2 rpad = len(mel) - len(f0) - lpad f0 = np.pad(f0, [[lpad, rpad]], mode='constant') # mel and f0 are extracted by 2 different libraries. we should force them to have the same length. # Attention: we find that new version of some libraries could cause ``rpad'' to be a negetive value... # Just to be sure, we recommend users to set up the same environments as them in requirements_auto.txt (by Anaconda) delta_l = len(mel) - len(f0) assert np.abs(delta_l) <= 8 if delta_l > 0: f0 = np.concatenate([f0, [f0[-1]] * delta_l], 0) f0 = f0[:len(mel)] pitch_coarse = f0_to_coarse(f0) return f0, pitch_coarse
remove empty lines
def remove_empty_lines(text): """remove empty lines""" assert (len(text) > 0) assert (isinstance(text, list)) text = [t.strip() for t in text] if "" in text: text.remove("") return text
Applies the preprocessing operations used in training the Speaker Encoder to a waveform either on disk or in memory. The waveform will be resampled to match the data hyperparameters. :param fpath_or_wav: either a filepath to an audio file (many extensions are supported, not just .wav), either the waveform as a numpy array of floats. :param source_sr: if passing an audio waveform, the sampling rate of the waveform before preprocessing. After preprocessing, the waveform's sampling rate will match the data hyperparameters. If passing a filepath, the sampling rate will be automatically detected and this argument will be ignored.
def preprocess_wav(fpath_or_wav: Union[str, Path, np.ndarray], source_sr: Optional[int] = None): """ Applies the preprocessing operations used in training the Speaker Encoder to a waveform either on disk or in memory. The waveform will be resampled to match the data hyperparameters. :param fpath_or_wav: either a filepath to an audio file (many extensions are supported, not just .wav), either the waveform as a numpy array of floats. :param source_sr: if passing an audio waveform, the sampling rate of the waveform before preprocessing. After preprocessing, the waveform's sampling rate will match the data hyperparameters. If passing a filepath, the sampling rate will be automatically detected and this argument will be ignored. """ # Load the wav from disk if needed if isinstance(fpath_or_wav, str) or isinstance(fpath_or_wav, Path): wav, source_sr = librosa.load(str(fpath_or_wav), sr=None) else: wav = fpath_or_wav # Resample the wav if needed if source_sr is not None and source_sr != sampling_rate: wav = librosa.resample(wav, source_sr, sampling_rate) # Apply the preprocessing: normalize volume and shorten long silences wav = normalize_volume(wav, audio_norm_target_dBFS, increase_only=True) wav = trim_long_silences(wav) return wav
Derives a mel spectrogram ready to be used by the encoder from a preprocessed audio waveform. Note: this not a log-mel spectrogram.
def wav_to_mel_spectrogram(wav): """ Derives a mel spectrogram ready to be used by the encoder from a preprocessed audio waveform. Note: this not a log-mel spectrogram. """ frames = librosa.feature.melspectrogram( wav, sampling_rate, n_fft=int(sampling_rate * mel_window_length / 1000), hop_length=int(sampling_rate * mel_window_step / 1000), n_mels=mel_n_channels ) return frames.astype(np.float32).T
Ensures that segments without voice in the waveform remain no longer than a threshold determined by the VAD parameters in params.py. :param wav: the raw waveform as a numpy array of floats :return: the same waveform with silences trimmed away (length <= original wav length)
def trim_long_silences(wav): """ Ensures that segments without voice in the waveform remain no longer than a threshold determined by the VAD parameters in params.py. :param wav: the raw waveform as a numpy array of floats :return: the same waveform with silences trimmed away (length <= original wav length) """ # Compute the voice detection window size samples_per_window = (vad_window_length * sampling_rate) // 1000 # Trim the end of the audio to have a multiple of the window size wav = wav[:len(wav) - (len(wav) % samples_per_window)] # Convert the float waveform to 16-bit mono PCM pcm_wave = struct.pack("%dh" % len(wav), *(np.round(wav * int16_max)).astype(np.int16)) # Perform voice activation detection voice_flags = [] vad = webrtcvad.Vad(mode=3) for window_start in range(0, len(wav), samples_per_window): window_end = window_start + samples_per_window voice_flags.append(vad.is_speech(pcm_wave[window_start * 2:window_end * 2], sample_rate=sampling_rate)) voice_flags = np.array(voice_flags) # Smooth the voice detection with a moving average def moving_average(array, width): array_padded = np.concatenate((np.zeros((width - 1) // 2), array, np.zeros(width // 2))) ret = np.cumsum(array_padded, dtype=float) ret[width:] = ret[width:] - ret[:-width] return ret[width - 1:] / width audio_mask = moving_average(voice_flags, vad_moving_average_width) audio_mask = np.round(audio_mask).astype(np.bool) # Dilate the voiced regions audio_mask = binary_dilation(audio_mask, np.ones(vad_max_silence_length + 1)) audio_mask = np.repeat(audio_mask, samples_per_window) return wav[audio_mask == True]
Loads the model in memory. If this function is not explicitely called, it will be run on the first call to embed_frames() with the default weights file. :param weights_fpath: the path to saved model weights. :param device: either a torch device or the name of a torch device (e.g. "cpu", "cuda"). The model will be loaded and will run on this device. Outputs will however always be on the cpu. If None, will default to your GPU if it"s available, otherwise your CPU.
def load_model(weights_fpath: Path, device=None): """ Loads the model in memory. If this function is not explicitely called, it will be run on the first call to embed_frames() with the default weights file. :param weights_fpath: the path to saved model weights. :param device: either a torch device or the name of a torch device (e.g. "cpu", "cuda"). The model will be loaded and will run on this device. Outputs will however always be on the cpu. If None, will default to your GPU if it"s available, otherwise your CPU. """ # TODO: I think the slow loading of the encoder might have something to do with the device it # was saved on. Worth investigating. global _model, _device if device is None: _device = torch.device("cuda" if torch.cuda.is_available() else "cpu") elif isinstance(device, str): _device = torch.device(device) _model = EmotionEncoder(_device, torch.device("cpu")) checkpoint = torch.load(weights_fpath) _model.load_state_dict(checkpoint["model_state"]) _model.eval() print("Loaded encoder trained to step %d" % (checkpoint["step"]))
Computes embeddings for a batch of mel spectrogram. :param frames_batch: a batch mel of spectrogram as a numpy array of float32 of shape (batch_size, n_frames, n_channels) :return: the embeddings as a numpy array of float32 of shape (batch_size, model_embedding_size)
def embed_frames_batch(frames_batch): """ Computes embeddings for a batch of mel spectrogram. :param frames_batch: a batch mel of spectrogram as a numpy array of float32 of shape (batch_size, n_frames, n_channels) :return: the embeddings as a numpy array of float32 of shape (batch_size, model_embedding_size) """ if _model is None: raise Exception("Model was not loaded. Call load_model() before inference.") frames = torch.from_numpy(frames_batch).to(_device) embed = _model.inference(frames).detach().cpu().numpy() return embed
Computes where to split an utterance waveform and its corresponding mel spectrogram to obtain partial utterances of <partial_utterance_n_frames> each. Both the waveform and the mel spectrogram slices are returned, so as to make each partial utterance waveform correspond to its spectrogram. This function assumes that the mel spectrogram parameters used are those defined in params_data.py. The returned ranges may be indexing further than the length of the waveform. It is recommended that you pad the waveform with zeros up to wave_slices[-1].stop. :param n_samples: the number of samples in the waveform :param partial_utterance_n_frames: the number of mel spectrogram frames in each partial utterance :param min_pad_coverage: when reaching the last partial utterance, it may or may not have enough frames. If at least <min_pad_coverage> of <partial_utterance_n_frames> are present, then the last partial utterance will be considered, as if we padded the audio. Otherwise, it will be discarded, as if we trimmed the audio. If there aren't enough frames for 1 partial utterance, this parameter is ignored so that the function always returns at least 1 slice. :param overlap: by how much the partial utterance should overlap. If set to 0, the partial utterances are entirely disjoint. :return: the waveform slices and mel spectrogram slices as lists of array slices. Index respectively the waveform and the mel spectrogram with these slices to obtain the partial utterances.
def compute_partial_slices(n_samples, partial_utterance_n_frames=partials_n_frames, min_pad_coverage=0.75, overlap=0.5): """ Computes where to split an utterance waveform and its corresponding mel spectrogram to obtain partial utterances of <partial_utterance_n_frames> each. Both the waveform and the mel spectrogram slices are returned, so as to make each partial utterance waveform correspond to its spectrogram. This function assumes that the mel spectrogram parameters used are those defined in params_data.py. The returned ranges may be indexing further than the length of the waveform. It is recommended that you pad the waveform with zeros up to wave_slices[-1].stop. :param n_samples: the number of samples in the waveform :param partial_utterance_n_frames: the number of mel spectrogram frames in each partial utterance :param min_pad_coverage: when reaching the last partial utterance, it may or may not have enough frames. If at least <min_pad_coverage> of <partial_utterance_n_frames> are present, then the last partial utterance will be considered, as if we padded the audio. Otherwise, it will be discarded, as if we trimmed the audio. If there aren't enough frames for 1 partial utterance, this parameter is ignored so that the function always returns at least 1 slice. :param overlap: by how much the partial utterance should overlap. If set to 0, the partial utterances are entirely disjoint. :return: the waveform slices and mel spectrogram slices as lists of array slices. Index respectively the waveform and the mel spectrogram with these slices to obtain the partial utterances. """ assert 0 <= overlap < 1 assert 0 < min_pad_coverage <= 1 samples_per_frame = int((sampling_rate * mel_window_step / 1000)) n_frames = int(np.ceil((n_samples + 1) / samples_per_frame)) frame_step = max(int(np.round(partial_utterance_n_frames * (1 - overlap))), 1) # Compute the slices wav_slices, mel_slices = [], [] steps = max(1, n_frames - partial_utterance_n_frames + frame_step + 1) for i in range(0, steps, frame_step): mel_range = np.array([i, i + partial_utterance_n_frames]) wav_range = mel_range * samples_per_frame mel_slices.append(slice(*mel_range)) wav_slices.append(slice(*wav_range)) # Evaluate whether extra padding is warranted or not last_wav_range = wav_slices[-1] coverage = (n_samples - last_wav_range.start) / (last_wav_range.stop - last_wav_range.start) if coverage < min_pad_coverage and len(mel_slices) > 1: mel_slices = mel_slices[:-1] wav_slices = wav_slices[:-1] return wav_slices, mel_slices
Computes an embedding for a single utterance. # TODO: handle multiple wavs to benefit from batching on GPU :param wav: a preprocessed (see audio.py) utterance waveform as a numpy array of float32 :param using_partials: if True, then the utterance is split in partial utterances of <partial_utterance_n_frames> frames and the utterance embedding is computed from their normalized average. If False, the utterance is instead computed from feeding the entire spectogram to the network. :param return_partials: if True, the partial embeddings will also be returned along with the wav slices that correspond to the partial embeddings. :param kwargs: additional arguments to compute_partial_splits() :return: the embedding as a numpy array of float32 of shape (model_embedding_size,). If <return_partials> is True, the partial utterances as a numpy array of float32 of shape (n_partials, model_embedding_size) and the wav partials as a list of slices will also be returned. If <using_partials> is simultaneously set to False, both these values will be None instead.
def embed_utterance(wav, using_partials=True, return_partials=False, **kwargs): """ Computes an embedding for a single utterance. # TODO: handle multiple wavs to benefit from batching on GPU :param wav: a preprocessed (see audio.py) utterance waveform as a numpy array of float32 :param using_partials: if True, then the utterance is split in partial utterances of <partial_utterance_n_frames> frames and the utterance embedding is computed from their normalized average. If False, the utterance is instead computed from feeding the entire spectogram to the network. :param return_partials: if True, the partial embeddings will also be returned along with the wav slices that correspond to the partial embeddings. :param kwargs: additional arguments to compute_partial_splits() :return: the embedding as a numpy array of float32 of shape (model_embedding_size,). If <return_partials> is True, the partial utterances as a numpy array of float32 of shape (n_partials, model_embedding_size) and the wav partials as a list of slices will also be returned. If <using_partials> is simultaneously set to False, both these values will be None instead. """ # Process the entire utterance if not using partials if not using_partials: frames = audio.wav_to_mel_spectrogram(wav) embed = embed_frames_batch(frames[None, ...])[0] if return_partials: return embed, None, None return embed # Compute where to split the utterance into partials and pad if necessary wave_slices, mel_slices = compute_partial_slices(len(wav), **kwargs) max_wave_length = wave_slices[-1].stop if max_wave_length >= len(wav): wav = np.pad(wav, (0, max_wave_length - len(wav)), "constant") # Split the utterance into partials frames = audio.wav_to_mel_spectrogram(wav) frames_batch = np.array([frames[s] for s in mel_slices]) partial_embeds = embed_frames_batch(frames_batch) # Compute the utterance embedding from the partial embeddings raw_embed = np.mean(partial_embeds, axis=0) embed = raw_embed / np.linalg.norm(raw_embed, 2) if return_partials: return embed, partial_embeds, wave_slices return embed
cosine schedule as proposed in https://openreview.net/forum?id=-NEXDKk8gZ
def cosine_beta_schedule(timesteps, s=0.008): """ cosine schedule as proposed in https://openreview.net/forum?id=-NEXDKk8gZ """ steps = timesteps + 1 x = np.linspace(0, steps, steps) alphas_cumprod = np.cos(((x / steps) + s) / (1 + s) * np.pi * 0.5) ** 2 alphas_cumprod = alphas_cumprod / alphas_cumprod[0] betas = 1 - (alphas_cumprod[1:] / alphas_cumprod[:-1]) return np.clip(betas, a_min=0, a_max=0.999)
linear schedule
def linear_beta_schedule(timesteps, max_beta=hparams.get('max_beta', 0.01)): """ linear schedule """ betas = np.linspace(1e-4, max_beta, timesteps) return betas
cosine schedule as proposed in https://openreview.net/forum?id=-NEXDKk8gZ
def cosine_beta_schedule(timesteps, s=0.008): """ cosine schedule as proposed in https://openreview.net/forum?id=-NEXDKk8gZ """ steps = timesteps + 1 x = np.linspace(0, steps, steps) alphas_cumprod = np.cos(((x / steps) + s) / (1 + s) * np.pi * 0.5) ** 2 alphas_cumprod = alphas_cumprod / alphas_cumprod[0] betas = 1 - (alphas_cumprod[1:] / alphas_cumprod[:-1]) return np.clip(betas, a_min=0, a_max=0.999)
Design prototype filter for PQMF. This method is based on `A Kaiser window approach for the design of prototype filters of cosine modulated filterbanks`_. Args: taps (int): The number of filter taps. cutoff_ratio (float): Cut-off frequency ratio. beta (float): Beta coefficient for kaiser window. Returns: ndarray: Impluse response of prototype filter (taps + 1,). .. _`A Kaiser window approach for the design of prototype filters of cosine modulated filterbanks`: https://ieeexplore.ieee.org/abstract/document/681427
def design_prototype_filter(taps=62, cutoff_ratio=0.15, beta=9.0): """Design prototype filter for PQMF. This method is based on `A Kaiser window approach for the design of prototype filters of cosine modulated filterbanks`_. Args: taps (int): The number of filter taps. cutoff_ratio (float): Cut-off frequency ratio. beta (float): Beta coefficient for kaiser window. Returns: ndarray: Impluse response of prototype filter (taps + 1,). .. _`A Kaiser window approach for the design of prototype filters of cosine modulated filterbanks`: https://ieeexplore.ieee.org/abstract/document/681427 """ # check the arguments are valid assert taps % 2 == 0, "The number of taps mush be even number." assert 0.0 < cutoff_ratio < 1.0, "Cutoff ratio must be > 0.0 and < 1.0." # make initial filter omega_c = np.pi * cutoff_ratio with np.errstate(invalid='ignore'): h_i = np.sin(omega_c * (np.arange(taps + 1) - 0.5 * taps)) \ / (np.pi * (np.arange(taps + 1) - 0.5 * taps)) h_i[taps // 2] = np.cos(0) * cutoff_ratio # fix nan due to indeterminate form # apply kaiser window w = kaiser(taps + 1, beta) h = h_i * w return h
Perform STFT and convert to magnitude spectrogram. Args: x (Tensor): Input signal tensor (B, T). fft_size (int): FFT size. hop_size (int): Hop size. win_length (int): Window length. window (str): Window function type. Returns: Tensor: Magnitude spectrogram (B, #frames, fft_size // 2 + 1).
def stft(x, fft_size, hop_size, win_length, window): """Perform STFT and convert to magnitude spectrogram. Args: x (Tensor): Input signal tensor (B, T). fft_size (int): FFT size. hop_size (int): Hop size. win_length (int): Window length. window (str): Window function type. Returns: Tensor: Magnitude spectrogram (B, #frames, fft_size // 2 + 1). """ x_stft = torch.stft(x, fft_size, hop_size, win_length, window) real = x_stft[..., 0] imag = x_stft[..., 1] # NOTE(kan-bayashi): clamp is needed to avoid nan or inf return torch.sqrt(torch.clamp(real ** 2 + imag ** 2, min=1e-7)).transpose(2, 1)
Find files recursively. Args: root_dir (str): Root root_dir to find. query (str): Query to find. include_root_dir (bool): If False, root_dir name is not included. Returns: list: List of found filenames.
def find_files(root_dir, query="*.wav", include_root_dir=True): """Find files recursively. Args: root_dir (str): Root root_dir to find. query (str): Query to find. include_root_dir (bool): If False, root_dir name is not included. Returns: list: List of found filenames. """ files = [] for root, dirnames, filenames in os.walk(root_dir, followlinks=True): for filename in fnmatch.filter(filenames, query): files.append(os.path.join(root, filename)) if not include_root_dir: files = [file_.replace(root_dir + "/", "") for file_ in files] return files
Read hdf5 dataset. Args: hdf5_name (str): Filename of hdf5 file. hdf5_path (str): Dataset name in hdf5 file. Return: any: Dataset values.
def read_hdf5(hdf5_name, hdf5_path): """Read hdf5 dataset. Args: hdf5_name (str): Filename of hdf5 file. hdf5_path (str): Dataset name in hdf5 file. Return: any: Dataset values. """ if not os.path.exists(hdf5_name): logging.error(f"There is no such a hdf5 file ({hdf5_name}).") sys.exit(1) hdf5_file = h5py.File(hdf5_name, "r") if hdf5_path not in hdf5_file: logging.error(f"There is no such a data in hdf5 file. ({hdf5_path})") sys.exit(1) hdf5_data = hdf5_file[hdf5_path][()] hdf5_file.close() return hdf5_data
Write dataset to hdf5. Args: hdf5_name (str): Hdf5 dataset filename. hdf5_path (str): Dataset path in hdf5. write_data (ndarray): Data to write. is_overwrite (bool): Whether to overwrite dataset.
def write_hdf5(hdf5_name, hdf5_path, write_data, is_overwrite=True): """Write dataset to hdf5. Args: hdf5_name (str): Hdf5 dataset filename. hdf5_path (str): Dataset path in hdf5. write_data (ndarray): Data to write. is_overwrite (bool): Whether to overwrite dataset. """ # convert to numpy array write_data = np.array(write_data) # check folder existence folder_name, _ = os.path.split(hdf5_name) if not os.path.exists(folder_name) and len(folder_name) != 0: os.makedirs(folder_name) # check hdf5 existence if os.path.exists(hdf5_name): # if already exists, open with r+ mode hdf5_file = h5py.File(hdf5_name, "r+") # check dataset existence if hdf5_path in hdf5_file: if is_overwrite: logging.warning("Dataset in hdf5 file already exists. " "recreate dataset in hdf5.") hdf5_file.__delitem__(hdf5_path) else: logging.error("Dataset in hdf5 file already exists. " "if you want to overwrite, please set is_overwrite = True.") hdf5_file.close() sys.exit(1) else: # if not exists, open with w mode hdf5_file = h5py.File(hdf5_name, "w") # write data to hdf5 hdf5_file.create_dataset(hdf5_path, data=write_data) hdf5_file.flush() hdf5_file.close()
labels = {idx: word for idx,word in enumerate(sentence.split(" ")) }
def plot_dgl_sentence_graph(dgl_graph, labels): """ labels = {idx: word for idx,word in enumerate(sentence.split(" ")) } """ import matplotlib.pyplot as plt nx_graph = dgl_graph.to_networkx() pos = nx.random_layout(nx_graph) nx.draw(nx_graph, pos, with_labels=False) nx.draw_networkx_labels(nx_graph, pos, labels) plt.show()
:param h: [B, T, H] :param seg_ids: [B, T] :return: h_ph: [B, T_ph, H]
def group_hidden_by_segs(h, seg_ids, max_len): """ :param h: [B, T, H] :param seg_ids: [B, T] :return: h_ph: [B, T_ph, H] """ B, T, H = h.shape h_gby_segs = h.new_zeros([B, max_len + 1, H]).scatter_add_(1, seg_ids[:, :, None].repeat([1, 1, H]), h) all_ones = h.new_ones(h.shape[:2]) cnt_gby_segs = h.new_zeros([B, max_len + 1]).scatter_add_(1, seg_ids, all_ones).contiguous() h_gby_segs = h_gby_segs[:, 1:] cnt_gby_segs = cnt_gby_segs[:, 1:] h_gby_segs = h_gby_segs / torch.clamp(cnt_gby_segs[:, :, None], min=1) # assert h_gby_segs.shape[-1] == 192 return h_gby_segs
compute right padding (final frame) or both sides padding (first and final frames)
def librosa_pad_lr(x, fsize, fshift, pad_sides=1): '''compute right padding (final frame) or both sides padding (first and final frames) ''' assert pad_sides in (1, 2) # return int(fsize // 2) pad = (x.shape[0] // fshift + 1) * fshift - x.shape[0] if pad_sides == 1: return 0, pad else: return pad // 2, pad // 2 + pad % 2
CONVERT F0 TO CONTINUOUS F0 Args: f0 (ndarray): original f0 sequence with the shape (T) Return: (ndarray): continuous f0 with the shape (T)
def convert_continuos_f0(f0): '''CONVERT F0 TO CONTINUOUS F0 Args: f0 (ndarray): original f0 sequence with the shape (T) Return: (ndarray): continuous f0 with the shape (T) ''' # get uv information as binary f0 = np.copy(f0) uv = np.float32(f0 != 0) # get start and end of f0 if (f0 == 0).all(): print("| all of the f0 values are 0.") return uv, f0 start_f0 = f0[f0 != 0][0] end_f0 = f0[f0 != 0][-1] # padding start and end of f0 sequence start_idx = np.where(f0 == start_f0)[0][0] end_idx = np.where(f0 == end_f0)[0][-1] f0[:start_idx] = start_f0 f0[end_idx:] = end_f0 # get non-zero frame index nz_frames = np.where(f0 != 0)[0] # perform linear interpolation f = interp1d(nz_frames, f0[nz_frames]) cont_f0 = f(np.arange(0, f0.shape[0])) return uv, cont_f0
input: signal of shape (N) output: Wavelet_lf0 of shape(10, N), scales of shape(10)
def get_lf0_cwt(lf0): ''' input: signal of shape (N) output: Wavelet_lf0 of shape(10, N), scales of shape(10) ''' mother = wavelet.MexicanHat() dt = 0.005 dj = 1 s0 = dt * 2 J = 9 Wavelet_lf0, scales, _, _, _, _ = wavelet.cwt(np.squeeze(lf0), dt, dj, s0, J, mother) # Wavelet.shape => (J + 1, len(lf0)) Wavelet_lf0 = np.real(Wavelet_lf0).T return Wavelet_lf0, scales
Computes Dynamic Time Warping (DTW) of two sequences. :param array x: N1*M array :param array y: N2*M array :param func dist: distance used as cost measure :param int warp: how many shifts are computed. :param int w: window size limiting the maximal distance between indices of matched entries |i,j|. :param float s: weight applied on off-diagonal moves of the path. As s gets larger, the warping path is increasingly biased towards the diagonal Returns the minimum distance, the cost matrix, the accumulated cost matrix, and the wrap path.
def dtw(x, y, dist, warp=1, w=inf, s=1.0): """ Computes Dynamic Time Warping (DTW) of two sequences. :param array x: N1*M array :param array y: N2*M array :param func dist: distance used as cost measure :param int warp: how many shifts are computed. :param int w: window size limiting the maximal distance between indices of matched entries |i,j|. :param float s: weight applied on off-diagonal moves of the path. As s gets larger, the warping path is increasingly biased towards the diagonal Returns the minimum distance, the cost matrix, the accumulated cost matrix, and the wrap path. """ assert len(x) assert len(y) assert isinf(w) or (w >= abs(len(x) - len(y))) assert s > 0 r, c = len(x), len(y) if not isinf(w): D0 = full((r + 1, c + 1), inf) for i in range(1, r + 1): D0[i, max(1, i - w):min(c + 1, i + w + 1)] = 0 D0[0, 0] = 0 else: D0 = zeros((r + 1, c + 1)) D0[0, 1:] = inf D0[1:, 0] = inf D1 = D0[1:, 1:] # view for i in range(r): for j in range(c): if (isinf(w) or (max(0, i - w) <= j <= min(c, i + w))): D1[i, j] = dist(x[i], y[j]) C = D1.copy() jrange = range(c) for i in range(r): if not isinf(w): jrange = range(max(0, i - w), min(c, i + w + 1)) for j in jrange: min_list = [D0[i, j]] for k in range(1, warp + 1): i_k = min(i + k, r) j_k = min(j + k, c) min_list += [D0[i_k, j] * s, D0[i, j_k] * s] D1[i, j] += min(min_list) if len(x) == 1: path = zeros(len(y)), range(len(y)) elif len(y) == 1: path = range(len(x)), zeros(len(x)) else: path = _traceback(D0) return D1[-1, -1], C, D1, path
Computes Dynamic Time Warping (DTW) of two sequences in a faster way. Instead of iterating through each element and calculating each distance, this uses the cdist function from scipy (https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cdist.html) :param array x: N1*M array :param array y: N2*M array :param string or func dist: distance parameter for cdist. When string is given, cdist uses optimized functions for the distance metrics. If a string is passed, the distance function can be 'braycurtis', 'canberra', 'chebyshev', 'cityblock', 'correlation', 'cosine', 'dice', 'euclidean', 'hamming', 'jaccard', 'kulsinski', 'mahalanobis', 'matching', 'minkowski', 'rogerstanimoto', 'russellrao', 'seuclidean', 'sokalmichener', 'sokalsneath', 'sqeuclidean', 'wminkowski', 'yule'. :param int warp: how many shifts are computed. Returns the minimum distance, the cost matrix, the accumulated cost matrix, and the wrap path.
def accelerated_dtw(x, y, dist, warp=1): """ Computes Dynamic Time Warping (DTW) of two sequences in a faster way. Instead of iterating through each element and calculating each distance, this uses the cdist function from scipy (https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cdist.html) :param array x: N1*M array :param array y: N2*M array :param string or func dist: distance parameter for cdist. When string is given, cdist uses optimized functions for the distance metrics. If a string is passed, the distance function can be 'braycurtis', 'canberra', 'chebyshev', 'cityblock', 'correlation', 'cosine', 'dice', 'euclidean', 'hamming', 'jaccard', 'kulsinski', 'mahalanobis', 'matching', 'minkowski', 'rogerstanimoto', 'russellrao', 'seuclidean', 'sokalmichener', 'sokalsneath', 'sqeuclidean', 'wminkowski', 'yule'. :param int warp: how many shifts are computed. Returns the minimum distance, the cost matrix, the accumulated cost matrix, and the wrap path. """ assert len(x) assert len(y) if ndim(x) == 1: x = x.reshape(-1, 1) if ndim(y) == 1: y = y.reshape(-1, 1) r, c = len(x), len(y) D0 = zeros((r + 1, c + 1)) D0[0, 1:] = inf D0[1:, 0] = inf D1 = D0[1:, 1:] D0[1:, 1:] = cdist(x, y, dist) C = D1.copy() for i in range(r): for j in range(c): min_list = [D0[i, j]] for k in range(1, warp + 1): min_list += [D0[min(i + k, r), j], D0[i, min(j + k, c)]] D1[i, j] += min(min_list) if len(x) == 1: path = zeros(len(y)), range(len(y)) elif len(y) == 1: path = range(len(x)), zeros(len(x)) else: path = _traceback(D0) return D1[-1, -1], C, D1, path
Decorator to make any fx with this use the lazy property :param fn: :return:
def data_loader(fn): """ Decorator to make any fx with this use the lazy property :param fn: :return: """ wraps(fn) attr_name = '_lazy_' + fn.__name__ def _get_data_loader(self): try: value = getattr(self, attr_name) except AttributeError: try: value = fn(self) # Lazy evaluation, done only once. if ( value is not None and not isinstance(value, list) and fn.__name__ in ['test_dataloader', 'val_dataloader'] ): value = [value] except AttributeError as e: # Guard against AttributeError suppression. (Issue #142) traceback.print_exc() error = f'{fn.__name__}: An AttributeError was encountered: ' + str(e) raise RuntimeError(error) from e setattr(self, attr_name, value) # Memoize evaluation. return value return _get_data_loader
Applies each `module` in :attr:`modules` in parallel on arguments contained in :attr:`inputs` (positional) and :attr:`kwargs_tup` (keyword) on each of :attr:`devices`. Args: modules (Module): modules to be parallelized inputs (tensor): inputs to the modules devices (list of int or torch.device): CUDA devices :attr:`modules`, :attr:`inputs`, :attr:`kwargs_tup` (if given), and :attr:`devices` (if given) should all have same length. Moreover, each element of :attr:`inputs` can either be a single object as the only argument to a module, or a collection of positional arguments.
def parallel_apply(modules, inputs, kwargs_tup=None, devices=None): # pragma: no cover r"""Applies each `module` in :attr:`modules` in parallel on arguments contained in :attr:`inputs` (positional) and :attr:`kwargs_tup` (keyword) on each of :attr:`devices`. Args: modules (Module): modules to be parallelized inputs (tensor): inputs to the modules devices (list of int or torch.device): CUDA devices :attr:`modules`, :attr:`inputs`, :attr:`kwargs_tup` (if given), and :attr:`devices` (if given) should all have same length. Moreover, each element of :attr:`inputs` can either be a single object as the only argument to a module, or a collection of positional arguments. """ assert len(modules) == len(inputs) if kwargs_tup is not None: assert len(modules) == len(kwargs_tup) else: kwargs_tup = ({},) * len(modules) if devices is not None: assert len(modules) == len(devices) else: devices = [None] * len(modules) devices = list(map(lambda x: _get_device_index(x, True), devices)) lock = threading.Lock() results = {} grad_enabled = torch.is_grad_enabled() def _worker(i, module, input, kwargs, device=None): torch.set_grad_enabled(grad_enabled) if device is None: device = get_a_var(input).get_device() try: with torch.cuda.device(device): # this also avoids accidental slicing of `input` if it is a Tensor if not isinstance(input, (list, tuple)): input = (input,) # --------------- # CHANGE if module.training: output = module.training_step(*input, **kwargs) elif module.testing: output = module.test_step(*input, **kwargs) else: output = module.validation_step(*input, **kwargs) # --------------- with lock: results[i] = output except Exception as e: with lock: results[i] = e # make sure each module knows what training state it's in... # fixes weird bug where copies are out of sync root_m = modules[0] for m in modules[1:]: m.training = root_m.training m.testing = root_m.testing if len(modules) > 1: threads = [threading.Thread(target=_worker, args=(i, module, input, kwargs, device)) for i, (module, input, kwargs, device) in enumerate(zip(modules, inputs, kwargs_tup, devices))] for thread in threads: thread.start() for thread in threads: thread.join() else: _worker(0, modules[0], inputs[0], kwargs_tup[0], devices[0]) outputs = [] for i in range(len(inputs)): output = results[i] if isinstance(output, Exception): raise output outputs.append(output) return outputs
Recursively find all tensors contained in the specified object.
def _find_tensors(obj): # pragma: no cover r""" Recursively find all tensors contained in the specified object. """ if isinstance(obj, torch.Tensor): return [obj] if isinstance(obj, (list, tuple)): return itertools.chain(*map(_find_tensors, obj)) if isinstance(obj, dict): return itertools.chain(*map(_find_tensors, obj.values())) return []
Strip ids_to_strip from the end ids.
def strip_ids(ids, ids_to_strip): """Strip ids_to_strip from the end ids.""" ids = list(ids) while ids and ids[-1] in ids_to_strip: ids.pop() return ids
根据数字系统类型返回创建相应的数字系统,默认为 mid NUMBERING_TYPES = ['low', 'mid', 'high']: 中文数字系统类型 low: '兆' = '亿' * '十' = $10^{9}$, '京' = '兆' * '十', etc. mid: '兆' = '亿' * '万' = $10^{12}$, '京' = '兆' * '万', etc. high: '兆' = '亿' * '亿' = $10^{16}$, '京' = '兆' * '兆', etc. 返回对应的数字系统
def create_system(numbering_type=NUMBERING_TYPES[1]): """ 根据数字系统类型返回创建相应的数字系统,默认为 mid NUMBERING_TYPES = ['low', 'mid', 'high']: 中文数字系统类型 low: '兆' = '亿' * '十' = $10^{9}$, '京' = '兆' * '十', etc. mid: '兆' = '亿' * '万' = $10^{12}$, '京' = '兆' * '万', etc. high: '兆' = '亿' * '亿' = $10^{16}$, '京' = '兆' * '兆', etc. 返回对应的数字系统 """ # chinese number units of '亿' and larger all_larger_units = zip( LARGER_CHINESE_NUMERING_UNITS_SIMPLIFIED, LARGER_CHINESE_NUMERING_UNITS_TRADITIONAL) larger_units = [CNU.create(i, v, numbering_type, False) for i, v in enumerate(all_larger_units)] # chinese number units of '十, 百, 千, 万' all_smaller_units = zip( SMALLER_CHINESE_NUMERING_UNITS_SIMPLIFIED, SMALLER_CHINESE_NUMERING_UNITS_TRADITIONAL) smaller_units = [CNU.create(i, v, small_unit=True) for i, v in enumerate(all_smaller_units)] # digis chinese_digis = zip(CHINESE_DIGIS, CHINESE_DIGIS, BIG_CHINESE_DIGIS_SIMPLIFIED, BIG_CHINESE_DIGIS_TRADITIONAL) digits = [CND.create(i, v) for i, v in enumerate(chinese_digis)] digits[0].alt_s, digits[0].alt_t = ZERO_ALT, ZERO_ALT digits[1].alt_s, digits[1].alt_t = ONE_ALT, ONE_ALT digits[2].alt_s, digits[2].alt_t = TWO_ALTS[0], TWO_ALTS[1] # symbols positive_cn = CM(POSITIVE[0], POSITIVE[1], '+', lambda x: x) negative_cn = CM(NEGATIVE[0], NEGATIVE[1], '-', lambda x: -x) point_cn = CM(POINT[0], POINT[1], '.', lambda x, y: float(str(x) + '.' + str(y))) # sil_cn = CM(SIL[0], SIL[1], '-', lambda x, y: float(str(x) + '-' + str(y))) system = NumberSystem() system.units = smaller_units + larger_units system.digits = digits system.math = MathSymbol(positive_cn, negative_cn, point_cn) # system.symbols = OtherSymbol(sil_cn) return system
Replace non-padding symbols with their position numbers. Position numbers begin at padding_idx+1. Padding symbols are ignored.
def make_positions(tensor, padding_idx): """Replace non-padding symbols with their position numbers. Position numbers begin at padding_idx+1. Padding symbols are ignored. """ # The series of casts and type-conversions here are carefully # balanced to both work with ONNX export and XLA. In particular XLA # prefers ints, cumsum defaults to output longs, and ONNX doesn't know # how to handle the dtype kwarg in cumsum. mask = tensor.ne(padding_idx).int() return ( torch.cumsum(mask, dim=1).type_as(mask) * mask ).long() + padding_idx
Helper for getting incremental state for an nn.Module.
def get_incremental_state(module, incremental_state, key): """Helper for getting incremental state for an nn.Module.""" full_key = _get_full_incremental_state_key(module, key) if incremental_state is None or full_key not in incremental_state: return None return incremental_state[full_key]
Helper for setting incremental state for an nn.Module.
def set_incremental_state(module, incremental_state, key, value): """Helper for setting incremental state for an nn.Module.""" if incremental_state is not None: full_key = _get_full_incremental_state_key(module, key) incremental_state[full_key] = value
FP16-compatible function that fills a tensor with -inf.
def fill_with_neg_inf(t): """FP16-compatible function that fills a tensor with -inf.""" return t.float().fill_(float('-inf')).type_as(t)
FP16-compatible function that fills a tensor with -inf.
def fill_with_neg_inf2(t): """FP16-compatible function that fills a tensor with -inf.""" return t.float().fill_(-1e8).type_as(t)
attn: bs x L_t x L_s
def get_focus_rate(attn, src_padding_mask=None, tgt_padding_mask=None): ''' attn: bs x L_t x L_s ''' if src_padding_mask is not None: attn = attn * (1 - src_padding_mask.float())[:, None, :] if tgt_padding_mask is not None: attn = attn * (1 - tgt_padding_mask.float())[:, :, None] focus_rate = attn.max(-1).values.sum(-1) focus_rate = focus_rate / attn.sum(-1).sum(-1) return focus_rate
attn: bs x L_t x L_s
def get_phone_coverage_rate(attn, src_padding_mask=None, src_seg_mask=None, tgt_padding_mask=None): ''' attn: bs x L_t x L_s ''' src_mask = attn.new(attn.size(0), attn.size(-1)).bool().fill_(False) if src_padding_mask is not None: src_mask |= src_padding_mask if src_seg_mask is not None: src_mask |= src_seg_mask attn = attn * (1 - src_mask.float())[:, None, :] if tgt_padding_mask is not None: attn = attn * (1 - tgt_padding_mask.float())[:, :, None] phone_coverage_rate = attn.max(1).values.sum(-1) # phone_coverage_rate = phone_coverage_rate / attn.sum(-1).sum(-1) phone_coverage_rate = phone_coverage_rate / (1 - src_mask.float()).sum(-1) return phone_coverage_rate
attn: bx x L_t x L_s attn_ks: shape: tensor with shape [batch_size], input_lens/output_lens diagonal: y=k*x (k=attn_ks, x:output, y:input) 1 0 0 0 1 0 0 0 1 y>=k*(x-width) and y<=k*(x+width):1 else:0
def get_diagonal_focus_rate(attn, attn_ks, target_len, src_padding_mask=None, tgt_padding_mask=None, band_mask_factor=5, band_width=50): ''' attn: bx x L_t x L_s attn_ks: shape: tensor with shape [batch_size], input_lens/output_lens diagonal: y=k*x (k=attn_ks, x:output, y:input) 1 0 0 0 1 0 0 0 1 y>=k*(x-width) and y<=k*(x+width):1 else:0 ''' # width = min(target_len/band_mask_factor, 50) width1 = target_len / band_mask_factor width2 = target_len.new(target_len.size()).fill_(band_width) width = torch.where(width1 < width2, width1, width2).float() base = torch.ones(attn.size()).to(attn.device) zero = torch.zeros(attn.size()).to(attn.device) x = torch.arange(0, attn.size(1)).to(attn.device)[None, :, None].float() * base y = torch.arange(0, attn.size(2)).to(attn.device)[None, None, :].float() * base cond = (y - attn_ks[:, None, None] * x) cond1 = cond + attn_ks[:, None, None] * width[:, None, None] cond2 = cond - attn_ks[:, None, None] * width[:, None, None] mask1 = torch.where(cond1 < 0, zero, base) mask2 = torch.where(cond2 > 0, zero, base) mask = mask1 * mask2 if src_padding_mask is not None: attn = attn * (1 - src_padding_mask.float())[:, None, :] if tgt_padding_mask is not None: attn = attn * (1 - tgt_padding_mask.float())[:, :, None] diagonal_attn = attn * mask diagonal_focus_rate = diagonal_attn.sum(-1).sum(-1) / attn.sum(-1).sum(-1) return diagonal_focus_rate, mask
:param attn_logits: [n_layers, B, n_head, T_sp, T_txt] :return:
def select_attn(attn_logits, type='best'): """ :param attn_logits: [n_layers, B, n_head, T_sp, T_txt] :return: """ encdec_attn = torch.stack(attn_logits, 0).transpose(1, 2) # [n_layers * n_head, B, T_sp, T_txt] encdec_attn = (encdec_attn.reshape([-1, *encdec_attn.shape[2:]])).softmax(-1) if type == 'best': indices = encdec_attn.max(-1).values.sum(-1).argmax(0) encdec_attn = encdec_attn.gather( 0, indices[None, :, None, None].repeat(1, 1, encdec_attn.size(-2), encdec_attn.size(-1)))[0] return encdec_attn elif type == 'mean': return encdec_attn.mean(0)
Make mask tensor containing indices of padded part. Args: lengths (LongTensor or List): Batch of lengths (B,). xs (Tensor, optional): The reference tensor. If set, masks will be the same shape as this tensor. length_dim (int, optional): Dimension indicator of the above tensor. See the example. Returns: Tensor: Mask tensor containing indices of padded part. dtype=torch.uint8 in PyTorch 1.2- dtype=torch.bool in PyTorch 1.2+ (including 1.2) Examples: With only lengths. >>> lengths = [5, 3, 2] >>> make_non_pad_mask(lengths) masks = [[0, 0, 0, 0 ,0], [0, 0, 0, 1, 1], [0, 0, 1, 1, 1]] With the reference tensor. >>> xs = torch.zeros((3, 2, 4)) >>> make_pad_mask(lengths, xs) tensor([[[0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 1], [0, 0, 0, 1]], [[0, 0, 1, 1], [0, 0, 1, 1]]], dtype=torch.uint8) >>> xs = torch.zeros((3, 2, 6)) >>> make_pad_mask(lengths, xs) tensor([[[0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1]], [[0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1]], [[0, 0, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1]]], dtype=torch.uint8) With the reference tensor and dimension indicator. >>> xs = torch.zeros((3, 6, 6)) >>> make_pad_mask(lengths, xs, 1) tensor([[[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1]], [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]], [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]], dtype=torch.uint8) >>> make_pad_mask(lengths, xs, 2) tensor([[[0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1]], [[0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1]], [[0, 0, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1]]], dtype=torch.uint8)
def make_pad_mask(lengths, xs=None, length_dim=-1): """Make mask tensor containing indices of padded part. Args: lengths (LongTensor or List): Batch of lengths (B,). xs (Tensor, optional): The reference tensor. If set, masks will be the same shape as this tensor. length_dim (int, optional): Dimension indicator of the above tensor. See the example. Returns: Tensor: Mask tensor containing indices of padded part. dtype=torch.uint8 in PyTorch 1.2- dtype=torch.bool in PyTorch 1.2+ (including 1.2) Examples: With only lengths. >>> lengths = [5, 3, 2] >>> make_non_pad_mask(lengths) masks = [[0, 0, 0, 0 ,0], [0, 0, 0, 1, 1], [0, 0, 1, 1, 1]] With the reference tensor. >>> xs = torch.zeros((3, 2, 4)) >>> make_pad_mask(lengths, xs) tensor([[[0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 1], [0, 0, 0, 1]], [[0, 0, 1, 1], [0, 0, 1, 1]]], dtype=torch.uint8) >>> xs = torch.zeros((3, 2, 6)) >>> make_pad_mask(lengths, xs) tensor([[[0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1]], [[0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1]], [[0, 0, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1]]], dtype=torch.uint8) With the reference tensor and dimension indicator. >>> xs = torch.zeros((3, 6, 6)) >>> make_pad_mask(lengths, xs, 1) tensor([[[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1]], [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]], [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]], dtype=torch.uint8) >>> make_pad_mask(lengths, xs, 2) tensor([[[0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1]], [[0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1]], [[0, 0, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1]]], dtype=torch.uint8) """ if length_dim == 0: raise ValueError("length_dim cannot be 0: {}".format(length_dim)) if not isinstance(lengths, list): lengths = lengths.tolist() bs = int(len(lengths)) if xs is None: maxlen = int(max(lengths)) else: maxlen = xs.size(length_dim) seq_range = torch.arange(0, maxlen, dtype=torch.int64) seq_range_expand = seq_range.unsqueeze(0).expand(bs, maxlen) seq_length_expand = seq_range_expand.new(lengths).unsqueeze(-1) mask = seq_range_expand >= seq_length_expand if xs is not None: assert xs.size(0) == bs, (xs.size(0), bs) if length_dim < 0: length_dim = xs.dim() + length_dim # ind = (:, None, ..., None, :, , None, ..., None) ind = tuple( slice(None) if i in (0, length_dim) else None for i in range(xs.dim()) ) mask = mask[ind].expand_as(xs).to(xs.device) return mask
Make mask tensor containing indices of non-padded part. Args: lengths (LongTensor or List): Batch of lengths (B,). xs (Tensor, optional): The reference tensor. If set, masks will be the same shape as this tensor. length_dim (int, optional): Dimension indicator of the above tensor. See the example. Returns: ByteTensor: mask tensor containing indices of padded part. dtype=torch.uint8 in PyTorch 1.2- dtype=torch.bool in PyTorch 1.2+ (including 1.2) Examples: With only lengths. >>> lengths = [5, 3, 2] >>> make_non_pad_mask(lengths) masks = [[1, 1, 1, 1 ,1], [1, 1, 1, 0, 0], [1, 1, 0, 0, 0]] With the reference tensor. >>> xs = torch.zeros((3, 2, 4)) >>> make_non_pad_mask(lengths, xs) tensor([[[1, 1, 1, 1], [1, 1, 1, 1]], [[1, 1, 1, 0], [1, 1, 1, 0]], [[1, 1, 0, 0], [1, 1, 0, 0]]], dtype=torch.uint8) >>> xs = torch.zeros((3, 2, 6)) >>> make_non_pad_mask(lengths, xs) tensor([[[1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 0]], [[1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0]], [[1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0]]], dtype=torch.uint8) With the reference tensor and dimension indicator. >>> xs = torch.zeros((3, 6, 6)) >>> make_non_pad_mask(lengths, xs, 1) tensor([[[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0]], [[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]], [[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]], dtype=torch.uint8) >>> make_non_pad_mask(lengths, xs, 2) tensor([[[1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 0]], [[1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0]], [[1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0]]], dtype=torch.uint8)
def make_non_pad_mask(lengths, xs=None, length_dim=-1): """Make mask tensor containing indices of non-padded part. Args: lengths (LongTensor or List): Batch of lengths (B,). xs (Tensor, optional): The reference tensor. If set, masks will be the same shape as this tensor. length_dim (int, optional): Dimension indicator of the above tensor. See the example. Returns: ByteTensor: mask tensor containing indices of padded part. dtype=torch.uint8 in PyTorch 1.2- dtype=torch.bool in PyTorch 1.2+ (including 1.2) Examples: With only lengths. >>> lengths = [5, 3, 2] >>> make_non_pad_mask(lengths) masks = [[1, 1, 1, 1 ,1], [1, 1, 1, 0, 0], [1, 1, 0, 0, 0]] With the reference tensor. >>> xs = torch.zeros((3, 2, 4)) >>> make_non_pad_mask(lengths, xs) tensor([[[1, 1, 1, 1], [1, 1, 1, 1]], [[1, 1, 1, 0], [1, 1, 1, 0]], [[1, 1, 0, 0], [1, 1, 0, 0]]], dtype=torch.uint8) >>> xs = torch.zeros((3, 2, 6)) >>> make_non_pad_mask(lengths, xs) tensor([[[1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 0]], [[1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0]], [[1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0]]], dtype=torch.uint8) With the reference tensor and dimension indicator. >>> xs = torch.zeros((3, 6, 6)) >>> make_non_pad_mask(lengths, xs, 1) tensor([[[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0]], [[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]], [[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]], dtype=torch.uint8) >>> make_non_pad_mask(lengths, xs, 2) tensor([[[1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 0]], [[1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0]], [[1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0]]], dtype=torch.uint8) """ return ~make_pad_mask(lengths, xs, length_dim)
:param h: [B, T, H] :param seg_ids: [B, T] :return: h_ph: [B, T_ph, H]
def group_hidden_by_segs(h, seg_ids, max_len): """ :param h: [B, T, H] :param seg_ids: [B, T] :return: h_ph: [B, T_ph, H] """ B, T, H = h.shape h_gby_segs = h.new_zeros([B, max_len + 1, H]).scatter_add_(1, seg_ids[:, :, None].repeat([1, 1, H]), h) all_ones = h.new_ones(h.shape[:2]) cnt_gby_segs = h.new_zeros([B, max_len + 1]).scatter_add_(1, seg_ids, all_ones).contiguous() h_gby_segs = h_gby_segs[:, 1:] cnt_gby_segs = cnt_gby_segs[:, 1:] h_gby_segs = h_gby_segs / torch.clamp(cnt_gby_segs[:, :, None], min=1) return h_gby_segs, cnt_gby_segs
Convert a list of 1d tensors into a padded 2d tensor.
def collate_1d(values, pad_idx=0, left_pad=False, shift_right=False, max_len=None, shift_id=1): """Convert a list of 1d tensors into a padded 2d tensor.""" size = max(v.size(0) for v in values) if max_len is None else max_len res = values[0].new(len(values), size).fill_(pad_idx) def copy_tensor(src, dst): assert dst.numel() == src.numel() if shift_right: dst[1:] = src[:-1] dst[0] = shift_id else: dst.copy_(src) for i, v in enumerate(values): copy_tensor(v, res[i][size - len(v):] if left_pad else res[i][:len(v)]) return res
Convert a list of 2d tensors into a padded 3d tensor.
def collate_2d(values, pad_idx=0, left_pad=False, shift_right=False, max_len=None): """Convert a list of 2d tensors into a padded 3d tensor.""" size = max(v.size(0) for v in values) if max_len is None else max_len res = values[0].new(len(values), size, values[0].shape[1]).fill_(pad_idx) def copy_tensor(src, dst): assert dst.numel() == src.numel() if shift_right: dst[1:] = src[:-1] else: dst.copy_(src) for i, v in enumerate(values): copy_tensor(v, res[i][size - len(v):] if left_pad else res[i][:len(v)]) return res
Yield mini-batches of indices bucketed by size. Batches may contain sequences of different lengths. Args: indices (List[int]): ordered list of dataset indices num_tokens_fn (callable): function that returns the number of tokens at a given index max_tokens (int, optional): max number of tokens in each batch (default: None). max_sentences (int, optional): max number of sentences in each batch (default: None). required_batch_size_multiple (int, optional): require batch size to be a multiple of N (default: 1).
def batch_by_size( indices, num_tokens_fn, max_tokens=None, max_sentences=None, required_batch_size_multiple=1, distributed=False ): """ Yield mini-batches of indices bucketed by size. Batches may contain sequences of different lengths. Args: indices (List[int]): ordered list of dataset indices num_tokens_fn (callable): function that returns the number of tokens at a given index max_tokens (int, optional): max number of tokens in each batch (default: None). max_sentences (int, optional): max number of sentences in each batch (default: None). required_batch_size_multiple (int, optional): require batch size to be a multiple of N (default: 1). """ max_tokens = max_tokens if max_tokens is not None else sys.maxsize max_sentences = max_sentences if max_sentences is not None else sys.maxsize bsz_mult = required_batch_size_multiple if isinstance(indices, types.GeneratorType): indices = np.fromiter(indices, dtype=np.int64, count=-1) sample_len = 0 sample_lens = [] batch = [] batches = [] for i in range(len(indices)): idx = indices[i] num_tokens = num_tokens_fn(idx) sample_lens.append(num_tokens) sample_len = max(sample_len, num_tokens) assert sample_len <= max_tokens, ( "sentence at index {} of size {} exceeds max_tokens " "limit of {}!".format(idx, sample_len, max_tokens) ) num_tokens = (len(batch) + 1) * sample_len if _is_batch_full(batch, num_tokens, max_tokens, max_sentences): mod_len = max( bsz_mult * (len(batch) // bsz_mult), len(batch) % bsz_mult, ) batches.append(batch[:mod_len]) batch = batch[mod_len:] sample_lens = sample_lens[mod_len:] sample_len = max(sample_lens) if len(sample_lens) > 0 else 0 batch.append(idx) if len(batch) > 0: batches.append(batch) return batches
Replace non-padding symbols with their position numbers. Position numbers begin at padding_idx+1. Padding symbols are ignored.
def make_positions(tensor, padding_idx): """Replace non-padding symbols with their position numbers. Position numbers begin at padding_idx+1. Padding symbols are ignored. """ # The series of casts and type-conversions here are carefully # balanced to both work with ONNX export and XLA. In particular XLA # prefers ints, cumsum defaults to output longs, and ONNX doesn't know # how to handle the dtype kwarg in cumsum. mask = tensor.ne(padding_idx).int() return ( torch.cumsum(mask, dim=1).type_as(mask) * mask ).long() + padding_idx
Initialize a Linear or Convolutional layer.
def init_layer(layer): """Initialize a Linear or Convolutional layer. """ nn.init.xavier_uniform_(layer.weight) if hasattr(layer, 'bias'): if layer.bias is not None: layer.bias.data.fill_(0.)
Initialize a Batchnorm layer.
def init_bn(bn): """Initialize a Batchnorm layer. """ bn.bias.data.fill_(0.) bn.weight.data.fill_(1.)
Initialize a GRU layer.
def init_gru(rnn): """Initialize a GRU layer. """ def _concat_init(tensor, init_funcs): (length, fan_out) = tensor.shape fan_in = length // len(init_funcs) for (i, init_func) in enumerate(init_funcs): init_func(tensor[i * fan_in: (i + 1) * fan_in, :]) def _inner_uniform(tensor): fan_in = nn.init._calculate_correct_fan(tensor, 'fan_in') nn.init.uniform_(tensor, -math.sqrt(3 / fan_in), math.sqrt(3 / fan_in)) for i in range(rnn.num_layers): _concat_init( getattr(rnn, 'weight_ih_l{}'.format(i)), [_inner_uniform, _inner_uniform, _inner_uniform] ) torch.nn.init.constant_(getattr(rnn, 'bias_ih_l{}'.format(i)), 0) _concat_init( getattr(rnn, 'weight_hh_l{}'.format(i)), [_inner_uniform, _inner_uniform, nn.init.orthogonal_] ) torch.nn.init.constant_(getattr(rnn, 'bias_hh_l{}'.format(i)), 0)
:param front: front-head audio, like vocal [samples,channel], will be normlized so any scale will be fine :param noise: noise, [samples,channel], any scale :param snr_l: Optional :param snr_h: Optional :param scale_lower: Optional :param scale_upper: Optional :return: scaled front and noise (noisy = front + noise), all_mel_e2e outputs are noramlized within [-1 , 1]
def add_noise_and_scale(front, noise, snr_l=0, snr_h=0, scale_lower=1.0, scale_upper=1.0): """ :param front: front-head audio, like vocal [samples,channel], will be normlized so any scale will be fine :param noise: noise, [samples,channel], any scale :param snr_l: Optional :param snr_h: Optional :param scale_lower: Optional :param scale_upper: Optional :return: scaled front and noise (noisy = front + noise), all_mel_e2e outputs are noramlized within [-1 , 1] """ snr = None noise, front = normalize_energy_torch(noise), normalize_energy_torch(front) # set noise and vocal to equal range [-1,1] # print("normalize:",torch.max(noise),torch.max(front)) if snr_l is not None and snr_h is not None: front, noise, snr = _random_noise(front, noise, snr_l=snr_l, snr_h=snr_h) # remix them with a specific snr noisy, noise, front = unify_energy_torch(noise + front, noise, front) # normalize noisy, noise and vocal energy into [-1,1] # print("unify:", torch.max(noise), torch.max(front), torch.max(noisy)) scale = _random_scale(scale_lower, scale_upper) # random scale these three signal # print("Scale",scale) noisy, noise, front = noisy * scale, noise * scale, front * scale # apply scale # print("after scale", torch.max(noisy), torch.max(noise), torch.max(front), snr, scale) front, noise = _to_numpy(front), _to_numpy(noise) # [num_samples] mixed_wav = front + noise return front, noise, mixed_wav, snr, scale
:param audio: 1d waveform, [batchsize, *], :param alpha: the value of output range from: [-alpha,alpha] :return: 1d waveform which value range from: [-alpha,alpha]
def normalize_energy(audio, alpha = 1): ''' :param audio: 1d waveform, [batchsize, *], :param alpha: the value of output range from: [-alpha,alpha] :return: 1d waveform which value range from: [-alpha,alpha] ''' val_max = activelev(audio) return (audio / val_max) * alpha
If the signal is almost empty(determined by threshold), if will only be divided by 2**15 :param audio: 1d waveform, 2**15 :param alpha: the value of output range from: [-alpha,alpha] :return: 1d waveform which value range from: [-alpha,alpha]
def normalize_energy_torch(audio, alpha = 1): ''' If the signal is almost empty(determined by threshold), if will only be divided by 2**15 :param audio: 1d waveform, 2**15 :param alpha: the value of output range from: [-alpha,alpha] :return: 1d waveform which value range from: [-alpha,alpha] ''' val_max = activelev_torch([audio]) return (audio / val_max) * alpha
need to update like matlab
def activelev(*args): ''' need to update like matlab ''' return np.max(np.abs([*args]))
need to update like matlab
def activelev_torch(*args): ''' need to update like matlab ''' res = [] args = args[0] for each in args: res.append(torch.max(torch.abs(each))) return max(res)
# from librosa 0.6 Compute the sum-square envelope of a window function at a given hop length. This is used to estimate modulation effects induced by windowing observations in short-time fourier transforms. Parameters ---------- window : string, tuple, number, callable, or list-like Window specification, as in `get_window` n_frames : int > 0 The number of analysis frames hop_length : int > 0 The number of samples to advance between frames win_length : [optional] The length of the window function. By default, this matches `n_fft`. n_fft : int > 0 The length of each analysis frame. dtype : np.dtype The data type of the output Returns ------- wss : np.ndarray, shape=`(n_fft + hop_length * (n_frames - 1))` The sum-squared envelope of the window function
def window_sumsquare(window, n_frames, hop_length=512, win_length=1024, n_fft=1024, dtype=np.float32, norm=None): """ # from librosa 0.6 Compute the sum-square envelope of a window function at a given hop length. This is used to estimate modulation effects induced by windowing observations in short-time fourier transforms. Parameters ---------- window : string, tuple, number, callable, or list-like Window specification, as in `get_window` n_frames : int > 0 The number of analysis frames hop_length : int > 0 The number of samples to advance between frames win_length : [optional] The length of the window function. By default, this matches `n_fft`. n_fft : int > 0 The length of each analysis frame. dtype : np.dtype The data type of the output Returns ------- wss : np.ndarray, shape=`(n_fft + hop_length * (n_frames - 1))` The sum-squared envelope of the window function """ if win_length is None: win_length = n_fft n = n_fft + hop_length * (n_frames - 1) x = np.zeros(n, dtype=dtype) # Compute the squared window at the desired length win_sq = get_window(window, win_length, fftbins=True) win_sq = librosa_util.normalize(win_sq, norm=norm)**2 win_sq = librosa_util.pad_center(win_sq, n_fft) # Fill the envelope for i in range(n_frames): sample = i * hop_length x[sample:min(n, sample + n_fft)] += win_sq[:max(0, min(n_fft, n - sample))] return x
https://github.com/openai/guided-diffusion/blob/27c20a8fab9cb472df5d6bdd6c8d11c8f430b924/guided_diffusion/nn.py#L86 Take the mean over all non-batch dimensions.
def mean_flat(tensor): """ https://github.com/openai/guided-diffusion/blob/27c20a8fab9cb472df5d6bdd6c8d11c8f430b924/guided_diffusion/nn.py#L86 Take the mean over all non-batch dimensions. """ return tensor.mean(dim=list(range(1, len(tensor.shape))))
Overwrite model.train with this function to make sure train/eval mode does not change anymore.
def disabled_train(self, mode=True): """Overwrite model.train with this function to make sure train/eval mode does not change anymore.""" return self
Overwrite model.train with this function to make sure train/eval mode does not change anymore.
def disabled_train(self, mode=True): """Overwrite model.train with this function to make sure train/eval mode does not change anymore.""" return self
Zero out the parameters of a module and return it.
def zero_module(module): """ Zero out the parameters of a module and return it. """ for p in module.parameters(): p.detach().zero_() return module
This matches the implementation in Denoising Diffusion Probabilistic Models: From Fairseq. Build sinusoidal embeddings. This matches the implementation in tensor2tensor, but differs slightly from the description in Section 3.5 of "Attention Is All You Need".
def get_timestep_embedding(timesteps, embedding_dim): """ This matches the implementation in Denoising Diffusion Probabilistic Models: From Fairseq. Build sinusoidal embeddings. This matches the implementation in tensor2tensor, but differs slightly from the description in Section 3.5 of "Attention Is All You Need". """ assert len(timesteps.shape) == 1 half_dim = embedding_dim // 2 emb = math.log(10000) / (half_dim - 1) emb = torch.exp(torch.arange(half_dim, dtype=torch.float32) * -emb) emb = emb.to(device=timesteps.device) emb = timesteps.float()[:, None] * emb[None, :] emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1) if embedding_dim % 2 == 1: # zero pad emb = torch.nn.functional.pad(emb, (0,1,0,0)) return emb
A counter for the `thop` package to count the operations in an attention operation. Meant to be used like: macs, params = thop.profile( model, inputs=(inputs, timestamps), custom_ops={QKVAttention: QKVAttention.count_flops}, )
def count_flops_attn(model, _x, y): """ A counter for the `thop` package to count the operations in an attention operation. Meant to be used like: macs, params = thop.profile( model, inputs=(inputs, timestamps), custom_ops={QKVAttention: QKVAttention.count_flops}, ) """ b, c, *spatial = y[0].shape num_spatial = int(np.prod(spatial)) # We perform two matmuls with the same number of ops. # The first computes the weight matrix, the second computes # the combination of the value vectors. matmul_ops = 2 * b * (num_spatial ** 2) * c model.total_ops += th.DoubleTensor([matmul_ops])
Create a beta schedule that discretizes the given alpha_t_bar function, which defines the cumulative product of (1-beta) over time from t = [0,1]. :param num_diffusion_timesteps: the number of betas to produce. :param alpha_bar: a lambda that takes an argument t from 0 to 1 and produces the cumulative product of (1-beta) up to that part of the diffusion process. :param max_beta: the maximum beta to use; use values lower than 1 to prevent singularities.
def betas_for_alpha_bar(num_diffusion_timesteps, alpha_bar, max_beta=0.999): """ Create a beta schedule that discretizes the given alpha_t_bar function, which defines the cumulative product of (1-beta) over time from t = [0,1]. :param num_diffusion_timesteps: the number of betas to produce. :param alpha_bar: a lambda that takes an argument t from 0 to 1 and produces the cumulative product of (1-beta) up to that part of the diffusion process. :param max_beta: the maximum beta to use; use values lower than 1 to prevent singularities. """ betas = [] for i in range(num_diffusion_timesteps): t1 = i / num_diffusion_timesteps t2 = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar(t2) / alpha_bar(t1), max_beta)) return np.array(betas)
Evaluate a function without caching intermediate activations, allowing for reduced memory at the expense of extra compute in the backward pass. :param func: the function to evaluate. :param inputs: the argument sequence to pass to `func`. :param params: a sequence of parameters `func` depends on but does not explicitly take as arguments. :param flag: if False, disable gradient checkpointing.
def checkpoint(func, inputs, params, flag): """ Evaluate a function without caching intermediate activations, allowing for reduced memory at the expense of extra compute in the backward pass. :param func: the function to evaluate. :param inputs: the argument sequence to pass to `func`. :param params: a sequence of parameters `func` depends on but does not explicitly take as arguments. :param flag: if False, disable gradient checkpointing. """ if flag: args = tuple(inputs) + tuple(params) return CheckpointFunction.apply(func, len(inputs), *args) else: return func(*inputs)
Create sinusoidal timestep embeddings. :param timesteps: a 1-D Tensor of N indices, one per batch element. These may be fractional. :param dim: the dimension of the output. :param max_period: controls the minimum frequency of the embeddings. :return: an [N x dim] Tensor of positional embeddings.
def timestep_embedding(timesteps, dim, max_period=10000, repeat_only=False): """ Create sinusoidal timestep embeddings. :param timesteps: a 1-D Tensor of N indices, one per batch element. These may be fractional. :param dim: the dimension of the output. :param max_period: controls the minimum frequency of the embeddings. :return: an [N x dim] Tensor of positional embeddings. """ if not repeat_only: half = dim // 2 freqs = torch.exp( -math.log(max_period) * torch.arange(start=0, end=half, dtype=torch.float32) / half ).to(device=timesteps.device) args = timesteps[:, None].float() * freqs[None] embedding = torch.cat([torch.cos(args), torch.sin(args)], dim=-1) if dim % 2: embedding = torch.cat([embedding, torch.zeros_like(embedding[:, :1])], dim=-1) else: embedding = repeat(timesteps, 'b -> b d', d=dim) return embedding