当前位置:网站首页>Pytoch learning notes (III) use, modification, training (cpu/gpu) and verification of the model
Pytoch learning notes (III) use, modification, training (cpu/gpu) and verification of the model
2022-07-26 13:32:00 【Did Xiao Hu get stronger today】
List of articles
Use and modification of existing models
import torchvision
vgg16_true = torchvision.models.vgg16(pretrained=True)
print(vgg16_true)
Run code :
The storage address of the downloaded data set :

Add a linear layer on the basis of the original model ;
import torchvision
from torch import nn
vgg16_true = torchvision.models.vgg16(pretrained=True)
print(vgg16_true)
train_data = torchvision.datasets.CIFAR10('../data', transform=torchvision.transforms.ToTensor(), download=True)
# add to
vgg16_true.classifier.add_module('add_linear', nn.Linear(1000, 10))
# modify
vgg16_true.classifier[6] = nn.Linear(4096, 10)
print(vgg16_true)

Save and read the network model
import torch
import torchvision
vgg16 = torchvision.models.vgg16(pretrained=False)
# Save the way 1, Model structure + Model parameters
torch.save(vgg16, "vgg16_method1.path")
# Save the way 2, Model parameters ( The official recommendation )
torch.save(vgg16.state_dict(), "vgg16_method2.path")
import torch
import torchvision
# The way 1》 Save the way 1, Load model
# model = torch.load("vgg16_method1.path")
# print(model)
# The way 1》 Save the way 2, Load model
vgg16 = torchvision.models.vgg16(pretrained=False)
vgg16.load_state_dict(torch.load("vgg16_method2.path"))
# model = torch.load("vgg16_method2.path")
print(vgg16)
Complete model training routine
import torch
import torchvision
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Flatten, Linear
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from model import *
# Prepare the dataset
train_data = torchvision.datasets.CIFAR10(root="../data", train=True, transform=torchvision.transforms.ToTensor(), download=True)
test_data = torchvision.datasets.CIFAR10(root="../data", train=False, transform=torchvision.transforms.ToTensor(), download=True)
# length
train_data_size = len(train_data)
test_data_size = len(test_data)
print(" The length of the training data set is :{}".format(train_data_size))
print(" The length of the test data set is :{}".format(test_data_size))
# utilize DataLoader To load data
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)
# Create a network model
tudui = Tudui()
# Loss function
loss_fn = nn.CrossEntropyLoss()
# Optimizer
learning_rate = 1e-2
optimizer = torch.optim.SGD(tudui.parameters(), lr=learning_rate)
# Set some parameters of the training network
# Record the number of workouts
total_train_step = 0
# Record the number of tests
total_test_step = 0
# Number of training rounds
epoch = 10
# add to tensorboard
writer = SummaryWriter("../logs_train")
for i in range(epoch):
print("-------- The first {} Round of training begins -------".format(i + 1))
# The training steps begin
for data in train_dataloader:
imgs, targets = data
outputs = tudui(imgs)
loss = loss_fn(outputs, targets)
# Optimizer optimization model
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_train_step += 1
if total_train_step % 100 == 0:
print(" Training times :{}, Loss:{}".format(total_train_step, loss.item()))
writer.add_scalar("train_loss", loss.item(), total_train_step)
# The test step begins
total_test = 0
total_accuracy = 0
with torch.no_grad():
for data in test_dataloader:
imgs, targets = data
outputs = tudui(imgs)
loss = loss_fn(outputs, targets)
total_test_loss = total_test + loss.item()
accuracy = (outputs.argmax(1) == targets).sum()
total_accuracy = total_accuracy + accuracy
print(" On the overall test set Loss:{}".format(total_test_loss))
print(" Accuracy on the overall test set :{}".format(total_accuracy/test_data_size))
writer.add_scalar("test_loss", total_test_loss, total_test_step)
writer.add_scalar("test_accuracy", total_accuracy/test_data_size, total_test_step)
total_test_step += 1
torch.save(tudui, "tudui_{}.pth".format(i))
print(" Model saved ")
writer.close()

