def get_dtw(self, dtw_dir, normal_speaker, dys_speaker):
        """
        Need to collect all utterances from train portion of partition from both speakers
        Then we find the corresponding utterances and pair them
        Then we dynamically-time warp them and save the features
        """
        feature_files = collect_files(config.directories.features)
        normal_files = []
        dys_files = []
        wordlist = joblib.load('wordlist.pkl')  # this is to save time so we don't load 'wordlist.pkl' for every file
        dictionary = joblib.load('dict.pkl')    # ditto to above
        for file in feature_files:
            metadata = utils.get_file_metadata(file, wordlist=wordlist, dictionary=dictionary)
            if normal_speaker == metadata['speaker']:
                normal_files.append(file)
            elif dys_speaker == metadata['speaker']:
                dys_files.append(file)

        pairs = []
        """Let's find the file pairs"""
        for normal_file in normal_files:
            dys_file = normal_file.replace(normal_speaker, dys_speaker)
            if os.path.exists(dys_file):
                pairs.append({'normal': normal_file, 'dys': dys_file})

        """Make multiprocess data"""
        multiprocess_data = []
        for pair in pairs:
            multiprocess_data.append([pair, dtw_dir, normal_speaker, dys_speaker])

        # dtw_multiprocess(multiprocess_data[0])
        with concurrent.futures.ProcessPoolExecutor(max_workers=multiprocessing.cpu_count()) as executor:
            for _ in tqdm(executor.map(dtw_multiprocess, multiprocess_data)):
                """"""
Exemple #2
0
 def get_train_test_wordsplit(self):
     wordlist = joblib.load('wordlist.pkl')
     dictionary = joblib.load('dict.pkl')
     """Get the triain and test files"""
     if not os.path.exists(os.path.join(exp_dir, 'train_test_files.pkl')):
         words = joblib.load(self.partition)
         """Turn train and test into dicts for fast check (hashable)"""
         words = self.wordlist_to_dict(words)
         train_files = []
         test_files = []
         for file in tqdm(collect_files(self.train_data_dir)):
             metadata = utils.get_file_metadata(file,
                                                wordlist=wordlist,
                                                dictionary=dictionary)
             try:
                 dummy = words['train'][metadata['word']]
                 train_files.append(file)
             except:
                 try:
                     dummy = words['test'][metadata['word']]
                     test_files.append(file)
                 except:
                     print("File in neither train nor test set...")
         joblib.dump({
             'train': train_files,
             'test': test_files
         }, os.path.join(exp_dir, 'train_test_files.pkl'))
     else:
         files = joblib.load(os.path.join(exp_dir, 'train_test_files.pkl'))
         train_files = files['train']
         test_files = files['test']
     return self.filter_speakers(train_files), self.filter_speakers(
         test_files)
Exemple #3
0
def get_true_test_utterances_ctc():
    all_files = collect_files(config.directories.features)
    vc_partition = joblib.load("vc_partition.pkl")
    potential_test_utterances = list(
        set(all_files).difference(
            set(vc_partition['train'] + vc_partition['test'] +
                vc_partition['val'])))
    """Now we only want the utterances with the dysarthic speakers"""
    test_utterances = []
    unique_dys_speakers = []
    for x in potential_test_utterances:
        utterance = x.split('/')[1]
        speaker = utterance.split('_')[0]
        if speaker in config.data.conversion_target_speakers:
            test_utterances.append(x)
            if speaker not in unique_dys_speakers:
                unique_dys_speakers.append(speaker)
    """Just do a quick sanity check that the words from these utterances aren't in the seen words"""
    # wordlist = joblib.load('wordlist.pkl')
    # dictionary = joblib.load('dict.pkl')
    # seen_unseen_words = joblib.load('seen_unseen_words.pkl')
    # for x in test_utterances:
    #     metadata = get_file_metadata(x, wordlist=wordlist, dictionary=dictionary)
    #     if metadata['word'].upper() in seen_unseen_words['seen']:
    #         print("Messed up code...")
    return test_utterances
Exemple #4
0
def main():
    if not os.path.isdir(config.directories.features):
        os.mkdir(config.directories.features)
    files = collect_files(config.directories.silence_removed)
    with concurrent.futures.ProcessPoolExecutor(max_workers=multiprocessing.cpu_count()) as executor:
        for _ in tqdm(executor.map(process, files)):
            """"""
