Esempio n. 1
0
    def build_network(self, build_encoder=True):
        if build_encoder:
            self.encoder = Encoder(self._hp)
        self.decoder = DecoderModule(self._hp,  # infer actions in decoder if not using SH-Pred model
                               regress_actions=self._hp.regress_actions and self._hp.one_step_planner is not 'sh_pred')

        self.build_inference()

        if self._hp.regress_length:
            self.length_pred = LengthPredictorModule(self._hp)

        self.build_inference_encoder()
        
        if self._hp.attach_inv_mdl:
            self.inv_mdl = InverseModel(self._hp.inv_mdl_params, self._logger)

        if self._hp.attach_cost_mdl:
            self.cost_mdl = CostModel(self._hp.cost_mdl_params, self._logger)

        if self._hp.attach_state_regressor:
            self.state_regressor = BaseProcessingNet(self._hp.nz_enc, self._hp.nz_mid, self._hp.state_dim,
                                                     self._hp.n_processing_layers, self._hp.fc_builder)

        if self._hp.separate_cnn_start_goal_encoder:
            from blox.torch.layers import LayerBuilderParams
            from tensorflow.contrib.training import HParams
            with_conv_hp = HParams()
            for k in self._hp.values().keys():
                with_conv_hp.add_hparam(k, self._hp.values()[k])
            #with_conv_hp = self._hp 
            with_conv_hp.set_hparam('use_convs', True)
            with_conv_hp.set_hparam('input_nc', 3)
            with_conv_hp.set_hparam('builder', LayerBuilderParams(
                            True, self._hp.use_batchnorm, self._hp.normalization, self._hp.predictor_normalization))
            self.start_goal_enc = Encoder(with_conv_hp)
Esempio n. 2
0
    def _default_hparams(self):
        # Data Dimensions
        default_dict = AttrDict({
            'batch_size': -1,
            'max_seq_len': -1,
            'n_actions': -1,
            'state_dim': -1,
            'input_nc': 3,  # number of input feature maps
            'device': None,
            'data_conf': None,
            'img_sz': None,
            'goal_cond': True
        })

        # Network params
        default_dict.update({
            'use_convs': True,
            'use_batchnorm': True,  # TODO deprecate
            'normalization': 'batch',
        })

        # add new params to parent params
        parent_params = HParams()
        for k in default_dict.keys():
            parent_params.add_hparam(k, default_dict[k])

        return parent_params
Esempio n. 3
0
 def _default_hparams(self):
     # put new parameters in here:
     default_dict = {
         'model': None,
         'model_test': None,
         'logger': None,
         'logger_test': None,
         'data_dir': None, # directory where dataset is in
         'batch_size': 64,
         'mpar': None,   # model parameters
         'data_conf': None,   # model parameters
         'exp_path': None,  # Path to the folder with experiments
         'num_epochs': 200,
         'epoch_cycles_train': 1,
         'mujoco_xml': None,
         'optimizer': 'adam',    # supported: 'adam', 'rmsprop', 'sgd'
         'lr': 1e-3,
         'momentum': 0,      # momentum in RMSProp / SGD optimizer
         'adam_beta': 0.9,       # beta1 param in Adam
     }
     # add new params to parent params
     parent_params = HParams()
     for k in default_dict.keys():
         parent_params.add_hparam(k, default_dict[k])
     return parent_params
Esempio n. 4
0
 def _default_hparams(self):
     default_dict = {
         'save_format': ['hdf5', 'raw'],
         'save_data': True,
         'agent': {},
         'policy': {},
         'start_index': -1,
         'end_index': -1,
         'ntraj': -1,
         'gpu_id': -1,
         'current_dir': '',
         'record_saver': None,
         'counter': None,
         'traj_per_file': 10,
         'data_save_dir': '',
         'log_dir': '',
         'result_dir': '',
         'split_train_val_test': True,
         'logging_conf': None,  # only needed for training loop
     }
     # add new params to parent params
     parent_params = HParams()
     for k in default_dict.keys():
         parent_params.add_hparam(k, default_dict[k])
     return parent_params
Esempio n. 5
0
 def _default_hparams(self):
     default_dict = {
         'T': None,
         'adim': None,
         'sdim': None,
         'ncam': 1,
         'rejection_sample': False,   # repeatedly attemp to collect a trajectory if error occurs
         'type': None,
         'env': None,
         'image_height': 48,
         'image_width': 64,
         'nchannels': 3,
         'data_save_dir': '',
         'log_dir': '',
         'make_final_gif': True,   # whether to make final gif
         'make_final_gif_freq': 1,   # final gif, frequency
         'make_final_gif_pointoverlay': False,
         'gen_xml': (True, 1),  # whether to generate xml, and how often
         'start_goal_confs': None,
         'show_progress': False,
         'state_resets': False,   # reset the simluator state zeroing velocities according to policies replan frequency
         'do_not_save_images': False  # dataset savers will not save images if True
     }
     # add new params to parent params
     parent_params = HParams()
     for k in default_dict.keys():
         parent_params.add_hparam(k, default_dict[k])
     return parent_params
