Example #1
0
    criterion = torch.nn.CrossEntropyLoss()
    alpha = 1.0
    epochs = 100

    for epoch in range(epochs):
        total_loss = 0
        for x, y in trainloader:
            optimizer.zero_grad()
            output, embeddings = model(x.to(device))
            loss, _ = margin_hard_loss(embeddings, y.to(device), alpha)
            loss.backward()
            optimizer.step()
            total_loss += loss.item()
        print('Epoch {} \t Loss: {:.3f}'.format(epoch, total_loss))

    train_X, train_Y = get_embeddings(model, trainloader, device)
    test_X, test_Y = get_embeddings(model, testloader, device)

    reduced_umap = umap.UMAP().fit_transform(test_X.numpy())
    reduced_tsne = TSNE(n_components=2).fit_transform(test_X.numpy())

    f, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 10))
    ax1.scatter(reduced_umap[:, 0], reduced_umap[:, 1], c=test_Y)
    ax2.scatter(reduced_tsne[:, 0], reduced_tsne[:, 1], c=test_Y)
    plt.tight_layout()
    plt.savefig(results_path+'scatter.pdf', format='pdf')
    plt.close()

    pred_class = euclidean_knn(test_X, train_X, train_Y)
    mAP = average_precision(test_Y.reshape(-1, 1), pred_class)
Example #2
0
                       len(trainset) - int(len(trainset) * 0.9)))
        trainloader = DataLoader(trainset,
                                 batch_size=256,
                                 shuffle=True,
                                 num_workers=0)
        validationloader = DataLoader(validationset,
                                      batch_size=256,
                                      shuffle=True,
                                      num_workers=0)

        model = give_me_resnet(pretrained=True)
        model = model.to(device)
        optimizer = torch.optim.Adam(model.parameters())
        for alpha in alpha_list:
            for epoch in range(epochs):
                total_loss = 0
                for x, y in trainloader:
                    optimizer.zero_grad()
                    output, embeddings = model(x.to(device))
                    loss = margin_loss(embeddings, y.to(device), alpha)
                    loss.backward()
                    optimizer.step()
                    total_loss += loss.item()
                print('Epoch {} \t Loss: {}'.format(epoch, total_loss))
            train_X, train_Y = get_embeddings(model, trainloader, device)
            valid_X, valid_Y = get_embeddings(model, validationloader, device)

            pred_class = euclidean_knn(valid_X, train_X, train_Y)
            mAP = average_precision(valid_Y.reshape(-1, 1), pred_class)
            results[str(alpha)].append(mAP.item())