utilize GPU Training
1. The first way
A network model data ( Input , mark ) Loss function Find the first three parameters , call .cuda() return
import torch
import torchvision
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Flatten, Linear
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
import time
from model import *
# Prepare the dataset
train_data = torchvision.datasets.CIFAR10(root="../data", train=True, transform=torchvision.transforms.ToTensor(), download=True)
test_data = torchvision.datasets.CIFAR10(root="../data", train=False, transform=torchvision.transforms.ToTensor(), download=True)
# length
train_data_size = len(train_data)
test_data_size = len(test_data)
print(" The length of the training data set is :{}".format(train_data_size))
print(" The length of the test data set is :{}".format(test_data_size))
# utilize DataLoader To load data
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)
# Create a network model
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.model = Sequential(
Conv2d(3, 32, 5, 1, padding=2),
MaxPool2d(2),
Conv2d(32, 32, 5, 1, padding=2),
MaxPool2d(2),
Conv2d(32, 64, 5, 1, padding=2),
MaxPool2d(2),
Flatten(),
Linear(64 * 4 *4, 64),
Linear(64, 10)
)
def forward(self, x):
x =self.model(x)
return x
tudui = Tudui()
if torch.cuda.is_available():
tudui = tudui.cuda()
# Loss function
loss_fn = nn.CrossEntropyLoss()
if torch.cuda.is_available():
loss_fn = loss_fn.cuda()
# Optimizer
learning_rate = 1e-2
optimizer = torch.optim.SGD(tudui.parameters(), lr=learning_rate)
# Set some parameters of the training network
# Record the number of workouts
total_train_step = 0
# Record the number of tests
total_test_step = 0
# Number of training rounds
epoch = 10
# add to tensorboard
writer = SummaryWriter("../logs_train")
start_time = time.time()
for i in range(epoch):
print("-------- The first {} Round of training begins -------".format(i + 1))
# The training steps begin
tudui.train()
for data in train_dataloader:
if torch.cuda.is_available():
imgs, targets = data
imgs = imgs.cuda()
targets = targets.cuda()
outputs = tudui(imgs)
loss = loss_fn(outputs, targets)
# Optimizer optimization model
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_train_step += 1
if total_train_step % 100 == 0:
end_time = time.time()
print(end_time - start_time)
print(" Training times :{}, Loss:{}".format(total_train_step, loss.item()))
writer.add_scalar("train_loss", loss.item(), total_train_step)
# The test step begins
tudui.eval()
total_test = 0
total_accuracy = 0
with torch.no_grad():
for data in test_dataloader:
if torch.cuda.is_available():
imgs, targets = data
imgs = imgs.cuda()
targets = targets.cuda()
outputs = tudui(imgs)
loss = loss_fn(outputs, targets)
total_test_loss = total_test + loss.item()
accuracy = (outputs.argmax(1) == targets).sum()
total_accuracy = total_accuracy + accuracy
print(" On the overall test set Loss:{}".format(total_test_loss))
print(" Accuracy on the overall test set :{}".format(total_accuracy/test_data_size))
writer.add_scalar("test_loss", total_test_loss, total_test_step)
writer.add_scalar("test_accuracy", total_accuracy/test_data_size, total_test_step)
total_test_step += 1
torch.save(tudui, "tudui_{}.pth".format(i))
print(" Model saved ")
writer.close()
GPU Training :

CPU Training :

Native configuration CPU by AMD Ryzen 7 5800H with Radeon Graphics Eight cores ,GPU by
Nvidia GeForce RTX 3060 Laptop GPU ( 6 GB / lenovo ), It can be seen that running with a graphics card is still much faster .
If the computer doesn't have GPU, You can use Google .
Sign in https://colab.research.google.com/ , You need to register your Google account and login to use , Select File , New notebook .

You can see GPU It doesn't work :![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-5wD1ii0y-1658804427176)(C:\Users\Husheng\Desktop\ Learning notes \image-20220725170218869.png)]](/img/08/02bcb39c9784c15d67155f9d094c85.png)
If you want to use, you can modify the following settings :
Click on modify —> Notebook settings , Select the hardware accelerator as GPU, Click save :

Test again at this time ,GPU You can use it :
The above gpu Copy the code of version , function

