- 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
- 🍖 原作者:K同学啊 | 接辅导、项目定制
import torch import torch.nn as nn import torchvision.transforms as transforms import torchvision from torchvision import transforms, datasets import os,PIL,random,pathlib import matplotlib.pyplot as plt import warnings device = torch.device("cuda" if torch.cuda.is_available() else "cpu") data_dir = './data/' data_dir = pathlib.Path(data_dir) data_paths = list(data_dir.glob('*')) classeNames = [str(path).split("\\")[1] for path in data_paths] total_datadir = './data/' train_transforms = transforms.Compose([ transforms.Resize([224, 224]), # 将输入图片resize成统一尺寸 transforms.ToTensor(), # 将PIL Image或numpy.ndarray转换为tensor,并归一化到[0,1]之间 transforms.Normalize( # 标准化处理-->转换为标准正太分布(高斯分布),使模型更容易收敛 mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # 其中 mean=[0.485,0.456,0.406]与std=[0.229,0.224,0.225] 从数据集中随机抽样计算得到的。 ]) total_data = datasets.ImageFolder(total_datadir,transform=train_transforms) train_size = int(0.8 * len(total_data)) test_size = len(total_data) - train_size train_dataset, test_dataset = torch.utils.data.random_split(total_data, [train_size, test_size]) train_dataset, test_dataset batch_size = 32 train_dl = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=1) test_dl = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=True, num_workers=1) for X, y in test_dl: print("Shape of X [N, C, H, W]: ", X.shape) print("Shape of y: ", y.shape, y.dtype) break import torch.nn.functional as F class Network_bn(nn.Module): def __init__(self): super(Network_bn, self).__init__() """ nn.Conv2d()函数: 第一个参数(in_channels)是输入的channel数量 第二个参数(out_channels)是输出的channel数量 第三个参数(kernel_size)是卷积核大小 第四个参数(stride)是步长,默认为1 第五个参数(padding)是填充大小,默认为0 """ self.conv1 = nn.Conv2d(in_channels=3, out_channels=15, kernel_size=5, stride=1, padding=0) self.bn1 = nn.BatchNorm2d(15) self.conv2 = nn.Conv2d(in_channels=15, out_channels=15, kernel_size=5, stride=1, padding=0) self.bn2 = nn.BatchNorm2d(15) self.pool1 = nn.MaxPool2d(2,2) self.conv3 = nn.Conv2d(in_channels=15, out_channels=30, kernel_size=5, stride=1, padding=0) self.bn3 = nn.BatchNorm2d(30) self.conv4 = nn.Conv2d(in_channels=30, out_channels=30, kernel_size=5, stride=1, padding=0) self.bn4 = nn.BatchNorm2d(30) self.pool2 = nn.MaxPool2d(2,2) self.conv5 = nn.Conv2d(in_channels=30, out_channels=60, kernel_size=5, stride=1, padding=0) self.bn5 = nn.BatchNorm2d(60) self.conv6 = nn.Conv2d(in_channels=60, out_channels=60, kernel_size=5, stride=1, padding=0) self.bn6 = nn.BatchNorm2d(60) self.pool3 = nn.MaxPool2d(2,2) self.fc1 = nn.Linear(60*21*21, len(classeNames)) def forward(self, x): x = F.relu(self.bn1(self.conv1(x))) x = F.relu(self.bn2(self.conv2(x))) x = self.pool1(x) x = F.relu(self.bn3(self.conv3(x))) x = F.relu(self.bn4(self.conv4(x))) x = self.pool2(x) x = F.relu(self.bn5(self.conv5(x))) x = F.relu(self.bn6(self.conv6(x))) x = self.pool3(x) x = x.view(-1, 60*21*21) x = self.fc1(x) return x device = "cuda" if torch.cuda.is_available() else "cpu" print("Using {} device".format(device)) model = Network_bn().to(device) from torchinfo import summary summary(model, (3, 224, 224), batch_dim = 0, col_names = ("input_size", "output_size", "num_params"), verbose = 0) loss_fn = nn.CrossEntropyLoss() learn_rate = 1e-3 opt = torch.optim.SGD(model.parameters(),lr=learn_rate) def train(dataloader, model, loss_fn, optimizer): size = len(dataloader.dataset) num_batches = len(dataloader) train_acc, train_loss = 0, 0 for X, y in dataloader: X, y= X.to(device), y.to(device) pred = model(X) loss = loss_fn(pred, y) # 反向传播 optimizer.zero_grad() loss.backward() optimizer.step() # 记录acc与loss train_acc += (pred.argmax(1) == y).type(torch.float).sum().item() train_loss += loss.item() train_acc /= size train_loss /= num_batches return train_acc, train_loss def test (dataloader, model, loss_fn): size = len(dataloader.dataset) num_batches = len(dataloader) test_loss, test_acc = 0, 0 # 当不进行训练时,停止梯度更新,节省计算内存消耗 with torch.no_grad(): for imgs, target in dataloader: imgs, target = imgs.to(device), target.to(device) # 计算loss target_pred = model(imgs) loss = loss_fn(target_pred, target) test_loss += loss.item() test_acc += (target_pred.argmax(1) == target).type(torch.float).sum().item() test_acc /= size test_loss /= num_batches return test_acc, test_loss epochs = 20 train_loss = [] train_acc = [] test_acc = [] test_loss = [] for epoch in range(epochs): model.train() epoch_train_acc, epoch_train_loss = train(train_dl, model, loss_fn, opt) model.eval() epoch_test_acc, epoch_test_loss = test(test_dl, model, loss_fn) train_acc.append(epoch_train_acc) train_loss.append(epoch_train_loss) test_acc.append(epoch_test_acc) test_loss.append(epoch_test_loss) template = ('Epoch:{:2d}, Train_acc:{:.1f}%, Train_loss:{:.3f}, Test_acc:{:.1f}%,Test_loss:{:.3f}') print(template.format(epoch+1, epoch_train_acc*100, epoch_train_loss, epoch_test_acc*100, epoch_test_loss)) print('Done') warnings.filterwarnings("ignore") plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False plt.rcParams['figure.dpi'] = 100 epochs_range = range(epochs) plt.figure(figsize=(12, 3)) plt.subplot(1, 2, 1) plt.plot(epochs_range, train_acc, label='Training Accuracy') plt.plot(epochs_range, test_acc, label='Test Accuracy') plt.legend(loc='lower right') plt.title('Training and Validation Accuracy') plt.subplot(1, 2, 2) plt.plot(epochs_range, train_loss, label='Training Loss') plt.plot(epochs_range, test_loss, label='Test Loss') plt.legend(loc='upper right') plt.title('Training and Validation Loss') plt.show() from PIL import Image classes = list(total_data.class_to_idx) def predict_one_image(image_path, model, transform, classes): test_img = Image.open(image_path).convert('RGB') test_img = transform(test_img) img = test_img.to(device).unsqueeze(0) model.eval() output = model(img) _, pred = torch.max(output, 1) pred_class = classes[pred] print(f"预测结果是:{pred_class}") # 预测训练集中的某张照片 predict_one_image(image_path='./data/Monkeypox/M01_03_00.jpg', model=model, transform=train_transforms, classes=classes) # 模型保存 PATH = './model.pth' # 保存的参数文件名 torch.save(model.state_dict(), PATH) # 将参数加载到model当中 model.load_state_dict(torch.load(PATH, map_location=device))
总结:对一些函数还是不太理解需要着重学习一下
第P4周:使用PyTorch实现猴痘病识别
最新推荐文章于 2024-07-16 23:30:07 发布