def test(test_loader, valid_loader, model, epoch):
    model.eval()
    valid_pbar = tqdm(enumerate(valid_loader))
    softmax = nn.Softmax(dim=1)
    correct_a = 0.0
    correct_b = 0.0
    total_datasize = 0.0
    for (batch_idx, (data, label_a, label_b)) in valid_pbar:
        data = Variable(data.cuda())
        (out_a, _, out_b, _) = model(data)
        if (args.loss_type == 'asoft'):
            (predicted_labels_a, _) = out_a
        else:
            predicted_labels_a = out_a
        predicted_labels_b = out_b
        true_labels_a = Variable(label_a.cuda())
        true_labels_b = Variable(label_b.cuda())
        predicted_one_labels_a = softmax(predicted_labels_a)
        predicted_one_labels_a = torch.max(predicted_one_labels_a, dim=1)[1]
        batch_correct_a = (predicted_one_labels_a.cuda() == true_labels_a.cuda()).sum().item()
        minibatch_acc_a = float((batch_correct_a / len(predicted_one_labels_a)))
        correct_a += batch_correct_a
        predicted_one_labels_b = softmax(predicted_labels_b)
        predicted_one_labels_b = torch.max(predicted_one_labels_b, dim=1)[1]
        batch_correct_b = (predicted_one_labels_b.cuda() == true_labels_b.cuda()).sum().item()
        minibatch_acc_b = float((batch_correct_b / len(predicted_one_labels_b)))
        correct_b += batch_correct_b
        total_datasize += len(predicted_one_labels_a)
        if ((batch_idx % args.log_interval) == 0):
            valid_pbar.set_description('Valid Epoch: {:2d} [{:8d}/{:8d} ({:3.0f}%)] Batch Spk Accuracy: {:.4f}% Dom Accuracy: {:.4f}%'.format(epoch, (batch_idx * len(data)), len(valid_loader.dataset), ((100.0 * batch_idx) / len(valid_loader)), (100.0 * minibatch_acc_a), (100.0 * minibatch_acc_b)))
    spk_valid_accuracy = ((100.0 * correct_a) / total_datasize)
    dom_valid_accuracy = ((100.0 * correct_b) / total_datasize)
    writer.add_scalar('Test/Spk_Valid_Accuracy', spk_valid_accuracy, epoch)
    writer.add_scalar('Test/Dom_Valid_Accuracy', dom_valid_accuracy, epoch)
    torch.cuda.empty_cache()
    (labels, distances) = ([], [])
    pbar = tqdm(enumerate(test_loader))
    for (batch_idx, (data_a, data_p, label)) in pbar:
        vec_a_shape = data_a.shape
        vec_p_shape = data_p.shape
        data_a = data_a.reshape((vec_a_shape[0] * vec_a_shape[1]), 1, vec_a_shape[2], vec_a_shape[3])
        data_p = data_p.reshape((vec_p_shape[0] * vec_p_shape[1]), 1, vec_p_shape[2], vec_p_shape[3])
        if args.cuda:
            (data_a, data_p) = (data_a.cuda(), data_p.cuda())
        (data_a, data_p, label) = (Variable(data_a), Variable(data_p), Variable(label))
        (_, out_a_, _, _) = model(data_a)
        (_, out_p_, _, _) = model(data_p)
        out_a = out_a_.reshape(vec_a_shape[0], vec_a_shape[1], args.embedding_size_a).mean(dim=1)
        out_p = out_p_.reshape(vec_p_shape[0], vec_p_shape[1], args.embedding_size_a).mean(dim=1)
        dists = l2_dist.forward(out_a, out_p)
        dists = dists.data.cpu().numpy()
        distances.append(dists)
        labels.append(label.data.cpu().numpy())
        if ((batch_idx % args.log_interval) == 0):
            pbar.set_description('Test Epoch: {} [{}/{} ({:.0f}%)]'.format(epoch, (batch_idx * len(data_a)), len(test_loader.dataset), ((100.0 * batch_idx) / len(test_loader))))
    labels = np.array([sublabel for label in labels for sublabel in label])
    distances = np.array([subdist for dist in distances for subdist in dist])
    (eer, eer_threshold, accuracy) = evaluate_kaldi_eer(distances, labels, cos=args.cos_sim, re_thre=True)
    writer.add_scalar('Test/EER', (100.0 * eer), epoch)
    writer.add_scalar('Test/Threshold', eer_threshold, epoch)
    (mindcf_01, mindcf_001) = evaluate_kaldi_mindcf(distances, labels)
    writer.add_scalar('Test/mindcf-0.01', mindcf_01, epoch)
    writer.add_scalar('Test/mindcf-0.001', mindcf_001, epoch)
    dist_type = ('cos' if args.cos_sim else 'l2')
    print(('\nFor %s_distance, ' % dist_type))
    print('  \x1b[91mTest Spk ERR is {:.4f}%, Threshold is {}'.format((100.0 * eer), eer_threshold))
    print('  mindcf-0.01 {:.4f}, mindcf-0.001 {:.4f},'.format(mindcf_01, mindcf_001))
    print(('  Valid Spk Accuracy is %.4f %%, Dom Accuracy is %.4f %% .\x1b[0m' % (spk_valid_accuracy, dom_valid_accuracy)))
    torch.cuda.empty_cache()