Exemple #5
0
def get_ctc_partition_offline_augmentation():
    """NOTE: WE DON'T HAVE A TEST PARTITION, BECAUSE WE TEST ON REAL DYSARTHRIC SPEECH, NOT AUGMENTED"""
    augmented_files = collect_files(config.directories.spec_augmented_data)
    random.shuffle(augmented_files)
    split_index = round(0.98 * len(augmented_files))
    train_files = augmented_files[0:split_index]
    val_files = augmented_files[split_index:]
    intersection = set(train_files).intersection(set(val_files))
    data = {'train': train_files, 'val': val_files}
    joblib.dump(data, 'ctc_partition.pkl')
Exemple #6
0
def get_vc_partition_utterance_split():
    """Set aside one utterance for each speaker of each word for test set and val set"""
    wordlist = joblib.load('wordlist.pkl')
    dictionary = joblib.load('dict.pkl')
    seen_unseen_words = joblib.load('seen_unseen_words.pkl')
    # unique_words = set([value for key, value in wordlist.items()])
    speaker_test_files = {}
    speaker_val_files = {}
    test_files = []
    val_files = []
    all_files = collect_files(config.directories.features)
    new_all_files = []
    for file in all_files:
        metadata = get_file_metadata(file,
                                     wordlist=wordlist,
                                     dictionary=dictionary)
        word = (metadata['word']).upper()
        if word in seen_unseen_words['seen']:
            new_all_files.append(file)
            if metadata["speaker"] not in speaker_test_files:
                speaker_test_files[metadata["speaker"]] = {}
            if metadata["speaker"] not in speaker_val_files:
                speaker_val_files[metadata["speaker"]] = {}
            if metadata["word"] not in speaker_test_files[metadata["speaker"]]:
                speaker_test_files[metadata["speaker"]][
                    metadata["word"]] = file
                test_files.append(file)
            if metadata["word"] in speaker_test_files[metadata["speaker"]] \
                    and metadata["word"] not in speaker_val_files[metadata["speaker"]] \
                    and speaker_test_files[metadata["speaker"]][metadata["word"]] != file:
                speaker_val_files[metadata["speaker"]][metadata["word"]] = file
                val_files.append(file)
    """All files stored in test_files is the test set
       All files stored in val_files is val set
       The list of all other files is the train set"""
    """Let's check that there's no intersection between test and val sets"""
    # test_val_intersection = list(set(test_files).intersection(set(val_files)))
    train_files = list(set(new_all_files) - set(test_files) - set(val_files))
    intersection_test = (set(train_files)).intersection(
        set(test_files))  # check there are no overlapping elements
    intersection_val = (set(train_files)).intersection(set(val_files))
    """Check that none of the unseen words are in any of these partitions"""
    # seen_files = train_files + test_files + val_files
    # for file in seen_files:
    #     metadata = get_file_metadata(file, wordlist=wordlist, dictionary=dictionary)
    #     word = (metadata['word']).upper()
    #     if word in seen_unseen_words['unseen']:
    #         print("unseen word in seen partition, something is wrong with code")
    # seen_files = list((set(train_files).add(set(test_files))).add(set(val_files)))
    joblib.dump({
        'train': train_files,
        'test': test_files,
        'val': val_files
    }, 'vc_partition.pkl')
Exemple #7
0
def main():
    """Directory creation"""
    if not os.path.isdir(config.directories.silence_removed):
        os.mkdir(config.directories.silence_removed)


    """Process the files using all cpus"""
    all_files = preprocessing.collect_files(config.directories.noise_removed)
    process_file(all_files[0])
    with concurrent.futures.ProcessPoolExecutor(max_workers=multiprocessing.cpu_count()) as executor:
        for _ in tqdm(executor.map(process_file, all_files)):
            """"""
