示例#1
0
 def get_preprocessor(self, online_config):
     # load the same preprocessor as pretraining stage
     upstream_input_feat = online_config['input']
     upstream_input_feat['channel'] = 0
     upstream_target_feat = online_config['target']
     upstream_target_feat['channel'] = 0
     preprocessor = OnlinePreprocessor(**online_config, feat_list=[upstream_input_feat, upstream_target_feat])
     upstream_feat = preprocessor()[0]
     upstream_input_dim = upstream_feat.size(-1)
     return preprocessor, upstream_input_dim
    def __init__(self,
                 file_path,
                 sets,
                 bucket_size,
                 max_timestep=0,
                 drop=False,
                 mam_config=None,
                 libri_root=None,
                 online_config=None):
        super(AcousticDataset, self).__init__(file_path, sets, bucket_size,
                                              max_timestep, drop)

        self.mam_config = mam_config
        self.libri_root = libri_root
        self.online_config = online_config
        if self.online_config is not None:
            assert libri_root is not None
            feat_list = [
                self.online_config['input'], self.online_config['target']
            ]
            self.preprocessor = OnlinePreprocessor(**self.online_config,
                                                   feat_list=feat_list)
        self.sample_step = mam_config[
            'max_input_length'] if 'max_input_length' in mam_config else 0
        if self.sample_step > 0:
            print(
                '[Dataset] - Sampling random segments for training, sample length:',
                self.sample_step)

        X = self.table['file_path'].tolist()
        X_lens = self.table['length'].tolist()

        # Use bucketing to allow different batch size at run time
        self.X = []
        batch_x, batch_len = [], []

        for x, x_len in zip(X, X_lens):
            batch_x.append(x)
            batch_len.append(x_len)

            # Fill in batch_x until batch is full
            if len(batch_x) == bucket_size:
                # Half the batch size if seq too long
                if (bucket_size >= 2) and (max(batch_len) > HALF_BATCHSIZE_TIME
                                           ) and self.sample_step == 0:
                    self.X.append(batch_x[:bucket_size // 2])
                    self.X.append(batch_x[bucket_size // 2:])
                else:
                    self.X.append(batch_x)
                batch_x, batch_len = [], []

        # Gather the last batch
        if len(batch_x) > 1:
            self.X.append(batch_x)
    def __init__(self, args, config, dataloader, ckpdir):

        self.device = torch.device('cuda') if (
            args.gpu and torch.cuda.is_available()) else torch.device('cpu')
        if torch.cuda.is_available(): print('[Runner] - CUDA is available!')
        self.model_kept = []
        self.global_step = 1
        self.log = SummaryWriter(ckpdir)

        self.args = args
        self.config = config
        self.dataloader = dataloader
        self.ckpdir = ckpdir

        # optimizer
        self.learning_rate = float(config['optimizer']['learning_rate'])
        self.warmup_proportion = config['optimizer']['warmup_proportion']
        self.gradient_accumulation_steps = config['optimizer'][
            'gradient_accumulation_steps']
        self.gradient_clipping = config['optimizer']['gradient_clipping']

        # Training details
        self.apex = config['runner']['apex']
        self.total_steps = config['runner']['total_steps']
        self.log_step = config['runner']['log_step']
        self.save_step = config['runner']['save_step']
        self.duo_feature = config['runner']['duo_feature']
        self.max_keep = config['runner']['max_keep']

        # model
        self.transformer_config = config['transformer']
        self.dr = config['transformer']['downsample_rate']
        self.dual_transformer = config['transformer'][
            'dual_transformer'] if 'dual_transformer' in config[
                'transformer'] else None
        if 'online' in config:
            print(f'[Runner] - Using features extracted on-the-fly')
            feat_list = [config['online']['input'], config['online']['target']]
            self.preprocessor = OnlinePreprocessor(**config['online'],
                                                   feat_list=feat_list).to(
                                                       device=self.device)
            self.input_dim, self.output_dim = [
                feat.size(-1) for feat in self.preprocessor()
            ]
        else:
            print(f'[Runner] - Using features pre-extracted and saved')
            self.input_dim = self.transformer_config['input_dim']
            self.output_dim = 1025 if self.duo_feature else None  # output dim is the same as input dim if not using duo features