def Eval_Smeasure(self):
     print('eval[SMeasure]:{} dataset with {} method.'.format(
         self.dataset, self.method))
     alpha, avg_q, img_num = 0.5, 0.0, 0.0
     with torch.no_grad():
         trans = transforms.Compose([transforms.ToTensor()])
         for pred, gt in self.loader:
             if self.cuda:
                 pred = trans(pred).cuda()
                 gt = trans(gt).cuda()
             else:
                 pred = trans(pred)
                 gt = trans(gt)
             y = gt.mean()
             if y == 0:
                 x = pred.mean()
                 Q = 1.0 - x
             elif y == 1:
                 x = pred.mean()
                 Q = x
             else:
                 Q = alpha * self._S_object(
                     pred, gt) + (1 - alpha) * self._S_region(pred, gt)
                 if Q.item() < 0:
                     Q = torch.FLoatTensor([0.0])
             img_num += 1.0
             avg_q += Q.item()
         avg_q /= img_num
         return avg_q
Exemple #2
0
def collate_fn(batch_data):
    src_seqs, trg_seqs = zip(*batch_data)

    def seq_len(sequences):
        lengths = [len(seq) for seq in sequences]
        return lengths

    return torch.FLoatTensor(src_seqs), seq_len(src_seqs), \
           torch.FloatTensor(trg_seqs), seq_len(trg_seqs)
Exemple #3
0
def generate(checkpoint_path, eval_data_dir, out_dir):
    """Generate waveforms from mel-spectrograms using WaveRNN
    """
    os.makedirs(out_dir, exist_ok=True)

    # Specify the device
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    # Instantiate the model
    model = WaveRNN(n_mels=cfg.num_mels,
                    hop_length=cfg.hop_length,
                    num_bits=cfg.num_bits,
                    audio_embedding_dim=cfg.audio_embedding_dim,
                    conditioning_rnn_size=cfg.conditioning_rnn_size,
                    rnn_size=cfg.rnn_size,
                    fc_size=cfg.fc_size)
    model = model.to(device)
    model.eval()

    checkpoint = torch.load(checkpoint_path,
                            map_location=lambda storage, loc: storage)
    model.load_state_dict(checkpoint["model"])
    model_step = checkpoint["step"]

    for filename in open(os.path.join(eval_data_dir, "eval.txt"), "r"):
        filename = filename.strip("\n")
        print("Generating {filename}")

        mel = np.load(os.path.join(eval_data_dir, "mel", filename + ".npy"))
        mel = torch.FLoatTensor(mel.T).unsqueeze(0).to(device)

        with torch.no_grad():
            wav_hat = model.generate(mel)

        out_path = os.path.join(out_dir,
                                f"model_step{model_step:09d}_{filename}.wav")

        sf.write(out_path, wav_hat, cfg.sampling_rate)
Exemple #4
0
def val(model, dataloader):
    """
    检验
    :param model:
    :param dataloader:
    :return:
    """
    model.eval()

    confusion_matrix = meter.ConfusionMeter(2)
    with t.no_grad():
        for ii, (data, label) in enumerate(dataloader):
            input = t.FLoatTensor(data)
            label = t.FloatTensor(label)
            if opt.use_gpu:
                input = input.cuda()
                label = label.cuda()
            score = model(input)
            confusion_matrix.add(score.data.squeeze(), label.long())

    model.train()
    cm_value = confusion_matrix.value()
    accuracy = 100. * (cm_value[0][0] + cm_value[1][1]) / cm_value.sum()
    return confusion_matrix, accuracy
Exemple #5
0
def dpsh(
    opt,
    train_dataloader,
    query_dataloader,
    database_dataloader,
):
    """DPSH_PyTorch algorithm
    
    Parameters
        opt: Parser
        配置

        train_dataloader: DataLoader
        训练数据

        query_data: DataLoader
        查询数据

        database_dataloader: DataLoader
        整个数据集数据

    Returns
        None
    """
    # 标签onehot处理
    if opt.dataset == 'cifar10':
        train_labels = torch.FloatTensor(
            encode_onehot(train_dataloader.dataset.targets)).to(opt.device)
    elif opt.dataset == 'nus-wide':
        train_labels = torch.FLoatTensor(train_dataloader.dataset.tags).to(
            opt.device)

    # 定义网络,optimizer,loss
    model = modelloader.load_model(opt.model, num_classes=opt.code_length)
    if opt.multi_gpu:
        model = torch.nn.DataParallel(model)
    model.to(opt.device)
    criterion = dlfh_loss.DLFHLoss(opt.eta)

    # 不知道为什么,加momentum无法收敛!!!
    # 不知道为什么,SGD不用zeros初始化U无法收敛!!!
    optimizer = optim.RMSprop(
        model.parameters(),
        lr=opt.lr,
        weight_decay=10**-5,
    )
    scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=50, gamma=0.1)

    # 初始化
    N = len(train_dataloader.dataset)
    B = torch.zeros(N, opt.code_length).to(opt.device)
    U = torch.zeros(N, opt.code_length).to(opt.device)
    logger.info('B\'s shape is {}'.format(B.shape))

    # 算法开始
    best_map = 0.0
    last_model = None
    for epoch in range(opt.epochs):
        scheduler.step()
        # CNN
        total_loss = 0.0
        model.train()
        for data, labels, index in tqdm(train_dataloader):
            data = data.to(opt.device)
            labels = labels.to(opt.device)

            optimizer.zero_grad()

            S = (labels @ train_labels.t() > 0).float()

            outputs = model(data)

            # tensor.data:输出结果不放入计算图中,也就是不参与梯度计算
            U[index, :] = outputs.data
            # tensor.clone():返回跟原数据相同大小类型的tensor
            B[index, :] = outputs.clone().sign()

            loss = criterion(S, outputs, U)
            # loss = criterion(outputs, labels)
            loss.backward()

            optimizer.step()

            total_loss += loss.item()

        if epoch % opt.evaluate_freq == opt.evaluate_freq - 1:
            meanAP = evaluate(model, query_dataloader, train_labels, B, opt)

            # 保存当前最好结果
            if best_map < meanAP:
                if last_model:
                    os.remove(os.path.join('result', last_model))
                best_map = meanAP
                last_model = 'model_{:.4f}.t'.format(best_map)
                torch.save(model, os.path.join('result', last_model))

            logger.info(
                'code_length: {}, epoch: {}, lr: {}, loss: {:.4f}, map: {:.4f}'
                .format(opt.code_length, epoch + 1, scheduler.get_lr(),
                        total_loss, meanAP))

    # 加载性能最好模型,对整个数据集产生hash code进行evaluate
    model = torch.load(os.path.join('result', last_model))
    database_code = generate_code(model, database_dataloader,
                                  opt).to(opt.device)
    if opt.dataset == 'cifar10':
        database_labels = torch.FloatTensor(
            encode_onehot(database_dataloader.dataset.targets)).to(opt.device)
    elif opt.dataset == 'nus-wide':
        database_labels = torch.FloatTensor(
            database_dataloader.dataset.tags).to(opt.device)
    final_map = evaluate(
        model,
        query_dataloader,
        database_labels,
        database_code,
        opt,
    )
    logger.info('code_length: {}, final_map: {:.4f}'.format(
        opt.code_length, final_map))