从报告的准确性降低pytorch预训练模型的top1准确性

时间:2019-07-01 14:16:22

标签: python

我已经从link下载了经过预训练的Pytorch CIFAR10模型,该模型具有自定义架构。关于所有预训练模型的描述的主要站点是here

正如您在top1 accuracy部分中所看到的那样,采用32浮点类型的CIFAR10预训练模型的准确性为93.78,但是当我使用CIFAR10测试数据集测试模型时,其top1准确性为86.72。是否需要我未做的预处理,或者这是开发人员的错误?

这是我的cifar10 pytorch模型(cifar10PytorchModel.py)

import torch.nn as nn
import torch as th
from collections import OrderedDict

class CIFAR(nn.Module):
    def __init__(self, features, n_channel, num_classes):
        super(CIFAR, self).__init__()
        assert isinstance(features, nn.Sequential), type(features)
        self.features = features
        self.classifier = nn.Sequential(
            nn.Linear(n_channel, num_classes)
        )
        # print(self.features)
        # print(self.classifier)

    def forward(self, x):
        x = self.features(x)
        x = x.view(x.size(0), -1)
        x = self.classifier(x)
        return x

def make_layers(cfg, batch_norm=False):
    layers = []
    in_channels = 3
    for i, v in enumerate(cfg):
        if v == 'M':
            layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
        else:
            padding = v[1] if isinstance(v, tuple) else 1
            out_channels = v[0] if isinstance(v, tuple) else v
            conv2d = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=padding)
            if batch_norm:
                layers += [conv2d, nn.BatchNorm2d(out_channels, affine=False), nn.ReLU()]
            else:
                layers += [conv2d, nn.ReLU()]
            in_channels = out_channels
    return nn.Sequential(*layers)

def returnModel():
    n_channel=128
    cfg = [n_channel, n_channel, 'M', 2*n_channel, 2*n_channel, 'M', 4*n_channel, 4*n_channel, 'M', (8*n_channel, 0), 'M']
    layers = make_layers(cfg, batch_norm=True)
    model = CIFAR(layers, n_channel=8*n_channel, num_classes=10)
    pretrained=True

    if pretrained:
        m = th.load('PATH_TO_MODEL/cifar10.pth')
        state_dict = m.state_dict() if isinstance(m, nn.Module) else m
        assert isinstance(state_dict, (dict, OrderedDict)), type(state_dict)
        model.load_state_dict(state_dict)
    return(model.eval())

这是我的预测代码,我使用的是傻瓜工具箱,其中包含用于预测的方法(cifar10PytorchModelPrediction.py)

import tensorflow as tf 
import foolbox  
import numpy as np 
import torch
import torchvision
from torch.utils.data import DataLoader
from src.cifar10PytorchModel import returnModel
np.set_printoptions(threshold=5)    

def load_dataset():
    data_path = 'PATH_TO_CIFAR10_DATASET/cifar32in32/test'
    train_dataset = torchvision.datasets.ImageFolder(
        root=data_path,
        transform = torchvision.transforms.Compose([
            torchvision.transforms.ToTensor(),
            torchvision.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
        ])
    )
    train_loader = torch.utils.data.DataLoader(
        train_dataset,
        batch_size=1,
        num_workers=0,
        shuffle=False
    )
    return train_loader

true_prediction=0
globalCount=0
kmodel = returnModel()
fmodel = foolbox.models.PyTorchModel(kmodel, bounds=(-1, 1), num_classes=10)
for batch_idx, (data, target) in enumerate(load_dataset()):
    cleanLabel = np.argmax(fmodel.predictions(data.numpy().squeeze()))
    globalCount=globalCount+1
    if cleanLabel == target:
        true_prediction = true_prediction + 1
    print(globalCount,'cleanLabel:', cleanLabel, '(', target, ')', true_prediction / (globalCount))

最后一个输出是:

10000 cleanLabel: 9 ( tensor([9]) ) 0.8672

0 个答案:

没有答案