Commit bf99460c authored by Félix Michaud's avatar Félix Michaud
Browse files

curves for crow

parent b7c69b9f
......@@ -190,7 +190,8 @@ def time_elong(sr, audio, max_time=2):
class Dataset(data.Dataset):
'Characterizes a dataset for PyTorch'
def __init__(self, path, name_cl, nb_classes=1, nb_classes_noise=1, augmentation=True, path_background="./noises"):
def __init__(self, mode, path, name_cl, nb_classes=1, nb_classes_noise=1, augmentation=True, path_background="./noises"):
self.mode = mode
self.dict_classes = self.load_data(path)
self.nb_classes = nb_classes
self.nb_classes_noise = nb_classes_noise
......@@ -244,8 +245,10 @@ class Dataset(data.Dataset):
def __len__(self):
'Denotes the total number of samples'
# return len(self.dict_classes)
nb_samples = 400000
if self.mode == 'train':
nb_samples = 200000
else:
nb_samples = 32
return nb_samples
......@@ -313,7 +316,7 @@ class Dataset(data.Dataset):
else:
n_noise = np.random.normal(loc=0, scale=1, size=(1, max_time*sr))
n_noise = librosa.to_mono(n_noise)
snr = np.random.randint(10, 20) #-10/5 for natural noise, 30/50
snr = np.random.randint(30, 50) #-10/5 for natural noise, 30/50
audio_mix = _add_noise(audio_mix, n_noise, snr, sr)
......
......@@ -72,7 +72,30 @@ def save_arguments(args, path):
file1.close()
def train(net, loader_train, optimizer, path, args):
def evaluation(net, loader, args):
#no upgrade over the gradient
torch.set_grad_enabled(False)
num_batch = 0
criterion = nn.BCELoss()
args.out_threshold = 0.4
for ii, batch_data in enumerate(loader):
# forward pass
magmix = batch_data[0]
magmix = magmix.to(args.device)
masks = unwrap_mask(batch_data[2])
masks = masks.to(args.device)
num_batch += 1
masks_pred = net(magmix)
# #loss
loss = criterion(masks_pred, masks)
#writing of the Loss values and elapsed time for every batch
batchtime = (time.time() - args.starting_training_time)/60 #minutes
with open(args.path + "/loss_times_eval.csv", "a") as f:
writer = csv.writer(f)
writer.writerow([str(loss.cpu().detach().numpy()), batchtime, num_batch])
def train(net, loader_train, loader_eval, optimizer, args):
torch.set_grad_enabled(True)
num_batch = 0
criterion = nn.BCELoss()
......@@ -97,37 +120,17 @@ def train(net, loader_train, optimizer, path, args):
# #writing of the Loss values and elapsed time for every batch
batchtime = (time.time() - args.starting_training_time)/60 #minutes
# #Writing of the elapsed time and loss for every batch
with open(args.path + "/loss_times.csv", "a") as f:
with open(args.path + "/loss_times_train.csv", "a") as f:
writer = csv.writer(f)
writer.writerow([str(loss.cpu().detach().numpy()), batchtime, num_batch])
writer.writerow([str(loss.cpu().detach().numpy()), batchtime, num_batch])
if ii%100 == 0:
evaluation(net, loader_eval, args)
torch.set_grad_enabled(True)
if ii%args.save_per_batchs == 0:
torch.save({ 'model_state_dict': net.state_dict(),
'optimizer_state_dict': optimizer.state_dict()},
args.path + '/Saved_models/model_batchs{}.pth.tar'.format(num_batch))
#def evaluation(net, loader, args):
##no upgrade over the gradient
# torch.set_grad_enabled(False)
# num_batch = 0
# criterion = nn.BCELoss()
# args.out_threshold = 0.4
# for ii, batch_data in enumerate(loader):
# # forward pass
# magmix = batch_data[0]
# magmix = magmix.to(args.device)
# masks = unwrap_mask(batch_data[2])
# masks = masks.to(args.device)
# num_batch += 1
# masks_pred = net(magmix)
## #loss
# loss = criterion(masks_pred, masks)
# #Visualization
# with open("./losses/loss_eval/loss_times_eval{}.csv".format(args.saved_model), "a") as f:
# writer = csv.writer(f)
# writer.writerow([str(loss.cpu().detach().numpy()), batchtime, num_batch])
#***************************************************
#****************** MAIN ***************************
......@@ -143,58 +146,47 @@ if __name__ == '__main__':
args.nb_classes = 1
args.mode = 'train'
args.lr_sounds = 1e-5
args.saved_model = '5_5000'
#model definition
net = UNet5(n_channels=1, n_classes=args.nb_classes)
net.apply(init_weights)
net = net.to(args.device)
# Set up optimizer
optimizer = create_optimizer(net, args)
args.path = "./Unet5/pitchr"
args._augment = 'natural_noise_gaussian noise_avec_masks_sans_pitch_shifting_2call_oiseaux_en_bruit noise snr reduced (10-20)'
bird = 'crow'
args.path = "./training_solo/5species/" + bird + '3'
args._augment = 'dropout0.2_natural_noise_gaussian noise_avec_masks_avec_pitch_shifting_3call_oiseaux_en_bruit_200000samples_5especes_oiseaux_for_noise'
###########################################################
################### TRAINING ##############################
###########################################################
if args.mode == 'train':
#OverWrite the Files for loss saving and time saving
fichierLoss = open(args.path+"/loss_times.csv", "w")
fichierLoss.close()
fichierTrain = open(args.path+"/loss_times_train.csv", "w")
fichierTrain.close()
fichierEval = open(args.path+"/loss_times_eval.csv", "w")
fichierEval.close()
#Dataset loading
root = './data_sound/trainset'
root_train = './data_sound/trainset2'
root_eval = './data_sound/valset'
ext = '.wav'
train_classes = Dataset(root,'crow', nb_classes=args.nb_classes, nb_classes_noise=2, path_background="./data_sound/noises/")
#training Dataset
train_classes = Dataset('train', root_train, bird, nb_classes=args.nb_classes, nb_classes_noise=2, path_background="./data_sound/noises/")
loader_train = torch.utils.data.DataLoader(
train_classes,
batch_size = args.batch_size,
shuffle=True,
num_workers=20)
#Eval Dataset
eval_classes = Dataset('eval', root_eval, bird, nb_classes=args.nb_classes, nb_classes_noise=2, path_background="./data_sound/noises/")
loader_eval = torch.utils.data.DataLoader(
eval_classes,
batch_size = args.batch_size,
shuffle=True,
num_workers=20)
for epoch in range(0, 1):
train(net, loader_train, optimizer, args.path, args)
train(net, loader_train, loader_eval, optimizer, args)
torch.save({
'model_state_dict': net.state_dict(),
'optimizer_state_dict': optimizer.state_dict()},
args.path+'/Saved_models/model_epoch{}.pth.tar'.format(epoch))
###########################################################
################### EVALUATION ############################
###########################################################
# if args.mode == 'eval':
# #OverWrite the Files for loss saving and time saving
# fichierLoss = open("./losses/loss_eval/loss_times_eval{}.csv".format(args.saved_model), "w")
# fichierLoss.close()
# #Dataset loading
# root = './data_sound/valset/'
# ext = '.wav'
# val_classes = Dataset(root, nb_classes=args.nb_classes, path_background="./data_sound/noises/")
# #inisialization of the model from the saved model
# checkpoint = torch.load('Saved_models2/model{}.pth.tar'.format(args.saved_model))
# net.load_state_dict(checkpoint['model_state_dict'])
#
# loader_eval = torch.utils.data.DataLoader(
# val_classes,
# batch_size = args.batch_size,
# shuffle=True,
# num_workers=20)
#
# for epoch in range(0, 1):
# evaluation(net, loader_eval, optimizer, args)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment