コード例 #1
0
    y_pred = []
    y_true = []

    with torch.set_grad_enabled(False):
        for local_batch, local_labels in validation_generator:
            y_true.extend(local_labels.numpy().tolist())
            local_batch, local_labels = local_batch.to(
                device), local_labels.to(device)
            output = model.forward(local_batch)
            val_loss = criterion(output, local_labels)
            output = model.softmax(output)
            output = torch.max(output, 1)[1]
            val_acc = pred_acc(local_labels.cpu(), output.cpu())
            writer.add_scalar('Validation/Accuracy', val_acc, global_val_step)
            writer.add_scalar('Validation/Loss', val_loss, global_val_step)
            global_val_step += params['batch_size']
            y_pred.extend(output.cpu().numpy().tolist())

    tqdm.write(
        classification_report(y_true=y_true,
                              y_pred=y_pred,
                              target_names=["No Findings", "Pneumonia"]))

    torch.save(
        model.state_dict(),
        os.path.join(
            model_dir,
            '{0}-epoch-{1}.pt'.format(today.strftime("%b%d_%H-%M-%S"), epoch)))
    if (use_cuda):
        model.cuda()