Example #1
0
def train_from_parser(parser):
    """
    :param parser: ``ArgumentParser`` object
    """
    parser.add_argument("--config", type=str, required=True)
    parser.add_argument("--savedir", type=str, required=True)
    args = parser.parse_args()

    config = load_config(args.config)
    serialization_dir = os.path.join(DATA_DIR, args.savedir)

    if not os.path.exists(serialization_dir):
        os.makedirs(serialization_dir)

    def call_training_func(plugins):
        for p in plugins:
            p.on_before_call(config, serialization_dir)

        # TODO(tomwesolowski): Add config overriding from params.
        train_from_config(config=config, serialization_dir=serialization_dir)

        for p in plugins:
            p.on_after_call(config, serialization_dir)

    run_with_redirection(os.path.join(serialization_dir, 'stdout.txt'),
                         os.path.join(serialization_dir, 'stderr.txt'),
                         call_training_func)([MetaSaver()])
def create_loss_functions(input_var, network, target_var, margin, scale):
    config = load_config(None, join(get_common(), 'config.cfg'))

    # define loss expression
    train_prediction = lasagne.layers.get_output(network)
    train_loss = cosface_loss(train_prediction, target_var, margin, scale)

    test_prediction = lasagne.layers.get_output(network, deterministic=True)
    test_loss = cosface_loss(test_prediction, target_var, margin, scale)

    # define update expression for training
    train_params = lasagne.layers.get_all_params(network, trainable=True)
    train_updates = lasagne.updates.adadelta(
        train_loss, train_params,
        config.getfloat('pairwise_cosface', 'adadelta_learning_rate'),
        config.getfloat('pairwise_cosface', 'adadelta_rho'),
        config.getfloat('pairwise_cosface', 'adadelta_epsilon'))
    test_class_predict = T.argmax(test_prediction)

    # compile theano functions for training and validation/accuracy
    train_fn = theano.function([input_var, target_var],
                               train_loss,
                               updates=train_updates)
    val_fn = theano.function([input_var, target_var],
                             [test_loss, test_class_predict])
    return train_fn, val_fn
def dump_from_args(args):
    config = load_config(args.config)
    rng = prepare_environment(config)

    experiment = Experiment.from_config(config, rng=rng)
    model = experiment.model

    names, outputs = get_all_layer_outputs(model, experiment.streams.train)
    save_outputs(args.savedir, names, outputs)
Example #4
0
    def setUp(self):
        super(BuildTest, self).setUp()

        self.TEST_FIXTURES_ROOT = self.FIXTURES_ROOT / "build"
        self.files = {
            "config": "esim-snli.json",
        }

        self.config = load_config(self.TEST_FIXTURES_ROOT /
                                  self.files['config'])
def main():
    length = 500
    n_pairs = 50

    cfg = load_config('..\\cfg.json')

    dg = DataGatherer(cfg)
    dg.video_catalog = get_catalog(length)
    dg.pairs = get_pairs(dg.video_catalog, n_pairs)
    sorted_catalog = dg.sort_by_pairs()
    print sorted_catalog
Example #6
0
 def __init__(self, config_name=DEFAULT_CONFIG,
              setup=DEFAULT_SETUP, networks=DEFAULT_NETWORKS, train=DEFAULT_TRAIN, test=DEFAULT_TEST,
              plot=DEFAULT_PLOT, best=DEFAULT_BEST, dev=DEFAULT_DEV):
     self.config_name = config_name
     self.config = load_config(None, join(get_configs(), config_name + '.cfg'))
     self.setup = setup
     self.networks = networks
     self.train = train
     self.test = test
     self.plot = plot
     self.best = best
     self.dev = dev
     self.network_controllers = []
Example #7
0
 def create_net(self):
     config = load_config(None, join(get_common(), 'config.cfg'))
     model = Sequential()
     model.add(Bidirectional(LSTM(self.n_hidden1, return_sequences=True), input_shape=self.input))
     model.add(Dropout(0.50))
     model.add(Bidirectional(LSTM(self.n_hidden2)))
     model.add(Dense(self.n_classes * 10))
     model.add(Dropout(0.25))
     model.add(Dense(self.n_classes * 5))
     model.add(Dense(self.n_classes))
     model.add(Activation('softmax'))
     adam = keras.optimizers.Adam(config.getfloat('pairwise_lstm', 'adam_lr'), config.getfloat('pairwise_lstm', 'adam_beta_1'), config.getfloat('pairwise_lstm', 'adam_beta_2'), config.getfloat('pairwise_lstm', 'adam_epsilon'), config.getfloat('pairwise_lstm', 'adam_decay'))
     # ada = keras.optimizers.Adadelta(lr=1.0, rho=0.95, epsilon=1e-08, decay=0.0)
     model.compile(loss=kld.pairwise_kl_divergence,
                   optimizer=adam,
                   metrics=['accuracy'])
     return model
