def inference(model, loader, label_name, logger, log_path, save_model_file): m, M = (144.78, 203.2) model.load_state_dict(torch.load(log_path + save_model_file)) true_labels = [] predictions = [] model.eval() # evaluation mode with torch.no_grad(): # no gradients/back propagation for evaluation for batch in loader: batch_x, batch_y = batch batch_x, batch_y = batch_x.float().to(device), batch_y.float().to( device) y_hat = model(batch_x) # forward pass throough model predictions += y_hat.cpu().detach().view(-1) true_labels += batch_y.cpu().detach().view(-1) prediction = torch.Tensor(predictions) * (M - m) + m label = torch.Tensor(true_labels) * (M - m) + m mse = MSE()(prediction, label) mae = MAE()(prediction, label) rmse = RMSE()(prediction, label) print(f'\n{label_name} Dataset :\tMSE : {mse}\tMAE : {mae}\tRMSE: {rmse}') logger.info( f'\n{label_name} Dataset :\tMSE : {mse}\tMAE : {mae}\tRMSE: {rmse}')
def __init__(self, HPARAMS): super().__init__() # HPARAMS self.save_hyperparameters() self.models = { 'wav2vecLSTMAttn': Wav2VecLSTMH, 'MultiScale': MultiScaleH, 'LSTMAttn': SpectralLSTMH, } self.model = self.models[HPARAMS['model_type']](HPARAMS['hidden_size']) self.regression_criterion = MSE() self.mae_criterion = MAE() self.rmse_criterion = RMSELoss() self.lr = HPARAMS['lr'] self.csv_path = HPARAMS['speaker_csv_path'] self.df = pd.read_csv(self.csv_path) self.h_mean = self.df['height'].mean() self.h_std = self.df['height'].std() print( f"Model Details: #Params = {self.count_total_parameters()}\t#Trainable Params = {self.count_trainable_parameters()}" )
def __init__(self, HPARAMS): super().__init__() # HPARAMS self.save_hyperparameters() self.model = Wav2VecLSTM(HPARAMS['model_hidden_size']) self.classification_criterion = MSE() self.regression_criterion = MSE() self.mae_criterion = MAE() self.rmse_criterion = RMSELoss() self.accuracy = Accuracy() self.alpha = HPARAMS['model_alpha'] self.beta = HPARAMS['model_beta'] self.gamma = HPARAMS['model_gamma'] self.lr = HPARAMS['training_lr'] self.csv_path = HPARAMS['speaker_csv_path'] self.df = pd.read_csv(self.csv_path, sep=' ') self.h_mean = self.df['Height'].mean() self.h_std = self.df['Height'].std() self.a_mean = self.df['Age'].mean() self.a_std = self.df['Age'].std() print(f"Model Details: #Params = {self.count_total_parameters()}\t#Trainable Params = {self.count_trainable_parameters()}")
def test_mae(pred, target, exp): mae = MAE() assert mae.name == 'mae' score = mae(pred=torch.tensor(pred), target=torch.tensor(target)) assert isinstance(score, torch.Tensor) assert score.item() == exp
def test_mae(): mae = MAE() assert mae.name == 'mae' pred = torch.tensor([0., 1, 2, 3]) target = torch.tensor([0., 1, 2, 2]) score = mae(pred, target) assert isinstance(score, torch.Tensor)
def __init__(self, HPARAMS): super().__init__() # HPARAMS self.save_hyperparameters() lstm_inp=512 lstm_h = HPARAMS['model_hidden_size'] alpha = HPARAMS['model_alpha'] beta = HPARAMS['model_beta'] gamma = HPARAMS['model_gamma'] csv_path = HPARAMS['speaker_csv_path'] self.encoder = Wav2Vec(pretrained=True) for param in self.encoder.parameters(): param.requires_grad = False for param in self.encoder.feature_extractor.conv_layers[5:].parameters(): param.requires_grad = True self.lstm = nn.LSTM(lstm_inp, lstm_h, batch_first=True) self.attention = Attention(lstm_h) self.height_regressor = nn.Linear(lstm_h, 1) self.age_regressor = nn.Linear(lstm_h, 1) self.gender_classifier = nn.Linear(lstm_h, 1) self.classification_criterion = MSE() self.regression_criterion = MSE() self.mae_criterion = MAE() self.rmse_criterion = RMSELoss() self.accuracy = Accuracy() self.alpha = alpha self.beta = beta self.gamma = gamma self.lr = HPARAMS['training_lr'] self.csv_path = csv_path self.df = pd.read_csv(self.csv_path, sep=' ') self.h_mean = self.df['Height'].mean() self.h_std = self.df['Height'].std() self.a_mean = self.df['Age'].mean() self.a_std = self.df['Age'].std() print(f"Model Details: #Params = {self.count_total_parameters()}\t#Trainable Params = {self.count_trainable_parameters()}")
def __init__( self, tasks: list, dm: DataModule, n_c: int, # number of convolutions latent_dim: int, n_final_convolutions: int = 1, params_scarcity: int = 0, gamma: float = 0.98, lr: float = 1e-3, ): super().__init__() self.encoder = TrajsEncoder(n_c=n_c, latent_dim=latent_dim, dm=dm) self.save_hyperparameters("gamma") self.save_hyperparameters("lr") self.save_hyperparameters("tasks") self.save_hyperparameters("latent_dim") self.save_hyperparameters("n_c") self.save_hyperparameters(dm.ds_params) self.save_hyperparameters(dm.graph_info) outputs = self.get_output_modules(tasks, latent_dim, dm.ds_params["dim"], dm.ds_params["RW_types"]) self.out_networks = {} self.losses = {} self.targets = {} for out in outputs: net, target, loss = outputs[out] self.out_networks[out] = net self.targets[out] = target self.losses[out] = loss self.out_networks = nn.ModuleDict(self.out_networks) self.loss_scale = {} self.set_loss_scale() if "alpha" in tasks: self.MAE = MAE() if "model" in tasks: self.F1 = F1(len(dm.ds_params["RW_types"])) if "drift_norm" in tasks: self.EV = EV()
def train(model, trainloader, valloader, criterion, optimizer, epochs, logger, tensorboard_path, log_path, save_model_file): writer = SummaryWriter( f'{tensorboard_path}{datetime.now().strftime("%m-%d-%Y_%H:%M:%S")}') criterion2 = RMSE().to(device) criterion3 = MAE().to(device) val_loss_min = np.inf scheduler_step = 0 # loss lists to visualize train_losses = [] train_accuracies = [] val_losses = [] val_accuracies = [] # scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=300) # scheduler = torch.optim.lr_scheduler.CyclicLR(optimizer, base_lr=1e-5, max_lr=1e-2, cycle_momentum=False) # training loop m, M = (144.78, 203.2) for epoch in range(epochs): t1 = time.time() epoch_loss = 0 epoch_accuracy = 0 model.train() # training mode for batch in tqdm(trainloader): model.train() batch_x, batch_y = batch batch_x, batch_y = batch_x.float().to(device), batch_y.view( -1).float().to(device) y_hat = model(batch_x) # forward pass throough model # loss = criterion(y_hat.view(-1), batch_y) + criterion2(y_hat.view(-1), batch_y) + criterion3(y_hat.view(-1), batch_y) loss = criterion2(y_hat.view(-1), batch_y) # + criterion(y_hat2, batch_y2) # compute the loss epoch_loss += loss.item() optimizer.zero_grad() # zero the previous computed gradients loss.backward() # calculate loss gradients wrt all parameters optimizer.step() # update all parameters in model with optimizer # scheduler.step() # writer.add_scalar('LR/lr', float(scheduler.get_lr()[0]), scheduler_step) scheduler_step += 1 # torch.cuda.empty_cache() # calculate the accuracy of prediction mae = MAE()(y_hat.view(-1) * (M - m) + m, batch_y * (M - m) + m).item() epoch_accuracy += mae # print(loss.item(), train_batch_accuracy) epoch_loss = epoch_loss / len(trainloader) epoch_accuracy = epoch_accuracy / len(trainloader) train_losses.append(epoch_loss) train_accuracies.append(epoch_accuracy) writer.add_scalar('MSE/train', epoch_loss, epoch) writer.add_scalar('MAE/train', epoch_accuracy, epoch) # torch.cuda.empty_cache() # gc.collect() model.eval() # evaluation mode with torch.no_grad(): # no gradients/back propagation for evaluation val_loss = 0 val_accuracy = 0 for batch in valloader: batch_x, batch_y = batch batch_x, batch_y = batch_x.float().to( device), batch_y.float().view(-1).to(device) y_hat = model(batch_x) # loss = criterion(y_hat.view(-1), batch_y) + criterion2(y_hat.view(-1), batch_y) + criterion3(y_hat.view(-1), batch_y) loss = criterion2(y_hat.view(-1), batch_y) # + criterion(y_hat2, batch_y2) val_loss += loss.item() # validation accuracy mae = MAE()(y_hat.view(-1) * (M - m) + m, batch_y * (M - m) + m).item() val_accuracy += mae val_loss = val_loss / len(valloader) val_accuracy = val_accuracy / len(valloader) val_losses.append(val_loss) val_accuracies.append(val_accuracy) writer.add_scalar('MSE/val', val_loss, epoch) writer.add_scalar('MAE/val', val_accuracy, epoch) if val_loss < val_loss_min: torch.save(model.state_dict(), log_path + save_model_file) logger.info( f'Validation loss reduced from {val_loss_min} to {val_loss}, saving model...' ) tqdm.write( f'Validation loss reduced from {val_loss_min} to {val_loss}, saving model...' ) val_loss_min = val_loss t2 = time.time() t = t2 - t1 logger.info( f'Epoch : {epoch+1:02}\tLoss : {epoch_loss:.4f}\tMAE : {epoch_accuracy:.4f}\tVal Loss : {val_loss:.4f}\tVal MAE : {val_accuracy:.4f}\tTime : {t:.2f} s' ) tqdm.write( f'\nEpoch : {epoch+1:02}\tLoss : {epoch_loss:.4f}\tMAE : {epoch_accuracy:.4f}\tVal Loss : {val_loss:.4f}\tVal MAE : {val_accuracy:.4f}\tTime : {t:.2f} s\n' ) writer.close() plt.figure(figsize=(20, 10)) plt.title('Training Learning Curve') plt.subplot(1, 2, 1) plt.xlabel('Epochs') plt.ylabel('Cross Entropy loss') plt.plot(train_losses, label='Loss') plt.plot(val_losses, label='Val Loss') plt.legend() plt.subplot(1, 2, 2) plt.xlabel('Epochs') plt.ylabel('Accuracy') plt.plot(train_accuracies, label='Accuracy') plt.plot(val_accuracies, label='Val Accuracy') plt.legend() plt.savefig(log_path + 'training_curve.png') plt.show()