Exemple #8
0
def put_seen_features_in_new_dir():
    if not os.path.isdir(
            config.directories.seen_words_augmented_dysarthric_data):
        os.mkdir(config.directories.seen_words_augmented_dysarthric_data)
    speaker_pairs = [
        f.path for f in os.scandir(attention_exp_path) if f.is_dir()
    ]
    for pair in speaker_pairs:
        pair_name = pair.split('/')[-1]
        normal_speaker = pair_name.split('_')[0]
        dys_speaker = pair_name.split('_')[2]
        model_path = os.path.join(pair, 'models',
                                  attention_model_number + '-G.ckpt')
        """First let's get all seen utterances from normal speaker"""
        potential_files = collect_files(
            os.path.join(config.directories.dtw, pair_name))
        correct_files = []
        for file in potential_files:
            metadata = utils.get_file_metadata_vc(file,
                                                  wordlist=WORDLIST,
                                                  dictionary=DICTIONARY)
            word = metadata['word'].upper()
            if word in SEEN_UNSEEN_WORDS['seen']:
                """We want to convert the original features (not dtw)"""
                filename = metadata['normal_filename']
                filename = filename.replace(normal_speaker, dys_speaker)
                original_path = os.path.join(config.directories.features,
                                             filename + '.pkl')
                if os.path.exists(original_path):
                    correct_files.append(original_path)

        print(model_path)
        for file in tqdm(correct_files):
            utterance_part = (file.split('/')[-1]).replace(pair_name, '')[:-4]
            modded_utterance_part = '_'.join(utterance_part.split('_')[1:])
            output = joblib.load(file)
            # src, tgt = solver.get_batch_transformer_augment(file)
            # solver.G = solver.G.eval()
            # converted_output = solver.G(src)
            # converted_output = np.squeeze(converted_output.detach().cpu().numpy())
            # plt.imshow(converted_output.T)
            # plt.show()
            """Filename convention for augmented data is DYS_utt_augment_pair"""
            """This way if we cut off everything after augment pair we get the original dys utterance"""
            """I think this will be easier for the eventual CTC training for recognition"""
            #utterance_name = dys_speaker + '_' + modded_utterance_part + '_augment_' + pair_name + '.pkl'
            utterance_name = file.split('/')[-1]
            dump_path = os.path.join(
                config.directories.seen_words_augmented_dysarthric_data,
                utterance_name)
            joblib.dump(output, dump_path)
Exemple #9
0
def augment_spec_dcgan():
    """Note: We want to use the original spectrograms. But we use the filenames from attention so we have the same
    amount of augmented data for each approach."""
    if not os.path.isdir(config.directories.spec_augmented_data_dcgan):
        os.mkdir(config.directories.spec_augmented_data_dcgan)
    files_to_augment = collect_files(config.directories.dcgan_augmented_data)

    # multiprocess_augment_dcgan(files_to_augment[0])

    with concurrent.futures.ProcessPoolExecutor(
            max_workers=multiprocessing.cpu_count()) as executor:
        for _ in tqdm(
                executor.map(multiprocess_augment_dcgan, files_to_augment)):
            """"""
Exemple #10
0
def dummy_check_metadata():
    """I used this to check that my assumptions about the naming convention
    are correct (B1, B2, B3 need to be added to certain words)
    After listening, the audios labeled with their words are actually those words so assumptions are correct"""
    files = collect_files('silence_removed')
    dummy_dir = 'dummy_files_check'
    if not os.path.isdir(dummy_dir):
        os.mkdir(dummy_dir)
    for i, file in tqdm(enumerate(files)):
        metadata = get_file_metadata(file)
        target_path = os.path.join(
            dummy_dir, metadata['speaker'] + '_' + metadata['word'] + '_' +
            str(i) + '.wav')
        shutil.copy(file, target_path)
Exemple #11
0
def get_partition_utterance_split():
    """Set aside one utterance for each speaker of each word for test set and val set"""
    wordlist = joblib.load('wordlist.pkl')
    dictionary = joblib.load('dict.pkl')
    speaker_test_files = {}
    speaker_val_files = {}
    test_files = []
    val_files = []
    all_files = collect_files(config.directories.features)
    for file in all_files:
        metadata = get_file_metadata(file,
                                     wordlist=wordlist,
                                     dictionary=dictionary)
        if metadata["speaker"] not in speaker_test_files:
            speaker_test_files[metadata["speaker"]] = {}
        if metadata["speaker"] not in speaker_val_files:
            speaker_val_files[metadata["speaker"]] = {}
        if metadata["word"] not in speaker_test_files[metadata["speaker"]]:
            speaker_test_files[metadata["speaker"]][metadata["word"]] = file
            test_files.append(file)
        if metadata["word"] in speaker_test_files[metadata["speaker"]] \
                and metadata["word"] not in speaker_val_files[metadata["speaker"]] \
                and speaker_test_files[metadata["speaker"]][metadata["word"]] != file:
            speaker_val_files[metadata["speaker"]][metadata["word"]] = file
            val_files.append(file)
    """All files stored in test_files is the test set
       All files stored in val_files is val set
       The list of all other files is the train set"""
    """Let's check that there's no intersection between test and val sets"""
    # test_val_intersection = list(set(test_files).intersection(set(val_files)))
    train_files = list(set(all_files) - set(test_files) - set(val_files))
    # intersection_test = (set(train_files)).intersection(set(test_files))  # check there are no overlapping elements
    # intersection_val = (set(train_files)).intersection(set(val_files))
    joblib.dump({
        'train': train_files,
        'test': test_files,
        'val': val_files
    }, 'partition.pkl')
