Example #1
0
def train(epoch, net, optimizer):
    print("\nEpoch: %d" % epoch)
    net.train()
    train_loss = 0
    correct = 0
    total = 0
    # optimizer = lr_scheduler(optimizer, epoch, init_lr=0.002, decay_epoch=start_epoch)
    for batch_idx, (inputs, targets) in enumerate(train_loader):
        if USE_GPU:
            inputs, targets = inputs.cuda(), targets.cuda()
        inputs, targets = Variable(inputs), Variable(targets)
        optimizer.zero_grad()

        out = net(inputs)
        loss = criterion(out, targets)
        loss.backward()
        optimizer.step()

        train_loss += loss.data[0]
        _, predicted = torch.max(out.data, 1)
        total += targets.size(0)
        correct += predicted.eq(targets.data).cpu().sum()

        progress_bar(
            batch_idx, len(train_loader), "Loss: %.3f | Acc: %.3f%% (%d/%d)" %
            (train_loss /
             (batch_idx + 1), 100. * correct / total, correct, total))

    log.write(str(epoch) + ' ' + str(correct / total) + ' ')
Example #2
0
def train(epoch, optimizer):
    print("\nEpoch: %d" % epoch)
    net.train()
    train_loss = 0
    correct = 0
    total = 0
    optimizer = lr_scheduler(optimizer, epoch, init_lr=0.0008, decay_epoch=start_epoch)
    for batch_idx, (inputs, targets) in enumerate(train_loader):
        if USE_GPU:
            inputs, targets = inputs.cuda(), targets.cuda()
        optimizer.zero_grad()
        inputs, targets = Variable(inputs), Variable(targets, requires_grad=False)
        out1, out2, tar, emb_w = net(inputs, targets, epoch, batch_idx)

        loss = comp_loss(out1, out2, tar, emb_w, targets, args.mode)
        loss.backward()
        optimizer.step()

        train_loss += loss.data[0]
        _, predicted = torch.max(out1.data, 1)
        total += targets.size(0)
        correct += predicted.eq(targets.data).cpu().sum()

        progress_bar(batch_idx, len(train_loader), "Loss: %.3f | Acc: %.3f%% (%d/%d)"
                     % (train_loss / (batch_idx + 1), 100. * correct / total, correct, total))

    log.write(str(epoch) + ' ' + str(correct / total) + ' ')
    if args.mode != "baseline":
        pickle.dump(emb_w.data.cpu().numpy(),
                    open("./log/embed/" + MODEL_NAME + '_' + args.mode + "embedding.pkl", "wb"))
