def train(args): src_root = args.src_root sr = args.sample_rate dt = args.delta_time batch_size = args.batch_size model_type = args.model_type params = {'N_CLASSES': len(os.listdir(args.src_root)), 'SR': sr, 'DT': dt} models = { 'conv1d': Conv1D(**params), 'conv2d': Conv2D(**params), 'lstm': LSTM(**params) } assert model_type in models.keys(), '{} not an available model'.format( model_type) csv_path = os.path.join('logs', '{}_history.csv'.format(model_type)) wav_paths = glob('{}/**'.format(src_root), recursive=True) wav_paths = [x.replace(os.sep, '/') for x in wav_paths if '.wav' in x] classes = sorted(os.listdir(args.src_root)) le = LabelEncoder() le.fit(classes) labels = [os.path.split(x)[0].split('/')[-1] for x in wav_paths] labels = le.transform(labels) wav_train, wav_val, label_train, label_val = train_test_split( wav_paths, labels, test_size=0.1, random_state=0) assert len( label_train ) >= args.batch_size, 'Anzahl der trainierten Dateien muss >= batch_size sein' tg = DataGenerator(wav_train, label_train, sr, dt, len(set(label_train)), batch_size=batch_size) vg = DataGenerator(wav_val, label_val, sr, dt, len(set(label_val)), batch_size=batch_size) model = models[model_type] cp = ModelCheckpoint('models/{}.h5'.format(model_type), monitor='val_loss', save_best_only=True, save_weights_only=False, mode='auto', save_freq='epoch', verbose=1) csv_logger = CSVLogger(csv_path, append=False) model.fit(tg, validation_data=vg, epochs=30, verbose=1, callbacks=[csv_logger, cp])
def train(args): src_root = args.src_root sr = args.sample_rate dt = args.delta_time batch_size = args.batch_size model_type = args.model_type models = {'conv1d': Conv1D(), 'conv2d': Conv2D(), 'lstm': LSTM()} assert model_type in models.keys(), '{} not an available model'.format( model_type) wav_paths = glob('{}/**'.format(src_root), recursive=True) wav_paths = [x for x in wav_paths if '.wav' in x] classes = sorted(os.listdir(args.src_root)) le = LabelEncoder() le.fit(classes) labels = [get_class(x, src_root) for x in wav_paths] labels = le.transform(labels) wav_train, wav_val, label_train, label_val = train_test_split( wav_paths, labels, test_size=0.1, random_state=0) tg = DataGenerator(wav_train, label_train, sr, dt, len(set(label_train)), batch_size=batch_size) vg = DataGenerator(wav_val, label_val, sr, dt, len(set(label_val)), batch_size=batch_size) model = models[model_type] cp = ModelCheckpoint('models/{}.h5'.format(model_type), monitor='val_loss', save_best_only=True, save_weights_only=False, mode='auto', save_freq='epoch', verbose=1) csv_logger = CSVLogger(os.path.join('logs', '{}_history.csv'.format(model_type)), append=True) model.fit(tg, validation_data=vg, epochs=30, verbose=1, callbacks=[csv_logger, cp])
def train(args): src_root = args.src_root sr = args.sample_rate dt = args.delta_time batch_size = args.batch_size model_type = args.model_type params = {'N_CLASSES':len(os.listdir(args.src_root)), 'SR':sr, 'DT':dt} models = {'conv1d':Conv1D(**params), 'conv2d':Conv2D(**params), 'lstm': LSTM(**params), 'transformer': Transformer(**params), 'ViT': ViT(**params)} assert model_type in models.keys(), '{} not an available model'.format(model_type) csv_path = os.path.join('logs', '{}_history.csv'.format(model_type)) wav_paths = glob('{}/**'.format(src_root), recursive=True) wav_paths = [x.replace(os.sep, '/') for x in wav_paths if '.wav' in x] classes = sorted(os.listdir(args.src_root)) le = LabelEncoder() le.fit(classes) labels = [os.path.split(x)[0].split('/')[-1] for x in wav_paths] labels = le.transform(labels) wav_train, wav_val, label_train, label_val = train_test_split(wav_paths, labels, test_size=0.1, random_state=0) assert len(label_train) >= args.batch_size, 'Number of train samples must be >= batch_size' if len(set(label_train)) != params['N_CLASSES']: warnings.warn('Found {}/{} classes in training data. Increase data size or change random_state.'.format(len(set(label_train)), params['N_CLASSES'])) if len(set(label_val)) != params['N_CLASSES']: warnings.warn('Found {}/{} classes in validation data. Increase data size or change random_state.'.format(len(set(label_val)), params['N_CLASSES'])) tg = DataGenerator(wav_train, label_train, sr, dt, params['N_CLASSES'], batch_size=batch_size) vg = DataGenerator(wav_val, label_val, sr, dt, params['N_CLASSES'], batch_size=batch_size) model = models[model_type] model.summary() cp = ModelCheckpoint('models/{}.h5'.format(model_type), monitor='val_loss', save_best_only=True, save_weights_only=False, mode='auto', save_freq='epoch', verbose=1) csv_logger = CSVLogger(csv_path, append=False) model.fit(tg, validation_data=vg, epochs=40, verbose=1, callbacks=[csv_logger])
def main(args): with open(args.config, 'r') as f: config = json.load(f) if config['optimizer'] == 'SGD': optimizer = SGD(lr=config['learning_rate'], decay=config['learning_rate'] / config['epochs'], momentum=config['momentum']) else: raise Exception('Unsupported optimizer: {}.'.format( config['optimizer'])) model_name = str.lower(config['model']) if model_name == 'lstm': model = LSTM(config['input_length'], 2) elif model_name == 'conv1d': model = Conv1D(config['input_length'], 2) elif model_name == 'conv2d': model = Conv2D(config['input_length'], 2) else: raise Exception('Unsupported model: {}.'.format(config['model'])) model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy']) wav_paths = glob('{}/**'.format(args.data_dir), recursive=True) wav_paths = [x for x in wav_paths if '.wav' in x] classes = sorted(os.listdir(args.data_dir)) le = LabelEncoder() le.fit(classes) labels = [get_class(x, args.data_dir) for x in wav_paths] labels = le.transform(labels) print('CLASSES: ', list(le.classes_)) print(le.transform(list(le.classes_))) wav_train, wav_val, label_train, label_val = train_test_split( wav_paths, labels, test_size=config['validation_split'], random_state=0) tg = DataGenerator(wav_train, label_train, config['input_length'], len(set(label_train)), batch_size=config['batch_size']) vg = DataGenerator(wav_val, label_val, config['input_length'], len(set(label_val)), batch_size=config['batch_size']) output_sub_dir = os.path.join(args.output_dir, model_name, datetime.now().strftime('%Y%m%d_%H%M%S')) os.makedirs(output_sub_dir) callbacks = [ EarlyStopping(monitor='val_loss', patience=config['patience'], restore_best_weights=True, verbose=1), ModelCheckpoint(filepath=os.path.join( output_sub_dir, 'model.{epoch:02d}-{val_loss:.4f}.h5'), monitor='val_loss', save_best_only=True, verbose=1), CSVLogger(os.path.join(output_sub_dir, 'epochs.csv')) ] model.fit(tg, validation_data=vg, epochs=config['epochs'], verbose=1, callbacks=callbacks)