示例#1
0
def complete_training(data, epochs=10, train_size=0.8, writer=None):
	num_items = len(data)
	num_train = round(num_items * train_size)
	num_test = num_items - num_train
	train_data, test_data = random_split(data, [num_train, num_test])

	train_dl = torch.utils.data.DataLoader(train_data, batch_size=16, shuffle=True)
	test_dl = torch.utils.data.DataLoader(test_data, batch_size=16, shuffle=False)

	model = AudioClassifier()
	device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
	model = model.to(device)
	next(model.parameters()).device

	training(model, train_dl, device, epochs, writer=writer)
	testing(model, test_dl, device, f'figures/confusion_{date.today().strftime("%m-%d-%y")}.png')
示例#2
0
def run_fold(fold, train_df, args,size=(224, 224), arch='resnet18', pretrained=True,   path='MODELS/', data_transforms=None):
  
  torch.cuda.empty_cache()

  fold_train = train_df[train_df.fold != fold].reset_index(drop=True)
  fold_val = train_df[train_df.fold == fold].reset_index(drop=True)

  train_ds = AudioDataset(images_path=args.specs_images_path, df=fold_train, transforms=data_transforms['train'])
  val_ds = AudioDataset(images_path=args.specs_images_path, df=fold_val, transforms=data_transforms['train'])

  trainloader = DataLoader(train_ds, batch_size=args.train_batch_size, shuffle=True , num_workers=os.cpu_count())
  validloader = DataLoader(val_ds, batch_size=args.test_batch_size, shuffle=False , num_workers=os.cpu_count())

  del train_ds
  del val_ds
  del fold_train
  del fold_val

  model = AudioClassifier(arch_name=arch, lr=args.lr, pretrained=pretrained)

  tb_logger = loggers.TensorBoardLogger(save_dir='./runs', name='ZINDI-GIZ-NLP-AGRI-KEYWORDS', version=fold)

  ckpt_callback = pl.callbacks.ModelCheckpoint(filename=f'ZINDI-GIZ-NLP-AGRI-KEYWORDS-{model.hparams.arch_name}-{fold}-based', 
                                               dirpath=path, 
                                               monitor='val_logLoss', 
                                               mode='min')
  
  trainer = Trainer(max_epochs=args.num_epochs, gpus=args.gpus, logger=tb_logger, callbacks=[ckpt_callback])

  trainer.fit(model, trainloader, validloader)


  gc.collect() # collect garbage

  return trainer.logged_metrics
示例#3
0
def load_model(path):
	model = AudioClassifier()
	model.load_state_dict(torch.load(f'./models/{path}'))
	model.eval()

	device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
	model = model.to(device)
	next(model.parameters()).device

	return model, device
示例#4
0
def load_model(file, train=False):
	model = AudioClassifier()
	optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

	checkpoint = torch.load('./models/'+file)
	model.load_state_dict(checkpoint['model_state_dict'])
	optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
	epoch = checkpoint['epoch']
	loss = checkpoint['loss']

	if train:
		model.train()
	else:
		model.eval()

	return model
示例#5
0
def load_models(models_path, arch=None, n_folds=3, device='cuda'):

    models = []
    for i in range(n_folds):
        models.append( AudioClassifier(arch_name=arch) )
        models[i].to(device)
        try:
            models[i].load_from_checkpoint(os.path.join(models_path, f'ZINDI-GIZ-NLP-AGRI-KEYWORDS-{arch}-{i}-based.ckpt'))
        except:
            models[i].load_from_checkpoint(os.path.join(models_path, f'ZINDI-GIZ-NLP-AGRI-KEYWORDS-{arch}-{i}-based-v0.ckpt'))
        models[i].eval()

    return models
示例#6
0
def kfold_cross_validation(data, folds=5, epochs=10, writer=None):
	accuracies = []
	kfold = KFold(n_splits=folds, shuffle=True)

	for fold, (train, test) in enumerate(kfold.split(data)):
		train_subsampler = torch.utils.data.SubsetRandomSampler(train)
		test_subsampler = torch.utils.data.SubsetRandomSampler(test)
		
		train_dl = torch.utils.data.DataLoader(data, batch_size=16, sampler=train_subsampler)
		test_dl = torch.utils.data.DataLoader(data, batch_size=16, sampler=test_subsampler)

		model = AudioClassifier()
		device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
		model = model.to(device)
		next(model.parameters()).device
		model.apply(reset_weights)

		training(model, train_dl, num_epochs=epochs, device=device, writer=writer)

		acc = testing(model, test_dl, device, f'figures/confusion_fold{fold}_{date.today().strftime("%m-%d-%y")}.png')
		accuracies.append(acc)

	print([f'{v:.2f} accuracy for fold {k+1}' for k,v in enumerate(accuracies)])
示例#7
0
        c = ComplexAudioClassifier(n_neurons=args.n_neurons,
                                   sess=sess,
                                   data_set=d,
                                   batch_size=args.batch_size,
                                   learning_rate=args.learning_rate,
                                   n_classes=n_classes,
                                   name=name,
                                   decay_op=decay_op,
                                   onehot_labels=args.onehot)
    else:
        decay_op = lambda x: 1.
        c = AudioClassifier(n_neurons=args.n_neurons,
                            sess=sess,
                            data_set=d,
                            batch_size=args.batch_size,
                            learning_rate=args.learning_rate,
                            n_classes=n_classes,
                            name=name,
                            decay_op=decay_op,
                            onehot_labels=args.onehot,
                            pool=args.pool)

    c.train(num_epochs=args.num_epochs,
            test=args.test,
            close=False,
            save=args.save,
            save_path=args.dest)

    if not args.mem_friendly:
        ns = []
        for i in range(c.n_classes):
            n = np.sum(np.argmax(c.get_data().list_of_data[1], axis=1) == i)