Example #3
0
def test(epoch):
    global best_acc
    net.eval()
    test_loss, correct, total, loss = 0, 0, 0, 0
    for batch_idx, (inputs, targets) in enumerate(test_loader):
        if USE_GPU:
            inputs, targets = inputs.cuda(), targets.cuda()
        inputs, targets = Variable(inputs, volatile=True), Variable(targets)
        out, _, _, _ = net(inputs, targets, -1, batch_idx)
        loss = F.cross_entropy(out, targets)

        test_loss = loss.data[0]
        _, predicted = torch.max(out.data, 1)
        total += targets.size(0)
        correct += predicted.eq(targets.data).cpu().sum()

        acc = 100. * correct / total
        progress_bar(batch_idx, len(test_loader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
                     % (test_loss / (batch_idx + 1), acc, correct, total))
    log.write(str(correct / total) + ' ' + str(test_loss) + '\n')
    log.flush()

    acc = 100. * correct / total
    if epoch > 15 and acc > best_acc:
        print("Saving checkpoint")
        state = {
            'net': net,
            'acc': acc,
            'epoch': epoch,
            'optimizer': optimizer
        }
        if not os.path.isdir("checkpoints"):
            os.mkdir('checkpoints')
        torch.save(state, "./checkpoints/" + args.mode + "_" + MODEL_SAVE_FILE)
        best_acc = acc
Example #4
0
def train(epoch):
    print("\nEpoch: %d" % epoch)
    net.train()
    train_loss, correct, total, loss = 0, 0, 0, 0
    for batch_idx, (batch_x, target) in enumerate(train_loader):
        batch_x = augmentation(batch_x)
        target = torch.LongTensor(target)
        labels = torch.sparse.torch.eye(NUM_CLASSES).index_select(dim=0,
                                                                  index=target)
        if args.cuda:
            batch_x, labels = batch_x.cuda(), labels.cuda()
        batch_x, labels = Variable(batch_x), Variable(labels)
        optimizer.zero_grad()
        classes, reconstructions = net(batch_x, labels)
        loss = capsuleLoss(batch_x, labels, classes, reconstructions)
        loss.backward()
        optimizer.step()

        train_loss += loss.data[0]
        total += target.size(0)
        pred = classes.data.max(1, keepdim=True)[1].cpu()
        correct += pred.eq(target.view_as(pred)).sum()
        progress_bar(
            batch_idx, len(train_loader), "Loss: %.3f | Acc: %.3f%% (%d/%d)" %
            (train_loss /
             (batch_idx + 1), 100. * correct / total, correct, total))
Example #5
0
def train(epoch, net, optimizer):
    print("\nEpoch: %d" % epoch)
    net.train()
    train_loss = 0
    correct = 0
    total = 0
    # optimizer = lr_scheduler(optimizer, epoch, init_lr=0.002, decay_epoch=start_epoch)
    for batch_idx, (inputs, targets) in enumerate(train_loader):
        y_emb = y_encoding(targets)
        if USE_GPU:
            inputs, y_emb = inputs.cuda(), y_emb.cuda()
        inputs, y_emb = Variable(inputs, requires_grad=True), Variable(y_emb, requires_grad=True)
        optimizer.zero_grad()

        v_x, v_y = net(inputs, y_emb)
        loss = criterion(v_x, v_y)
        loss.backward()
        optimizer.step()

        train_loss += loss.data[0]
        # y_pred = predict_y(v_x, v_y)
        total += targets.size(0)
        # correct += y_pred.squeeze().cpu().eq(targets.data).sum()

        progress_bar(batch_idx, len(train_loader), "Loss: %.3f "% (train_loss / (batch_idx + 1)))

    log.write(str(epoch) + ' ' + str(correct / total) + ' ')
Example #6
0
def train(epoch):
    print("\nEpoch: %d" % epoch)
    net.train()
    train_loss = 0
    correct = 0
    total = 0
    for batch_idx, (inputs, targets) in enumerate(train_loader):
        if USE_GPU:
            inputs, targets = inputs.cuda(), targets.cuda()
        optimizer.zero_grad()
        inputs, targets = Variable(inputs), Variable(targets,
                                                     requires_grad=False)
        out, theta, _ = net(inputs)

        if batch_idx % 60 == 0:
            print(theta.cpu()[0])
        mode = "baseline"
        loss = comp_loss(out, targets, mode)
        loss.backward()
        optimizer.step()

        train_loss += loss.data[0]
        _, predicted = torch.max(out.data, 1)
        total += targets.size(0)
        correct += predicted.eq(targets.data).cpu().sum()

        progress_bar(
            batch_idx, len(train_loader), "Loss: %.3f | Acc: %.3f%% (%d/%d)" %
            (train_loss /
             (batch_idx + 1), 100. * correct / total, correct, total))

    log.write(str(epoch) + ' ' + str(correct / total) + ' ')
Example #7
0
def test(epoch):
    global best_acc
    net.eval()
    test_loss, correct, total, loss = 0, 0, 0, 0
    for batch_idx, (inputs, targets) in enumerate(test_loader):
        if USE_GPU:
            inputs, targets = inputs.cuda(), targets.cuda()
        inputs, targets = Variable(inputs, volatile=True), Variable(targets)
        out, theta, x1 = net(inputs)
        # img = data_loader.un_normalize(x1.data[0].cpu())
        # to_pil = torchvision.transforms.ToPILImage()
        # img = to_pil(img)
        # img.save("imgs/grid/batch_%d.jpg" % batch_idx)
        loss = F.cross_entropy(out, targets)

        test_loss += loss.data[0]
        _, predicted = torch.max(out.data, 1)
        total += targets.size(0)
        correct += predicted.eq(targets.data).cpu().sum()

        acc = 100. * correct / total
        progress_bar(
            batch_idx, len(test_loader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)' %
            (test_loss / (batch_idx + 1), acc, correct, total))
    log.write(str(correct / total) + ' ' + str(test_loss) + '\n')
    log.write(str(theta.cpu()[0]) + '\n')
    log.flush()
    img = data_loader.un_normalize(x1.data.cpu()[0])
    to_pil = torchvision.transforms.ToPILImage()
    img = to_pil(img)
    img.save("imgs/grid/test_%d.jpg" % epoch)

    acc = 100. * correct / total
    if epoch > 30 and acc > best_acc:
        print("Saving checkpoint")
        state = {
            'net': net,
            'acc': acc,
            'epoch': epoch,
        }
        if not os.path.isdir("checkpoints"):
            os.mkdir('checkpoints')
        torch.save(state, "./checkpoints/" + MODEL_SAVE_FILE)
        best_acc = acc
Example #8
0
def test(epoch, net):
    global best_acc
    net.eval()
    test_loss, correct, total, loss = 0, 0, 0, 0
    y_all = torch.arange(0, NUM_CLASSES, out=torch.LongTensor())
    y_code = y_encoding(y_all)
    test_x = torch.zeros(200, 3, 224, 224)
    if USE_GPU:
        test_x, y_code = test_x.cuda(), y_code.cuda()
    test_x, y_code = Variable(test_x, volatile=True), Variable(y_code, volatile=True)
    _, v_y_all = net(test_x, y_code)
    for batch_idx, (inputs, targets) in enumerate(test_loader):
        y_emb = y_encoding(targets)
        if USE_GPU:
            inputs, y_emb = inputs.cuda(), y_emb.cuda()
        inputs, y_emb = Variable(inputs, volatile=True), Variable(y_emb, volatile=True)
        v_x, v_y = net(inputs, y_emb)
        loss = criterion(v_x, v_y)
        test_loss = loss.data[0]

        y_pred = predict_y(v_x, v_y_all)
        total += targets.size(0)
        correct += y_pred.data.cpu().eq(targets).sum()

        acc = 100. * correct / total
        progress_bar(batch_idx, len(test_loader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
                     % (test_loss / (batch_idx + 1), acc, correct, total))
    log.write(str(correct / total) + ' ' + str(test_loss) + '\n')
    log.flush()

    acc = 100. * correct / total
    if epoch > 9 and acc > best_acc:
        print("Saving checkpoint")
        state = {
            'net': net,
            'acc': acc,
            'epoch': epoch,
            'optimizer': optimizer
        }
        if not os.path.isdir("checkpoints"):
            os.mkdir('checkpoints')
        torch.save(state, "./checkpoints/" + MODEL_SAVE_FILE)
        best_acc = acc
Example #9
0
def test(epoch):
    global best_acc
    net.eval()
    test_loss, correct, total, loss = 0, 0, 0, 0
    for batch_idx, (batch_x, target) in enumerate(test_loader):
        target = torch.LongTensor(target)
        labels = torch.sparse.torch.eye(NUM_CLASSES).index_select(dim=0,
                                                                  index=target)
        if args.cuda:
            batch_x, labels = batch_x.cuda(), labels.cuda()
        batch_x, labels = Variable(batch_x, volatile=True), Variable(labels)
        classes, reconstructions = net(batch_x)
        loss = capsuleLoss(batch_x, labels, classes, reconstructions)
        test_loss += loss.data[0]
        total += target.size(0)
        pred = classes.data.max(1, keepdim=True)[1].cpu()
        correct += pred.eq(target.view_as(pred)).sum()
        progress_bar(
            batch_idx, len(test_loader), "Loss: %.3f | Acc: %.3f%% (%d/%d)" %
            (test_loss /
             (batch_idx + 1), 100. * correct / total, correct, total))

    log.write("Test Epoch %d: acc: %.3f | loss: %.3f\n" %
              (epoch, correct / total, test_loss))
    log.flush()

    acc = 100. * correct / len(test_loader.dataset)
    if epoch > 0 and acc > best_acc:
        print("Saving checkpoint")
        state = {
            'net': net,
            'acc': acc,
            'epoch': epoch,
        }
        if not os.path.isdir("checkpoints"):
            os.mkdir('checkpoints')
        torch.save(state, "./checkpoints/" + MODEL_SAVE_FILE)
        best_acc = acc
Example #10
0
def train(epoch):
    print("\nEpoch: %d" % epoch)
    net.train()
    train_loss, correct, total, loss = 0, 0, 0, 0
    for batch_idx, (batch_x, target) in enumerate(train_loader):
        if args.cuda:
            batch_x, target = batch_x.cuda(), target.cuda()
        batch_x, target = Variable(batch_x), Variable(target,
                                                      requires_grad=False)
        optimizer.zero_grad()
        output = net(batch_x)
        loss = criterion(output, target)
        loss.backward()
        optimizer.step()

        train_loss += loss.data[0]
        _, pred = torch.max(output.data, 1)
        total += target.size(0)
        correct += pred.eq(target.data).cpu().sum()
        progress_bar(
            batch_idx, len(train_loader), "Loss: %.3f | Acc: %.3f%% (%d/%d)" %
            (train_loss /
             (batch_idx + 1), 100. * correct / total, correct, total))
Example #11
0
def test(epoch):
    global best_acc
    net.eval()
    test_loss, correct, total, loss = 0, 0, 0, 0
    for batch_idx, (inputs, targets) in enumerate(train_loader):
        if use_cuda:
            inputs, targets = inputs.cuda(), targets.cuda()
        inputs, targets = Variable(inputs, volatile=True), Variable(
            targets, requires_grad=False)
        out, _, _, _ = net(inputs, targets, -1, batch_idx)
        loss = F.cross_entropy(out, targets)

        test_loss = loss.data[0]
        _, predicted = torch.max(out.data, 1)
        total += targets.size(0)
        correct += predicted.eq(targets.data).cpu().sum()

        acc = 100. * correct / total
        progress_bar(
            batch_idx, len(test_loader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)' %
            (test_loss / (batch_idx + 1), acc, correct, total))
    log.write(str(correct / total) + ' ' + str(test_loss) + '\n')
    log.flush()

    acc = 100. * correct / total
    if epoch > 30 and acc > best_acc:
        print("Saving checkpoint")
        state = {
            'net': net.module if use_cuda else net,
            'acc': acc,
            'epoch': epoch,
        }
        if not os.path.isdir("checkpoint"):
            os.mkdir('checkpoint')
        torch.save(state, "./checkpoint/cifar10_resnet8_" + args.mode + ".pt")
        best_acc = acc
Example #12
0
def test(epoch):
    global best_acc
    net.eval()
    test_loss, correct, total, loss = 0, 0, 0, 0
    for batch_idx, (batch_x, target) in enumerate(test_loader):
        if args.cuda:
            batch_x, target = batch_x.cuda(), target.cuda()
        batch_x, target = Variable(batch_x, volatile=True), Variable(
            target, volatile=True, requires_grad=False)
        output = net(batch_x)
        loss = criterion(output, target)
        test_loss += loss.data[0]
        _, pred = torch.max(output.data, 1)
        total += target.size(0)
        correct += pred.eq(target.data).cpu().sum()
        progress_bar(
            batch_idx, len(test_loader), "Loss: %.3f | Acc: %.3f%% (%d/%d)" %
            (test_loss /
             (batch_idx + 1), 100. * correct / total, correct, total))

    log.write("Test Epoch %d: acc: %.3f | loss: %.3f\n" %
              (epoch, correct / total, test_loss))
    log.flush()

    acc = 100. * correct / len(test_loader.dataset)
    if (epoch > 5 or acc > 50.) and acc > best_acc:
        print("Saving checkpoint")
        state = {
            'net': net,
            'acc': acc,
            'epoch': epoch,
        }
        if not os.path.isdir("checkpoints"):
            os.mkdir('checkpoints')
        torch.save(state, "./checkpoints/" + MODEL_SAVE_FILE)
        best_acc = acc
Example #13
0
def gzsl_test0(epoch, net, optimizer, log, gamma=2.):
    NUM_CLASSES = 50  # set the number of classes in your dataset
    num_seen_classes = 40
    NUM_ATTR = 85
    DATA_DIR = "/home/elvis/data/attribute/AwA/Animals_with_Attributes2/zsl/gzsl_test"
    BATCH_SIZE = 32
    IMAGE_SIZE = 224
    best_h = 55
    USE_GPU = torch.cuda.is_available()
    data_loader = DataLoader(data_dir=DATA_DIR,
                             image_size=IMAGE_SIZE,
                             batch_size=BATCH_SIZE)
    # train_loader = data_loader.load_data(data_set='train')
    test_loader = data_loader.load_data(data_set='val')
    criterion = nn.CrossEntropyLoss()

    net.eval()
    test_loss, correct_seen, correct_unseen, total_seen, total_unseen, total, loss = 0, 0, 0, 0, 0, 0, 0
    for batch_idx, (inputs, targets) in enumerate(test_loader):
        if USE_GPU:
            inputs, targets = inputs.cuda(), targets.cuda()
        inputs, targets = Variable(inputs, volatile=True), Variable(targets)
        out, attr = net(inputs)
        loss = criterion(out, targets)

        test_loss = loss.data[0]
        logit = out.data
        seen_prob, seen_class = torch.max(logit[:, :num_seen_classes], 1)
        unseen_prob, unseen_class = torch.max(logit[:, num_seen_classes:], 1)
        predicted = seen_class
        for i, spi in enumerate(seen_prob):
            if seen_prob[i] < unseen_prob[i] * gamma:
                predicted[i] = unseen_class[i] + num_seen_classes

        total += targets.size(0)
        correct_list = predicted.eq(targets.data).cpu()
        target_list = targets.data.cpu()
        for i, targeti in enumerate(target_list):
            if targeti < num_seen_classes:
                correct_seen += correct_list[i]
                total_seen += 1
            else:
                correct_unseen += correct_list[i]
                total_unseen += 1

        acc_seen = 100. * correct_seen / total_seen
        if total_unseen > 0:
            acc_unseen = 100. * correct_unseen / total_unseen
        else:
            acc_unseen = 0.
        progress_bar(
            batch_idx, len(test_loader),
            'Loss: %.3f | acc_seen: %.3f%% (%d/%d) | acc_unseen: %.3f%% (%d/%d)'
            % (test_loss / (batch_idx + 1), acc_seen, correct_seen, total_seen,
               acc_unseen, correct_unseen, total_unseen))
    acc_seen = 100. * correct_seen / total_seen
    acc_unseen = 100. * correct_unseen / total_unseen
    h = 2. / (1. / acc_seen + 1. / acc_unseen)
    print("acc_seen: %.3f%% (%d/%d) | acc_unseen: %.3f%% (%d/%d) | H: %.3f%%" %
          (acc_seen, correct_seen, total_seen, acc_unseen, correct_unseen,
           total_unseen, h))
    log.write(str(acc_seen) + ' ' + str(acc_unseen) + ' ' + str(h) + " ")
    if h > best_h:
        MODEL_SAVE_FILE = "gzsl_awa2_epoch%dacc%d.pth" % (epoch, int(h))
        print(MODEL_SAVE_FILE)
        state = {'net': net, 'acc': h, 'epoch': epoch, 'optimizer': optimizer}
        torch.save(state, "./checkpoints/" + MODEL_SAVE_FILE)
Example #14
0
def gzsl_test(epoch, net, optimizer):
    NUM_CLASSES = 50  # set the number of classes in your dataset
    num_seen_classes = 40
    NUM_ATTR = 85
    DATA_DIR = "/home/elvis/data/attribute/AwA/Animals_with_Attributes2/zsl/gzsl_test"
    BATCH_SIZE = 32
    IMAGE_SIZE = 224
    best_h = 40
    USE_GPU = torch.cuda.is_available()
    # order_awa2_attr = np.load("data/order_awa2_attr.npy")
    # w_attr_sum = np.sum(w_attr, 0)
    # w_attr = w_attr/w_attr_sum
    # w_attr[:, 0].sum()
    # order_awa2_attr = torch.FloatTensor(order_awa2_attr / 100.).cuda()  # 50 * 312

    data_loader = DataLoader(data_dir=DATA_DIR,
                             image_size=IMAGE_SIZE,
                             batch_size=BATCH_SIZE)
    # train_loader = data_loader.load_data(data_set='train')
    test_loader = data_loader.load_data(data_set='val')
    criterion = nn.CrossEntropyLoss()

    net.eval()
    test_loss, correct_seen, correct_unseen, total_seen, total_unseen, total, loss = 0, 0, 0, 0, 0, 0, 0
    for batch_idx, (inputs, targets) in enumerate(test_loader):
        if USE_GPU:
            inputs, targets = inputs.cuda(), targets.cuda()
        inputs, targets = Variable(inputs, volatile=True), Variable(targets)
        out, attr = net(inputs)
        loss = criterion(out, targets)

        test_loss = loss.data[0]
        _, predicted = torch.max(out.data, 1)
        total += targets.size(0)
        correct_list = predicted.eq(targets.data).cpu()
        target_list = targets.data.cpu()
        for i, targeti in enumerate(target_list):
            if targeti < 40:
                correct_seen += correct_list[i]
                total_seen += 1
            else:
                correct_unseen += correct_list[i]
                total_unseen += 1

        acc_seen = 100. * correct_seen / total_seen
        if total_unseen > 0:
            acc_unseen = 100. * correct_unseen / total_unseen
        else:
            acc_unseen = 0.
        progress_bar(
            batch_idx, len(test_loader),
            'Loss: %.3f | acc_seen: %.3f%% (%d/%d) | acc_unseen: %.3f%% (%d/%d)'
            % (test_loss / (batch_idx + 1), acc_seen, correct_seen, total_seen,
               acc_unseen, correct_unseen, total_unseen))
    acc_seen = 100. * correct_seen / total_seen
    acc_unseen = 100. * correct_unseen / total_unseen
    h = 2. / (1. / acc_seen + 1. / acc_unseen)
    print("acc_seen: %.3f%% (%d/%d) | acc_unseen: %.3f%% (%d/%d) | H: %.3f%%" %
          (acc_seen, correct_seen, total_seen, acc_unseen, correct_unseen,
           total_unseen, h))
    if h > best_h:
        MODEL_SAVE_FILE = "gzsl_awa2_epoch%dacc%d.pth" % (epoch, int(h))
        print(MODEL_SAVE_FILE)
        state = {'net': net, 'acc': h, 'epoch': epoch, 'optimizer': optimizer}
        torch.save(state, "./checkpoints/" + MODEL_SAVE_FILE)
Example #15
0
def zsl_test(epoch, net, optimizer, log):
    NUM_CLASSES = 50  # set the number of classes in your dataset
    NUM_SEEN = 40
    NUM_UNSEEN = NUM_CLASSES - NUM_SEEN
    NUM_ATTR = 85
    DATA_DIR = "/home/elvis/data/attribute/AwA/Animals_with_Attributes2/zsl/zsl_test"
    BATCH_SIZE = 32
    IMAGE_SIZE = 224
    best_acc = 74
    USE_GPU = torch.cuda.is_available()
    order_awa2_attr = np.load("data/order_awa2_attr.npy")
    # w_attr_sum = np.sum(w_attr, 0)
    # w_attr = w_attr/w_attr_sum
    # w_attr[:, 0].sum()
    order_awa2_attr = order_awa2_attr[NUM_SEEN:, :]
    order_awa2_attr = torch.FloatTensor(order_awa2_attr /
                                        100.).cuda()  # 50 * 312
    net.fc2 = nn.Linear(NUM_ATTR, NUM_CLASSES, bias=False)
    net.fc2.weight = nn.Parameter(order_awa2_attr, requires_grad=False)
    # print(torch_summarize(net))
    # print(net)
    net.cuda()
    data_loader = DataLoader(data_dir=DATA_DIR,
                             image_size=IMAGE_SIZE,
                             batch_size=BATCH_SIZE)
    train_loader = data_loader.load_data(data_set='train')
    test_loader = data_loader.load_data(data_set='val')
    criterion = nn.CrossEntropyLoss()

    net.eval()
    test_loss, correct, total, loss = 0, 0, 0, 0
    correct_bin = np.zeros(NUM_UNSEEN)
    total_bin = np.zeros(NUM_UNSEEN)
    for batch_idx, (inputs, targets) in enumerate(test_loader):
        if USE_GPU:
            inputs, targets = inputs.cuda(), targets.cuda()
        inputs, targets = Variable(inputs, volatile=True), Variable(targets)
        out, attr = net(inputs)
        loss = criterion(out, targets)

        test_loss = loss.data[0]
        _, predicted = torch.max(out.data, 1)
        total += targets.size(0)
        correct += predicted.eq(targets.data).cpu().sum()

        correct_list = predicted.eq(targets.data).cpu()
        target_list = targets.data.cpu()
        for i, targeti in enumerate(target_list):
            correct_bin[targeti] += correct_list[i]
            total_bin[targeti] += 1.

        acc = 100. * correct / total
        progress_bar(
            batch_idx, len(test_loader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)' %
            (test_loss / (batch_idx + 1), acc, correct, total))

    acc = 100. * correct / total
    acc_bin = 100. * correct_bin / total_bin
    np.save("data/sun_acc_bin.npy", acc_bin)
    print("ZSL acc_per_class: %.3f%%(%d/%d)" %
          (np.mean(acc_bin), correct_bin[0], total_bin[0]))
    log.write(str(np.mean(acc_bin)) + ' ')
    if acc > best_acc:
        MODEL_SAVE_FILE = "zsl_resnet18_awa2_epoch%dacc%d.pth" % (epoch,
                                                                  int(acc))
        print(MODEL_SAVE_FILE)
        state = {
            'net': net,
            'acc': acc,
            'epoch': epoch,
            'optimizer': optimizer
        }
        torch.save(state, "./checkpoints/" + MODEL_SAVE_FILE)
Example #16
0
def gzsl_test(epoch, net, optimizer):
    NUM_CLASSES = 717  # set the number of classes in your dataset
    Num_SEEN = 645
    NUM_ATTR = 102
    DATA_DIR = "/home/elvis/data/attribute/SUN/zsl/gzsl_test"
    BATCH_SIZE = 32
    IMAGE_SIZE = 224
    best_h = 50
    USE_GPU = torch.cuda.is_available()
    order_sun_attr = np.load("data/order_sun_attr.npy")
    # order_sun_attr[Num_SEEN:, :] = order_sun_attr[Num_SEEN:, :]
    # order_cub_attr = order_cub_attr[150:, :]
    order_sun_attr = torch.FloatTensor(order_sun_attr).cuda()  # 50 * 312
    net.fc2 = nn.Linear(NUM_ATTR, NUM_CLASSES, bias=False)
    net.fc2.weight = nn.Parameter(order_sun_attr, requires_grad=False)
    net.cuda()
    data_loader = DataLoader(data_dir=DATA_DIR,
                             image_size=IMAGE_SIZE,
                             batch_size=BATCH_SIZE)
    # train_loader = data_loader.load_data(data_set='train')
    test_loader = data_loader.load_data(data_set='val')
    criterion = nn.CrossEntropyLoss()

    net.eval()
    test_loss, correct_seen, correct_unseen, total_seen, total_unseen, total, loss = 0, 0, 0, 0, 0, 0, 0
    for batch_idx, (inputs, targets) in enumerate(test_loader):
        if USE_GPU:
            inputs, targets = inputs.cuda(), targets.cuda()
        inputs, targets = Variable(inputs, volatile=True), Variable(targets)
        out, attr = net(inputs)
        loss = criterion(out, targets)

        test_loss = loss.data[0]
        _, predicted = torch.max(out.data, 1)
        total += targets.size(0)
        correct_list = predicted.eq(targets.data).cpu()
        target_list = targets.data.cpu()
        for i, targeti in enumerate(target_list):
            if targeti < Num_SEEN:
                correct_seen += correct_list[i]
                total_seen += 1
            else:
                correct_unseen += correct_list[i]
                total_unseen += 1

        acc_seen = 100. * correct_seen / total_seen
        if total_unseen > 0:
            acc_unseen = 100. * correct_unseen / total_unseen
        else:
            acc_unseen = 0.
        progress_bar(
            batch_idx, len(test_loader),
            'Loss: %.3f | acc_seen: %.3f%% (%d/%d) | acc_unseen: %.3f%% (%d/%d)'
            % (test_loss / (batch_idx + 1), acc_seen, correct_seen, total_seen,
               acc_unseen, correct_unseen, total_unseen))
    acc_seen = 100. * correct_seen / total_seen
    acc_unseen = 100. * correct_unseen / total_unseen
    h = 2. / (1. / acc_seen + 1. / acc_unseen)
    print("acc_seen: %.3f%% (%d/%d) | acc_unseen: %.3f%% (%d/%d) | H: %.3f%%" %
          (acc_seen, correct_seen, total_seen, acc_unseen, correct_unseen,
           total_unseen, h))
    if h > best_h:
        MODEL_SAVE_FILE = "gzsl_resnet50_sun_epoch%dacc%d.pth" % (epoch,
                                                                  int(h))
        print(MODEL_SAVE_FILE)
        state = {'net': net, 'acc': h, 'epoch': epoch, 'optimizer': optimizer}
        torch.save(state, "./checkpoints/" + MODEL_SAVE_FILE)