城市建设鹤岗市网站,域名服务商平台,汽车案例网站,淄博桓台网站建设报价文章目录 预训练和微调代码测试结果参考来源 预训练和微调代码
数据集#xff1a;CIFAR10 CIFAR-10数据集由10类32x32的彩色图片组成#xff0c;一共包含60000张图片#xff0c;每一类包含6000图片。其中50000张图片作为训练集#xff0c;10000张图片作为测试集。数据集介… 文章目录 预训练和微调代码测试结果参考来源 预训练和微调代码
数据集CIFAR10 CIFAR-10数据集由10类32x32的彩色图片组成一共包含60000张图片每一类包含6000图片。其中50000张图片作为训练集10000张图片作为测试集。数据集介绍来自CIFAR10 图片来源https://paperswithcode.com/dataset/cifar-10
预训练模型: vgg16
代码
# Imports
import torch
import torchvision
import torch.nn as nn # All neural network modules, nn.Linear, nn.Conv2d, BatchNorm, Loss functions
import torch.optim as optim # For all Optimization algorithms, SGD, Adam, etc.
import torch.nn.functional as F # All functions that dont have any parameters
from torch.utils.data import (DataLoader,
) # Gives easier dataset managment and creates mini batches
import torchvision.datasets as datasets # Has standard datasets we can import in a nice way
import torchvision.transforms as transforms # Transformations we can perform on our dataset
from tqdm import tqdmdevice torch.device(cuda if torch.cuda.is_available() else cpu)# Hyperparameters
num_classes 10
learning_rate 1e-3
batch_size 1024
num_epochs 2class Identity(nn.Module):def __init__(self):super(Identity, self).__init__()def forward(self, x):return x# Load pretrain model modify itmodel torchvision.models.vgg16(weightsDEFAULT)
# # If you want to do finetuning then set requires_grad False
# # Remove these two lines if you want to train entire model,
# # and only want to load the pretrain weights.
# for param in model.parameters():
# param.requires_grad False
for param in model.parameters():param.requires_grad Falsemodel.avgpool Identity() # 站位层使得该层啥事不做
model.classifier nn.Sequential(nn.Linear(512, 100),nn.ReLU(),nn.Linear(100, 10)) # 修改原模型的后几层
model.to(device)# Load Data
train_dataset datasets.CIFAR10(rootdataset/, trainTrue, transformtransforms.ToTensor(), downloadTrue)train_loader DataLoader(datasettrain_dataset, batch_sizebatch_size, shuffleTrue)# Loss and optimizer
criterion nn.CrossEntropyLoss()
optimizer optim.Adam(model.parameters(), lrlearning_rate)# Train Network
for epoch in range(num_epochs):losses []for batch_idx, (data, targets) in enumerate(tqdm(train_loader)):# Get data to cuda if possibledata data.to(devicedevice)targets targets.to(devicedevice)# forwardscores model(data)loss criterion(scores, targets)losses.append(loss.item())# backwardoptimizer.zero_grad()loss.backward()# gradient descent or adam stepoptimizer.step()print(fCost at epoch {epoch} is {sum(losses)/len(losses):.5f})# Check accuracy on training test to see how good our modeldef check_accuracy(loader, model):if loader.dataset.train:print(Checking accuracy on training data)else:print(Checking accuracy on test data)num_correct 0num_samples 0model.eval()with torch.no_grad():for x, y in loader:x x.to(devicedevice)y y.to(devicedevice)scores model(x)_, predictions scores.max(1)num_correct (predictions y).sum()num_samples predictions.size(0)print(fGot {num_correct} / {num_samples} with accuracy {float(num_correct)/float(num_samples)*100:.2f}%)model.train()check_accuracy(train_loader, model)测试结果
Checking accuracy on training data
Got 29449 / 50000 with accuracy 58.90%可以看到本次预训练模型的导入测试结果并不理想。但并不妨碍我们对Pytorch预训练和微调的学习。
参考来源
【1】 https://www.youtube.com/watch?vqaDe0qQZ5AQlistPLhhyoLH6IjfxeoooqP9rhU3HJIAVAJ3Vzindex8 【2】https://github.com/aladdinpersson/Machine-Learning-Collection/blob/master/ML/Pytorch/Basics/pytorch_pretrain_finetune.py