Example #8
0
def _add_cluster_subplot(grid, position, y_label, colspan=1):
    """
    Adds a cluster subplot to the current figure.

    :param grid: a tuple that contains number of rows as the first entry and number of columns as the second entry
    :param position: the position of this subplot
    :param y_label: the label of the y axis
    :param colspan: number of columns for the x axis, default is 1
    :return: the subplot itself
    """
    config = load_config(None, join(get_common(), 'config.cfg'))
    plot_width = config.getint('common', 'plot_width')
    subplot = plt.subplot2grid(grid, position, colspan=colspan)
    subplot.set_ylabel(y_label)
    subplot.set_xlabel('number of clusters')
    subplot.set_xlim([-3, plot_width + 3])
    subplot.set_ylim([-0.05, 1.05])
    return subplot
Example #9
0
        :param test_size: size of test set. It must be a float between 0 and 1
        :param random_state: random state / bean
        :param shuffle: boolean that determines if data should be shuffled
        :param stratify: If True, data is split in a stratified fashion
        :return:
        """
        train_x, test_x, train_y, test_y = \
            self.get_train_test_split(test_size=test_size,
                                      random_state=random_state,
                                      shuffle=shuffle,
                                      stratify=stratify)
        self.save_train_test_sets(inputs=(train_x, test_x),
                                  targets=(train_y, test_y),
                                  names=('training', 'test'))


if __name__ == '__main__':
    cfg = load_config('./conf/conf.yaml')
    init_logger(cfg['logging'], cfg['logging']['name'])
    prep = Preprocessor(cfg['data']['raw_path'],
                        cfg['preprocess']['criteria'],
                        cfg['data']['file_name_format'],
                        cfg['data']['classes_list'],
                        cfg['preprocess']['classes_ranges'],
                        cfg['preprocess']['dest_path'])
    prep.run(test_size=cfg['preprocess']['test_size'],
             random_state=cfg['preprocess']['random_state'],
             shuffle=cfg['preprocess']['shuffle'],
             stratify=True)

import numpy as np
import theano
import theano.tensor as T

import common.spectrogram.speaker_train_splitter as sts
from common.spectrogram.spectrogram_extractor import extract_spectrogram
from common.utils import pickler
from common.utils.paths import *
from common.utils.load_config import *
from . import network_factory as nf
from .objectives_clustering import create_loss_functions_kl_div
from ..core import analytics
from ..core.batch_iterators import SpectTrainBatchIterator, SpectValidBatchIterator
from common.spectrogram.speaker_dev_selector import get_sentences_for_speaker_index

config = load_config(None, join(get_common(), 'config.cfg'))

def create_and_train(num_epochs=1000, batch_size=100, epoch_batches=10, network_params_file_in=None,
                     network_params_file_out=None,
                     train_file=None,
                     network_fun=nf.create_network_10_speakers, with_validation=True):
    # load training data
    with open(train_file, 'rb') as f:
        (X, y, speaker_names) = pickle.load(f)

    # create symbolic theano variables
    input_var = T.tensor4('inputs')
    target_var = T.ivector('targets')
    margin = T.scalar('margin')

    # create network
Example #11
0
import numpy as np
import torch
import zarr
from dask import delayed, compute
from dask.diagnostics import ProgressBar
from skimage.io import imread
from skimage.transform import resize
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from torch.utils.data import Dataset, DataLoader
from tqdm import tqdm

import common
from common.utils import extract_video_frames, load_config, load_cnn

load_config()
URL = 'https://cloud.xibalba.com.mx/s/swZKJGnSFqdBXj4/download'
SPLITS_URL = 'https://cloud.xibalba.com.mx/s/X248be2WBPwsbzM/download'
FILENAME = 'ucf101-videos.tar.gz'
SPLITS_FILENAME = 'ucf101-splits.tar.gz'
DATASETS_DIR = os.getenv('DATASETS_DIR')
DS_DIR = join(DATASETS_DIR, 'ucf101')
SPLITS_DIR = join(DS_DIR, 'splits')
VIDEOS_DIR = join(DS_DIR, 'videos')
FRAMES_DIR = join(DS_DIR, 'frames')
FPS = 24
NAME_PADDING = 3
RESNET_INPUT_SIZE = (224, 224)
REPS_2048_DIR = join(DS_DIR, 'resnet50_2048.zarr')
REPS_1024_DIR = join(DS_DIR, 'resnet50_1024.zarr')
REPS_0512_DIR = join(DS_DIR, 'resnet50_0512.zarr')
Example #12
0
                    action='store_true',
                    help='NCHW Format, GPU only')
parser.add_argument('-ckpt',
                    dest='ckpt',
                    type=int,
                    help='Desired Checkpoint',
                    required=True)
parser.add_argument('-list',
                    dest='list',
                    type=int,
                    default=1,
                    help='Desired List to test. Default: List 1')

args = parser.parse_args()

cfg = load_config(path_master_config, args.config)

logger = get_logger('cluster', logging.INFO)

# Select List
if args.list == 1:
    list = 'test_list1'
elif args.list == 2:
    list = 'test_list2'
elif args.list == 3:
    list = 'test_list3'

output_layer_name = cfg.get('test', 'output_layer')

# Output folder
output_layer_name_folder = output_layer_name.replace('/', '-')
Example #13
0
 def __init__(self, n_classes=100, regularizer=None, **kwargs):
     super(CosFace, self).__init__(**kwargs)
     config = load_config(None, join(get_common(), 'config.cfg'))
     self.n_classes = config.getint('pairwise_lstm_cosface', 'n_classes')
     self.regularizer = regularizer
def train_network(config, clear, debug, nchw):
    # Read config
    path_master_config = get_configs('master')
    config = load_config(path_master_config, config)

    flow_me = "flow_me"
    flow_me_logs = get_experiment_logs(flow_me)
    flow_me_nets = get_experiment_nets(flow_me)

    if clear:
        try:
            shutil.rmtree(flow_me_logs)
        except OSError:
            pass

    if debug:
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '0'
        logger = get_logger('network', logging.DEBUG)
    else:
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'  # Filter warnings out
        logger = get_logger('network', logging.INFO)

    add_file_handler(logger, get_experiment_logs(flow_me, "training.log"))

    if not check_config(config):
        logger.critical('Config file {0} seems not valid.'.format(config.get('exp', 'name')))
        logger.info('Check if logfiles already exists or cannot be deleted.')
        sys.exit('Exit')

    logger.info('Experiment: \t' + config.get('exp', 'name'))
    logger.info('Description:\t' + config.get('exp', 'desc'))

    # Set parameters for network for nhwc oder nchw
    if nchw:
        data_format = 'channels_first'
        norm_axis = 1
        shape_ph_input_data = (
            None, 1, config.getint('spectrogram', 'frequency_elements'), config.getint('spectrogram', 'duration'))
    else:
        data_format = 'channels_last'
        norm_axis = 3
        shape_ph_input_data = (
            None, config.getint('spectrogram', 'frequency_elements'), config.getint('spectrogram', 'duration'), 1)

    logger.info("Starting to build Network")

    # placeholder
    with tf.name_scope('placeholders'):
        ph_input_data = tf.placeholder(tf.float32, shape=shape_ph_input_data, name='input_data')
        ph_map_labels = tf.placeholder(tf.int32, shape=(config.getint('net', 'batch_size')), name='map_labels')
        ph_class_input_data = tf.placeholder(tf.float32, shape=(config.getint(
            'train', 'total_speakers'), config.getfloat('net', 'dense10_factor') * config.getint('train',
                                                                                                 'total_speakers')),
                                             name='class_input_data')
        ph_train_mode = tf.placeholder(tf.bool, shape=[], name='train_mode')

        if debug:
            tf.summary.histogram('input', ph_input_data)
            if not nchw:
                tf.summary.image('input_image', ph_input_data, max_outputs=3)

    # L1: convolution (#32)
    with tf.name_scope('l1_conv'):
        l1 = tf.layers.conv2d(ph_input_data, filters=config.getint('net', 'conv1_filter'), kernel_size=[config.getint(
            'net', 'conv_kernel'), config.getint('net', 'conv_kernel')], padding=config.get('net', 'conv_pad'),
                              data_format=data_format, activation=tf.nn.relu)

    # L2: batch-norm
    with tf.name_scope('l2_batch'):
        if config.getboolean('net', 'norm_on'):
            l2 = tf.layers.batch_normalization(l1, axis=norm_axis, epsilon=config.getfloat('net', 'norm_eps'),
                                               momentum=config.getfloat('net', 'norm_mom'), training=ph_train_mode)
        else:
            l2 = l1

    # L3: max-pooling (4x4)
    with tf.name_scope('l3_max_pooling'):
        l3 = tf.layers.max_pooling2d(inputs=l2, pool_size=[config.getint('net', 'pool_size'), config.getint(
            'net', 'pool_size')], strides=config.getint('net', 'pool_strides'), data_format=data_format)

    # L4: convolution (#64)
    with tf.name_scope('l4_conv'):
        l4 = tf.layers.conv2d(inputs=l3, filters=config.getint('net', 'conv4_filter'), kernel_size=[config.getint(
            'net', 'conv_kernel'), config.getint('net', 'conv_kernel')], padding=config.get('net', 'conv_pad'),
                              data_format=data_format, activation=tf.nn.relu)

    # L5: batch-norm
    with tf.name_scope('l5_batch'):
        if config.getboolean('net', 'norm_on'):
            l5 = tf.layers.batch_normalization(l4, axis=norm_axis, epsilon=config.getfloat('net', 'norm_eps'),
                                               momentum=config.getfloat('net', 'norm_mom'), training=ph_train_mode)
        else:
            l5 = l4

    # L6: max-pooling (4x4)
    with tf.name_scope('l6_max_pooling'):
        l6 = tf.layers.max_pooling2d(inputs=l5, pool_size=[config.getint('net', 'pool_size'), config.getint(
            'net', 'pool_size')], strides=config.getint('net', 'pool_strides'), data_format=data_format)

    # L6: reshape after last conv
    with tf.name_scope('l6_reshape'):
        dim_flatted = int(np.prod(l6.shape[1:]))
        l6 = tf.reshape(l6, [-1, dim_flatted])

    # L7: dense
    with tf.name_scope('l7_dense'):
        l7 = tf.layers.dense(inputs=l6, units=(config.getfloat('net', 'dense7_factor') *
                                               config.getint('train', 'total_speakers')), activation=tf.nn.relu)

    # L8: batch-norm
    with tf.name_scope('l8_batch'):
        if config.getboolean('net', 'norm_on'):
            l8 = tf.layers.batch_normalization(l7, axis=1, epsilon=config.getfloat('net', 'norm_eps'),
                                               momentum=config.getfloat('net', 'norm_mom'), training=ph_train_mode)
        else:
            l8 = l7
    # L9: dropout
    with tf.name_scope('l9_dropout'):
        l9 = tf.layers.dropout(inputs=l8, rate=config.getfloat('net', 'dropout_rate'), training=ph_train_mode)

    # L10: dense
    with tf.name_scope('l10_dense'):
        l10 = tf.layers.dense(inputs=l9, units=(config.getfloat('net', 'dense10_factor') *
                                                config.getint('train', 'total_speakers')), activation=tf.nn.relu)

    # L11: dense
    with tf.name_scope('l11_dense'):
        l11 = tf.layers.dense(inputs=l10, units=(config.getfloat('net', 'dense11_factor') *
                                                 config.getint('train', 'total_speakers')), activation=None)

    # loss
    with tf.name_scope('loss'):
        loss = tf.map_fn(lambda x: tf.add(tf.norm(tf.subtract(x[0], ph_class_input_data[x[1]]), ord='euclidean'),
                                          tf.log(tf.add(1e-6, tf.reduce_sum(
                                              tf.map_fn(lambda z: tf.exp(tf.negative(tf.norm(tf.subtract(x[0], z),
                                                                                             ord='euclidean'))),
                                                        ph_class_input_data), 0)))), (l10, ph_map_labels),
                         dtype=tf.float32)

        loss = tf.reduce_mean(loss)

        tf.summary.scalar('loss', loss)

    if debug:
        with tf.name_scope('debug'):
            tf.summary.histogram('emb', l10)
            all_vars = tf.trainable_variables()
            for var in all_vars:
                tf.summary.histogram(var.name, var)

    # optimizer
    with tf.name_scope('optimizer'):
        if config.get('optimizer', 'name') == 'adam':
            optimizer = tf.train.AdamOptimizer(learning_rate=config.getfloat('optimizer', 'learning_rate'),
                                               beta1=config.getfloat(
                                                   'optimizer', 'beta1'), beta2=config.getfloat('optimizer', 'beta2'),
                                               epsilon=config.getfloat('optimizer', 'eps'))
        elif config.get('optimizer', 'name') == 'adadelta':
            optimizer = tf.train.AdadeltaOptimizer(learning_rate=config.getfloat(
                'optimizer', 'learning_rate'), rho=config.getfloat('optimizer', 'rho'),
                epsilon=config.getfloat('optimizer', 'eps'))
        else:
            optimizer = tf.train.MomentumOptimizer(learning_rate=config.getfloat(
                'optimizer', 'learning_rate'), momentum=config.getfloat('optimizer', 'momentum'), use_nesterov=True)
        train = optimizer.minimize(loss)

    logger.info("Finished building network")

    # Init
    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init)

    # TensorBoard ops
    summary = tf.summary.merge_all()
    saver = tf.train.Saver(max_to_keep=1000)
    train_writer = tf.summary.FileWriter(flow_me_logs, sess.graph)

    # load training and test data
    train_data_gen = DataGen.DataGen(config, data_set='train')
    validation_data_gen = DataGen.DataGen(config, data_set='validation')

    # training loop
    for step in range(config.getint('net', 'max_iter')):
        start_time = time.time()

        input_data_train, class_input_data_train, map_labels = train_data_gen.create_batch()

        if not nchw:
            input_data_train = np.transpose(input_data_train, axes=(0, 2, 3, 1))
            class_input_data_train = np.transpose(class_input_data_train, axes=(0, 2, 3, 1))

        # run z
        # it has to be ph_input_data, seems strange, but is right
        class_embeddings = sess.run(l10, feed_dict={ph_input_data: class_input_data_train, ph_train_mode: False})

        # run x
        _, loss_value = sess.run([train, loss], feed_dict={ph_input_data: input_data_train,
                                                           ph_map_labels: map_labels,
                                                           ph_class_input_data: class_embeddings,
                                                           ph_train_mode: True})

        duration = time.time() - start_time
        logger.info('Step {}: loss = {:.4f} ({:.3f} sec)'.format(step, loss_value, duration))

        # Write the summaries and print an overview every x-th step.
        if step % config.getint('net', 'sum_iter') == 0:
            # Print status

            start_time = time.time()
            # Update events file
            summary_str = sess.run(summary, feed_dict={ph_input_data: input_data_train,
                                                       ph_map_labels: map_labels,
                                                       ph_class_input_data: class_embeddings,
                                                       ph_train_mode: False})

            train_writer.add_summary(summary_str, step)
            train_writer.flush()

            duration = time.time() - start_time
            logger.debug('Step {}: Summary ({:.3f} sec)'.format(step, duration))

        # Save a checkpoint and evaluate the model periodically.
        if (step + 1) % config.getint('net', 'chkp_iter') == 0 or (step + 1) == config.getint('net', 'max_iter'):
            start_time = time.time()

            step_folder = 'step_{:05d}'.format(step)
            step_path = join(flow_me_logs, step_folder)
            test_writer = tf.summary.FileWriter(step_path, sess.graph)

            # Evaluate against the training set.
            input_data_test, map_labels = validation_data_gen.get_random_samples(config.getint('validation', 'samples'))

            if not nchw:
                input_data_test = np.transpose(input_data_test, axes=(0, 2, 3, 1))
            labels = validation_data_gen.get_labels()

            embeddings_test = sess.run(l10, feed_dict={ph_input_data: input_data_test, ph_train_mode: False})

            # Embeddings
            emb_var = tf.Variable(embeddings_test, name='embeddings')
            init_vars = tf.variables_initializer([emb_var])
            sess.run(init_vars)

            projector_config = projector.ProjectorConfig()
            embedding = projector_config.embeddings.add()
            embedding.tensor_name = emb_var.name

            # Save metafile with labels and link it to tensorboard.
            metafile_path = join(step_path, 'metadata.tsv')
            metafile_dir = os.path.dirname(metafile_path)
            if not os.path.exists(metafile_dir):
                os.makedirs(metafile_dir)

            with open(metafile_path, mode='w+') as f:
                for label_no in map_labels:
                    f.write(str(labels[label_no]) + '\n')

            embedding.metadata_path = metafile_path
            projector.visualize_embeddings(test_writer, projector_config)
            emb_saver = tf.train.Saver([emb_var])

            emb_saver.save(sess, join(step_path, 'emb.ckpt'), global_step=step)

            # Checkpoint
            saver.save(sess, join(flow_me_nets, 'model.ckpt'), global_step=step)

            duration = time.time() - start_time
            logger.info('Step {}: Checkpoint and Evaluation ({:.3f} sec)'.format(step, duration))

    sess.close()
from sqlalchemy import create_engine, MetaData
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from common.utils import load_config
from models.video import Video

__author__ = 'Rugnar'
cfg = load_config('..\\cfg.json')
Base = declarative_base()
engine = create_engine(cfg['db_address'], echo=True)
metadata = MetaData()
Session = sessionmaker(bind=engine)


def db_transaction(f):
    def magic(*args, **kwargs):
        session = args[0].db_session
        session.begin(subtransactions=True)
        try:
            ret = f(*args, **kwargs)
            session.commit()
            session.commit()
            return ret
        except BaseException:
            session.rollback()
            raise
    return magic


class DBStage(object):
    def __init__(self):
Example #16
0
def _plot_curves(plot_file_name, curve_names, metric_sets,
                 number_of_embeddings):
    """
    Plots all specified curves and saves the plot into a file.
    :param plot_file_name: String value of save file name
    :param curve_names: Set of names used in legend to describe this curve
    :param metric_sets: A list of 2D matrices, each row of a metrics 2D matrix describes one dataset for a curve
    :param number_of_embeddings: set of integers, each integer describes how many embeddings is in this curve
    """
    logger = get_logger('analysis', logging.INFO)
    logger.info('Plot results')

    config = load_config(None, join(get_common(), 'config.cfg'))
    plot_width = config.getint('common', 'plot_width')
    fig_width = config.getint('common', 'fig_width')
    fig_height = config.getint('common', 'fig_height')
    #Slice results to only 1-80 clusters
    for i in range(0, len(metric_sets)):
        for j in range(0, len(metric_sets[i])):
            metric_sets[i][j] = metric_sets[i][j][-plot_width:]
            print(len(metric_sets[i][j]))

    best_results = [[] for _ in metric_names]
    for m, min_value in enumerate(metric_min_values):
        for results in metric_sets[m]:
            if (metric_min_values[m] == 0):
                best_results[m].append(np.max(results))
            else:
                best_results[m].append(np.min(results))
    '''
    This code is used to sort the lines by min mr. Because we now use mutliple metrics and dont sort by a single
    metric, this code is not used anymore, but we keep it for now.
    min_mrs, curve_names, mrs, acps, aris, homogeneity_scores, completeness_scores, number_of_embeddings = \
        (list(t) for t in
         zip(*sorted(zip(min_mrs, curve_names, mrs, acps, aris, homogeneity_scores, completeness_scores, number_of_embeddings))))
    '''

    # How many lines to plot
    number_of_lines = len(curve_names)

    # Get various colors needed to plot
    color_map = plt.get_cmap('gist_rainbow')
    colors = [color_map(i) for i in np.linspace(0, 1, number_of_lines)]

    #Set fontsize for all plots
    plt.rcParams.update({'font.size': 12})

    # Define number of figures
    fig1 = plt.figure(figsize=(fig_width, fig_height))

    # Define Plots
    plot_grid = (3, 2)

    plots = [None] * len(metric_names)

    plots[0] = _add_cluster_subplot(plot_grid, (0, 0), metric_names[0], 1)
    plots[1] = _add_cluster_subplot(plot_grid, (0, 1), metric_names[1], 1)
    plots[2] = _add_cluster_subplot(plot_grid, (1, 0), metric_names[2], 1)
    plots[3] = _add_cluster_subplot(plot_grid, (1, 1), metric_names[3], 1)

    #Set the horizontal space between subplots
    plt.subplots_adjust(hspace=0.3)

    # Define curves and their values
    curves = [[] for _ in metric_names]

    for m, metric_set in enumerate(metric_sets):
        curves[m] = [plots[m], metric_set]

    # Plot all curves
    for index in range(number_of_lines):
        label = curve_names[index]
        for m, metric_name in enumerate(metric_names):
            label = label + '\n {} {}: {}'.format(
                'Max' if metric_min_values[m] == 0 else 'Min', metric_name,
                str(best_results[m][index]))
        color = colors[index]
        number_of_clusters = np.arange(plot_width, 0, -1)

        for plot, value in curves:
            plot.plot(number_of_clusters,
                      value[index],
                      color=color,
                      label=label)

    # Add legend and save the plot
    fig1.legend(loc='upper center', bbox_to_anchor=(0.5, 0.33), ncol=4)
    #fig1.show()
    fig1.savefig(get_result_png(plot_file_name))
    fig1.savefig(get_result_png(plot_file_name + '.svg'), format='svg')
Example #17
0
def test(checkpoint, list, configPath, nchw):
    path_master_config = './config/master.cfg'
    config = load_config(path_master_config, configPath)
    logger = get_logger('cluster', logging.INFO)

    list = 'test_list' + list.__str__()

    output_layer_name = config.get('test', 'output_layer')

    # Output folder
    output_layer_name = output_layer_name.replace('\\', '/')
    # output_layer_name = output_layer_name.replace(':', '')
    output_layer_name_folder = output_layer_name.replace(
        '\\', '-')  # NOTE: Windows modification, change for deploy
    output_layer_name_folder = output_layer_name_folder.replace(':', '')

    output_folder = os.path.join(config.get('output', 'plot_path'),
                                 config.get('exp',
                                            'name'), output_layer_name_folder,
                                 'ckpt_{:05d}'.format(checkpoint),
                                 config.get(list, 'name'))

    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    add_file_handler(logger, os.path.join(output_folder, 'mr.log'))

    # Load Session
    path_meta_graph = os.path.join(config.get('output', 'log_path'),
                                   'train_' + config.get('exp', 'name'),
                                   'model.ckpt-' + str(checkpoint) + '.meta')
    path_ckpt = os.path.join(config.get('output', 'log_path'),
                             'train_' + config.get('exp', 'name'),
                             'model.ckpt-' + str(checkpoint))

    sess, input_layer, output_layer, train_mode = restore_session(
        path_meta_graph, path_ckpt, output_layer_name)

    # Load test data
    data_gen_8 = DataGen.DataGen(config, data_set=list, pickle_no='pickle1')
    data_gen_2 = DataGen.DataGen(config, data_set=list, pickle_no='pickle2')

    input_data_8, map_labels_8 = data_gen_8.get_timit_test_set(
        sentence_pickle=config.getint(list, 'sentences_pickle1'))
    input_data_2, map_labels_2 = data_gen_2.get_timit_test_set(
        sentence_pickle=config.getint(list, 'sentences_pickle2'))

    labels = data_gen_8.get_labels(
    )  # Both data_gen instances should return the same labels

    if not nchw:
        input_data_8 = np.transpose(input_data_8, axes=(0, 2, 3, 1))
        input_data_2 = np.transpose(input_data_2, axes=(0, 2, 3, 1))

        batch_size = config.getint('net', 'batch_size')
        input_data_8 = input_data_8[0:batch_size][:][:][:]
        input_data_2 = input_data_2[0:batch_size][:][:][:]

        # (858, 128, 100, 1)
        # (?, 128, 100, 1)
        print(np.shape(input_data_8))
        print(np.shape(input_data_2))

    label_colors = get_colors(len(labels))

    # Pass samples trough network, get embeddings
    embeddings_8 = sess.run(output_layer,
                            feed_dict={
                                input_layer: input_data_8,
                                train_mode: False
                            })
    embeddings_2 = sess.run(output_layer,
                            feed_dict={
                                input_layer: input_data_2,
                                train_mode: False
                            })
    sess.close()

    # Reduce to mean, merge to one list of embeddings
    # Init data structures according to used embeddings dimension
    if 'l10_dense' in config.get('test', 'output_layer'):
        embeddings_mean_8 = np.zeros(
            (config.getint(list, 'total_speakers'),
             int(
                 config.getfloat('net', 'dense10_factor') *
                 config.getint('train', 'total_speakers'))))
        embeddings_mean_2 = np.zeros(
            (config.getint(list, 'total_speakers'),
             int(
                 config.getfloat('net', 'dense10_factor') *
                 config.getint('train', 'total_speakers'))))
    elif 'l11_dense' in config.get('test', 'output_layer'):
        embeddings_mean_8 = np.zeros(
            (config.getint(list, 'total_speakers'),
             int(
                 config.getfloat('net', 'dense11_factor') *
                 config.getint('train', 'total_speakers'))))
        embeddings_mean_2 = np.zeros(
            (config.getint(list, 'total_speakers'),
             int(
                 config.getfloat('net', 'dense11_factor') *
                 config.getint('train', 'total_speakers'))))
    else:
        embeddings_mean_8 = np.zeros(
            (config.getint(list, 'total_speakers'),
             int(
                 config.getfloat('net', 'dense7_factor') *
                 config.getint('train', 'total_speakers'))))
        embeddings_mean_2 = np.zeros(
            (config.getint(list, 'total_speakers'),
             int(
                 config.getfloat('net', 'dense7_factor') *
                 config.getint('train', 'total_speakers'))))

    # Sum of all embeddings
    for embedding, label in zip(embeddings_8, map_labels_8):
        embeddings_mean_8[label] += embedding
    for embedding, label in zip(embeddings_2, map_labels_2):
        embeddings_mean_2[label] += embedding

    # Divsion trough count of embeddings from the same speaker
    unique_8, counts_8 = np.unique(map_labels_8, return_counts=True)
    for label, count in zip(unique_8, counts_8):
        embeddings_mean_8[label] /= count

    unique_2, counts_2 = np.unique(map_labels_2, return_counts=True)
    for label, count in zip(unique_2, counts_2):
        embeddings_mean_2[label] /= count

    # Concatenate to one date set for analysis
    embeddings = np.concatenate((embeddings_mean_8, embeddings_mean_2))
    map_labels = np.concatenate((unique_8, unique_2))

    map_labels_full = []

    for label_no in map_labels:
        map_labels_full.append(labels[label_no])

    # Cluster embeddings
    embeddings += 0.00001  # hotfix for division by zero = nan
    embeddings_dist, embeddings_linkage = cluster_embeddings(embeddings)

    # Dendogram
    if config.getint(list, 'total_speakers') > 10:
        draw_dendogram(embeddings_linkage,
                       embeddings_dist,
                       labels,
                       map_labels,
                       map_labels_full,
                       label_colors,
                       figsize=(20, 4.8))
    elif config.getint(list, 'total_speakers') > 40:
        draw_dendogram(embeddings_linkage,
                       embeddings_dist,
                       labels,
                       map_labels,
                       map_labels_full,
                       label_colors,
                       figsize=(50, 4.8))
    else:
        draw_dendogram(embeddings_linkage, embeddings_dist, labels, map_labels,
                       map_labels_full, label_colors)

    plt.savefig(os.path.join(output_folder, 'dendo.png'), bbox_inches='tight')
    plt.savefig(os.path.join(output_folder, 'dendo.svg'), bbox_inches='tight')

    axes = plt.gca()
    axes.set_ylim([0, 0.1])
    plt.yticks(np.arange(0, 0.1, 0.005))
    plt.grid(axis='y', linestyle='dotted')
    plt.draw()
    plt.savefig(os.path.join(output_folder, 'dendo_0.1.png'),
                bbox_inches='tight')
    plt.savefig(os.path.join(output_folder, 'dendo_0.1.svg'),
                bbox_inches='tight')

    # t-SNE Plot
    ''' Take out tsne because of sklearn misbehaving
    if config.getint(list, 'total_speakers') > 20:
        legend = False
    else:
        legend = True

    draw_t_sne(embeddings, labels, map_labels, label_colors, legend=legend)
    plt.savefig(os.path.join(output_folder, 'tsne.png'), bbox_inches='tight')
    plt.savefig(os.path.join(output_folder, 'tsne.svg'), bbox_inches='tight')

    # t-SNE Plot of some random (raw) embeddings
    plt.clf()
    embeddings_raw = np.concatenate((embeddings_8, embeddings_2))  # original embeddings
    map_labels_raw = np.concatenate((map_labels_8, map_labels_2))

    embeddings_raw, map_labels_raw = shuffle(embeddings_raw, map_labels_raw)

    draw_t_sne(embeddings_raw[:50], labels, map_labels_raw[:50], label_colors, legend=legend)
    plt.savefig(os.path.join(output_folder, 'tsne_rand.png'), bbox_inches='tight')
    plt.savefig(os.path.join(output_folder, 'tsne_rand.svg'), bbox_inches='tight')
    '''

    # Calculate MR
    mr, map_clusters, threshold = calculate_minimal_mr(embeddings_linkage,
                                                       map_labels)
    legacy_mr = calculate_legacy_minimal_mr(
        embeddings_linkage, map_labels, config.getint(list, 'total_speakers'))

    # Save Matching of samples to clusters for the minimal MR
    with open(os.path.join(output_folder, 'cluster_map.txt'), mode='w+') as f:
        f.write('Matching of segments and clusters with minimal MR:\n')
        f.write('| {:8s} | {:8s} |\n'.format('Speaker', 'Cluster'))
        for label_no, cluster_no in zip(map_labels, map_clusters):
            f.write('| {:8s} | {:8d} |\n'.format(labels[label_no], cluster_no))

    # Print results
    logger.info(
        'MR = {:.4f} \t Threshold = {:.4f} \t (Legacy MR = {:.4f})'.format(
            mr, threshold, legacy_mr))
 def __init__(self, name, net_path):
     super().__init__()
     self.network_name = name
     self.logger = get_logger("luvo", logging.INFO)
     self.net_path = net_path
     self.config = load_config(None, join(get_common(), 'config.cfg'))
Example #19
0
    def test(self, checkpoint=0, list=1, config_path=None):

        if config_path is None:
            config_path = self.config_path

        path_master_config = get_configs('master')
        config = load_config(path_master_config, get_configs(config_path))
        logger = get_logger('cluster', logging.INFO)

        list = 'test_list' + str(list)

        # Log Output folder
        folder_name = str(config.get(list, 'total_speakers')) + "_speakers"
        checkpoint_folder = 'checkpoint_{:05d}'.format(checkpoint)
        output_folder = get_experiment_logs("flow_me", folder_name, checkpoint_folder)

        if not os.path.exists(output_folder):
            os.makedirs(output_folder)

        add_file_handler(logger, join(output_folder, 'mr.log'))

        # Load test data
        logger.info("Loading Test Data")
        data_gen_8 = DataGen.DataGen(config, data_set=list, pickle_no='pickle1')
        data_gen_2 = DataGen.DataGen(config, data_set=list, pickle_no='pickle2')

        input_data_8, map_labels_8 = data_gen_8.get_timit_test_set(
            sentence_pickle=config.getint(list, 'sentences_pickle1'))
        input_data_2, map_labels_2 = data_gen_2.get_timit_test_set(
            sentence_pickle=config.getint(list, 'sentences_pickle2'))

        labels = data_gen_8.get_labels()  # Both data_gen instances should return the same labels

        if not self.nchw:
            input_data_8 = np.transpose(input_data_8, axes=(0, 2, 3, 1))
            input_data_2 = np.transpose(input_data_2, axes=(0, 2, 3, 1))

        # Load Session
        output_layer_name = config.get('test', 'output_layer')
        output_layer_name = output_layer_name.replace('\\', '/')
        flow_me_nets = get_experiment_nets("flow_me")
        path_meta_graph = join(flow_me_nets, 'model.ckpt-' + str(checkpoint) + '.meta')
        path_ckpt = join(flow_me_nets, 'model.ckpt-' + str(checkpoint))

        sess, input_layer, output_layer, train_mode = restore_session(path_meta_graph, path_ckpt, output_layer_name)
        logger.info("Restored Session:\tCheckpoint = {:}\t{:}".format(checkpoint, folder_name))

        # Pass samples trough network, get embeddings
        embeddings_8 = sess.run(output_layer, feed_dict={input_layer: input_data_8, train_mode: False})
        embeddings_2 = sess.run(output_layer, feed_dict={input_layer: input_data_2, train_mode: False})
        sess.close()
        logger.info("Created Embeddings")

        # Reduce to mean, merge to one list of embeddings
        # Init data structures according to used embeddings dimension
        if 'l10_dense' in config.get('test', 'output_layer'):
            embeddings_mean_8 = np.zeros((config.getint(list, 'total_speakers'), int(config.getfloat(
                'net', 'dense10_factor') * config.getint('train', 'total_speakers'))))
            embeddings_mean_2 = np.zeros((config.getint(list, 'total_speakers'), int(config.getfloat(
                'net', 'dense10_factor') * config.getint('train', 'total_speakers'))))
        elif 'l11_dense' in config.get('test', 'output_layer'):
            embeddings_mean_8 = np.zeros((config.getint(list, 'total_speakers'), int(config.getfloat(
                'net', 'dense11_factor') * config.getint('train', 'total_speakers'))))
            embeddings_mean_2 = np.zeros((config.getint(list, 'total_speakers'), int(config.getfloat(
                'net', 'dense11_factor') * config.getint('train', 'total_speakers'))))
        else:
            embeddings_mean_8 = np.zeros((config.getint(list, 'total_speakers'), int(config.getfloat(
                'net', 'dense7_factor') * config.getint('train', 'total_speakers'))))
            embeddings_mean_2 = np.zeros((config.getint(list, 'total_speakers'), int(config.getfloat(
                'net', 'dense7_factor') * config.getint('train', 'total_speakers'))))

        # Sum of all embeddings
        for embedding, label in zip(embeddings_8, map_labels_8):
            embeddings_mean_8[label] += embedding
        for embedding, label in zip(embeddings_2, map_labels_2):
            embeddings_mean_2[label] += embedding

        # Divsion trough count of embeddings from the same speaker
        unique_8, counts_8 = np.unique(map_labels_8, return_counts=True)
        for label, count in zip(unique_8, counts_8):
            embeddings_mean_8[label] /= count

        unique_2, counts_2 = np.unique(map_labels_2, return_counts=True)
        for label, count in zip(unique_2, counts_2):
            embeddings_mean_2[label] /= count

        # Concatenate to one date set for analysis
        embeddings = np.concatenate((embeddings_mean_8, embeddings_mean_2))
        map_labels = np.concatenate((unique_8, unique_2))

        return embeddings, map_labels