コード例 #1
0
    def __init__(self, config_file, logger=logging.getLogger(__name__)):

        self.logger = logger
        self._about()

        self.cfg = read_yaml.read_yaml(config_file)
        self.config_file = config_file

        if not self.cfg["output_path"].exists():
            self.cfg["output_path"].mkdir(parents=True)
        if not self.cfg["project_path"].exists():
            self.cfg["project_path"].mkdir(parents=True)

        self.run_name = self.cfg["run_name"]
        self.run_id = "_".join([self.run_name, stamp_time()])
        self.project_file = Path(self.cfg["project_path"],
                                 '.'.join([self.run_id, 'psx']))

        self._init_logging()
        self._check_environment()
        self._init_metashape_document()

        if "networkProcessing" in self.cfg and self.cfg["networkProcessing"][
                "enabled"]:
            self._init_network_processing()
            self.network = True
            self.logger.info('Network mode activated.')
            self._return_parameters(stage="networkProcessing")
        else:
            self.network = False

        self.init_tasks()

        self._terminate_logging()
コード例 #2
0
def main(message):
    """Hangouts Chat incoming webhook quickstart."""
    url = ryaml.read_yaml(credential)['test_bot']['webhook']
    bot_message = {'text': message}

    message_headers = {'Content-Type': 'application/json; charset=UTF-8'}

    http_obj = Http()

    response = http_obj.request(
        uri=url,
        method='POST',
        headers=message_headers,
        body=dumps(bot_message),
    )

    print(response)
コード例 #3
0
ファイル: networks.py プロジェクト: adilgupta/FSEGAN
    def __init__(self, mode):

        # read config fiel which contains parameters
        self.config_file = read_yaml()
        self.mode = mode

        arch_name = '_'.join(
            [self.config_file['rnn'], str(self.config_file['num_layers']), str(self.config_file['hidden_dim'])])
        self.config_file['dir']['models'] = self.config_file['dir']['models'].split('/')[0] + '_' + arch_name + '/'
        self.config_file['dir']['plots'] = self.config_file['dir']['plots'].split('/')[0] + '_' + arch_name + '/'

        #if not os.path.exists(self.config_file['dir']['models']):
        #    os.mkdir(self.config_file['dir']['models'])
        #if not os.path.exists(self.config_file['dir']['plots']):
        #    os.mkdir(self.config_file['dir']['plots'])

        if self.config_file['rnn'] == 'LSTM':
            from model import LSTM as Model
        elif self.config_file['rnn'] == 'GRU':
            from model import GRU as Model
        else:
            print("Model not implemented")
            exit(0)

        self.cuda = (self.config_file['cuda'] and torch.cuda.is_available())
        self.output_dim = self.config_file['num_phones']

        if mode == 'train' or mode == 'test':

            self.plots_dir = self.config_file['dir']['plots']
            # store hyperparameters
            self.total_epochs = self.config_file['train']['epochs']
            self.test_every = self.config_file['train']['test_every_epoch']
            self.test_per = self.config_file['train']['test_per_epoch']
            self.print_per = self.config_file['train']['print_per_epoch']
            self.save_every = self.config_file['train']['save_every']
            self.plot_every = self.config_file['train']['plot_every']
            # dataloader which returns batches of data
            self.train_loader = timit_loader('train', self.config_file)
            self.test_loader = timit_loader('test', self.config_file)

            self.start_epoch = 1
            self.test_acc = []
            self.train_losses, self.test_losses = [], []
            # declare model
            self.model = Model(self.config_file, weights=self.train_loader.weights)

        else:

            self.model = Model(self.config_file, weights=None)

        if self.cuda:
            self.model.cuda()

        # resume training from some stored model
        if self.mode == 'train' and self.config_file['train']['resume']:
            self.start_epoch, self.train_losses, self.test_losses, self.test_acc = self.model.load_model(mode,
                                                                                                         self.config_file[
                                                                                                             'rnn'],
                                                                                                         self.model.num_layers,
                                                                                                         self.model.hidden_dim)
            self.start_epoch += 1

        # load best model for testing/feature extraction
        elif self.mode == 'test' or mode == 'test_one':
            self.model.load_model(mode, self.config_file['rnn'], self.model.num_layers, self.model.hidden_dim)

        self.replacement = {'aa': ['ao'], 'ah': ['ax', 'ax-h'], 'er': ['axr'], 'hh': ['hv'], 'ih': ['ix'],
                            'l': ['el'], 'm': ['em'], 'n': ['en', 'nx'], 'ng': ['eng'], 'sh': ['zh'],
                            'pau': ['pcl', 'tcl', 'kcl', 'bcl', 'dcl', 'gcl', 'h#', 'epi', 'q'],
                            'uw': ['ux']}