Exemple #12
0
    data['lack']['2'][
        'dir'] = './exps/PARTITION_2_trial_1_Lack_Baseline_CTC_training'
    data['limited']['2'][
        'dir'] = './exps/PARTITION_2_trial_1_Limited_Baseline_CTC_training'

    phones = joblib.load('phones.pkl')
    p2c = utils.phone2class(phones)
    c2p = utils.class2phone(phones)

    no_lm_folder = 'predictions'
    lm_folder = 'language_model_predictions_test'
    for exp_type, exp_data in data.items():
        for partition_number, partition_data in exp_data.items():
            no_lm_dir = os.path.join(partition_data['dir'], no_lm_folder)
            lm_dir = os.path.join(partition_data['dir'], lm_folder)
            partition_data['no_lm_results'] = collect_files(no_lm_dir)
            partition_data['lm_results'] = collect_files(lm_dir)
            """Let's get the levenshtein distances"""
            partition_data['no_lm_scores'] = {}
            for file in partition_data['no_lm_results']:
                file_key = file.split('/')[-1]
                results = joblib.load(file)
                predicted_string = [
                    p2c[x] for x in results['predicted_phones']
                ]
                predicted_string = [chr(x) for x in predicted_string]
                predicted_string = ''.join(predicted_string)
                true_string = [p2c[x] for x in results['true_phones']]
                true_string = [chr(x) for x in true_string]
                true_string = ''.join(true_string)
                distance = levenshtein_distance(predicted_string, true_string)
