Esempio n. 1
0
def save_confusion_matrix(epoch, path, model, args, dataset, test_loader):
    model.eval()
    test_loss = 0
    correct = 0
    cMatrix = confusionmeter.ConfusionMeter(dataset.classes, True)

    for data, target in test_loader:
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data, volatile=True), Variable(target)
        output = model(data)
        test_loss += F.nll_loss(
            output, target, size_average=False).data[0]  # sum up batch loss
        #test_loss += F.nll_loss(output, target, reduction='sum').data[0]  # sum up batch loss
        pred = output.data.max(
            1, keepdim=True)[1]  # get the index of the max log-probability
        correct += pred.eq(target.data.view_as(pred)).cpu().sum()
        if epoch > 0:
            cMatrix.add(pred, target.data.view_as(pred))

    test_loss /= len(test_loader.dataset)
    img = cMatrix.value()

    plt.imshow(img, cmap='plasma', interpolation='nearest')
    plt.colorbar()
    plt.savefig(path + str(epoch) + ".jpg")
    plt.gcf().clear()
    return 100. * correct / len(test_loader.dataset)
Esempio n. 2
0
    def get_confusion_matrix(self, model, loader, size):
        '''
        
        :param model: Trained model
        :param loader: Data iterator
        :param size: Size of confusion matrix (Equal to largest possible label predicted by the model)
        :return: 
        '''
        model.eval()
        test_loss = 0
        correct = 0
        # Get the confusion matrix object
        cMatrix = confusionmeter.ConfusionMeter(size, True)

        for data, y, target in loader:
            if self.cuda:
                data, target = data.cuda(), target.cuda()
                self.means = self.means.cuda()
            data, target = Variable(data, volatile=True), Variable(target)
            output = model(data, True).unsqueeze(1)
            result = (output.data - self.means.float())

            result = torch.norm(result, 2, 2)
            # NMC for classification
            _, pred = torch.min(result, 1)
            # Evaluate results
            correct += pred.eq(target.data.view_as(pred)).cpu().sum()
            # Add the results in appropriate places in the matrix.
            cMatrix.add(pred, target.data.view_as(pred))

        test_loss /= len(loader.dataset)
        # Get 2d numpy matrix to remove the dependency of other code on confusionmeter
        img = cMatrix.value()
        return img
Esempio n. 3
0
def get_confusion_matrix_nmc(path, model, loader, size, args, means, epoch):
    model.eval()
    test_loss = 0
    correct = 0
    # Get the confusion matrix object
    cMatrix = confusionmeter.ConfusionMeter(size, True)

    for data, target in loader:
        if args.cuda:
            data, target = data.cuda(), target.cuda()
            means = means.cuda()
        data, target = Variable(data, volatile=True), Variable(target)
        output = model(data, True).unsqueeze(1)
        result = (output.data - means.float())

        result = torch.norm(result, 2, 2)
        # NMC for classification
        _, pred = torch.min(result, 1)
        # Evaluate results
        correct += pred.eq(target.data.view_as(pred)).cpu().sum()
        # Add the results in appropriate places in the matrix.
        cMatrix.add(pred, target.data.view_as(pred))

    test_loss /= len(loader.dataset)
    img = cMatrix.value()
    import matplotlib.pyplot as plt

    plt.imshow(img, cmap='plasma', interpolation='nearest')
    #plt.colorbar()
    plt.savefig(path + str(epoch) + ".jpg")
    plt.savefig(path + str(epoch) + ".eps", format='eps')
    plt.gcf().clear()
    return 100. * correct / len(loader.dataset)
    def get_confusion_matrix(self, model, loader, size, scale=None, older_classes=None, step_size=10, descriptor=False):
        '''
        :return: Returns the confusion matrix on the data given by loader
        '''
        model.eval()
        test_loss = 0
        correct = 0
        # Initialize confusion matrix
        cMatrix = confusionmeter.ConfusionMeter(size, True)

        # Checks is threshold moving should be used
        if scale is not None:
            scale = np.copy(scale)
            scale = scale / np.max(scale)
            scale = 1 / scale
            scale = torch.from_numpy(scale).unsqueeze(0)

            if self.cuda:
                scale = scale.cuda()

        # Iterate over the data and stores the results in the confusion matrix
        for data, y, target in loader:

            if self.cuda:
                data, target = data.cuda(), target.cuda()
            data, target = Variable(data), Variable(target)

            if scale is not None:
                output = model(data, scale=Variable(scale.float()))
            else:
                output = model(data)

            if descriptor:
                # To compare with FB paper
                outputTemp = output.data.cpu().numpy()
                targetTemp = target.data.cpu().numpy()

                for a in range(0, len(targetTemp)):
                    outputTemp[a, int(float(targetTemp[a]) / step_size) * step_size:(int(
                        float(targetTemp[a]) / step_size) * step_size) + step_size] += 20

                output = torch.from_numpy(outputTemp)
                if self.cuda:
                    output = output.cuda()
                output = Variable(output)

            test_loss += F.nll_loss(output, target, reduction='sum').data.item()  # sum up batch loss
            # test_loss += F.nll_loss(output, target, size_average=False).data[0]  # sum up batch loss
            pred = output.data.max(1, keepdim=True)[1]  # get the index of the m
            # ax log-probability
            correct += pred.eq(target.data.view_as(pred)).cpu().sum()
            cMatrix.add(pred.squeeze(), target.data.view_as(pred).squeeze())

        # Returns normalized matrix.
        test_loss /= len(loader.dataset)
        img = cMatrix.value()
        return img
