Ejemplo n.º 1
0
def setup():
    """Parse command line arguments, load model parameters, load configurations
    and setup environment."""
    # Parse the command line arguments
    args = parse_arguments()

    # Load parameters
    params = load_yaml(args.params)

    # Load training configurations
    config = load_yaml(args.config)
    update_not_none(config, vars(args))

    # Set unspecified schedule steps to default values
    for target in (config['learning_rate_schedule'], config['slope_schedule']):
        if target['start'] is None:
            target['start'] = 0
        if target['end'] is None:
            target['end'] = config['steps']

    # Make sure result directory exists
    make_sure_path_exists(config['result_dir'])

    # Setup GPUs
    os.environ["CUDA_VISIBLE_DEVICES"] = config['gpu']

    return params, config
Ejemplo n.º 2
0
def get_save_metric_ops(tensor,
                        beat_resolution,
                        step,
                        result_dir,
                        suffix=None):
    """Return save metric ops."""
    # Get metric ops
    metric_ops = get_metric_ops(tensor, beat_resolution)

    # Make sure paths exist
    for key in metric_ops:
        make_sure_path_exists(os.path.join(result_dir, key))

    def _save_array(array, step, name):
        """Save the input array."""
        suffix_ = step if suffix is None else suffix
        filepath = os.path.join(result_dir, name,
                                '{}_{}.npy'.format(name, suffix_))
        np.save(filepath, array)
        return np.array([0], np.int32)

    save_metric_ops = {}
    for key, value in metric_ops.items():
        save_metric_ops[key] = tf.py_func(
            lambda array, step, k=key: _save_array(array, step, k),
            [value, step],
            tf.int32)

    return save_metric_ops
Ejemplo n.º 3
0
def setup():
    """Parse command line arguments, load model parameters, load configurations
    and setup environment."""
    # Parse the command line arguments
    args = parse_arguments()

    # Load parameters
    params = load_yaml(args.params)

    # Load training configurations
    config = load_yaml(args.config)
    update_not_none(config, vars(args))

    # Set unspecified schedule steps to default values
    for target in (config['learning_rate_schedule'], config['slope_schedule']):
        if target['start'] is None:
            target['start'] = 0
        if target['end'] is None:
            target['end'] = config['steps']

    # Make sure result directory exists
    make_sure_path_exists(config['result_dir'])

    # Setup GPUs
    os.environ["CUDA_VISIBLE_DEVICES"] = config['gpu']

    return params, config
Ejemplo n.º 4
0
def load_or_create_samples(params, config):
    """Load or create the samples used as the sampler inputs."""
    # greg: En caso de no estar condicionando, solamente se sacan muestras de z.

    # Load sample_z
    LOGGER.info("Loading sample_z.")
    sample_z_path = os.path.join(config['model_dir'], 'sample_z.npy')
    if os.path.exists(sample_z_path):
        sample_z = np.load(sample_z_path)
        if sample_z.shape[1] != params['latent_dim']:
            LOGGER.info("Loaded sample_z has wrong shape")
            resample = True
        else:
            resample = False
    else:
        LOGGER.info("File for sample_z not found")
        resample = True

    # Draw new sample_z
    if resample:
        LOGGER.info("Drawing new sample_z.")
        sample_z = scipy.stats.truncnorm.rvs(
            -2, 2, size=(np.prod(config['sample_grid']), params['latent_dim']))
        make_sure_path_exists(config['model_dir'])
        np.save(sample_z_path, sample_z)

    # greg: Solo si estamos condicionando se extraen muestras de sample_x
    if params.get('is_accompaniment'):
        # Load sample_x
        LOGGER.info("Loading sample_x.")
        sample_x_path = os.path.join(config['model_dir'], 'sample_x.npy')
        if os.path.exists(sample_x_path):
            sample_x = np.load(sample_x_path)
            if sample_x.shape[1:] != params['data_shape']:
                LOGGER.info("Loaded sample_x has wrong shape")
                resample = True
            else:
                resample = False
        else:
            LOGGER.info("File for sample_x not found")
            resample = True

        # Draw new sample_x
        if resample:
            LOGGER.info("Drawing new sample_x.")
            data = load_data(config['data_source'], config['data_filename'])
            sample_x = get_samples(
                np.prod(config['sample_grid']),
                data,
                use_random_transpose=config['use_random_transpose'])
            make_sure_path_exists(config['model_dir'])
            np.save(sample_x_path, sample_x)
    else:
        sample_x = None

    return sample_x, None, sample_z