Esempio n. 6
0
 def _default_hparams(self):
     # put new parameters in here:
     default_dict = {
         'model': None,
         'logger': None,
         'dataset_name': None,  # directory where dataset is in
         'batch_size': 64,
         'exp_path': None,  # Path to the folder with experiments
         'num_epochs': 200,
         'epoch_cycles_train': 1,
         'optimizer':
         'radam',  # supported: 'radam', 'adam', 'rmsprop', 'sgd'
         'lr': None,
         'gradient_clip': None,
         'momentum': 0,  # momentum in RMSProp / SGD optimizer
         'adam_beta': 0.9,  # beta1 param in Adam
         'metric_pruning_scheme': 'dtw',
         'top_of_100_eval': True,
         'n_rooms': None,
     }
     # add new params to parent params
     parent_params = HParams()
     for k in default_dict.keys():
         parent_params.add_hparam(k, default_dict[k])
     return parent_params
Esempio n. 7
0
def args2hparam(args, vocab):
    params = vars(args)
    params['vocab'] = vocab
    p = HParams()
    for k, v in params.items():
        p.add_hparam(k, v)
    return p
Esempio n. 8
0
def set_params() -> HParams:
	params = HParams(coin_pairs = ["BTC_BTS", "BTC_ZEC", "BTC_STRAT", "BTC_XEM", "BTC_STEEM", "BTC_LTC", "BTC_ETC",
								   "BTC_XRP", "BTC_XMR", "BTC_DASH", "BTC_ETH", "BTC_STR", "BTC_LSK", "BTC_DOGE",
								   "BTC_SC", "BTC_SYS", "BTC_DGB", "BTC_MAID", "BTC_NXT", "BTC_BCN"],
					 num_input_channels=5,
					 live_bot_num_models=5,
					 len_conv3_filters=1,) # currently only 1x1 filters for conv3 possible
	params.add_hparam("num_coins", len(params.coin_pairs))
	return params
Esempio n. 9
0
def merge_hparams(p1, p2):
    params = HParams()
    v1 = p1.values()
    v2 = p2.values()
    for (k, v) in v1.items():
        params.add_hparam(k, v)
    for (k, v) in v2.items():
        params.add_hparam(k, v)
    return params
Esempio n. 10
0
def new_hparam(yaml_fn, config_name):
    hparam = HParams()
    with open(yaml_fn) as fp:
        for k, v in YAML().load(fp)[config_name].items():
            if isinstance(v, str) and v[0:2] == 'tf':
                hparam.add_hparam(k, eval(v))
            else:
                hparam.add_hparam(k, v)
    return hparam
Esempio n. 11
0
    def run_experiment(self, experiment_fn):
        """Run the training experiment."""
        # Define model parameters
        hparams = HParams()
        for k, v in self.default_param_dict.items():
            hparams.add_hparam(k, v)

        learn_runner.run(
            experiment_fn=experiment_fn,  # First-class function
            run_config=self.run_config,  # RunConfig
            schedule="train_and_evaluate",  # What to run
            hparams=hparams  # HParams
        )
Esempio n. 12
0
def parse_yaml(yaml_path, model_id):
    from tensorflow.contrib.training import HParams
    from ruamel.yaml import YAML

    hparams = HParams()
    hparams.add_hparam('model_id', model_id)

    with open(yaml_path) as fp:
        customized = YAML().load(fp)
        for k, v in customized.items():
            if k in hparams:
                hparams.set_hparam(k, v)
            else:
                hparams.add_hparam(k, v)
    return hparams
Esempio n. 13
0
    def _default_hparams(self):
        # Data Dimensions
        default_dict = AttrDict({
            'batch_size': -1,
            'max_seq_len': -1,
            'n_actions': -1,
            'state_dim': -1,
            'img_sz': 32,  # image resolution
            'input_nc': 3,  # number of input feature maps
            'n_conv_layers':
            None,  # Number of conv layers. Can be of format 'n-<int>' for any int for relative spec
        })

        # Network params
        default_dict.update({
            'use_convs': True,
            'use_batchnorm': True,  # TODO deprecate
            'normalization': 'batch',
            'predictor_normalization': 'group',
        })

        # Misc params
        default_dict.update({
            'filter_repeated_tail':
            False,  # whether to remove repeated states from the dataset
            'rep_tail': False,
            'dataset_class': None,
            'standardize': None,
            'split': None,
            'subsampler': None,
            'subsample_args': None,
            'checkpt_path': None,
        })

        # add new params to parent params
        parent_params = HParams()
        for k in default_dict.keys():
            parent_params.add_hparam(k, default_dict[k])
        return parent_params
