在图像分类中获得不同类别的准确性的问题

时间:2019-09-03 11:39:24

标签: python-3.x deep-learning conv-neural-network pytorch

我正在做一个与图像分类有关的任务。我的数据集有5个不同的类(标签)。我必须分别计算每个类(标签)的准确性。但是我的问题是,每当测试CNN模型时,我都会得到100一个标签的准确性百分比为0,其他标签的准确性为0%。我无法理解实现问题在哪里。

这是我的CNN模型

class ConvNet(nn.Module):
    def __init__(self):
        super(ConvNet, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv2d(1, 16, kernel_size=3,stride=1,padding=1),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        self.layer2 = nn.Sequential(
            nn.Conv2d(16, 32, kernel_size=3,stride=1,padding=1),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        self.drop_out = nn.Dropout()
        self.fc1 = nn.Linear(in_features=16 * 16 * 32, out_features=256)
        self.fc2 = nn.Linear(in_features=256, out_features=5)
        #self.fc3 = nn.Linear(in_features=64, out_features=5)
def forward(self, x):
    out = self.layer1(x)
    out = self.layer2(out)
    #out = out.view(out.size(0), -1)
    out = out.reshape(-1,16 * 16 * 32)
    out = self.drop_out(out)
    out = self.fc1(out)
    out = self.fc2(out)
    #out = self.fc3(out)
    return out

这是我用于计算类标签准确性的代码

classes = ['1', '2', '3', '4', '5']

class_correct = list(0. for i in range(5))
class_total = list(0. for i in range(5))

net.eval()  # prep model for evaluation

for batch in test_loader:
    data, target = batch['image'], batch['grade']

    if len(target.data) != BS:
        break
    # # forward pass: compute predicted outputs by passing inputs to the model
    output = net(data)
    # convert output probabilities to predicted class

    _, pred = torch.max(output, 1)
    # # compare predictions to true label
    correct = np.squeeze(pred.eq(target.data.view_as(pred)))
    # # calculate test accuracy for each object class
    for i in range(BS):
        label = target.data[i]
        class_correct[label] += correct[i].item()
        class_total[label] += 1

for i in range(5):
    print('Test Accuracy of %5s: %2d%% (%2d/%2d)' % (
        classes[i], 100 * class_correct[i] / class_total[i],
        np.sum(class_correct[i]), np.sum(class_total[i])))

这是我获得的结果

Accuracy of the network (overall): 46 %
Test Accuracy of     1: 100% (652/652)
Test Accuracy of     2:  0% ( 0/279)
Test Accuracy of     3:  0% ( 0/459)
Test Accuracy of     4:  0% ( 0/263)
Test Accuracy of     5:  0% ( 0/47)

为评估模型,我还绘制了我在此处发布的ROC曲线和精度召回曲线
ROC Precision recall curve

0 个答案:

没有答案