def train(args, i): '''Training. Model will be saved after several iterations. Args: dataset_dir: string, directory of dataset workspace: string, directory of workspace holdout_fold: '1' | 'none', set 1 for development and none for training on all data without validation model_type: string, e.g. 'Cnn_9layers_AvgPooling' batch_size: int cuda: bool mini_data: bool, set True for debugging on a small part of data ''' # Arugments & parameters dataset_dir = args.dataset_dir workspace = args.workspace holdout_fold = args.holdout_fold model_type = args.model_type batch_size = args.batch_size cuda = args.cuda and torch.cuda.is_available() mini_data = args.mini_data filename = args.filename audio_num = config.audio_num mel_bins = config.mel_bins frames_per_second = config.frames_per_second max_iteration = None # Number of mini-batches to evaluate on training data reduce_lr = True in_domain_classes_num = len(config.labels) # Paths if mini_data: prefix = 'minidata_' else: prefix = '' train_csv = os.path.join(sys.path[0], 'fold' + str(i) + '_train.csv') validate_csv = os.path.join(sys.path[0], 'fold' + str(i) + '_test.csv') feature_hdf5_path = os.path.join( workspace, 'features', '{}logmel_{}frames_{}melbins.h5'.format(prefix, frames_per_second, mel_bins)) checkpoints_dir = os.path.join( workspace, 'checkpoints', filename, '{}logmel_{}frames_{}melbins.h5'.format(prefix, frames_per_second, mel_bins), 'holdout_fold={}'.format(holdout_fold), model_type) create_folder(checkpoints_dir) validate_statistics_path = os.path.join( workspace, 'statistics', filename, '{}logmel_{}frames_{}melbins'.format(prefix, frames_per_second, mel_bins), 'holdout_fold={}'.format(holdout_fold), model_type, 'validate_statistics.pickle') create_folder(os.path.dirname(validate_statistics_path)) logs_dir = os.path.join( workspace, 'logs', filename, args.mode, '{}logmel_{}frames_{}melbins'.format(prefix, frames_per_second, mel_bins), 'holdout_fold={}'.format(holdout_fold), model_type) create_logging(logs_dir, 'w') logging.info(args) if cuda: logging.info('Using GPU.') else: logging.info('Using CPU. Set --cuda flag to use GPU.') # Model Model = eval(model_type) model = Model(in_domain_classes_num, activation='logsoftmax') loss_func = nll_loss if cuda: model.cuda() # Optimizer optimizer = optim.Adam(model.parameters(), lr=1e-3, betas=(0.9, 0.999), eps=1e-08, weight_decay=0., amsgrad=True) # optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9, weight_decay=1e-5) # Data generator data_generator = DataGenerator(feature_hdf5_path=feature_hdf5_path, train_csv=train_csv, validate_csv=validate_csv, holdout_fold=holdout_fold, batch_size=batch_size) # Evaluator evaluator = Evaluator(model=model, data_generator=data_generator, cuda=cuda) # Statistics validate_statistics_container = StatisticsContainer( validate_statistics_path) train_bgn_time = time.time() iteration = 0 # Train on mini batches for batch_data_dict in data_generator.generate_train(): # Evaluate if iteration % 100 == 0 and iteration >= 1500: logging.info('------------------------------------') logging.info('Iteration: {}'.format(iteration)) train_fin_time = time.time() train_statistics = evaluator.evaluate(data_type='train', iteration=iteration, max_iteration=None, verbose=False) if holdout_fold != 'none': validate_statistics = evaluator.evaluate(data_type='validate', iteration=iteration, max_iteration=None, verbose=False) validate_statistics_container.append_and_dump( iteration, validate_statistics) train_time = train_fin_time - train_bgn_time validate_time = time.time() - train_fin_time logging.info('Train time: {:.3f} s, validate time: {:.3f} s' ''.format(train_time, validate_time)) train_bgn_time = time.time() # Save model if iteration % 100 == 0 and iteration > 0: checkpoint = { 'iteration': iteration, 'model': model.state_dict(), 'optimizer': optimizer.state_dict() } checkpoint_path = os.path.join( checkpoints_dir, '{}_iterations.pth'.format(iteration)) torch.save(checkpoint, checkpoint_path) logging.info('Model saved to {}'.format(checkpoint_path)) # Reduce learning rate if reduce_lr and iteration % 100 == 0 and iteration > 0: for param_group in optimizer.param_groups: param_group['lr'] *= 0.9 # Move data to GPU for key in batch_data_dict.keys(): if key in ['feature', 'target']: batch_data_dict[key] = move_data_to_gpu( batch_data_dict[key], cuda) # Train for i in range(audio_num): model.train() data, target_a, target_b, lam = mixup_data( x=batch_data_dict['feature'][:, i, :, :], y=batch_data_dict['target'], alpha=0.2) batch_output = model(data) # batch_output = model(batch_data_dict['feature']) # loss loss = loss_func(batch_output, batch_data_dict['target']) loss = mixup_criterion(loss_func, batch_output, target_a, target_b, lam) # Backward optimizer.zero_grad() loss.backward() optimizer.step() # Stop learning if iteration == 4000: break iteration += 1
def inference_validation(args): '''Inference and calculate metrics on validation data. Args: dataset_dir: string, directory of dataset workspace: string, directory of workspace model_type: string, e.g. 'Cnn_9layers' iteration: int batch_size: int cuda: bool mini_data: bool, set True for debugging on a small part of data visualize: bool ''' # Arugments & parameters dataset_dir = args.dataset_dir workspace = args.workspace model_type = args.model_type holdout_fold = args.holdout_fold iteration = args.iteration batch_size = args.batch_size cuda = args.cuda and torch.cuda.is_available() mini_data = args.mini_data visualize = args.visualize filename = args.filename data_length = args.data_length mel_bins = config.mel_bins frames_per_second = config.frames_per_second in_domain_classes_num = len(config.labels) # Paths if mini_data: prefix = 'minidata_' else: prefix = '' train_csv = 'fold1_train.csv' validate_csv = 'fold1_test.csv' feature_hdf5_path = os.path.join( workspace, 'features', '{}logmel_{}frames_{}melbins.h5'.format(prefix, frames_per_second, mel_bins)) scalar_path = os.path.join( workspace, 'scalars', '{}logmel_{}frames_{}melbins.h5'.format(prefix, frames_per_second, mel_bins)) checkpoint_path = os.path.join( workspace, 'checkpoints', filename, '{}logmel_{}frames_{}melbins'.format(prefix, frames_per_second, mel_bins), 'holdout_fold={}'.format(holdout_fold), model_type, '{}_iterations.pth'.format(iteration)) logs_dir = os.path.join( workspace, 'logs', filename, args.mode, '{}logmel_{}frames_{}melbins'.format(prefix, frames_per_second, mel_bins), 'holdout_fold={}'.format(holdout_fold), model_type) create_logging(logs_dir, 'w') logging.info(args) # Load scalar scalar = load_scalar(scalar_path) # Load model Model = eval(model_type) model = Model(in_domain_classes_num, activation='logsoftmax') loss_func = nll_loss checkpoint = torch.load(checkpoint_path) model.load_state_dict(checkpoint['model']) if cuda: model.cuda() # Data generator data_generator = DataGenerator(feature_hdf5_path=feature_hdf5_path, train_csv=train_csv, validate_csv=validate_csv, holdout_fold=holdout_fold, scalar=scalar, batch_size=batch_size) # Evaluator evaluator = Evaluator(model=model, data_generator=data_generator, cuda=cuda) if subtask in ['a', 'c']: evaluator.evaluate(data_type='validate', verbose=True) elif subtask == 'b': evaluator.evaluate(data_type='validate', verbose=True) evaluator.evaluate(data_type='validate', verbose=True) evaluator.evaluate(data_type='validate', verbose=True) # Visualize log mel spectrogram if visualize: evaluator.visualize(data_type='validate')
parser.add_argument("-data_parallel", "--data_parallel", type = int, help="1 if model is to be distributed across multiple GPUs") parser.add_argument("-model_type", "--model_type", type = str, choices=["MTL_SEDNetwork","GMP", "GAP", "GWRP", "AttrousCNN_2DAttention"]) parser.add_argument("-val_fold", "--val_fold", required=True, type=int, help="holdout fold between 1,2,3,4") parser.add_argument("-snr", "--snr", required=True, type=int, help="SNR between 0, 10, 20") parser.add_argument("-alpha", "--alpha", required=True, type=float, help="Alpha (weightage) for reconstruction loss") args = parser.parse_args() # Setting up experiments data_parallel = args.data_parallel == 1 MTL = args.model_type == "MTL_SEDNetwork" # if true use auxillary task MTL loss cuda = torch.cuda.is_available() create_folder('experiments') base_path = os.path.join('experiments', args.exp_name) model_path = os.path.join(base_path, 'best.pth') create_folder(base_path) create_logging(base_path, filemode = 'w') logging.info(f'logging started for experiment = {args.exp_name} and validation fold = {args.val_fold}') # Data and yaml path data_path = os.path.join(args.data_dir, f'logmel/logmel_snr_{args.snr}.h5') yaml_path = os.path.join(args.data_dir, 'mixture.yaml') with open(yaml_path, 'r') as f: meta = yaml.load(f, Loader=yaml.FullLoader) f.close() # Dataset and Dataloader data_container = get_dataloader(data_path, yaml_path, args, cuda) # model and dataparallel Model = get_model(args.model_type) model = Model(config.classes_num, config.seq_len, config.mel_bins, cuda)
import time import microbit import utils import logging from envirobit import sound utils.create_logging('.') def show_txt(txt): microbit.display.show(txt) if __name__ == "__main__": logger = logging.getLogger() for i in range(0, 100): got_clap = sound.wait_for_clap() #logger.info('the temperature is %s degrees celsius' % temp) logger.info('I got a clap now, that is %s' % got_clap) time.sleep(1)
for i in range(len(pred)): # first second if pred[i] == 0 and start == -1: start = i end = i+3 if pred[i] == 0 and start != -1: end = i+3 if start != -1: return True, start, end else : return False, -1, -1 if __name__ == '__main__': logs_dir = '.logs/' create_logging(logs_dir, 'w') test_bgn_time = time.time() checkpoint_path = '/home/cdd/code/cry_sound_detection/FPNet/workspace/checkpoints/main/logmel_50frames_64melbins.h5/holdout_fold=1/Cnns/2000_iterations.pth' model_type = 'Cnns' test_fold = '/home/cdd/code/cry_sound_detection/FPNet/util/test_babycry' # test_wav = 'mixture_devtest_babycry_000_c02f92b79f2bbefa98d008f3c2d9b704.wav' # test_wav = 'mixture_devtest_babycry_004_19c4bcb10576524449c83b01fae0b731.wav' for root, dirs, files in os.walk(test_fold): file = files logging.info('------------------------------------') for f in file: test_wav = f logging.info('Test: {}'.format(test_wav)) res, start, end = test_one(checkpoint_path=checkpoint_path, model_type=model_type, cuda=True, test_fold=test_fold, test_wav=test_wav, test_segment= False) if res == True: logging.info('Test result: Babycry! ({}~{}s)'.format(start, end))