Exemple #13
0
    def performance(self):
        """"""
        speakers = {}
        for file in tqdm(collect_files(self.predict_dir)):
            utterance = file.split('/')[-1]
            speaker = utterance.split('_')[0]
            if speaker not in speakers:
                speakers[speaker] = [joblib.load(file)]
            else:
                speakers[speaker].append(joblib.load(file))

        delimiter = '_'
        """Percent correct words"""
        correct_words = {}
        WER = {}
        for speaker, utt_list in speakers.items():
            correct_word_count = 0
            for i, utt in enumerate(utt_list):
                true_seq = delimiter.join(utt['true_phones'])
                pred_seq = delimiter.join(utt['predicted_phones'])
                if true_seq == pred_seq:
                    correct_word_count += 1
            word_accuracy = correct_word_count / (i + 1)
            correct_words[speaker] = word_accuracy
            WER[speaker] = (1 - word_accuracy) * 100
        """Percent phones recognized in the utterance that are in the true phones"""
        delimiter = '_'
        correct_perc = {}
        for speaker, utt_list in speakers.items():
            correct_word_perc = 0
            for i, utt in enumerate(utt_list):
                true_phones = set(utt['true_phones'])
                pred_phones = set(utt['predicted_phones'])
                intersection = true_phones.intersection(pred_phones)
                percent_correct_phones = len(list(intersection)) / len(
                    list(true_phones))
                correct_word_perc += percent_correct_phones
            word_accuracy = correct_word_perc / (i + 1)
            correct_perc[speaker] = word_accuracy

        self.dump_json(dict=correct_words,
                       path=os.path.join(
                           exp_dir, 'test_accuracies_' + model_name + '.json'))
        """Let's sort best to worst WER"""
        sorted_WER = {
            k: v
            for k, v in sorted(WER.items(), key=lambda item: item[1])
        }
        self.dump_json(dict=sorted_WER,
                       path=os.path.join(exp_dir,
                                         'test_WER_' + model_name + '.json'))

        stats = {}
        """Add more code for different stats on the results here"""
        """Let's compare dysarthric performance to normal performance"""
        stats['dysarthric_mean_WER'] = np.mean(
            np.asarray([value for key, value in WER.items()
                        if 'C' not in key]))
        stats['normal_mean_WER'] = np.mean(
            np.asarray([value for key, value in WER.items() if 'C' in key]))
        stats['female_WER'] = np.mean(
            np.asarray([value for key, value in WER.items() if 'F' in key]))
        stats['male_WER'] = np.mean(
            np.asarray([value for key, value in WER.items() if 'M' in key]))
        self.dump_json(dict=stats,
                       path=os.path.join(exp_dir,
                                         'test_stats_' + model_name + '.json'))
    def get_train_val(self, pair_dir, normal_speaker, dys_speaker):
        """WE MAKE A PAIR TRAIN/VAL PARTITION HERE AND SAVE TO EXPERIMENT
        We shouldn't need this partition for anything else but just save for records"""
        # wordlist = joblib.load('wordlist.pkl')  # this is to save time so we don't load 'wordlist.pkl' for every file
        # dictionary = joblib.load('dict.pkl')  # ditto to above
        files = joblib.load(self.partition)
        files = files['partition_' + str(GLOBAL_PARTITION)]['seen']['filenames_only']
        # train_files = files['train']
        # val_files = files['val']
        # test_files = files['test']

        # intersection = set(train_files).intersection(set(test_files))

        # """Convert test_files to dict for fast searching (hashable, try except block)"""
        # test_dict = {}
        # for file in test_files:
        #     test_dict[file] = ''
        # """Convert val_files to dict for fast searching (hashable, try except block)"""
        # val_dict = {}
        # for file in val_files:
        #     val_dict[file] = ''
        # """Convert train_files to dict for fast searching (hashable, try except block)"""
        # train_dict = {}
        # for file in train_files:
        #     train_dict[file] = ''

        """Collect all paired files in the pair_dir"""
        all_files = collect_files(os.path.join(config.directories.dtw, pair_dir.split('/')[-1]))
        normal_seen_files = (files['normal']['train'].copy())
        normal_seen_files.extend(files['normal']['val'].copy())
        normal_seen_files.extend(files['normal']['test'].copy())

        dys_seen_files = (files['dys']['train'].copy())
        dys_seen_files.extend(files['dys']['val'].copy())
        dys_seen_files.extend(files['dys']['test'].copy())
        usable_files = []
        for pair in all_files:
            """Construct the original paths of BOTH speakers and make sure in seen set"""
            file_end = (pair.split('/')[-1]).replace(normal_speaker + '_to_' + dys_speaker, '')
            normal_original_path = os.path.join(config.directories.features, normal_speaker + file_end)
            dys_original_path = os.path.join(config.directories.features, dys_speaker + file_end)

            if normal_original_path in normal_seen_files and dys_original_path in dys_seen_files:
                usable_files.append(pair)



            # if normal_original_path in test_dict or dys_original_path in test_dict:
            #     train_usable = False
            # if normal_original_path in val_dict or dys_original_path in val_dict:
            #     train_usable = False
            # if train_usable:
            #     usable_train_files.append(pair)
            # val_usable = True
            # if normal_original_path in test_dict or dys_original_path in test_dict:
            #     val_usable = False
            # if normal_original_path in train_dict or dys_original_path in train_dict:
            #     val_usable = False
            # if val_usable:
            #     usable_val_files.append(pair)

        """Make the pair-wise partition after testing this code"""
        size = len(usable_files)
        train_size = round(0.98*size)
        train_files = usable_files[0:train_size]
        val_files = usable_files[train_size:]

        return train_files, val_files