Ejemplo n.º 5
0
def load_or_create_samples(params, config):
    """Load or create the samples used as the sampler inputs."""
    # Load sample_z
    LOGGER.info("Loading sample_z.")
    sample_z_path = os.path.join(config['model_dir'], 'sample_z.npy')
    if os.path.exists(sample_z_path):
        sample_z = np.load(sample_z_path)
        if sample_z.shape[1] != params['latent_dim']:
            LOGGER.info("Loaded sample_z has wrong shape")
            resample = True
        else:
            resample = False
    else:
        LOGGER.info("File for sample_z not found")
        resample = True

    # Draw new sample_z
    if resample:
        LOGGER.info("Drawing new sample_z.")
        sample_z = scipy.stats.truncnorm.rvs(
            -2, 2, size=(np.prod(config['sample_grid']), params['latent_dim']))
        make_sure_path_exists(config['model_dir'])
        np.save(sample_z_path, sample_z)

    if params['is_accompaniment']:
        # Load sample_x
        LOGGER.info("Loading sample_x.")
        sample_x_path = os.path.join(config['model_dir'], 'sample_x.npy')
        if os.path.exists(sample_x_path):
            sample_x = np.load(sample_x_path)
            if sample_x.shape[1:] != params['data_shape']:
                LOGGER.info("Loaded sample_x has wrong shape")
                resample = True
            else:
                resample = False
        else:
            LOGGER.info("File for sample_x not found")
            resample = True

        # Draw new sample_x
        if resample:
            LOGGER.info("Drawing new sample_x.")
            data = load_data(config['data_source'], config['data_filename'])
            sample_x = get_samples(
                np.prod(config['sample_grid']), data,
                use_random_transpose = config['use_random_transpose'])
            make_sure_path_exists(config['model_dir'])
            np.save(sample_x_path, sample_x)
    else:
        sample_x = None

    return sample_x, None, sample_z
Ejemplo n.º 6
0
def setup_dirs(config):
    """Setup an experiment directory structure and update the `params`
    dictionary with the directory paths."""
    # Get experiment directory structure
    config['exp_dir'] = os.path.realpath(config['exp_dir'])
    config['src_dir'] = os.path.join(config['exp_dir'], 'src')
    config['eval_dir'] = os.path.join(config['exp_dir'], 'eval')
    config['model_dir'] = os.path.join(config['exp_dir'], 'model')
    config['sample_dir'] = os.path.join(config['exp_dir'], 'samples')
    config['log_dir'] = os.path.join(config['exp_dir'], 'logs', 'train')

    # Make sure directories exist
    for key in ('log_dir', 'model_dir', 'sample_dir', 'src_dir'):
        make_sure_path_exists(config[key])
Ejemplo n.º 7
0
def setup_dirs(config):
    """Setup an experiment directory structure and update the `params`
    dictionary with the directory paths."""
    # Get experiment directory structure
    config['exp_dir'] = os.path.realpath(config['exp_dir'])
    config['src_dir'] = os.path.join(config['exp_dir'], 'src')
    config['eval_dir'] = os.path.join(config['exp_dir'], 'eval')
    config['model_dir'] = os.path.join(config['exp_dir'], 'model')
    config['sample_dir'] = os.path.join(config['exp_dir'], 'samples')
    config['log_dir'] = os.path.join(config['exp_dir'], 'logs', 'train')

    # Make sure directories exist
    for key in ('log_dir', 'model_dir', 'sample_dir', 'src_dir'):
        make_sure_path_exists(config[key])