コード例 #4
0
def batch_test(dec_type,
               top_n,
               num_templates,
               num_compares,
               num_none,
               pr_dump_path,
               results_dump_path,
               wrong_pred_path,
               cnn_dump_path=None,
               exp_factor=1):
    """
    Master function which carries out actual testing
    :param cnn_dump_path: Path where LSTM out is stored
    :param dec_type: max or CTC
    :param top_n: top-n sequences are considered
    :param num_templates: number of templates for each keyword
    :param num_compares: number of clips in which each keyword needs to be searched for
    :param num_none: number of clips in which none of the keywords is present
    :param pr_dump_path: dump precision recall values
    :param results_dump_path: dump comparison results so that c values can be tweaked easily
    :param wrong_pred_path: path to folder where txt files are stored
    :param exp_factor: weight assigned to probability score
    """
    config = read_yaml()
    # keywords = ['academic', 'reflect', 'equipment', 'program', 'rarely', 'national', 'social',
    #             'movies', 'greasy', 'water']
    keywords = list({
        'oily', 'people', 'before', 'living', 'potatoes', 'children',
        'overalls', 'morning', 'enough', 'system', 'water', 'greasy', 'suit',
        'dark', 'very', 'without', 'money', 'reflect', 'program', 'national',
        'social', 'water', 'carry', 'time', 'before', 'always', 'often',
        'people', 'money', 'potatoes', 'children'
    })
    # keywords = ['oily', 'people', 'before', 'living', 'water', 'children']
    # keywords = ['water', 'carry', 'time', 'before', 'always', 'often', 'people', 'money', 'potatoes', 'children']

    pkl_name = config['dir']['pickle'] + 'test_cases_' + str(num_templates) + '_' + \
               str(num_compares) + '_' + str(num_none) + '.pkl'

    # generate cases to be tested on
    cases = gen_cases('../datasets/TIMIT/TEST/', '../datasets/TIMIT/TRAIN/',
                      pkl_name, num_templates, num_compares, num_none,
                      keywords, config['gen_template'])

    a = BatchTestModel(config, cases)
    a.gen_pickle()

    infer_mode = config['infer_mode']

    if os.path.exists(results_dump_path):
        with open(results_dump_path, 'rb') as f:
            final_results = pickle.load(f)
    else:

        config = read_yaml()
        a = BatchTestModel(config, cases)

        # out_for_cnn = {}

        # Q values and probabilities are loaded. Important to load probability values from HERE since
        # they influence thresholds and Q-values
        qval_pth = config['dir']['pickle'] + 'final_q_vals.pkl'
        prob_pth = config['dir']['pickle'] + a.rnn.arch_name + '_probs.pkl'

        (thresholds, insert_prob, delete_prob,
         replace_prob) = find_batch_q(qval_pth, prob_pth, 75, dec_type, top_n,
                                      exp_factor)

        # dictionary for storing c values required to declare keyword
        final_results = {}
        for kw in cases.keys():
            final_results[kw] = {}
            # out_for_cnn[kw] = []

        # initialise model
        db, phone_to_id = a.get_outputs()
        id_to_phone = {v: k for k, v in phone_to_id.items()}

        assert len(phone_to_id) == replace_prob.shape[0] + 1

        # iterate over every clip and compare it with every template one-by-one
        # note that gr_phone_entire_clip is NOT USED
        for i, (output, length, gr_phone_entire_clip, label_lens, word_in_clip,
                wav_path) in enumerate(db):
            print("On output:", str(i) + "/" + str(len(db)))
            cur_out = output[:length]

            # generate lattice from current predictions
            lattices = generate_lattice(cur_out, a.rnn.model.blank_token_id,
                                        dec_type, top_n)
            # compare with every template
            for template_word, templates in cases.items():

                # if no keyword, then continue
                if template_word == 'NONE':
                    continue

                templates = templates['templates']
                final_results[template_word][i] = {'data': [], 'metadata': ()}

                for gr_phones in templates:
                    # template phone sequence
                    gr_phone_ids = [phone_to_id[x] for x in gr_phones]

                    (pred_phones,
                     node_prob), final_lattice = traverse_best_lattice(
                         lattices, dec_type, gr_phone_ids, insert_prob,
                         delete_prob, replace_prob)
                    # out_for_cnn[word_in_clip].append((pred_phones, node_prob, word_in_clip == template_word))
                    # node probabilities of best lattice
                    substring_phones = [id_to_phone[x] for x in pred_phones]
                    final_lattice = [id_to_phone[x[0]] for x in final_lattice]
                    # calculate q values
                    q_vals = find_q_values(gr_phone_ids, pred_phones,
                                           node_prob, insert_prob, delete_prob,
                                           replace_prob)
                    metadata = (wav_path, template_word, gr_phone_entire_clip,
                                final_lattice, substring_phones, gr_phones)

                    if infer_mode == 'group':
                        # sum up the predicted q values
                        predicted_log_val, gr_log_val = 0, 0

                        for pred_phone, vals in q_vals.items():
                            for val in vals:
                                predicted_log_val += np.log(val)
                            gr_log_val += (np.log(thresholds[pred_phone][0]) *
                                           len(vals))

                        if template_word == word_in_clip:
                            # gr_log_val should be < predicted_log_val + c
                            final_results[template_word][i]['data'].append(
                                ('right', gr_log_val, predicted_log_val))
                        else:
                            # gr_log_val should be > predicted_log_val + c
                            final_results[template_word][i]['data'].append(
                                ('wrong', gr_log_val, predicted_log_val))

                    elif infer_mode == 'indi':
                        above = 0
                        total_phones = 0
                        for pred_phone, vals in q_vals.items():
                            total_phones += len(vals)
                            for val in vals:
                                if val >= thresholds[pred_phone][0]:
                                    above += 1

                        if template_word == word_in_clip:
                            # gr_log_val should be < predicted_log_val + c
                            final_results[template_word][i]['data'].append(
                                ('right', above / total_phones))
                            # print('YES', above / total_phones)
                        else:
                            # gr_log_val should be > predicted_log_val + c
                            # print('NO', above / total_phones)
                            final_results[template_word][i]['data'].append(
                                ('wrong', above / total_phones))

                    else:
                        print("Infer Mode not defined")
                        exit(0)

                    # storing multiple results for each i corresponding to multiple templates
                    final_results[template_word][i]['metadata'] = metadata

        with open(results_dump_path, 'wb') as f:
            pickle.dump(final_results, f)
            print("Dumped final results of testing")

        # with open(cnn_dump_path, 'wb') as f:
        #     pickle.dump((out_for_cnn, cases), f)
        #     print("Dumped outputs for CNN training")

    # grid search over parameter C
    if config['infer_mode'] == 'group':
        cvals = list(np.arange(0, 5, 0.1))
        prec_recall_dat = {}
        for c in cvals:
            prec_recall_dat[c] = {'tp': 0, 'fp': 0, 'tn': 0, 'fn': 0}

    elif config['infer_mode'] == 'indi':
        cvals = list(np.arange(0, 1, 0.05))
        prec_recall_dat = {}
        for c in cvals:
            prec_recall_dat[c] = {'tp': 0, 'fp': 0, 'tn': 0, 'fn': 0}

    else:
        print("Infer Mode not defined")
        exit(0)

    # store incorect predictions
    wrong = []

    # if any one of the templates match, declare keyword found, else not found
    for c in cvals:
        for word, res in final_results.items():
            for iteration, d in res.items():
                metadata = d['metadata']
                egs = d['data']
                if egs[0][0] == 'right':
                    found = False

                    if infer_mode == 'group':
                        for _, gr, pred in egs:
                            if pred + c >= gr:
                                found = True
                    elif infer_mode == 'indi':
                        for _, ratio in egs:
                            if ratio >= c:
                                found = True
                    else:
                        print("Infer Mode not defined")
                        exit(0)

                    if found:
                        prec_recall_dat[c]['tp'] += 1
                    else:
                        prec_recall_dat[c]['fn'] += 1
                        if metadata not in wrong and 1.5 <= c <= 2.5:
                            wrong.append(metadata)

                else:
                    found = False
                    if infer_mode == 'group':
                        for _, gr, pred in egs:
                            if pred + c >= gr:
                                found = True
                    elif infer_mode == 'indi':
                        for _, ratio in egs:
                            if ratio >= c:
                                found = True
                    else:
                        print("Infer Mode not defined")
                        exit(0)

                    if found:
                        prec_recall_dat[c]['fp'] += 1
                        if metadata not in wrong and 1.5 <= c <= 2.5:
                            wrong.append(metadata)
                    else:
                        prec_recall_dat[c]['tn'] += 1

    # store metrics in dictionary
    fscore = []
    for c, vals in prec_recall_dat.items():
        prec = vals['tp'] / (vals['tp'] + vals['fp'])
        recall = vals['tp'] / (vals['tp'] + vals['fn'])
        prec_recall_dat[c]['prec-recall'] = (prec, recall, 2 * prec * recall /
                                             (prec + recall))
        fscore.append(2 * prec * recall / (prec + recall))

    # dump JSON
    print('Max F-score for exp_factor', exp_factor, 'is', max(fscore))
    with open(pr_dump_path, 'w') as f:
        json.dump(prec_recall_dat, f, indent=4)
    print("Dumped JSON")

    # store incorrectly predicted metadata
    word_name_dict = {}

    if not os.path.exists(wrong_pred_path):
        os.mkdir(wrong_pred_path)

    np.random.shuffle(wrong)

    for (wav_path, template_word, gr_phone_entire_clip, final_lattice,
         substring_phones, gr_phones) in wrong[:20]:

        gr_phones = list(gr_phones)

        if template_word not in word_name_dict:
            word_name_dict[template_word] = 0

        if len(gr_phone_entire_clip) > len(final_lattice):
            final_lattice += ['-'] * (len(gr_phone_entire_clip) -
                                      len(final_lattice))
        else:
            gr_phone_entire_clip += ['-'] * (len(final_lattice) -
                                             len(gr_phone_entire_clip))

        if len(substring_phones) > len(gr_phones):
            gr_phones += ['-'] * (len(substring_phones) - len(gr_phones))
        else:
            substring_phones += ['-'
                                 ] * (len(gr_phones) - len(substring_phones))

        final_string = ".wav path: " + wav_path + '\n'
        final_string += "Looking for: " + template_word + '\n'
        final_string += "Ground truth lattice || Predicted Lattice\n"
        for gr, pred in zip(gr_phone_entire_clip, final_lattice):
            final_string += (gr + '\t\t\t\t' + pred + '\n')

        final_string += "\nTemplate || Best substring\n"
        for gr, pred in zip(gr_phones, substring_phones):
            final_string += (gr + '\t\t\t\t' + pred + '\n')

        fname = wrong_pred_path + template_word + '_' + str(
            word_name_dict[template_word]) + '.txt'
        with open(fname, 'w') as f:
            f.write(final_string)
        word_name_dict[template_word] += 1

    return max(fscore)