Exemple #15
0
def augment_all_data():
    feature_target_dir = config.directories.features_specaugmented
    feature_source_dir = config.directories.features
    if GLOBAL_PARTITION == 1:
        attention_target_dir = config.directories.attention_specaugmented_data_1
        dcgan_target_dir = config.directories.dcgan_specaugmented_data_1
        attention_source_dir = config.directories.attention_augmented_data_1
        dcgan_source_dir = config.directories.dcgan_augmented_data_1
    elif GLOBAL_PARTITION == 2:
        attention_target_dir = config.directories.attention_specaugmented_data_2
        dcgan_target_dir = config.directories.dcgan_specaugmented_data_2
        attention_source_dir = config.directories.attention_augmented_data_2
        dcgan_source_dir = config.directories.dcgan_augmented_data_2
    """Attention"""
    if ALL_DATA_AUGMENT_ATTENTION:
        if not os.path.isdir(attention_target_dir):
            os.mkdir(attention_target_dir)
        attention_files = collect_files(attention_source_dir)
        multiprocess_attention_files = []
        for file in attention_files:
            multiprocess_attention_files.append({
                'file':
                file,
                'target_dir':
                attention_target_dir
            })
        # multiprocess_augment_all_data(multiprocess_attention_files[0])

        # """Debugging"""
        # for file in tqdm(multiprocess_attention_files):
        #     multiprocess_augment_all_data(file)

        with concurrent.futures.ProcessPoolExecutor(
                max_workers=multiprocessing.cpu_count()) as executor:
            for _ in tqdm(
                    executor.map(multiprocess_augment_all_data,
                                 multiprocess_attention_files)):
                """"""

    if ALL_DATA_AUGMENT_DCGAN:
        if not os.path.isdir(dcgan_target_dir):
            os.mkdir(dcgan_target_dir)
        """DCGAN"""
        dcgan_files = collect_files(dcgan_source_dir)
        multiprocess_dcgan_files = []
        for file in dcgan_files:
            multiprocess_dcgan_files.append({
                'file': file,
                'target_dir': dcgan_target_dir
            })
        multiprocess_augment_all_data(multiprocess_dcgan_files[0])
        with concurrent.futures.ProcessPoolExecutor(
                max_workers=multiprocessing.cpu_count()) as executor:
            for _ in tqdm(
                    executor.map(multiprocess_augment_all_data,
                                 multiprocess_dcgan_files)):
                """"""

    if ALL_DATA_AUGMENT_FEATURES:
        if not os.path.isdir(feature_target_dir):
            os.mkdir(feature_target_dir)
        """Features"""
        feature_files = collect_files(feature_source_dir)
        multiprocess_feature_files = []
        for file in feature_files:
            multiprocess_feature_files.append({
                'file': file,
                'target_dir': feature_target_dir
            })
        multiprocess_augment_all_data(multiprocess_feature_files[0])
        with concurrent.futures.ProcessPoolExecutor(
                max_workers=multiprocessing.cpu_count()) as executor:
            for _ in tqdm(
                    executor.map(multiprocess_augment_all_data,
                                 multiprocess_feature_files)):
                """"""
    stop = None
