def decompose(self): sr = self.sr out_dir = self.out_dir cache = self.cache log = self.log log_info = self.log_info gpu_num = self.gpu_num file_name = self.file_name track_audio = self.track_audio downbeat_path = os.path.join( get_save_dir(out_dir, ['extracted', 'downbeat']), f'{file_name}.npy') core_path = os.path.join(get_save_dir(out_dir, ['extracted', 'core']), f'{file_name}.npy') factors_path = os.path.join( get_save_dir(out_dir, ['extracted', 'factors']), f'{file_name}.npy') downbeat_times = load_np(downbeat_path, cache) core = load_np(core_path, cache) factors = load_np(factors_path, cache) downbeat_times = downbeat_times if type(downbeat_times) != type( None) else get_downbeats(track_audio) downbeat_frames = librosa.time_to_samples(downbeat_times, sr=sr) if log: log_message('Downbeat completed', log_info) spectral_cube = make_spectral_cube(track_audio, downbeat_frames) n_sounds, n_rhythms, n_loops = validate_template_sizes( spectral_cube, n_templates=[0, 0, 0]) if type(core) == type(None) or type(factors) == type(None): core, factors = tl.decomposition.non_negative_tucker( tl.tensor(np.abs(spectral_cube), device=f'cuda:{gpu_num}'), [n_sounds, n_rhythms, n_loops], n_iter_max=500, verbose=True) core = np.array(core.detach().to('cpu').numpy()) factors = [ np.array(factor.detach().to('cpu').numpy()) for factor in factors ] if cache: save_np(downbeat_path, downbeat_times) save_np(core_path, core) save_np(factors_path, factors) self.n_loops = n_loops self.spectral_cube = spectral_cube self.core = core self.factors = factors self.downbeat_frames = downbeat_frames
def load_tracks(col_tracks): int_dir = settings.INT_DIR int_files = [ int_file for int_file in os.listdir(int_dir) if int_file != '.ipynb_checkpoints' ] save_count, exist_count, total = 0, 0, len(int_files) log_message('Load tracks start') for int_file in int_files: int_path = os.path.join(int_dir, int_file) file_name, media_type = get_file_name(int_path) find_item = col_tracks.find_one({'file_name': file_name}) if find_item: exist_count += 1 log_message('Exist', [save_count, exist_count, total]) else: save_count += 1 col_tracks.save({ 'extracted': False, 'file_name': file_name, 'media_type': media_type, }) log_message('Save', [save_count, exist_count, total]) log_message('Load tracks completed')
def loops_tag(col_tags, tracks, tag): if settings.NG_TYPES['selected']: if tag == 1: log_message('Tag started') loops_path = [ loop_path for track in tracks for loop_path in track['loops_path'] ] for i, loop_path in enumerate(loops_path): log_info = [i + 1, len(loops_path)] find_item = col_tags.find_one({'loop_path': loop_path}) if find_item: log_message(f'{loop_path} existed', log_info) else: loop_tag = Tagger(loop_path).tag() log_message(f'{loop_path} tagged', log_info) col_tags.save({'loop_path': loop_path, 'tag': loop_tag}) log_message('Tag completed') harm_datas = [ loop['loop_path'] for loop in col_tags.find({'tag': 'harm'}) ] else: harm_datas = [] return harm_datas
def __init__(self, file_name, media_type, gpu_num, log_info=[]): self.sr = settings.SR self.cache = settings.CACHE self.log = settings.LOG self.int_dir = settings.INT_DIR self.out_dir = settings.OUT_DIR self.exist_thre = settings.EXISTED_THRESHOLD self.log_info = log_info self.file_name = file_name self.media_type = media_type self.gpu_num = gpu_num self.file_path = os.path.join(self.int_dir, f'{file_name}.{media_type}') self.track_audio = librosa.load(self.file_path, self.sr)[0] if self.log: log_message('Extractor started', self.log_info) self.decompose() if self.log: log_message('Decomposition completed', self.log_info) self.layout_retrieve() self.template = discrete_matrix(self.layout, self.exist_thre) if self.log: log_message('Layout & Template retrive completed', self.log_info) self.loop_extract() if self.log: log_message('Extractor completed', self.log_info)
def save_model(self): log = self.log model = self.model out_dir = self.out_dir model_dir = get_save_dir(out_dir, ['models']) model_id = datetime.datetime.utcnow() model_name = f'{model_id}.pkl' model_path = os.path.join(model_dir, model_name) torch.save(model.state_dict(), model_path) if log: log_message(f'Saved modle {model_name}') return model_id, model_name
def __init__(self, extractor, log_info=[]): self.sr = settings.SR self.dur = settings.DUR self.log = settings.LOG self.exist_thre = settings.EXISTED_THRESHOLD self.dupl_thre = settings.HASH_THRESHOLD self.log_info = log_info self.extractor = extractor if self.log: log_message('Refintor started', self.log_info) self.refinement() self.template = discrete_matrix(self.layout, self.exist_thre) if self.log: log_message('Refintor completed', self.log_info)
def train(self): log = self.log device = self.device model = self.model loss_fn = self.loss_fn scheduler = self.scheduler optimizer = self.optimizer train_loader = self.train_loader log_interval = self.log_interval model.train() train_loss = 0 for batch_idx, (*datas, targets) in enumerate(train_loader): inputs, *others = datas if not type(inputs) in (tuple, list): inputs = (inputs, ) if device: inputs = tuple(i.to(device) for i in inputs) if targets is not None: targets = targets.to(device) targets = torch.tensor(targets, dtype=torch.float, device=device) optimizer.zero_grad() outputs = model(*inputs) if type(outputs) not in (tuple, list): outputs = (outputs, ) loss_inputs = outputs if targets is not None: targets = (targets, ) loss_inputs += targets loss = loss_fn(*loss_inputs) loss.backward() train_loss += loss.item() optimizer.step() if batch_idx % log_interval == 0 & log: log_message(f'Loss: {loss.item()}', [batch_idx, len(train_loader)]) self.model = model return train_loss / (batch_idx + 1)
def fit(self): device = self.device scheduler = self.scheduler epochs = self.epochs model = self.model losses_history = {'train': [], 'val': []} for epoch in range(0, epochs): scheduler.step() train_loss = self.train() val_loss = self.validate() log_message(f'Train Loss: {train_loss}, Val Loss: {val_loss}', [epoch + 1, epochs]) losses_history['val'].append(val_loss) losses_history['train'].append(train_loss) losses_avg = { data_type: np.mean(np.array(losses_history[data_type])) for data_type in losses_history } return losses_avg, losses_history
def sampling(self): tracks_key = self.tracks_key tracks_dict = self.tracks_dict ng_types = self.ng_types idv_datas = self.idv_datas harm_datas = self.harm_datas data_type = self.data_type log = self.log neg_datas = {ng_type:[] for ng_type in ng_types} total = len(tracks_key) for i, track_key in enumerate(tracks_key): excl_datas = tracks_dict[track_key]['loops_path'] pair_datas = tracks_dict[track_key]['pairs_path'] other_datas = data_exclude(idv_datas, excl_datas) for pair_data in pair_datas: neg_dict = Sampler(pair_data, other_datas, harm_datas).sampling() for neg_type in neg_dict: neg_datas[neg_type].append(neg_dict[neg_type]) if log: log_message(f'Negative Sampling processing ({data_type})', [i+1, total]) if log: log_message(f'Negative Sampling completed ({data_type})') return neg_datas
def data_generation(col_tracks, col_loops): failed_count = 0 break_loop = False count = failed_count failed_count = 0 break_loop = False count = failed_count while 1: try: tracks = col_tracks.find({'extracted': False}) total = tracks.count() count = failed_count if total == 0 or count + 1 == total: print('Finished.....') break for track in tracks[count:]: track_info = track.copy() file_name = track['file_name'] media_type = track['media_type'] log_info = [failed_count, count, total] track_loop = Pipeline(file_name, media_type, gpu_num, log_info) track_loop_info = track_loop.start() track_info['extracted'] = True col_tracks.save(track_info) find_item = col_loops.find_one({'file_name': file_name}) if find_item: track_loop_info['_id'] = find_item['_id'] col_loops.save(track_loop_info) count += 1 log_message('Save Success', log_info) except Exception as e: if type(e).__name__ == 'CursorNotFound': log_message('Restart') else: log_message(f'Save Failed: {e}') failed_count += 1
default=settings.LOG_INTERVAL) parser.add_argument('--neg_type', help='neg type', default='random') parser.add_argument('--model_type', help='model type', default='cnn') col_datasets = initialize_database(settings.MONGODB_DATASET_COL) col_models = initialize_database(settings.MONGODB_MODEL_COL) datas = col_datasets.find({}).sort('date', pymongo.DESCENDING)[0] datasets = load_dataset(settings.OUT_DIR, datas['data_path']) gpu_num = parser.parse_args().gpu_num log_interval = parser.parse_args().log_interval neg_type = parser.parse_args().neg_type if neg_type not in datas['neg_types']: log_message(f'{neg_type} not exists') else: model_type = 'snn' if parser.parse_args( ).model_type != 'cnn' else parser.parse_args().model_type lr = parser.parse_args().lr epochs = parser.parse_args().epochs batch_size = parser.parse_args().batch_size model, loss_fn, optimizer, scheduler, device = load_parameters( model_type, lr, batch_size, gpu_num) train_loader = load_dataloader(model_type, 'train', neg_type, datasets) val_loader = load_dataloader(model_type, 'val', neg_type, datasets) trainer = Trainer(model, train_loader, val_loader, loss_fn, optimizer, scheduler, epochs, device, log_interval) log_message(f'Start train {model_type} with {neg_type}')
def dataset_creation(col_datasets, tracks, harm_datas): tracks_dict = {track['file_name']: track for track in tracks} log_message('Create dataset started') dataset = Dataset(tracks_dict, harm_datas).datas_retrieve() col_datasets.save(dataset) log_message('Create dataset completed')