It can also work normally , And it's very fast , Than my notebook 3060 It's much faster , It's ridiculous !!!
2. The second way :
.to(device)
Device = torch.device(“cpu”)
Torch.device(“cuda”)
Torch.device(“cuda.0”)
Torch.device(“cuda.1”)
import torch
import torchvision
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Flatten, Linear
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
import time
from model import *
# Define the training equipment
device = torch.device("cuda")
# Prepare the dataset
train_data = torchvision.datasets.CIFAR10(root="../data", train=True, transform=torchvision.transforms.ToTensor(), download=True)
test_data = torchvision.datasets.CIFAR10(root="../data", train=False, transform=torchvision.transforms.ToTensor(), download=True)
# length
train_data_size = len(train_data)
test_data_size = len(test_data)
print(" The length of the training data set is :{}".format(train_data_size))
print(" The length of the test data set is :{}".format(test_data_size))
# utilize DataLoader To load data
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)
# Create a network model
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.model = Sequential(
Conv2d(3, 32, 5, 1, padding=2),
MaxPool2d(2),
Conv2d(32, 32, 5, 1, padding=2),
MaxPool2d(2),
Conv2d(32, 64, 5, 1, padding=2),
MaxPool2d(2),
Flatten(),
Linear(64 * 4 *4, 64),
Linear(64, 10)
)
def forward(self, x):
x =self.model(x)
return x
tudui = Tudui()
tudui = tudui.to(device)
# Loss function
loss_fn = nn.CrossEntropyLoss()
loss_fn = loss_fn.to(device)
# Optimizer
learning_rate = 1e-2
optimizer = torch.optim.SGD(tudui.parameters(), lr=learning_rate)
# Set some parameters of the training network
# Record the number of workouts
total_train_step = 0
# Record the number of tests
total_test_step = 0
# Number of training rounds
epoch = 10
# add to tensorboard
writer = SummaryWriter("../logs_train")
start_time = time.time()
for i in range(epoch):
print("-------- The first {} Round of training begins -------".format(i + 1))
# The training steps begin
tudui.train()
for data in train_dataloader:
imgs, targets = data
imgs = imgs.to(device)
targets = targets.to(device)
outputs = tudui(imgs)
loss = loss_fn(outputs, targets)
# Optimizer optimization model
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_train_step += 1
if total_train_step % 100 == 0:
end_time = time.time()
print(end_time - start_time)
print(" Training times :{}, Loss:{}".format(total_train_step, loss.item()))
writer.add_scalar("train_loss", loss.item(), total_train_step)
# The test step begins
tudui.eval()
total_test = 0
total_accuracy = 0
with torch.no_grad():
for data in test_dataloader:
imgs, targets = data
imgs = imgs.to(device)
targets = targets.to(device)
outputs = tudui(imgs)
loss = loss_fn(outputs, targets)
total_test_loss = total_test + loss.item()
accuracy = (outputs.argmax(1) == targets).sum()
total_accuracy = total_accuracy + accuracy
print(" On the overall test set Loss:{}".format(total_test_loss))
print(" Accuracy on the overall test set :{}".format(total_accuracy/test_data_size))
writer.add_scalar("test_loss", total_test_loss, total_test_step)
writer.add_scalar("test_accuracy", total_accuracy/test_data_size, total_test_step)
total_test_step += 1
torch.save(tudui, "tudui_{}.pth".format(i))
print(" Model saved ")
writer.close()
Completed model validation routine
Complete model validation ( test ,demo) tricks : Using a trained model , Then give it input .
import torch
import torchvision
from PIL import Image
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
image_path = "../imgs/airplane.png"
image = Image.open(image_path)
print(image)
# image = image.convert('RGB')
transform = torchvision.transforms.Compose([torchvision.transforms.Resize((32, 32)),
torchvision.transforms.ToTensor()])
image = transform(image)
print(image.shape)
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.model = Sequential(
Conv2d(3, 32, 5, 1, padding=2),
MaxPool2d(2),
Conv2d(32, 32, 5, 1, padding=2),
MaxPool2d(2),
Conv2d(32, 64, 5, 1, padding=2),
MaxPool2d(2),
Flatten(),
Linear(64 * 4 * 4, 64),
Linear(64, 10)
)
def forward(self, x):
x =self.model(x)
return x
model = torch.load("tudui_0.pth")
print(model)
image = torch.reshape(image, (1, 3, 32, 32))
model.eval()
with torch.no_grad():
image = image.cuda()
output = model(image)
print(output)
print(output.argmax(1))
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-0smcLlpD-1658804427178)(C:\Users\Husheng\Desktop\ Learning notes \image-20220725203146266.png)]](/img/f9/e44708f5160436365ed2cac7c6b9e7.png)
Reference material
边栏推荐
- LeetCode 1523. 在区间范围内统计奇数数目
- Team research and development from ants' foraging process (Reprint)
- 白帽子揭秘:互联网千亿黑产吓退马斯克
- Detailed explanation of factory mode
- JSON format execution plan (6) - MySQL execution plan (52)
- Target detection network r-cnn series
- Using the geoprocessor tool
- 【OAuth2】八、OAuth2登录的配置逻辑-OAuth2LoginConfigurer和OAuth2ClientConfigurer
- Uncover the secret of white hat: 100 billion black products on the Internet scare musk away
- 【黑马早报】字节旗下多款APP下架;三只松鼠脱氧剂泄露致孕妇误食;CBA公司向B站索赔4.06亿;马斯克否认与谷歌创始人妻子有婚外情...
猜你喜欢
Exploration on cache design optimization of community like business

Ultimate doll 2.0 | cloud native delivery package

一笔画问题(中国邮递员问题)

This article explains the FS file module and path module in nodejs in detail

【花雕动手做】有趣好玩的音乐可视化系列小项目(13)---有机棒立柱灯

Algorithm -- continuous sequence (kotlin)

云智技术论坛工业专场 明天见!

Solution 5g technology helps build smart Parks

AI theory knowledge map 1 Foundation

Activity. Onstop() delay 10 seconds? Wonderful investigation process
随机推荐
【花雕动手做】有趣好玩的音乐可视化系列小项目(12)---米管快速节奏灯
【开源之美】nanomsg(2) :req/rep 模式
[beauty of open source] nanomsg (2): req/rep mode
《Kotlin系列》之MVVM架构封装(kotlin+mvvm)
Familiarize you with the "phone book" of cloud network: DNS
Hcip day 12 notes sorting (BGP Federation, routing rules)
7-25 0-1 backpack (50 points)
JS object assignment problem
Hcip day 11 comparison (BGP configuration and release)
WPS凭什么拒绝广告?
【Oauth2】七、微信OAuth2授权登录
Time complexity and space complexity
key&key_ Len & ref & filtered (4) - MySQL execution plan (50)
Leetcode 217. there are duplicate elements
pomerium
一笔画问题(中国邮递员问题)
深度学习3D人体姿态估计国内外研究现状及痛点
Algorithm -- continuous sequence (kotlin)
A college archives management system based on asp.net
Unity中序列化类为json格式