Esempio n. 14
0
 def _fetch_model_specific_hparams(network_class, network_type, models_dir):
     model_hparams = HParams(model_class=None)
     if network_class is not None and network_type is not None:
         model_module_name = 'model_%s' % network_type
         model_class_name = underline_to_camel(model_module_name)
         try:
             src_module = __import__(
                 '%s.%s.%s' %
                 (models_dir, network_class, model_module_name))
             model_class = eval(
                 'src_module.models.%s.%s.%s' %
                 (network_class, model_module_name, model_class_name))
             model_hparams = model_class.get_default_model_parameters()
             model_hparams.add_hparam('model_class',
                                      model_class)  # add model class
         except ImportError:
             print('Fatal Error: no model module: \"src.models.%s.%s\"' %
                   (network_class, model_module_name))
         except AttributeError:
             print(
                 'Fatal Error: probably (1) no model class named as %s.%s, '
                 'or (2) the class no \"get_default_model_parameters()\"' %
                 (network_class, model_module_name))
     return model_hparams
Esempio n. 15
0
class HParamsCenter(object):
    default_namespace = 'other_hparams'

    def __init__(self,
                 default_preprocessing_hparams=None,
                 default_model_hparams=None,
                 default_training_hparams=None,
                 models_dir="src.models"):
        self.hparams_dict = defaultdict(HParams)
        self.models_dir = models_dir

        # parsed
        args = self._parsed_args()
        self.parsed_hparams = HParams()
        for key, val in args.__dict__.items():
            self.parsed_hparams.add_hparam(key, val)
        self.register_hparams(self.parsed_hparams, 'parsed_hparams')

        # pre-processing
        self.preprocessing_hparams = default_preprocessing_hparams or HParams()
        self.preprocessing_hparams.parse(
            self.parsed_hparams.preprocessing_hparams)
        self.register_hparams(self.preprocessing_hparams,
                              'preprocessing_hparams')

        # model
        self.model_hparams = default_model_hparams or HParams()
        self.model_hparams = merge_hparams(
            self.model_hparams,
            self._fetch_model_specific_hparams(
                self.parsed_hparams.network_class,
                self.parsed_hparams.network_type, self.models_dir))
        self.model_hparams.parse(self.parsed_hparams.model_hparams)
        self.register_hparams(self.model_hparams, 'model_hparams')

        # traning
        self.training_hparams = default_training_hparams or HParams()
        self.training_hparams.parse(self.parsed_hparams.training_hparams)
        self.register_hparams(self.training_hparams, 'training_hparams')

    @staticmethod
    def _fetch_model_specific_hparams(network_class, network_type, models_dir):
        model_hparams = HParams(model_class=None)
        if network_class is not None and network_type is not None:
            model_module_name = 'model_%s' % network_type
            model_class_name = underline_to_camel(model_module_name)
            try:
                src_module = __import__(
                    '%s.%s.%s' %
                    (models_dir, network_class, model_module_name))
                model_class = eval(
                    'src_module.models.%s.%s.%s' %
                    (network_class, model_module_name, model_class_name))
                model_hparams = model_class.get_default_model_parameters()
                model_hparams.add_hparam('model_class',
                                         model_class)  # add model class
            except ImportError:
                print('Fatal Error: no model module: \"src.models.%s.%s\"' %
                      (network_class, model_module_name))
            except AttributeError:
                print(
                    'Fatal Error: probably (1) no model class named as %s.%s, '
                    'or (2) the class no \"get_default_model_parameters()\"' %
                    (network_class, model_module_name))
        return model_hparams

    @staticmethod
    def _parsed_args():
        parser = argparse.ArgumentParser()
        parser.register('type', 'bool', (lambda x: x.lower() in
                                         ("yes", "true", "t", "1")))
        parser.add_argument('--mode', type=str, default='train', help='')
        parser.add_argument('--dataset', type=str, default='none', help='')
        parser.add_argument('--network_class',
                            type=str,
                            default='transformer',
                            help='')
        parser.add_argument('--network_type', type=str, default=None, help='')
        parser.add_argument('--gpu',
                            type=str,
                            default='3',
                            help='selected gpu index')
        parser.add_argument('--gpu_mem',
                            type=float,
                            default=None,
                            help='selected gpu index')
        parser.add_argument('--model_dir_prefix',
                            type=str,
                            default='prefix',
                            help='model dir name prefix')
        parser.add_argument('--machine',
                            type=str,
                            default='none',
                            help='using aws')

        # parsing parameters group
        parser.add_argument('--preprocessing_hparams',
                            type=str,
                            default='',
                            help='')
        parser.add_argument('--model_hparams', type=str, default='', help='')
        parser.add_argument('--training_hparams',
                            type=str,
                            default='',
                            help='')

        parser.set_defaults(shuffle=True)
        return parser.parse_args()

    def register_hparams(self, hparams, name):
        assert isinstance(hparams, HParams)
        assert isinstance(name, str)

        if name in self.hparams_dict:
            self.hparams_dict[name] = merge_hparams(self.hparams_dict[name],
                                                    hparams)
        else:
            self.hparams_dict[name] = hparams

    @property
    def all_hparams(self):
        all_hparams = HParams()
        for name, hp in self.hparams_dict.items():
            all_hparams = merge_hparams(all_hparams, hp)
        return all_hparams

    def __setitem__(self, key, value):
        assert isinstance(key, str)
        # this is added to the default
        # self.hparams_dict[self.default_namespace][key] = value
        key_found = False
        for _, hps in self.hparams_dict.items():
            try:
                if key in hps:
                    key_found = True
            # when tf==1.4.1, directly use "in" will raise TypeError: argument of type 'HParams' is not iterable
            except TypeError:
                if key in hps.values():
                    key_found = True
            if key_found:
                hps.set_hparam(key, value)
                break

        if not key_found:  # not found, set it
            self.hparams_dict[self.default_namespace].add_hparam(key, value)

    def __getitem__(self, item):
        assert isinstance(item, str)

        for name, hp in self.hparams_dict.items():
            try:
                return getattr(hp, item)
            except AttributeError:
                pass
        raise AttributeError('no item named as \'%s\'' % item)

    def __contains__(self, item):
        if isinstance(item, str):
            for key, hps in self.hparams_dict.items():
                try:
                    if item in hps:
                        return True
                # when tf==1.4.1, directly use "in" will raise TypeError: argument of type 'HParams' is not iterable
                except TypeError:
                    if item in hps.values():
                        return True
        return False
