Example #1
0
# for mf in model_files:

model.eval()
losses  = []
y_score = []
y_test  = []
#val_loader.init_epoch()
for inputs, targets in test_loader:
    inputs = to_one_hot(inputs, n_dims=4).permute(0,3,1,2).squeeze().float()
    targets = targets.float()
    inp_batch = Variable( inputs ).cuda()
    trg_batch = Variable(targets).cuda()        
    outputs = model(inp_batch)
    loss = criterion(outputs.view(-1), trg_batch.view(-1))
    losses.append(loss.item())
    y_score.append( outputs.cpu().data.numpy() )
    y_test.append(  targets.cpu().data.numpy() )

epoch_loss = sum(losses)/len(val)
avg_ROC_auc = calc_auc(model, np.row_stack(y_test), np.row_stack(y_score), "ROC")
avg_PR_auc = calc_auc(model, np.row_stack(y_test), np.row_stack(y_score), "PR")
timenow = timeSince(start)
print( "Time: {}, Validation loss: {}, Mean ROC AUC: {}, Mean PRAUC: {}".format( 
                                                                            timenow, epoch_loss, avg_ROC_auc,avg_PR_auc),
       file=Logger)

if args.stop_instance:
    Logger.close()
    subprocess.call(['sudo','halt'])

Example #2
0
 y_test = []
 #val_loader.init_epoch()
 for inputs, targets in val_loader:
     inputs = to_one_hot(inputs, n_dims=4).permute(0, 3, 1,
                                                   2).squeeze().float()
     targets = targets.float()
     inp_batch = Variable(inputs).cuda()
     trg_batch = Variable(targets).cuda()
     outputs = model(inp_batch)
     loss = criterion(outputs.view(-1), trg_batch.view(-1))
     losses.append(loss.item())
     y_score.append(outputs.cpu().data.numpy())
     y_test.append(targets.cpu().data.numpy())
 epoch_loss = sum(losses) / len(val)
 avg_ROC_auc = calc_auc(model,
                        np.row_stack(y_test)[:, v_idx],
                        np.row_stack(y_score)[:, v_idx], "ROC")
 avg_PR_auc = calc_auc(model,
                       np.row_stack(y_test)[:, v_idx],
                       np.row_stack(y_score)[:, v_idx], "PR")
 timenow = timeSince(start)
 print(
     "Epoch [{}/{}], Time: {}, Validation loss: {}, Mean ROC AUC: {}, Mean PRAUC: {}"
     .format(epoch + 1, args.num_epochs, timenow, epoch_loss, avg_ROC_auc,
             avg_PR_auc),
     file=Logger)
 if epoch_loss <= best_loss:
     torch.save(model.state_dict(), args.model_file)
     print("Delta loss: {}, Model saved at {}".format(
         (epoch_loss - best_loss), args.model_file),
           file=Logger)
Example #3
0
    model.eval()
    losses  = []
    y_score = []
    y_test  = []
    #val_loader.init_epoch()
    for inputs, targets in val_loader:
        inputs = to_one_hot(inputs, n_dims=4).permute(0,3,1,2).squeeze().float()
        targets = targets.float()
        inp_batch = Variable( inputs ).cuda()
        trg_batch = Variable(targets).cuda()        
        outputs = model(inp_batch)
        loss = criterion(outputs.view(-1), trg_batch.view(-1))
        losses.append(loss.item())
        y_score.append( outputs.cpu().data.numpy() )
        y_test.append(  targets.cpu().data.numpy() )
    epoch_loss = sum(losses)/len(val)
    avg_ROC_auc = calc_auc(model, np.row_stack(y_test), np.row_stack(y_score), "ROC")
    avg_PR_auc = calc_auc(model, np.row_stack(y_test), np.row_stack(y_score), "PR")
    timenow = timeSince(start)
    print( "Epoch [{}/{}], Time: {}, Validation loss: {}, Mean ROC AUC: {}, Mean PRAUC: {}".format( epoch+1, args.num_epochs, 
                                                                                timenow, epoch_loss, avg_ROC_auc,avg_PR_auc),
           file=Logger)
    if epoch_loss <= best_loss:
        torch.save(model.state_dict(), args.model_file)
        print( "Delta loss: {}, Model saved at {}".format((epoch_loss-best_loss),args.model_file) , file=Logger)
        best_loss = epoch_loss

if args.stop_instance:
    Logger.close()
    subprocess.call(['sudo','halt'])
Example #4
0
    model.eval()
    losses  = []
    y_score = []
    y_test  = []
    #val_loader.init_epoch()
    for inputs, targets in val_loader:
        inputs = to_one_hot(inputs, n_dims=4).permute(0,3,1,2).squeeze().float()
        targets = targets.float()
        inp_batch = Variable( inputs ).cuda()
        trg_batch = Variable(targets).cuda()        
        outputs = model(inp_batch)
        loss = criterion(outputs.view(-1), trg_batch.view(-1))
        losses.append(loss.item())
        y_score.append( outputs.cpu().data.numpy() )
        y_test.append(  targets.cpu().data.numpy() )
    epoch_loss = sum(losses)/len(val)
    avg_ROC_auc = calc_auc(model, np.row_stack(y_test)[:,v_idx], np.row_stack(y_score)[:,v_idx],"ROC")
    avg_PR_auc = calc_auc(model, np.row_stack(y_test)[:,v_idx], np.row_stack(y_score)[:,v_idx],"PR")
    timenow = timeSince(start)
    print( "Epoch [{}/{}], Time: {}, Validation loss: {}, Mean ROC AUC: {}, Mean PRAUC: {}".format( epoch+1, args.num_epochs, 
                                                                                timenow, epoch_loss, avg_ROC_auc, avg_PR_auc),
           file=Logger)
    if epoch_loss <= best_loss:
        torch.save(model.state_dict(), args.model_file)
        print( "Delta loss: {}, Model saved at {}".format((epoch_loss-best_loss),args.model_file) , file=Logger)
        best_loss = epoch_loss

if args.stop_instance:
    Logger.close()
    subprocess.call(['sudo','halt'])