Exemple #16
0
def get_full_filenames_data_partition_utterance_split():
    """Set aside one utterance for each speaker of each word for test set and val set"""
    wordlist = joblib.load('wordlist.pkl')
    dictionary = joblib.load('dict.pkl')
    seen_unseen_partitions = joblib.load('seen_unseen_partitions.pkl')

    full_partition = {}

    print('Creating full partition...')

    for key, partition in seen_unseen_partitions.items():
        seen_unseen_words = partition
        big_partition = {}
        """Now we want to create train/val/test partitions for both seen and unseen for every speaker"""
        for partition_type in ['seen', 'unseen']:
            speaker_test_files = {}
            speaker_val_files = {}
            speaker_train_files = {}
            test_files = []
            val_files = []
            train_files = []
            all_files = collect_files(config.directories.features)
            new_all_files = []
            for file in all_files:
                metadata = get_file_metadata(file,
                                             wordlist=wordlist,
                                             dictionary=dictionary)
                word = (metadata['word']).upper()
                if word in seen_unseen_words[partition_type]:
                    new_all_files.append(file)
                    if metadata["speaker"] not in speaker_test_files:
                        speaker_test_files[metadata["speaker"]] = {}
                    if metadata["speaker"] not in speaker_val_files:
                        speaker_val_files[metadata["speaker"]] = {}
                    if metadata["speaker"] not in speaker_train_files:
                        speaker_train_files[metadata["speaker"]] = {}
                    if metadata["word"] not in speaker_test_files[
                            metadata["speaker"]]:
                        speaker_test_files[metadata["speaker"]][
                            metadata["word"]] = file
                        test_files.append(file)
                    if metadata["word"] in speaker_test_files[metadata["speaker"]] \
                            and metadata["word"] not in speaker_val_files[metadata["speaker"]] \
                            and speaker_test_files[metadata["speaker"]][metadata["word"]] != file:
                        speaker_val_files[metadata["speaker"]][
                            metadata["word"]] = file
                        val_files.append(file)
                    if metadata["word"] in speaker_test_files[metadata["speaker"]] \
                        and metadata["word"] in speaker_val_files[metadata["speaker"]] \
                        and metadata["word"] not in speaker_train_files[metadata["speaker"]] \
                        and speaker_test_files[metadata["speaker"]][metadata["word"]] != file \
                        and speaker_val_files[metadata["speaker"]][metadata["word"]] != file:
                        speaker_train_files[metadata["speaker"]][
                            metadata["word"]] = [file]
                        train_files.append(file)
                    elif metadata["word"] in speaker_test_files[metadata["speaker"]] \
                        and metadata["word"] in speaker_val_files[metadata["speaker"]] \
                        and speaker_test_files[metadata["speaker"]][metadata["word"]] != file \
                        and speaker_val_files[metadata["speaker"]][metadata["word"]] != file:
                        speaker_train_files[metadata["speaker"]][
                            metadata["word"]].append(file)
                        train_files.append(file)
            """All files stored in test_files is the test set
               All files stored in val_files is val set
               The list of all other files is the train set"""
            """Now we have files sorted by speaker into train/val/test sets"""
            """Let's put them into normal and dysarthric speaker sets"""

            big_partition[partition_type] = {
                'normal': {
                    'train': {},
                    'val': {},
                    'test': {}
                },
                'dys': {
                    'train': {},
                    'val': {},
                    'test': {}
                },
                'filenames_only': {
                    'normal': {
                        'train': {},
                        'val': {},
                        'test': {}
                    },
                    'dys': {
                        'train': {},
                        'val': {},
                        'test': {}
                    }
                }
            }

            for speaker, value in speaker_test_files.items():
                if "C" not in speaker:
                    if speaker in config.data.conversion_target_speakers:
                        big_partition[partition_type]['dys']['test'][
                            speaker] = value
                else:
                    if speaker in config.data.conversion_source_speakers:
                        big_partition[partition_type]['normal']['test'][
                            speaker] = value

            for speaker, value in speaker_train_files.items():
                if "C" not in speaker:
                    if speaker in config.data.conversion_target_speakers:
                        big_partition[partition_type]['dys']['train'][
                            speaker] = value
                else:
                    if speaker in config.data.conversion_source_speakers:
                        big_partition[partition_type]['normal']['train'][
                            speaker] = value

            for speaker, value in speaker_val_files.items():
                if "C" not in speaker:
                    if speaker in config.data.conversion_target_speakers:
                        big_partition[partition_type]['dys']['val'][
                            speaker] = value
                else:
                    if speaker in config.data.conversion_source_speakers:
                        big_partition[partition_type]['normal']['val'][
                            speaker] = value

            #big_partition[partition_type]['filenames_only'] = {}
            """We've nicely split by speaker, now we combine into just filename lists"""
            for speech_type in ['normal', 'dys']:
                for parttype in ['train', 'val', 'test']:
                    filelist = []
                    if parttype == 'val' or parttype == 'test':
                        for key_, value in big_partition[partition_type][
                                speech_type][parttype].items():
                            #sublist = []
                            for subkey, subfile in value.items():
                                filelist.append(subfile)
                    if parttype == 'train':
                        for key_, value in big_partition[partition_type][
                                speech_type][parttype].items():
                            #sublist = []
                            for subkey, subfiles in value.items():
                                filelist.extend(subfiles)
                    big_partition[partition_type]['filenames_only'][
                        speech_type][parttype] = filelist
            stop = None
        full_partition[key] = big_partition
    joblib.dump(full_partition, 'full_filenames_data_partition.pkl')
    print('Done creating full partition.')
Exemple #17
0
#     word_to_key[item_plus_key[i][0]] = item_plus_key[i][1]
# for i in range(len(item_plus_key)-1):
#     if item_plus_key[i][0] == item_plus_key[i+1][0]:
#         repeated_pairs.append([item_plus_key[i], item_plus_key[i+1]])
#         print([item_plus_key[i], item_plus_key[i+1]])
#
# """Find number of uncommon words"""
# uncommon_words = []
# common_words = []
# for key, item in word_to_key.items():
#     if 'UW' in item:
#         uncommon_words.append(key)
#     else:
#         common_words.append(key)

all_files = collect_files(config.directories.features)
used_utterances = {}
used_utterances_list = []
used_speakers = config.data.conversion_source_speakers
used_speakers.extend(config.data.conversion_target_speakers)
for file in all_files:
    metadata = utils.get_file_metadata(file,
                                       wordlist=wordlist,
                                       dictionary=dictionary)
    if metadata['speaker'] in used_speakers:
        if metadata['speaker'] not in used_utterances:
            used_utterances[metadata['speaker']] = [file]
        else:
            used_utterances[metadata['speaker']].append(file)
        used_utterances_list.append(file)