Esempio n. 16
0
class TunatorLSTM:
    """
    LSTM model for synthesizing polyphonic MIDI.

    When the class is instantiated,
    the `update_datastore` method ensures that all the MIDI data from the
    files in `midi_dir` are stored in the HDF5 at `hdf5_path`. It queries the
    datastore for the song names in `midi_dir` using the `query_datastore`
    method, then updates it with any missing songs. The datastore schema is
    provided in the `update_datastore` method docstring.

    The model is instantiated either via `build_model` or `load_model` and
    trained via `train`. The model can be sampled via `compose`, which will
    output a MIDI file.

    Args:
        midi_dir (str): directory to MIDI files
        hdf5_path (str): path for HDF5 datastore
        hparams (dict): any hyperparameters to be changed from defaults.
            Defaults are shown under `hparams` property. They can also be
            changed dynamically by passing a dict to the `hparams` setter.
    """
    def __init__(self,
                 midi_dir='music/midi/final_fantasy/',
                 hdf5_path='data/songs.hdf5',
                 hparams=None):
        self.midi_dir = midi_dir
        self.hdf5_path = hdf5_path
        self._hparams = hparams

        # set up piano roll
        octaves = 10
        scale = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
        sharps = ['A#', 'C#', 'D#', 'F#', 'G#']
        flats = ['B-', 'D-', 'E-', 'G-', 'A-']
        sharps_scale = sorted(scale + sharps)
        sharps_oct = [note + str(i) for i in range(octaves) for note in sharps]
        flats_oct = [note + str(i) for i in range(octaves) for note in flats]
        flat_sharp_dict = dict(zip(flats_oct, sharps_oct))
        self.piano_roll = [
            note + str(i) for i in range(octaves) for note in sharps_scale
        ]
        self.piano_roll_dict = {
            note: i
            for i, note in enumerate(self.piano_roll)
        }
        self.piano_roll_dict.update({
            flat: self.piano_roll_dict[sharp]
            for flat, sharp in flat_sharp_dict.items()
        })

        # prepare data
        self.song_file_dict = self.get_song_file_dict()
        songs = list(self.song_file_dict)
        random.shuffle(songs)
        split = int(.8 * len(songs))
        self.train_songs = songs[:split]
        self.val_songs = songs[split:]
        self.update_datastore()

        # instantiate tensor generators for lazy evaluation during training
        self.train_tensor_gen = NoteChordOneHotTensorGen(
            self.train_songs, self.hparams.batch_size, self.hparams.timesteps,
            hdf5_path, self.piano_roll_dict, self.n_vocab)
        self.val_tensor_gen = NoteChordOneHotTensorGen(
            self.val_songs, self.hparams.batch_size, self.hparams.timesteps,
            hdf5_path, self.piano_roll_dict, self.n_vocab)

    @property
    def hparams(self):
        defaults = {
            'learning_rate': 0.001,
            'dropout': 0.0,
            'lstm_units': 512,
            'dense_units': 512,
            'batch_size': 32,
            'timesteps': 256,
            'epochs': 3,
        }

        if isinstance(self._hparams, HParams):
            return self._hparams
        elif self._hparams:
            user_entered = self._hparams
        else:
            user_entered = dict()
        combined = dict()
        combined.update(defaults)
        combined.update(user_entered)
        self._hparams = HParams()
        for k, v in combined.items():
            self._hparams.add_hparam(k, v)

        return self._hparams

    @hparams.setter
    def hparams(self, values):
        if not isinstance(self._hparams, HParams):
            self._hparams = HParams()
        for k, v in values.items():
            self._hparams.add_hparam(k, v)

    @property
    def n_vocab(self):
        return len(self.piano_roll)

    @property
    def timestamp(self):
        return datetime.now()

    def build_model(self):
        """

        Returns:

        """
        self.model = Sequential()
        input_shape = (None, self.n_vocab)

        self.model.add(
            CuDNNLSTM(
                self.hparams.lstm_units,
                input_shape=input_shape,
                return_sequences=True,
            ))
        self.model.add(Dropout(self.hparams.dropout))

        self.model.add(
            CuDNNLSTM(self.hparams.lstm_units, return_sequences=True))
        self.model.add(Dropout(self.hparams.dropout))

        self.model.add(
            CuDNNLSTM(self.hparams.lstm_units, return_sequences=True))
        self.model.add(Dropout(self.hparams.dropout))

        self.model.add(TimeDistributed(Dense(self.n_vocab)))
        self.model.add(Dropout(self.hparams.dropout))

        self.model.add(TimeDistributed(Dense(self.n_vocab)))
        self.model.add(Dropout(self.hparams.dropout))

        self.model.add(TimeDistributed(Activation('sigmoid')))
        self.model.compile(loss='binary_crossentropy', optimizer='rmsprop')

    def load_model(self, model_path):
        """

        Args:
            model_path:

        Returns:

        """
        self.build_model()
        self.model.load_weights(model_path)

    def train(self):
        """

        Returns:

        """
        timestamp = datetime.now()
        log_name = f'note-chord-one-hot-songs_{timestamp}'
        tensorboard = TensorBoard(log_dir=f'logs/{log_name}',
                                  histogram_freq=1,
                                  write_graph=True,
                                  write_grads=True,
                                  batch_size=4)  #write_images
        # if adding embeddings, add those parameters
        checkpoint_name = 'weights-improvement-epoch_{epoch:02d}-loss_{loss:.4f}.hdf5'
        checkpoint = ModelCheckpoint(f'checkpoints/{checkpoint_name}',
                                     monitor='loss',
                                     verbose=0,
                                     save_best_only=True,
                                     mode='min')

        val_slice = list(islice(self.val_tensor_gen, 10))
        X_val_list = list()
        Y_val_list = list()
        for item in val_slice:
            X_val_list.append(item[0])
            Y_val_list.append(item[1])
        X_val = np.concatenate(X_val_list, axis=0)
        del X_val_list
        Y_val = np.concatenate(Y_val_list, axis=0)
        del Y_val_list
        val_data = (X_val, Y_val)
        self.model.fit_generator(
            self.train_tensor_gen,
            validation_data=val_data,
            # validation_steps=10,
            steps_per_epoch=self.train_tensor_gen.n_batches,
            epochs=self.hparams.epochs,
            callbacks=[checkpoint, tensorboard])

    def get_song_file_dict(self):
        """
        Creates a lookup dictionary to get filepath from song name.
        Returns:
            song_file_dict (dict): Dictionary with song names as keys and
                filepaths relative to the current directory as values, including
                file extension.
        """
        file_exts = ('*.mid', '*.midi', '*.MID', '*.MIDI')
        song_files = list()
        for ext in file_exts:
            song_files += glob.glob(os.path.join(self.midi_dir, ext))
        get_song_name = lambda x: os.path.splitext(os.path.basename(x))[0]
        song_names = [get_song_name(file) for file in song_files]
        # dict to look up filepath by song name
        song_file_dict = dict(zip(song_names, song_files))
        return song_file_dict

    def query_datastore(self, query, grp_path='songs'):
        """
        Checks datastore at `hdf5_path` for keys specified by `query` within the
        group specified by `grp_path` non-recursively. If the datastore does not
        exist, the query will not raise an error, but instead return all of the
        queried items in `not_found` and none of them in `found`.
        Args:
            query (iterable): keys for which to search within the grp_path in
                the datastore
            grp_path (str): path to the group in which to search for they keys

        Returns:
            found (set): query items that were found in the group
            not_found (set): query items that were not found in the group
        """
        # TODO: what about querying the base group?
        if os.path.isfile(self.hdf5_path):
            grp = h5py.File(self.hdf5_path, 'r')[grp_path]
            keys = list(grp.keys())
        else:
            keys = list()

        found = set(query).intersection(keys)
        not_found = set(query) - set(found)
        return found, not_found

    def update_datastore(self):
        """
        Updates HDF5 datastore with note sequences for any songs in midi_dir
        that are not already present in the datastore.
        """
        def _parse_midi(song):
            """
            The songs are transposed to the key of A. The parser extracts
            the lowest numbered part that has greater than 50 notes. If that
            doesn't work, it flattens all of the parts into a single "flat" part
            which contains all the instruments.
            Args:
                song:

            Returns:

            """
            # TODO: convert output to namedtuples with metadata
            file = self.song_file_dict[song]
            print(f'updating datastore: {file}...')
            midi = m21.converter.parse(file)

            # transpose to A
            transpose_dict = {
                'A#': 11,
                'B-': 11,
                'B': 10,
                'C': 9,
                'C#': 8,
                'D-': 8,
                'D': 7,
                'D#': 6,
                'E-': 6,
                'E': 5,
                'F': 4,
                'F#': 3,
                'G-': 3,
                'G': 2,
                'G#': 1,
                'A-': 1,
                'A': 0,
            }
            key = midi.analyze('key').getTonic().name
            midi = midi.transpose(transpose_dict[key])
            # extract piano, or other
            try:
                midi_parts = m21.instrument.partitionByInstrument(midi).parts
                part = midi_parts[0]
                if not part.partName == 'Piano':
                    pass
                notes_to_parse = part.recurse().notes
                part_i = 0
                while len(notes_to_parse) < 50:
                    part_i += 1
                    part = midi_parts[part_i]
                    notes_to_parse = part.recurse().notes

            except Exception:  # file has notes in a flat structure
                notes_to_parse = midi.flat.chordify().notes

            return notes_to_parse

        def _parse_notes(notes_to_parse):
            """
            Parse MIDI data to a dictionary of timesteps and corresponding
            notes.
            """
            notes = dict()
            for elem in notes_to_parse:
                time = elem.offset

                # TODO: remove after time fix
                if time % 0.5 != 0:
                    continue

                if time not in notes:
                    notes[time] = set()

                if isinstance(elem, m21.note.Note):
                    note_int = self.piano_roll_dict[str(elem.pitch)]
                    notes[time].add(note_int)
                elif isinstance(elem, m21.chord.Chord):
                    note_ints = [
                        self.piano_roll_dict[str(pitch)]
                        for pitch in elem.pitches
                    ]
                    notes[time].update(note_ints)
                else:
                    raise ValueError()

            # TODO: SongMap slicable hashmap class
            # correct fractional indices
            frac_notes = {
                k: v
                for k, v in notes.items() if isinstance(k, fractions.Fraction)
            }
            for k, v in frac_notes.items():
                del notes[k]
                nearest_quarter = round(k * 4) / 4
                if nearest_quarter in notes:
                    notes[nearest_quarter].update(v)
                else:
                    notes[nearest_quarter] = v

            # fill missing time indices
            # temporarily remove because only rests were generated

            time_list = sorted(notes)
            if not time_list:
                raise ValueError()
            end_time = max(time_list)
            min_space = min(
                [j - i for i, j in zip(time_list[:-1], time_list[1:])])
            """
            expected_times = np.array(range(int(end_time / min_space))) * min_space
            missing_times = set(expected_times) - set(time_list)
            if missing_times:
                print(f'filling in {len(missing_times)} missing timepoints in '
                      f'existing {len(notes)}...')
                notes.update({time: set() for time in missing_times})
            """
            # convert to half notes

            # convert notes to a list of strings
            #str_notes = ['.'.join(sorted(notes[k])) for k in sorted(notes)]
            # remove leading and trailing rests
            #for i in (0, -1):
            #    while str_notes and str_notes[i] == '':
            #        str_notes.pop(i)
            # encoding required by h5py
            #str_notes = np.array(str_notes).astype('|S9')

            #vocab = np.array(list(set(str_notes))).astype('|S9')
            return notes, min_space

        def _write_to_datastore(notes, min_space):
            """
            Write a sequence of piano roll integers to HDF5 datastore.
            Args:
                notes (np.array):
                min_space (float):

            Returns:

            """
            notes_list = np.array(
                [np.array(list(notes[k])).astype('i8') for k in sorted(notes)])
            with h5py.File(self.hdf5_path, 'a') as f:
                grp = f.create_group(f'songs/{song}')
                dt = h5py.special_dtype(vlen=np.dtype('int8'))
                dset_notes = grp.create_dataset(name='notes',
                                                shape=(len(notes_list), 1),
                                                data=notes_list,
                                                dtype=dt)
                dset_notes.attrs['spacing'] = min_space

        song_names = set(self.song_file_dict)
        _, missing_songs = self.query_datastore(song_names)

        for song in missing_songs:
            notes_to_parse = _parse_midi(song)
            notes, min_space = _parse_notes(notes_to_parse)
            _write_to_datastore(notes, min_space)

    def compose(self, timesteps):
        """
        Generate MIDI file of length `timesteps` starting from a random seed
        note from a song in the datastore.
        Args:
            timesteps (int): number to timesteps to synthesize

        Returns:

        """
        seed_note = np.array([])
        while seed_note.size == 0:
            with h5py.File(self.hdf5_path) as f:
                grp = f['songs']
                song_names = list(grp.keys())
                song_idx = np.random.randint(0, len(song_names))
                song = grp[song_names[song_idx]]['notes']
                note_idx = np.random.randint(0, len(song))
                seed_note = song[note_idx][0]

        x = np.zeros(self.n_vocab)
        x[seed_note] = 1

        # generate notes
        Y_hat_inds_seq = []
        for i in range(timesteps):
            x = np.expand_dims(x, axis=0)
            x = np.expand_dims(x, axis=0)
            y_hat = self.model.predict(x)
            y_hat_inds = np.argwhere(y_hat > .5).flatten()
            if y_hat_inds.size == 0:
                y_hat_inds = np.argmax(y_hat).flatten()
            Y_hat_inds_seq.append(y_hat_inds)
            x = np.zeros(self.n_vocab)
            x[y_hat_inds] = 1

        ipdb.set_trace()
        rev_piano_roll_dict = {v: k for k, v in self.piano_roll_dict.items()}
        Y_hat_strs = [[rev_piano_roll_dict[ind] for ind in Y_hat_inds]
                      for Y_hat_inds in Y_hat_inds_seq]

        self._output_midi(Y_hat_strs)

        return Y_hat_strs

    def _output_midi(self, Y_hat_strs):
        timesteps = len(Y_hat_strs)
        offset = 0
        output_notes = []

        for event_strs in Y_hat_strs:  # chord
            if len(event_strs) > 1:
                notes = []
                for note_str in event_strs:
                    note = m21.note.Note(int(note_str))
                    m21.note.storedInstrument = m21.instrument.Piano()
                    notes.append(note)
                chord = m21.chord.Chord(notes)
                chord.offset = offset
                output_notes.append(chord)
            elif event_strs:  # note
                note = m21.note.Note(event_strs[0])
                note.offset = offset
                note.storedInstrument = m21.instrument.Piano()
                output_notes.append(note)
            else:  # rest
                rest = m21.note.Rest()
                rest.offset = offset
                rest.storedInstrument = m21.instrument.Piano()
                output_notes.append(rest)

            offset += 1

        midi = m21.stream.Stream(output_notes)
        midi.write('midi', fp=f'test_output-{timesteps}-{self.timestamp}.mid')
