def get_model(nfets, size, nclass): Timer.start('Building model') model = keras.Sequential() model.add(keras.layers.BatchNormalization(batch_size=300)) model.add( keras.layers.Conv1D(64, kernel_size=4 * nfets, strides=nfets, input_shape=(size, 1), activation='elu')) model.add(keras.layers.Dropout(0.005)) model.add(keras.layers.MaxPooling1D(pool_size=6, strides=4)) model.add( keras.layers.Conv1D(128, kernel_size=10, strides=2, activation='elu')) model.add(keras.layers.MaxPooling1D(pool_size=4, strides=2)) model.add( keras.layers.Conv1D(128, kernel_size=2, strides=2, activation='elu')) model.add(keras.layers.MaxPooling1D(pool_size=2, strides=2)) model.add( keras.layers.Conv1D(128, kernel_size=2, strides=2, activation='elu')) model.add(keras.layers.MaxPooling1D(pool_size=2, strides=2)) model.add(keras.layers.GRU(32, return_sequences=True)) model.add(keras.layers.GRU(32, return_sequences=False)) model.add(keras.layers.Dropout(0.001)) # model.add(keras.layers.Flatten()) model.add(keras.layers.Dense(units=1000, activation='elu')) model.add(keras.layers.Dropout(0.005)) model.add(keras.layers.Dense(units=nclass, activation='softmax')) Timer.end('Building model') return model
def make_data(ext, in_dir, out_dir, n_pack): mkdirp(out_dir) allfiles = os.listdir(in_dir) packs = dist(allfiles, n_pack) print([len(p) for p in packs]) for pi, files in enumerate(packs): Timer.start('Making data for pack %i / %i' % (pi + 1, len(packs))) out_file = '%s/data_%i.%s' % (out_dir, pi, ext) if os.path.exists(out_file): continue exe = ThreadPoolExecutor(max_workers=16) records = {} meta_dict = { 'hop': hop, 'nfft': nfft, 'win_len': win_len, 'records': records, 'labels': [] } n = len(files) pack_data = [] for idx, _file in enumerate(files): def f(full_file, i): file_with_lbl, file_ext = os.path.splitext(full_file) if file_ext not in audio_exts: return lbl, file = os.path.splitext(file_with_lbl) lbl = int(lbl) file = file[1:] Timer.start('Making data for %s(%i) (%i/%i)' % (file, pi + 1, i + 1, n)) meta = {'file': file, 'label': lbl} y, sr = librosa.load('%s/%i.%s%s' % (in_dir, lbl, file, file_ext)) meta['sr'] = sr # data = make_file_data(y, sr, hop, nfft, win_len) data = extract_features(y, sr, nfft, hop) pack_data.append(data) records[file] = meta meta_dict['labels'].append(lbl) Timer.end('Making data for %s(%i) (%i/%i)' % (file, pi + 1, i + 1, n)) exe.submit(lambda _file=_file, idx=idx: f(_file, idx)) exe.shutdown(wait=True) nppack = np.asarray(pack_data) print(nppack.shape) meta_dict['rows'] = len(meta_dict['labels']) meta_dict['nfets'] = nppack.shape[2] with open('%s/meta_%i.json' % (out_dir, pi), 'w') as fp: json.dump(meta_dict, fp) nppack.tofile(out_file) Timer.end('Making data for pack %i / %i' % (pi + 1, len(packs)))
def f(i, file): Timer.start('Make test data for file: %i / %i' % (i + 1, len(pack))) y, sr = lrs.load('%s/%s' % (dir, file), sr=base_sr) n = 1 + ((len(y) - base_len) // stride) for j in range(n): start = j * stride end = start + base_len chunk_data = make_file_data(y[start:end], sr, hop, nfft, win_len) data.append(chunk_data) file_names.append(file) Timer.end('Make test data for file: %i / %i' % (i + 1, len(pack)))
def train_file(model, dir, file, x_val, y_val): nb_epoch = 15 batch_size = 200 print('Training file %s' % file) Timer.start('Training file %s' % file) x_train, y_train, meta = load_file(dir, file) # early_stopping_callback = keras.callbacks.EarlyStopping(monitor='acc', min_delta=0.001, patience=200, verbose=1, mode='auto', baseline=None) # save_model = keras.callbacks.ModelCheckpoint(model_file, monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=False, mode='auto', period=10) model.fit(x_train, y_train, batch_size=batch_size, epochs=nb_epoch, validation_data=(x_val, y_val), verbose=2) Timer.end('Training file %s' % file)
def run(): ext = 'pdt3' run_id = 3 run_key = '%s:%i' % (ext, run_id) dir = '/Users/salm/Desktop/zalo_music/extra_%s' % ext model_file = '/Users/salm/Desktop/zalo_music/model_%s.%s.mdl' % (run_id, ext) files = list( filter(lambda f: os.path.splitext(f)[1] == '.%s' % ext, os.listdir(dir))) val_file = files[len(files) - 1] files = files[:len(files) - 1] classes = [ 'cai_luong', 'cach_mang', 'dan_ca', 'dance', 'khong_loi', 'thieu_nhi', 'trinh', 'tru_tinh', 'rap', 'rock' ] x_val, y_val, meta = load_file(dir, val_file) nfets = meta['nfets'] if os.path.isfile(model_file): model = keras.models.load_model(model_file) else: model = get_model(nfets, x_val.shape[1], len(classes)) model.compile(loss='sparse_categorical_crossentropy', optimizer=keras.optimizers.Adam(), metrics=['accuracy']) nb_runs = 10 r = int(rd.get('zlm:%s:runs' % run_key) or 0) while r < nb_runs: Timer.start('Training run %i / %i' % (r + 1, nb_runs)) i = int(rd.hget('zlm:%s:file' % run_key, r) or 0) while i < len(files): file = files[i] train_file(model, dir, file, x_val, y_val) model.save(model_file) i = rd.hincrby('zlm:%s:file' % run_key, r) r = rd.incr('zlm:%s:runs' % run_key) Timer.end('Training run %i / %i' % (r + 1, nb_runs))
def make_test_data(dir, out_dir, ext): mkdirp(out_dir) files = list( filter(lambda f: os.path.splitext(f)[1] in music_exts, os.listdir(dir))) packs = dist(files, len(files) // 300) n_pack = len(packs) for pi, pack in enumerate(packs): out_file = '%s/data_%i.%s' % (out_dir, pi, ext) if os.path.isfile(out_file): continue Timer.start('Make test data pack %i / %i' % (pi + 1, n_pack)) exe = ThreadPoolExecutor(max_workers=4) file_names = [] data = [] for _i, _file in enumerate(pack): def f(i, file): Timer.start('Make test data for file: %i / %i' % (i + 1, len(pack))) y, sr = lrs.load('%s/%s' % (dir, file), sr=base_sr) n = 1 + ((len(y) - base_len) // stride) for j in range(n): start = j * stride end = start + base_len chunk_data = make_file_data(y[start:end], sr, hop, nfft, win_len) data.append(chunk_data) file_names.append(file) Timer.end('Make test data for file: %i / %i' % (i + 1, len(pack))) exe.submit(lambda _file=_file, _i=_i: f(_i, _file)) exe.shutdown(wait=True) np.asarray(data).tofile(out_file) with open('%s/meta_%i.json' % (out_dir, pi), 'w') as fp: json.dump(file_names, fp) Timer.end('Make test data pack %i / %i' % (pi + 1, n_pack))
def f(full_file, i): file_with_lbl, file_ext = os.path.splitext(full_file) if file_ext not in audio_exts: return lbl, file = os.path.splitext(file_with_lbl) lbl = int(lbl) file = file[1:] Timer.start('Making data for %s(%i) (%i/%i)' % (file, pi + 1, i + 1, n)) meta = {'file': file, 'label': lbl} y, sr = librosa.load('%s/%i.%s%s' % (in_dir, lbl, file, file_ext)) meta['sr'] = sr # data = make_file_data(y, sr, hop, nfft, win_len) data = extract_features(y, sr, nfft, hop) pack_data.append(data) records[file] = meta meta_dict['labels'].append(lbl) Timer.end('Making data for %s(%i) (%i/%i)' % (file, pi + 1, i + 1, n))
def predict(model, dir, ext): files = list( filter(lambda f: os.path.splitext(f)[1] == '.%s' % ext, os.listdir(dir))) results = [] Timer.start('Prediction') for file in files: Timer.start('Predicting file %s' % file) idx = int(file.split('.')[0].split('_')[1]) meta_file = 'meta_%s.json' % idx with open('%s/%s' % (dir, meta_file), 'r') as fp: file_names = json.load(fp) x_predict = np.fromfile('%s/%s' % (dir, file)) x_predict = x_predict.reshape(len(file_names), -1, 1) predictions = model.predict(x_predict) for file_name, prediction in zip(file_names, predictions): results.append({'file': file_name, 'predict': prediction.tolist()}) Timer.end('Predicting file %s' % file) Timer.end('Prediction') return results
def f(i, file): Timer.start('Resampling file %i / %i' % (i + 1, n)) resampling('%s/%s' % (dir, file), lbl, out_dir) Timer.end('Resampling file %i / %i' % (i + 1, n))