使用预训练模型的 Alexnet 进行图片分类,准确率与网络数据不符,可能是什么原因导致的? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Richard14
V2EX    问与答

使用预训练模型的 Alexnet 进行图片分类,准确与网络数据不符,可能是什么原因导致的?

  •  
  •   Richard14 2021-11-21 01:48:54 +08:00 1196 次点击
    这是一个创建于 1471 天前的主题,其中的信息可能已经有所发展或是发生改变。

    预训练的意思是用 torchvision 里写好的 alexnet (修改最后一层),不是指导入训练好的,尝试用 quickstart 里的代码训练 cifar10 ,但是网上普遍查到的实验数据,准确率大概在 80%,78%左右,我迭代到收敛也只能得到 70%的准确率,这个差异产生的原因是啥呢?

    完整代码:

    from utils import * from pipeit import * import os,sys,time,pickle,random import matplotlib.pyplot as plt import numpy as np import torch from torch import nn from torchvision import datasets, models from torch.utils.data import Dataset, DataLoader, TensorDataset from torchvision.transforms import ToTensor, Lambda, Resize, Compose, InterpolationMode device = "cuda" if torch.cuda.is_available() else "cpu" print("Using {} device".format(device)) torch.backends.cudnn.benchmark=True # Download training data from open datasets. training_data = datasets.CIFAR10( root=".\\data\\cifar10", train=True, download=True, transform=Compose([ Resize((64, 64), InterpolationMode.BICUBIC), ToTensor() ]) ) # Download test data from open datasets. test_data = datasets.CIFAR10( root=".\\data\\cifar10", train=False, download=True, transform=Compose([ Resize((64, 64), InterpolationMode.BICUBIC), ToTensor() ]) ) def imshow(training_data): labels_map = { 0: "plane", 1: "car", 2: "bird", 3: "cat", 4: "deer", 5: "dog", 6: "frog", 7: "horse", 8: "ship", 9: "truck", } cols, rows = 3, 3 figure = plt.figure(figsize=(8,8)) for i in range(1, cols * rows + 1): sample_idx = torch.randint(len(training_data), size=(1,)).item() img, label = training_data[sample_idx] img = img.swapaxes(0,1) img = img.swapaxes(1,2) figure.add_subplot(rows, cols, i) plt.title(labels_map[label]) plt.axis("off") plt.imshow(img) plt.show() # imshow(training_data) def train_loop(dataloader, net, loss_fn, optimizer): size = len(dataloader) train_loss = 0 for batch_idx, (X, tag) in enumerate(dataloader): X, tag = X.to(device), tag.to(device) pred = net(X) loss = loss_fn(pred, tag) train_loss += loss.item() # Back propagation optimizer.zero_grad() loss.backward() optimizer.step() train_loss /= size return train_loss def test_loop(dataloader, model, loss_fn): size = len(dataloader.dataset) num_batches = len(dataloader) test_loss, correct = 0, 0 with torch.no_grad(): for X, y in dataloader: X, y = X.to(device), y.to(device) pred = model(X) test_loss += loss_fn(pred, y).item() correct += (pred.argmax(1) == y).type(torch.float).sum().item() test_loss /= num_batches correct /= size return test_loss, correct net = models.alexnet().to(device) net.classifier[6] = nn.Linear(4096, 10).to(device) learning_rate = 0.01 batch_size = 128 weight_decay = 0 train_dataloader = DataLoader(training_data, batch_size = batch_size) test_dataloader = DataLoader(test_data, batch_size = batch_size) loss_fn = nn.CrossEntropyLoss() optimizer = torch.optim.SGD(net.parameters(), lr = learning_rate) epochs = 50 for t in range(epochs): print(f"Epoch {t+1}\n-------------------------------") st_time = time.time() train_loss = train_loop(train_dataloader, net, loss_fn, optimizer) test_loss, correct = test_loop(test_dataloader, net, loss_fn) print(f"Train loss: {train_loss:>8f}, Test loss: {test_loss:>8f}, Accuracy: {(100*correct):>0.1f}%, Epoch time: {time.time() - st_time:.2f}s\n") print("Done!") torch.save(net.state_dict(), 'alexnet-pre1.model') 

    最后收敛时的数据在这样:

    Epoch 52 ------------------------------- Train loss: 0.399347, Test loss: 0.970927, Accuracy: 70.3%, Epoch time: 17.20s 
    1 条回复    2021-11-21 23:53:55 +08:00
    KangolHsu
        1
    KangolHsu  
       2021-11-21 23:53:55 +08:00 via iPhone
    输入的图片 64*64 ?是不是有点小啊
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5077 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 01:23 PVG 09:23 LAX 17:23 JFK 20:23
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86