Esempio n. 17
0
        for iids in recommended_items:
            item_ranks.append([item_popularities[iid] for iid in iids])
        median_ranks = np.median(item_ranks, axis=1)
    tf.reset_default_graph()
    return np.mean(median_ranks)


if __name__ == '__main__':
    logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s',
                        level=logging.DEBUG)

    tf.set_random_seed(RANDOM_SEED)
    np.random.seed(RANDOM_SEED)

    # Add parameter for results folder
    params.add_hparam('models_home', '')
    params.add_hparam('sampler', '')
    params.add_hparam('spreadout_weight', 0.)
    params.add_hparam('n_try', 10)
    params.add_hparam('num_neg_candidates', 0)
    params.add_hparam('max_count', None)
    params.add_hparam('update_embeddings_every_n_batches', -1)

    # update tensorflow's hparams object using values from
    # python's ArgumentParser.
    parser = argparse.ArgumentParser()
    params, pargs = update_params_from_parser(params, parser)

    logger.info('Load echonest dataset')
    dataset = fetch_echonest(data_home='data/echonest',
                             min_playcount=params.min_playcount,
Esempio n. 18
0
def parse_args(yaml_path, model_id, default_set, followup=None):
    logger = logging.getLogger(APP_NAME)

    hparams = HParams()
    hparams.add_hparam('model_id', model_id)

    with open('default.yaml') as fp:
        configs = YAML().load(fp)
        default_cfg = configs[default_set]

        add_param_recur(hparams, default_cfg)

        if yaml_path:
            logger.info('loading parameters...')
            with open(yaml_path) as fp:
                customized = YAML().load(fp)
                for k, v in customized.items():
                    if k in hparams and hparams.get(k) != v:
                        logger.info('%20s: %20s -> %20s' %
                                    (k, hparams.get(k), v))
                        hparams.set_hparam(k, v)
                    elif k not in hparams:  # add new parameter
                        hparams.add_hparam(k, v)
                        logger.info(
                            '%30s %20s: %20s' %
                            ("[add from %s]" % yaml_path, k, hparams.get(k)))

    if followup:
        # useful when changing args for prediction
        logger.info('override args with follow-up args...')
        for k, v in followup.items():
            if k in hparams and hparams.get(k) != v:
                logger.info('%20s: %20s -> %20s' % (k, hparams.get(k), v))
                hparams.set_hparam(k, v)
            elif k not in hparams:
                logger.warning('%s is not a valid attribute! ignore!' % k)
    if 'save_dir' not in hparams:
        hparams.add_hparam(
            'save_dir',
            os.path.join(hparams.get('model_dir'), hparams.get('model_id')))
    if 'code_dir' not in hparams:
        hparams.add_hparam('code_dir',
                           os.path.join(hparams.get('save_dir'), 'code'))
    hparams.set_hparam('summary_dir',
                       os.path.join(hparams.get('save_dir'), 'summary'))
    # reset logger model id
    logger = set_logger(model_id='%s:%s' %
                        (DEVICE_ID, hparams.get('model_id')))

    try:
        shutil.copytree('./',
                        hparams.get('code_dir'),
                        ignore=shutil.ignore_patterns(*IGNORE_PATTERNS))
        logger.info('current code base is copied to %s' %
                    hparams.get('save_dir'))
    except FileExistsError:
        logger.info('code base exist, no need to copy!')

    # if hparams.get('model_id') != model_id:
    #     logger.warning('model id is changed %s -> %s! '
    #                    'This happens when you train a pretrained model' % (
    #                        hparams.get('model_id'), model_id))
    #     hparams.set_hparam('model_id', model_id)
    if 'loss_csv_file' not in hparams:
        hparams.add_hparam('loss_csv_file',
                           os.path.join(hparams.get('save_dir'), 'loss.csv'))
    if 'is_serving' not in hparams:
        hparams.add_hparam('is_serving', False)

    logger.info('current parameters')
    for k, v in sorted(vars(hparams).items()):
        if not k.startswith('_'):
            logger.info('%20s = %-20s' % (k, v))

    return hparams
Esempio n. 19
0
def smnist_hparams():
    hparams = HParams()
    hparams.add_hparam("g_dim", 128)
    hparams.add_hparam("z_dim", 10)
    hparams.add_hparam("prior_rnn_layers", 1)
    hparams.add_hparam("posterior_rnn_layers", 1)
    hparams.add_hparam("predictor_rnn_layers", 2)
    hparams.add_hparam("num_input_frames", 5)
    hparams.add_hparam("num_target_frames", 10)
    hparams.add_hparam("beta", 1e-4)
    hparams.add_hparam("learning_rate", 0.002)
    hparams.add_hparam("frame_size", (64, 64, 1))
    hparams.add_hparam("batch_size", 100)
    hparams.add_hparam("rnn_size", 256)
    hparams.add_hparam("num_digits", 2)
    return hparams
Esempio n. 20
0
    'quant_size': args.quant_size,

    'learning_rate': args.learning_rate,
    'decay_steps':args.decay_steps,
    'decay_rate':args.decay_rate,
    
    'batch_size': args.batch_size,
    'steps': args.steps,
    'test_per_iterations': args.test_per_iterations,
    'queue_capacity':args.queue_capacity,

    'max_alpha': args.max_alpha,
    'alpha_div': args.alpha_div,
}
for a,b in hyper_parameters.items():
    hparams.add_hparam(a, b)

# Load data
data_generator = Dataset(hparams)

# Prepare train dirs
model_name = args.model_name + '_' + datetime.datetime.now().strftime("%Y_%m_%d__%H_%M_%S")
print('Running model: ' + model_name)
print('Hyperparameters: ')
for a,b in hyper_parameters.items():
    print(str(a) + ":" + str(b))


pathlib.Path(args.train_dir + '/' + args.savedir).mkdir(parents=True, exist_ok=True) 
saved_models_dir = args.train_dir + '/' + args.savedir + '/' + model_name + '/' + 'tmp'