Ejemplo n.º 8
0
def setup_dirs(config):
    """Setup an experiment directory structure and update the `params`
    dictionary with the directory paths."""
    # Get experiment directory structure
    config['exp_dir'] = os.path.realpath(config['exp_dir'])
    config['src_dir'] = os.path.join(config['exp_dir'], 'src')
    config['eval_dir'] = os.path.join(config['exp_dir'], 'eval')
    config['model_dir'] = os.path.join(config['exp_dir'], 'model')
    config['sample_dir'] = os.path.join(config['exp_dir'], 'samples')
    config['log_dir'] = os.path.join(config['exp_dir'], 'logs', 'train')

    config['metric_map'] = np.array([
            # indices of tracks for the metrics to compute
            [True] * 8,  # empty bar rate
            [True] * 8,  # number of pitch used
            [False] + [True] * 7,  # qualified note rate
            [False] + [True] * 7,  # polyphonicity
            [False] + [True] * 7,  # in scale rate
            [True] + [False] * 7,  # in drum pattern rate
            [False] + [True] * 7  # number of chroma used
        ], dtype=bool)

    config['tonal_distance_pairs']= [(1, 2)]  # pairs to compute the tonal distance
    config['scale_mask']= list(map(bool, [1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1]))
    config['drum_filter']= np.tile([1., .1, 0., 0., 0., .1], 16)
    config['tonal_matrix_coefficient']= (1., 1., .5)

    config['track_names'] = (
        'Drums', 'Piano', 'Guitar', 'Bass', 'Ensemble', 'Reed', 'Synth Lead',
        'Synth Pad'
    )
    config['beat_resolution']= 24

    # Make sure directories exist
    for key in ('log_dir', 'model_dir', 'sample_dir', 'src_dir'):
        make_sure_path_exists(config[key])
Ejemplo n.º 9
0
def get_save_metric_ops(tensor, beat_resolution, step, result_dir, suffix=None):
    """Return save metric ops."""
    # Get metric ops
    metric_ops = get_metric_ops(tensor, beat_resolution)

    # Make sure paths exist
    for key in metric_ops:
        make_sure_path_exists(os.path.join(result_dir, key))

    def _save_array(array, step, name):
        """Save the input array."""
        suffix_ = step if suffix is None else suffix
        filepath = os.path.join(
            result_dir, name, '{}_{}.npy'.format(name, suffix_))
        np.save(filepath, array)
        return np.array([0], np.int32)

    save_metric_ops = {}
    for key, value in metric_ops.items():
        save_metric_ops[key] = tf.py_func(
            lambda array, step, k=key: _save_array(array, step, k),
            [value, step], tf.int32)

    return save_metric_ops