コード例 #5
0
#!/usr/bin/env python3

import yaml
from read_yaml import read_yaml
from read_yaml import write_yaml
from pprint import pprint as pp


filename = "test_yml.yml"    # Update filename


# def read_yaml(filename):
#     with open(filename) as f:
#         return pp(yaml.load(f))




read_yaml("tst.yml")
コード例 #6
0
import numpy as np
import matplotlib.pyplot as plt
import pickle
from read_yaml import read_yaml

import json
import torch
import torch.optim as optim
import torch.nn.functional as F
import torch.nn as nn

db_path = 'pickle/cnn.pkl'
config = read_yaml()
np.random.seed(7)

# Load mapping
with open(config['dir']['dataset'] + 'phone_mapping.json', 'r') as f:
    phone_to_id = json.load(f)
    # drop weights
    phone_to_id = {k: v[0] for k, v in phone_to_id.items() if k != 'PAD'}
    print("Total phone classes:", len(phone_to_id))

with open(db_path, 'rb') as f:
    out_rnn, cases = pickle.load(f)
    cases = {k: v['templates'] for k, v in cases.items()}
    # out_rnn elements are: pred_phones, node_prob
    print("Read outputs for CNN training")

blank_id = len(phone_to_id)
use_cuda = torch.cuda.is_available()
max_l = 20
コード例 #7
0
                pathlib.Path(photo_files[0]).parent.parent,
                "." + pathlib.Path(photo_files[0]).parent.name + "_original"))

        pathlib.Path(output_path).parent.rename(
            pathlib.Path(
                pathlib.Path(photo_files[0]).parent.parent,
                pathlib.Path(photo_files[0]).parent.name))