def test(test_loader, valid_loader, model, epoch):
    model.eval()
    valid_pbar = tqdm(enumerate(valid_loader))
    softmax = nn.Softmax(dim=1)
    correct_a = 0.0
    correct_b = 0.0
    total_datasize = 0.0
    for (batch_idx, (data, label_a, label_b)) in valid_pbar:
        data = Variable(data.cuda())
        (out_a, _, out_b, _) = model(data)
        if (args.loss_type == 'asoft'):
            (predicted_labels_a, _) = out_a
        else:
            predicted_labels_a = out_a
        predicted_labels_b = out_b
        true_labels_a = Variable(label_a.cuda())
        true_labels_b = Variable(label_b.cuda())
        predicted_one_labels_a = softmax(predicted_labels_a)
        predicted_one_labels_a = torch.max(predicted_one_labels_a, dim=1)[1]
        batch_correct_a = (predicted_one_labels_a.cuda() == true_labels_a.cuda()).sum().item()
        minibatch_acc_a = float((batch_correct_a / len(predicted_one_labels_a)))
        correct_a += batch_correct_a
        predicted_one_labels_b = softmax(predicted_labels_b)
        predicted_one_labels_b = torch.max(predicted_one_labels_b, dim=1)[1]
        batch_correct_b = (predicted_one_labels_b.cuda() == true_labels_b.cuda()).sum().item()
        minibatch_acc_b = float((batch_correct_b / len(predicted_one_labels_b)))
        correct_b += batch_correct_b
        total_datasize += len(predicted_one_labels_a)
        if ((batch_idx % args.log_interval) == 0):
            valid_pbar.set_description('Valid Epoch: {:2d} [{:8d}/{:8d} ({:3.0f}%)] Batch Spk Accuracy: {:.4f}% Dom Accuracy: {:.4f}%'.format(epoch, (batch_idx * len(data)), len(valid_loader.dataset), ((100.0 * batch_idx) / len(valid_loader)), (100.0 * minibatch_acc_a), (100.0 * minibatch_acc_b)))
    spk_valid_accuracy = ((100.0 * correct_a) / total_datasize)
    dom_valid_accuracy = ((100.0 * correct_b) / total_datasize)
    writer.add_scalar('Test/Spk_Valid_Accuracy', spk_valid_accuracy, epoch)
    writer.add_scalar('Test/Dom_Valid_Accuracy', dom_valid_accuracy, epoch)
    torch.cuda.empty_cache()
    (labels, distances) = ([], [])
    pbar = tqdm(enumerate(test_loader))
    for (batch_idx, (data_a, data_p, label)) in pbar:
        vec_a_shape = data_a.shape
        vec_p_shape = data_p.shape
        data_a = data_a.reshape((vec_a_shape[0] * vec_a_shape[1]), 1, vec_a_shape[2], vec_a_shape[3])
        data_p = data_p.reshape((vec_p_shape[0] * vec_p_shape[1]), 1, vec_p_shape[2], vec_p_shape[3])
        if args.cuda:
            (data_a, data_p) = (data_a.cuda(), data_p.cuda())
        (data_a, data_p, label) = (Variable(data_a), Variable(data_p), Variable(label))
        (_, out_a_, _, _) = model(data_a)
        (_, out_p_, _, _) = model(data_p)
        out_a = out_a_.reshape(vec_a_shape[0], vec_a_shape[1], args.embedding_size_a).mean(dim=1)
        out_p = out_p_.reshape(vec_p_shape[0], vec_p_shape[1], args.embedding_size_a).mean(dim=1)
        dists = l2_dist.forward(out_a, out_p)
        dists = dists.data.cpu().numpy()
        distances.append(dists)
        labels.append(label.data.cpu().numpy())
        if ((batch_idx % args.log_interval) == 0):
            pbar.set_description('Test Epoch: {} [{}/{} ({:.0f}%)]'.format(epoch, (batch_idx * len(data_a)), len(test_loader.dataset), ((100.0 * batch_idx) / len(test_loader))))
    labels = np.array([sublabel for label in labels for sublabel in label])
    distances = np.array([subdist for dist in distances for subdist in dist])
    (eer, eer_threshold, accuracy) = evaluate_kaldi_eer(distances, labels, cos=args.cos_sim, re_thre=True)
    writer.add_scalar('Test/EER', (100.0 * eer), epoch)
    writer.add_scalar('Test/Threshold', eer_threshold, epoch)
    (mindcf_01, mindcf_001) = evaluate_kaldi_mindcf(distances, labels)
    writer.add_scalar('Test/mindcf-0.01', mindcf_01, epoch)
    writer.add_scalar('Test/mindcf-0.001', mindcf_001, epoch)
    dist_type = ('cos' if args.cos_sim else 'l2')
    print(('\nFor %s_distance, ' % dist_type))
    print('  \x1b[91mTest Spk ERR is {:.4f}%, Threshold is {}'.format((100.0 * eer), eer_threshold))
    print('  mindcf-0.01 {:.4f}, mindcf-0.001 {:.4f},'.format(mindcf_01, mindcf_001))
    print(('  Valid Spk Accuracy is %.4f %%, Dom Accuracy is %.4f %% .\x1b[0m' % (spk_valid_accuracy, dom_valid_accuracy)))
    torch.cuda.empty_cache()