Ejemplo n.º 10
0
    def get_predict_nodes(self,
                          z=None,
                          y=None,
                          c=None,
                          params=None,
                          config=None):
        """Return a dictionary of graph nodes for training."""
        LOGGER.info("Building prediction nodes.")
        with tf.variable_scope(self.name, reuse=tf.AUTO_REUSE):

            nodes = {'z': z}

            # Get slope tensor (for straight-through estimators)
            nodes['slope'] = tf.get_variable('slope', [],
                                             tf.float32,
                                             tf.constant_initializer(1.0),
                                             trainable=False)

            # --- Generator output ---------------------------------------------
            if params['use_binary_neurons']:
                if params.get('is_accompaniment'):
                    nodes['fake_x'], nodes['fake_x_preactivated'] = self.gen(
                        nodes['z'], y, c, False, nodes['slope'])
                else:
                    nodes['fake_x'], nodes['fake_x_preactivated'] = self.gen(
                        nodes['z'], y, False, nodes['slope'])
            else:
                if params.get('is_accompaniment'):
                    nodes['fake_x'] = self.gen(nodes['z'], y, c, False)
                else:
                    nodes['fake_x'] = self.gen(nodes['z'], y, False)

            # ============================ Save ops ============================
            def _get_filepath(folder_name, name, suffix, ext):
                """Return the filename."""
                if suffix:
                    return os.path.join(
                        config['result_dir'], folder_name, name,
                        '{}_{}.{}'.format(name, str(suffix, 'utf8'), ext))
                return os.path.join(config['result_dir'], folder_name, name,
                                    '{}.{}'.format(name, ext))

            def _array_to_image(array, colormap=None):
                """Convert an array to an image array and return it."""
                if array.ndim == 2:
                    return vector_to_image(array)
                return pianoroll_to_image(array, colormap)

            # --- Save array ops -----------------------------------------------
            if config['collect_save_arrays_op']:

                def _save_array(array, suffix, name):
                    """Save the input array."""
                    filepath = _get_filepath('arrays', name, suffix, 'npy')
                    np.save(filepath, array.astype(np.float16))
                    return np.array([0], np.int32)

                arrays = {'fake_x': nodes['fake_x']}
                if params['use_binary_neurons']:
                    arrays['fake_x_preactivated'] = nodes[
                        'fake_x_preactivated']

                save_array_ops = []
                for key, value in arrays.items():
                    save_array_ops.append(
                        tf.py_func(lambda array, suffix, k=key: _save_array(
                            array, suffix, k), [value, config['suffix']],
                                   tf.int32))
                    make_sure_path_exists(
                        os.path.join(config['result_dir'], 'arrays', key))
                nodes['save_arrays_op'] = tf.group(save_array_ops)

            # --- Save image ops -----------------------------------------------
            if config['collect_save_images_op']:

                def _save_image_grid(array, suffix, name):
                    image = image_grid(array, config['image_grid'])
                    filepath = _get_filepath('images', name, suffix, 'png')
                    imageio.imwrite(filepath, image)
                    return np.array([0], np.int32)

                def _save_images(array, suffix, name):
                    """Save the input image."""
                    if 'hard_thresholding' in name:
                        array = (array > 0).astype(np.float32)
                    elif 'bernoulli_sampling' in name:
                        rand_num = np.random.uniform(size=array.shape)
                        array = (.5 * (array + 1.) > rand_num)
                        array = array.astype(np.float32)
                    images = _array_to_image(array)
                    return _save_image_grid(images, suffix, name)

                def _save_colored_images(array, suffix, name):
                    """Save the input image."""
                    if 'hard_thresholding' in name:
                        array = (array > 0).astype(np.float32)
                    elif 'bernoulli_sampling' in name:
                        rand_num = np.random.uniform(size=array.shape)
                        array = (.5 * (array + 1.) > rand_num)
                        array = array.astype(np.float32)
                    images = _array_to_image(array, config['colormap'])
                    return _save_image_grid(images, suffix, name)

                images = {'fake_x': .5 * (nodes['fake_x'] + 1.)}
                if params['use_binary_neurons']:
                    images['fake_x_preactivated'] = .5 * (
                        nodes['fake_x_preactivated'] + 1.)
                else:
                    images['fake_x_hard_thresholding'] = nodes['fake_x']
                    images['fake_x_bernoulli_sampling'] = nodes['fake_x']

                save_image_ops = []
                for key, value in images.items():
                    save_image_ops.append(
                        tf.py_func(lambda array, suffix, k=key: _save_images(
                            array, suffix, k), [value, config['suffix']],
                                   tf.int32))
                    save_image_ops.append(
                        tf.py_func(
                            lambda array, suffix, k=key: _save_colored_images(
                                array, suffix, k + '_colored'),
                            [value, config['suffix']],
                            tf.int32))
                    make_sure_path_exists(
                        os.path.join(config['result_dir'], 'images', key))
                    make_sure_path_exists(
                        os.path.join(config['result_dir'], 'images',
                                     key + '_colored'))
                nodes['save_images_op'] = tf.group(save_image_ops)

            # --- Save pianoroll ops -------------------------------------------
            if config['collect_save_pianorolls_op']:

                def _save_pianoroll(array, suffix, name):
                    filepath = _get_filepath('pianorolls', name, suffix, 'npz')
                    if 'hard_thresholding' in name:
                        array = (array > 0)
                    elif 'bernoulli_sampling' in name:
                        rand_num = np.random.uniform(size=array.shape)
                        array = (.5 * (array + 1.) > rand_num)
                    save_pianoroll(filepath, array, config['midi']['programs'],
                                   list(map(bool, config['midi']['is_drums'])),
                                   config['midi']['tempo'],
                                   params['beat_resolution'],
                                   config['midi']['lowest_pitch'])
                    return np.array([0], np.int32)

                if params['use_binary_neurons']:
                    pianorolls = {'fake_x': nodes['fake_x'] > 0}
                else:
                    pianorolls = {
                        'fake_x_hard_thresholding': nodes['fake_x'],
                        'fake_x_bernoulli_sampling': nodes['fake_x']
                    }

                save_pianoroll_ops = []
                for key, value in pianorolls.items():
                    save_pianoroll_ops.append(
                        tf.py_func(lambda array, suffix, k=key:
                                   _save_pianoroll(array, suffix, k),
                                   [value, config['suffix']],
                                   tf.int32))
                    make_sure_path_exists(
                        os.path.join(config['result_dir'], 'pianorolls', key))
                nodes['save_pianorolls_op'] = tf.group(save_pianoroll_ops)

        return nodes