# def create_BG_mask(cfg):
#     if "masks" in cfg and cfg["masks"]["enabled"] and cfg["masks"]["generate"]:
#         a = glob.iglob(str(pathlib.Path(cfg["photo_path"],"**","*.*")))   #(([jJ][pP][gG])|([tT][iI][fF]))
#         b = [path for path in a]
#         photo_files = [x for x in b if (re.search("(.tif$)|(.jpg$)|(.TIF$)|(.JPG$)",x) and (not re.search("_mask.",x)))]

#         for file in photo_files:
#             if pathlib.Path(file).name in cfg["masks"]['file_list']:
#                 print(pathlib.Path(file).name)

if __name__ == "__main__":
    try:
        config_file = sys.argv[1]
    except:
        config_file = manual_config_file

    # read the YML configuration file with input
    # TODO: move rename parameter to cfg file
    cfg = read_yaml(config_file)
    if "extractObject" in cfg and cfg["extractObject"]["enabled"]:
        extract_object(cfg)
コード例 #8
0
def find_batch_q(dump_path, prob_path, dec_type, top_n, exp_factor, rnn_model=None,
                 min_phones=200, min_sub_len=4, max_sub_len=15):
    """
    Computes the q-vale for each phone averaged over a specified number of instances
    :param rnn_model: dl_model object handle
    :param max_sub_len: max length of random subsequence chosen from gr_phone for q-value calculation
    :param min_sub_len: min length of random subsequence chosen from gr_phone for q-value calculation
    :param prob_path: path to probability file
    :param dump_path: path to dump file
    :param min_phones: minimum number of phones to be covered
    :param dec_type: max or CTC
    :param top_n: top_n sequences to be considered
    :param exp_factor: weight assigned to probability score
    :return: a dictionary of q-value for each phone and probabilities for insertion, deletion, substitution
    """

    if os.path.exists(dump_path):
        with open(dump_path, 'rb') as f:
            vals = pickle.load(f)
            print('Loaded Q values from dump:', vals[0])
            return vals

    config = read_yaml()
    phone_to_id = utils.load_phone_mapping(config)
    blank_token_id = phone_to_id['BLANK']

    if rnn_model is None:
        rnn_model = dl_model('infer')

    if not os.path.exists(config['dir']['pickle']):
        os.mkdir(config['dir']['pickle'])

    database_name = os.path.join(config['dir']['pickle'], rnn_model.arch_name, 'QValGenModel_in_' + str(min_phones) + '.pkl')
    model_out_name = os.path.join(config['dir']['pickle'], rnn_model.arch_name, 'QValGenModel_out_' + str(min_phones) + '.pkl')

    # Instantiates the model to calculate predictions
    dataloader = qval_dataloader(config, min_phones, database_name)
    model = qval_model(config, model_out_name, dataloader, rnn_model)

    db = model.get_outputs()

    # load probabilities vectors
    with open(prob_path, 'rb') as f:
        insert_prob, delete_prob, replace_prob = pickle.load(f)
        div = config['prob_thesh_const']

        temp = np.where(replace_prob == 0, 1, replace_prob)
        minimum = np.min(np.min(temp))
        print("Minimum substitution prob:", minimum)
        replace_prob = np.where(replace_prob == 0, minimum / div, replace_prob)

        temp = np.where(insert_prob == 0, 1, insert_prob)
        minimum = np.min(temp)
        print("Minimum insertion prob:", minimum)
        insert_prob = np.where(insert_prob == 0, minimum / div, insert_prob)

        temp = np.where(delete_prob == 0, 1, delete_prob)
        minimum = np.min(temp)
        print("Minimum deletion prob:", minimum)
        delete_prob = np.where(delete_prob == 0, minimum / div, delete_prob)

    final_dict = {}
    insert_prob_pow, delete_prob_pow, replace_prob_pow = np.power(insert_prob, exp_factor), \
                                                         np.power(delete_prob, exp_factor), \
                                                         np.power(replace_prob, exp_factor)

    print("Probabilities:\nInsert:", insert_prob, '\nDelete:', delete_prob, '\nSubsti:', replace_prob)

    # for each sentence in database, find best subsequence, align and calculate q values
    for i, (output, length, gr_phone, label_lens) in enumerate(db):
        if i % (len(db)//10) == 0:
            print("On output:", str(i) + "/" + str(len(db)))
        cur_out = output[:length]
        gr_phone_ids = np.array(gr_phone[:label_lens])
        random_subsequence_len = min(np.random.randint(min_sub_len, max_sub_len), len(gr_phone_ids)-1)
        sub_start = np.random.randint(0, len(gr_phone_ids) - random_subsequence_len)
        random_subsequence = gr_phone_ids[sub_start:sub_start + random_subsequence_len]

        # Generate lattice from current predictions
        lattices = generate_lattice(cur_out, blank_token_id, dec_type, top_n)
        # Find best subsequence in lattice
        res_substring, _ = traverse_best_lattice(lattices, dec_type, random_subsequence,
                                                 insert_prob, delete_prob, replace_prob)
        # Calculate q values by comparing template and best match
        q_vals = find_q_values(random_subsequence, res_substring[0], res_substring[1],
                               insert_prob_pow, delete_prob_pow, replace_prob_pow)

        # Add them up
        for ph_id, list_of_qvals in q_vals.items():
            if ph_id not in final_dict.keys():
                final_dict[ph_id] = []
            final_dict[ph_id] += list_of_qvals

    # Average out the values
    final_dict = {k: (sum(v) / len(v), len(v)) for k, v in final_dict.items()}

    with open(dump_path, 'wb') as f:
        pickle.dump((final_dict, insert_prob, delete_prob, replace_prob), f)

    print("Q values:", final_dict)
    return final_dict, insert_prob, delete_prob, replace_prob
コード例 #9
0
def find_batch_q(dump_path, prob_path, min_phones, dec_type, top_n,
                 exp_factor):
    """
    Computes the q-vale for each phone averaged over a specified number of instances
    :param dump_path: path to dump file
    :param min_phones: minimum number of phones to be covered
    :param dec_type: max or CTC
    :param top_n: top_n sequences to be considered
    :param exp_factor: weight assigned to probability score
    :return: a dictionary of q-value for each phone and probabilities for insertion, deletion, substitution
    """

    if os.path.exists(dump_path):
        with open(dump_path, 'rb') as f:
            vals = pickle.load(f)
            print('Loaded Q values from dump:', vals[0])
            return vals

    config = read_yaml()

    if not os.path.exists(config['dir']['pickle']):
        os.mkdir(config['dir']['pickle'])

    database_name = config['dir']['pickle'] + 'QValGenModel_in_' + str(
        min_phones) + '.pkl'
    model_out_name = config['dir']['pickle'] + 'QValGenModel_out_' + str(
        min_phones) + '.pkl'

    # Instantiates the model to calculate predictions
    a = QValGenModel(config, min_phones, database_name, model_out_name)
    db, phone_to_id = a.get_outputs()

    # load probabilities vectors
    with open(prob_path, 'rb') as f:
        insert_prob, delete_prob, replace_prob = pickle.load(f)
        div = config['prob_thesh_const']

        temp = np.where(replace_prob == 0, 1, replace_prob)
        minimum = np.min(np.min(temp))
        print("Minimum substitution prob:", minimum)
        replace_prob = np.where(replace_prob == 0, minimum / div, replace_prob)

        temp = np.where(insert_prob == 0, 1, insert_prob)
        minimum = np.min(temp)
        print("Minimum insertion prob:", minimum)
        insert_prob = np.where(insert_prob == 0, minimum / div, insert_prob)

        temp = np.where(delete_prob == 0, 1, delete_prob)
        minimum = np.min(temp)
        print("Minimum deletion prob:", minimum)
        delete_prob = np.where(delete_prob == 0, minimum / div, delete_prob)

    final_dict = {}
    insert_prob, delete_prob, replace_prob = np.power(insert_prob, exp_factor), np.power(delete_prob, exp_factor), \
                                             np.power(replace_prob, exp_factor)

    print("Probabilities:\nInsert:", insert_prob, '\nDelete:', delete_prob,
          '\nSubsti:', replace_prob)

    # for each sentence in database, find best subsequence, align and caluclate q values
    for i, (output, length, gr_phone, label_lens) in enumerate(db):
        print("On output:", str(i) + "/" + str(len(db)))
        cur_out = output[:length]
        gr_phone_ids = np.array(gr_phone[:label_lens])

        # Generate lattice from current predictions
        lattices = generate_lattice(cur_out, a.rnn.model.blank_token_id,
                                    dec_type, top_n)
        # Find best subsequence in lattice
        res_substring, final_lattice = traverse_best_lattice(
            lattices, dec_type, gr_phone_ids, insert_prob, delete_prob,
            replace_prob)
        # Calculate q values by comparing template and best match
        q_vals = find_q_values(gr_phone_ids, res_substring[0],
                               res_substring[1], insert_prob, delete_prob,
                               replace_prob)

        # Add them up
        for ph_id, list_of_qvals in q_vals.items():
            if ph_id not in final_dict.keys():
                final_dict[ph_id] = []
            final_dict[ph_id] += list_of_qvals
    # Average out the values
    final_dict = {k: (sum(v) / len(v), len(v)) for k, v in final_dict.items()}

    with open(dump_path, 'wb') as f:
        pickle.dump((final_dict, insert_prob, delete_prob, replace_prob), f)

    print("Q values:", final_dict)
    return final_dict, insert_prob, delete_prob, replace_prob
コード例 #10
0
ファイル: dataloader.py プロジェクト: zzdxlee/rnn_kws
        else:
            return inputs, labels, input_lens, label_lens, 0

    def __len__(self):

        return (self.num_egs + self.batch_size - 1) // self.batch_size


class timit_dataloader(generic_dataloader):
    def __init__(self, type_, config_file):
        if type_ == 'train':
            super().__init__(config_file, config_file['train']['batch_size'])
        else:
            super().__init__(config_file, config_file['train']['batch_size'])

        from metadata import timit_metadata
        metadata = timit_metadata(type_.upper(), config_file)
        # Returns huge list of feature vectors of audio recordings and phone sequences as tuples
        list_of_sent = metadata.gen_pickle()
        phone_to_id = utils.load_phone_mapping(config_file)

        self.build_dataset(list_of_sent, phone_to_id)


if __name__ == '__main__':

    from read_yaml import read_yaml
    config = read_yaml('config.yaml')
    a = timit_dataloader('test', config)
    print(a.return_batch())
コード例 #11
0
ファイル: dl_model.py プロジェクト: xdcesc/rnn_kws
    def __init__(self, mode):

        # Read config fielewhich contains parameters
        self.config = read_yaml()
        self.mode = mode

        if self.config['rnn'] == 'liGRU':
            from ligru import liGRU as Model
        elif self.config['rnn'] == 'GRU' or self.config['rnn'] == 'LSTM':
            from rnn import RNN as Model
        elif self.config['rnn'] == 'TCN':
            from tcnn import TCN as Model
        elif self.config['rnn'] == 'BTCN':
            from tcnn import bidirectional_TCN as Model
        elif 'custom' in self.config['rnn']:
            from rnn import customRNN as Model
        else:
            print("Model import failed")
            exit(0)

        # Architecture name decides prefix for storing models and plots
        feature_dim = self.config['n_fbank'] + self.config['n_mfcc']
        self.arch_name = '_'.join([
            self.config['rnn'],
            str(self.config['num_layers']),
            str(self.config['hidden_dim']),
            str(feature_dim)
        ])

        print("Architecture:", self.arch_name)
        # Change paths for storing models
        self.config['dir']['models'] = self.config['dir']['models'].split(
            '/')[0] + '_' + self.arch_name + '/'
        self.config['dir']['plots'] = self.config['dir']['plots'].split(
            '/')[0] + '_' + self.arch_name + '/'

        # Make folders if DNE
        if not os.path.exists(self.config['dir']['models']):
            os.mkdir(self.config['dir']['models'])
        if not os.path.exists(self.config['dir']['plots']):
            os.mkdir(self.config['dir']['plots'])
        if not os.path.exists(self.config['dir']['pickle']):
            os.mkdir(self.config['dir']['pickle'])

        self.cuda = (self.config['cuda'] and torch.cuda.is_available())

        # load/initialise metrics to be stored and load model
        if mode == 'train' or mode == 'test':

            self.plots_dir = self.config['dir']['plots']
            # store hyperparameters
            self.total_epochs = self.config['train']['epochs']
            self.test_every = self.config['train']['test_every_epoch']
            self.test_per = self.config['train']['test_per_epoch']
            self.print_per = self.config['train']['print_per_epoch']
            self.save_every = self.config['train']['save_every']
            self.plot_every = self.config['train']['plot_every']

            # declare model

            # dataloader which returns batches of data
            self.train_loader = timit_loader('train', self.config)
            self.test_loader = timit_loader('test', self.config)
            self.model = Model(self.config, mode)

            self.start_epoch = 1
            self.edit_dist = []
            self.train_losses, self.test_losses = [], []

        else:

            self.model = Model(self.config, mode)

        if self.cuda:
            self.model.cuda()

        # resume training from some stored model
        if self.mode == 'train' and self.config['train']['resume']:
            self.start_epoch, self.train_losses, self.test_losses, self.edit_dist = self.model.load_model(
                mode, self.model.rnn_name, self.model.num_layers,
                self.model.hidden_dim)
            self.start_epoch += 1

        # load best model for testing/feature extraction
        elif self.mode == 'test' or mode == 'test_one':
            self.model.load_model(mode, self.config['rnn'],
                                  self.model.num_layers, self.model.hidden_dim)

        # Replacement phones
        self.replacement = utils.replacement_dict()
コード例 #12
0
    def __init__(self, mode):

        #read config fiel which contains parameters
        self.config_file = read_yaml()
        self.mode = mode

        self.cuda = (self.config_file['cuda'] and torch.cuda.is_available())
        self.output_dim = self.config_file['output_dim']

        self.lcont, self.rcont = self.config_file[
            'left_context'], self.config_file['right_context']

        #import the necessary architecture
        if self.config_file['depth'] == 'deep':
            from model import DeepVanillaNN as Model
        elif self.config_file['depth'] == 'shallow':
            from model import ShallowVanillaNN as Model
        else:
            print("Can't identify model")
            exit(0)

        if mode == 'train' or mode == 'test':

            self.plots_dir = self.config_file['dir']['plots']
            #store hyperparameters
            self.total_epochs = self.config_file['train']['epochs']
            self.test_every = self.config_file['train']['test_every_epoch']
            self.test_per = self.config_file['train']['test_per_epoch']
            self.print_per = self.config_file['train']['print_per_epoch']
            self.save_every = self.config_file['train']['save_every']
            self.plot_every = self.config_file['train']['plot_every']
            #dataloader which returns batches of data
            self.train_loader = timit_loader('train', self.config_file)
            self.test_loader = timit_loader('test', self.config_file)

            #ensure that no of phone classes = output dimension
            assert self.train_loader.num_phoenes == self.output_dim

            self.start_epoch = 1
            self.test_acc = []
            self.train_losses, self.test_losses = [], []
            #declare model
            self.model = Model(self.config_file,
                               weights=self.train_loader.distri_weights)

        else:

            self.model = Model(self.config_file,
                               weights=np.ones((self.output_dim)))

        #load id to phone mapping
        fname = self.config_file['dir']['dataset'] + 'test_mapping.json'
        try:
            with open(fname, 'r') as f:
                self.id_to_phones = json.load(f)
        except:
            print("Cant find mapping")
            exit(0)

        if self.cuda:
            self.model.cuda()

        #resume training from some stored model
        if (self.mode == 'train' and self.config_file['train']['resume']):
            self.start_epoch, self.train_losses, self.test_losses, self.test_acc = self.model.load_model(
                mode)
            self.start_epoch += 1

        #load bets model for testing/feature extraction
        elif self.mode == 'test' or mode == 'test_one':
            self.model.load_model(mode)
コード例 #13
0
#!/usr/bin/env python3

import yaml
from read_yaml import read_yaml
from read_yaml import write_yaml
from pprint import pprint as pp

filename = "foo.yml"  # Update filename

# def read_yaml(filename):
#     with open(filename) as f:
#         return pp(yaml.load(f))

read_yaml("nutz.yml")
コード例 #14
0
ファイル: key_work.py プロジェクト: tianmeng-wxk/pycharm
import requests
from read_yaml import read_yaml
data = read_yaml()


class Httpclien():
    def __init__(self):
        self.url = data["login"]["url"]
        self.headers = {'Content-Type': 'application/x-www-form-urlencoded'}

    def get(self, data):
        try:
            r = requests.get(url=self.url, params=data, headers=self.headers)
            return r.json()
        except BaseException as e:
            raise ("接口发生未知错误", e)

    def post(self, data, files):
        try:
            r = requests.post(url=self.url,
                              data=data,
                              headers=self.headers,
                              files=files)
            return r.json()
        except BaseException as e:
            raise ("接口发生未知错误", e)

    def detelet(self, data):
        try:
            r = requests.post(url=self.url, data=data, headers=self.headers)
            return r.json()