Exemplo n.º 1
0
def main():
    features = np.load('0_feat.npy')
    labels = np.load('0_label.npy')

    num_instances = 32
    batch_size = 128
    Batch = BatchGenerator(labels,
                           num_instances=num_instances,
                           batch_size=batch_size)
    batch = Batch.batch()

    inputs = Variable(torch.FloatTensor(features[batch, :])).cuda()
    targets = Variable(torch.LongTensor(labels[batch])).cuda()
    print(KmeanLoss(n_cluster=32)(inputs, targets))
Exemplo n.º 2
0
def main():
    features = np.load('0_feat.npy')
    labels = np.load('0_label.npy')

    centers, center_labels = cluster_(features, labels, n_clusters=3)
    centers = Variable(torch.FloatTensor(centers).cuda(), requires_grad=True)
    center_labels = Variable(torch.LongTensor(center_labels)).cuda()
    cluster_counter = np.zeros([100, 3])
    num_instances = 3
    batch_size = 120
    Batch = BatchGenerator(labels,
                           num_instances=num_instances,
                           batch_size=batch_size)
    batch = Batch.batch()

    # _mask = Variable(torch.ByteTensor(np.ones([num_class_dict[args.data], args.n_cluster])).cuda())
    _mask = Variable(torch.ByteTensor(np.ones([100, 3])).cuda())
    inputs = Variable(torch.FloatTensor(features[batch, :])).cuda()
    targets = Variable(torch.LongTensor(labels[batch])).cuda()
    # print(torch.mean(inputs))
    mca = MCALoss(alpha=16,
                  centers=centers,
                  center_labels=center_labels,
                  cluster_counter=cluster_counter)
    for i in range(2):
        # loss, accuracy, dist_ap, dist_an =
        # MCALoss(alpha=16, centers=centers, center_labels=center_labels)(inputs, targets)
        loss, accuracy, dist_ap, dist_an = \
            mca(inputs, targets, _mask)
        # print(loss.data[0])
        loss.backward()
        # print(centers.grad.data)
        centers.data -= centers.grad.data
        centers.grad.data.zero_()
        # print(centers.grad)

    print(cluster_counter)