上一篇
网络模型训练
小插曲
区别
import torch
a=torch.tensor(5)
print(a)
print(a.item())
import torchoutput=torch.tensor([[0.1,0.2],[0.05,0.4]])
print(output.argmax(1))preds=output.argmax(1)
target=torch.tensor([0,1])
print(preds==target)
print((preds==target).sum())
训练模型
import torch
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Linear, Flatten
class Dun(nn.Module):def __init__(self):super().__init__()self.model1 = Sequential(Conv2d(3, 32, 5, padding=2),MaxPool2d(2),Conv2d(32, 32, 5, padding=2),MaxPool2d(2),Conv2d(32, 64, 5, padding=2),MaxPool2d(2),Flatten(),Linear(1024, 64),Linear(64, 10))def forward(self,x):x=self.model1(x)return x
if __name__=='__main__':dun=Dun()input=torch.ones((64,3,32,32))print(dun(input).shape)
数据训练
import torchvision
from torch.utils.tensorboard import SummaryWriterfrom model import *
from torch.utils.data import DataLoadertrain_data=torchvision.datasets.CIFAR10(root="./data_set_train",train=True,transform=torchvision.transforms.ToTensor(),download=True)
test_data=torchvision.datasets.CIFAR10(root="./data_set_test",train=False,transform=torchvision.transforms.ToTensor(),download=True)
train_data_size=len(train_data)
test_data_size=len(test_data)
print("train_data_size:{}",format(train_data_size))
print("test_data_size:{}",format(test_data_size))
train_dataloader=DataLoader(train_data,batch_size=64)
test_dataloder=DataLoader(test_data,batch_size=64)
dun=Dun()
loss_fn=nn.CrossEntropyLoss()
learning_rate=1e-2
optimizerr=torch.optim.SGD(dun.parameters(),lr=learning_rate)
total_train_step=0
total_test_step=0
epoch=10
writer=SummaryWriter("./logs")for i in range(epoch):print("----------第{}轮训练------".format(i+1))dun.train()for data in train_dataloader:img,target=dataoutput=dun(img)loss=loss_fn(output,target)optimizerr.zero_grad()loss.backward()optimizerr.step()total_train_step+=1print("训练次数:{},loss:{}".format(total_train_step,loss.item()))writer.add_scalar("train_loss",loss.item(),total_train_step)total_test_loss=0total_accuracy=0dun.eval()with torch.no_grad():for data in test_dataloder:img,target=dataoutput=dun(img)total_test_loss+=loss_fn(output,target).item()accuracy=(output.argmax(1)==target).sum()total_accuracy+=accuracyprint("整体测试集上的Loss:{}".format(total_test_loss))writer.add_scalar("test_loss",total_test_loss,total_test_step)print("整体测试集上的正确率:{}".format(total_accuracy/test_data_size))writer.add_scalar("test_accuracy",total_accuracy/test_data_size,total_test_step)total_test_step+=1torch.save(dun,"dun{}.pth".format(i))print("保存模型")writer.close()
GPU 训练
第一种方式
将以上的三部分调用cuda方法,以上面训练数据的代码为例
if torch.cuda.is_available():dun=dun.cuda()
if torch.cuda.is_available():loss_fn=loss_fn.cuda()
if torch.cuda.is_available():img = img.cuda()target = target.cuda()
方式二:
device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
将方式一的代码换成
dun=dun.to(device)
查看GPU信息
完整模型验证
查看数据集CIFAR10的类别
import torchvision
from PIL import Image
import torch
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Linear, Flatten
class Dun(nn.Module):def __init__(self):super().__init__()self.model1 = Sequential(Conv2d(3, 32, 5, padding=2),MaxPool2d(2),Conv2d(32, 32, 5, padding=2),MaxPool2d(2),Conv2d(32, 64, 5, padding=2),MaxPool2d(2),Flatten(),Linear(1024, 64),Linear(64, 10))def forward(self,x):x=self.model1(x)return ximage_path="./img/1.png"
image=Image.open(image_path)
print(image)
transform=torchvision.transforms.Compose([torchvision.transforms.Resize((32,32)),torchvision.transforms.ToTensor()])
image=transform(image)
print(image.shape)
model=torch.load("dun0.pth",map_location=torch.device("cpu"))
print(model)
image=torch.reshape(image,(1,3,32,32))model.eval()
with torch.no_grad():output=model(image)
print(output)print(output.argmax(1))