Esempio n. 5
0
    def classify(self, model, test_loader, cuda, verbose=False):
        model.eval()
        test_loss = 0
        correct = 0
        c_matrix = confusionmeter.ConfusionMeter(self.classes, True)

        for data, target in test_loader:
            if cuda:
                data, target = data.cuda(), target.cuda()
                self.means = self.means.cuda()
            data, target = Variable(data, volatile=True), Variable(target)
            output = model(data, True).unsqueeze(1)
            result = (output.data - self.means.float())
            result = torch.norm(result, 2, 2)

            _, pred = torch.min(result, 1)

            correct += pred.eq(target.data.view_as(pred)).cpu().sum()
            c_matrix.add(pred, target.data.view_as(pred))
            # 0/0
        test_loss /= len(test_loader.dataset)

        return 100. * correct / len(test_loader.dataset)
Esempio n. 6
0
def saveConfusionMatrix(epoch, path, model, args, dataset, test_loader):
    model.eval()
    test_loss = 0
    correct = 0
    cMatrix = confusionmeter.ConfusionMeter(dataset.classes, True)

    for data, target in test_loader:
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data, volatile=True), Variable(target)
        output = model(data)
        test_loss += F.nll_loss(
            output, target, size_average=False).data[0]  # sum up batch loss
        pred = output.data.max(
            1, keepdim=True)[1]  # get the index of the max log-probability
        correct += pred.eq(target.data.view_as(pred)).cpu().sum()
        if epoch > 0:
            cMatrix.add(pred.squeeze(), target.data.view_as(pred).squeeze())

    test_loss /= len(test_loader.dataset)
    import cv2
    img = cMatrix.value() * 255
    cv2.imwrite(path + str(epoch) + ".jpg", img)
    return 100. * correct / len(test_loader.dataset)
        inputs = inputs.to(device)
        labels = labels.to(device)
        outputs = model_ft(inputs)
        _, predicted = torch.max(outputs, 1)
        point = (predicted == labels).squeeze()
        for j in range(len(labels)):
            label = labels[j]
            class_correct[label] += point[j].item()
            class_total[label] += 1

for i in range(8):
    print('Accuracy of %5s : %2d %%' %
          (class_names[i], 100 * class_correct[i] / class_total[i]))

#Get the confusion matrix for testing data
confusion_matrix = cm.ConfusionMeter(8)
with torch.no_grad():
    for i, (inputs, labels) in enumerate(dataloaders['test']):
        inputs = inputs.to(device)
        labels = labels.to(device)
        outputs = model_ft(inputs)
        _, predicted = torch.max(outputs, 1)
        confusion_matrix.add(predicted, labels)
    print(confusion_matrix.conf)

#Confusion matrix as a heatmap
con_m = confusion_matrix.conf
df_con_m = pd.DataFrame(con_m,
                        index=[i for i in class_names],
                        columns=[i for i in class_names])
sn.set(font_scale=1.1)