Pytorch实战 | P3 天气图片识别(深度学习实践pytorch)

一、我的环境:

● 语言环境:Python3.8
● 编译器:pycharm
● 深度学习环境:Pytorch
● 数据来源:链接: https://pan.baidu.com/s/1SEfd8mvWt7BpzmWOeaIRkQ 提取码: gdie

二、主要代码实现

1、main.py

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import torch.utils.data
from model import *

from handle import *

# 一、数据处理 headle.py
# 1、加载数据
path = './data/'
headle = Handle(path)
total_data = headle.handle()

# 2、划分数据集
train_size = int(0.8 * len(total_data))  # 80%用来做训练数据
test_size = len(total_data) - train_size  # 20%用来做测试数据

train_dataset, test_dataset = torch.utils.data.random_split(total_data, [train_size, test_size])  # 按比例随机划分数据

batch_size = 32

train_dl = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_dl = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=True)

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
# 二、网络构建 model().__init__ model.forward()
# 三、模型训练 model.train()
device = "cuda" if torch.cuda.is_available() else "cpu"
print("Using {} device".format(device))
model = Network_bn().to(device)
# 1、设置模型参数
loss_fn = nn.CrossEntropyLoss()  # 损失函数
learn_rate = 1e-4  # 学习率
opt = torch.optim.SGD(model.parameters(), lr=learn_rate)
# 2、训练函数 model.train1()
# 3、测试函数 model.test1()
# 4、模型训练
epochs = 20
train_loss = []
train_acc = []
test_loss = []
test_acc = []

for epoch in range(epochs):
    model.train()
    epoch_train_acc, epoch_train_loss = model.train1(train_dl, model, loss_fn, opt, device)

    model.eval()
    epoch_test_acc, epoch_test_loss = model.test(test_dl, model, loss_fn, device)

    train_loss.append(epoch_train_loss)
    train_acc.append(epoch_train_acc)
    test_loss.append(epoch_test_loss)
    test_acc.append(epoch_test_acc)

    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')

# 用测试数据做预测
test_pred = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=True)
acc = 0
for X, y in test_dl:
    pred = model(X)
    # classNames = ['cloudy', 'rain', 'shine', 'sunrise']
    # index = pred.argmax(1)
    # predict = classNames[index]
    # r_index = y.item()
    # print('predict:' + str(predict))
    # print('real:' + str(classNames[r_index]))
    # print('     ')
    acc += (pred.argmax(1) == y).type(torch.float).sum().item()
size = len(test_dl.dataset)
print(acc / size)


# 保存模型
torch.save(model.state_dict(), './model/model1.pkl')

# 四、结果可视化
# 隐藏警告
import warnings

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=(20, 5))

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()

2、handle.py

# -*- coding: utf-8 -*-
import pathlib
import torchvision.transforms as transforms
from torchvision import transforms, datasets
from PIL import Image
import numpy as np


class Handle():

    def __init__(self, path):
        self.path = path

    def handle(self):
        data_dir = pathlib.Path(self.path)

        data_paths = list(data_dir.glob('*'))
        classNames = [str(path).split("\\")[1] for path in data_paths]
        print("数据类别:" + str(classNames))

        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] 从数据集中随机抽样计算得到的。
        ])
        data = datasets.ImageFolder(self.path, transform=train_transforms)
        return data

    def single_handle(self):
        img = Image.open(self.path)
        # 转为numpy数组
        npy_img = np.array(img)
        print(npy_img.shape)
        transforms1 = transforms.Compose([
            transforms.Resize([224, 224]),  # 将输入图片resize成统一尺寸
            transforms.ToTensor()  # 将PIL Image或numpy.ndarray转换为tensor,并归一化到[0,1]之间
        ])
        # 转为tensor
        img = transforms1(img)
        print(img.shape)
        return img

3、model.py

# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F

classNames = 4


class Network_bn(nn.Module):
    def __init__(self):
        super(Network_bn, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=12, kernel_size=5, stride=1, padding=0)
        self.bn1 = nn.BatchNorm2d(12)  # 积层之后总会添加BatchNorm2d进行数据的归一化处理,这使得数据在进行Relu之前不会因为数据过大而导致网络性能的不稳定; 传入的参数为特征的维度
        self.conv2 = nn.Conv2d(in_channels=12, out_channels=12, kernel_size=5, stride=1, padding=0)
        self.bn2 = nn.BatchNorm2d(12)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv4 = nn.Conv2d(in_channels=12, out_channels=24, kernel_size=5, stride=1, padding=0)
        self.bn4 = nn.BatchNorm2d(24)
        self.conv5 = nn.Conv2d(in_channels=24, out_channels=24, kernel_size=5, stride=1, padding=0)
        self.bn5 = nn.BatchNorm2d(24)
        self.fc1 = nn.Linear(24 * 50 * 50, classNames)

    def forward(self, x):
        x = F.relu(self.bn1(self.conv1(x)))
        x = F.relu(self.bn2(self.conv2(x)))
        x = self.pool(x)
        x = F.relu(self.bn4(self.conv4(x)))
        x = F.relu(self.bn5(self.conv5(x)))
        x = self.pool(x)
        x = x.view(-1, 24 * 50 * 50) # 维度进行变换
        x = self.fc1(x)

        return x

    def train1(self, dataloader, model, loss_fn, optimizer, device):
        size = len(dataloader.dataset)  # 训练数据大小
        num_batch = 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)  # 求loss

            # 反向传播
            optimizer.zero_grad()  # 梯度属性归零
            loss.backward()  # 反向传播
            optimizer.step()  # 自动更新参数

            train_acc += (pred.argmax(1) == y).type(torch.float).sum().item()  # 预测正确结果的个数
            train_loss += loss.item()

        train_acc /= size
        train_loss /= num_batch

        return train_acc, train_loss

    def test(self, dataloader, model, loss_fn, device):
        size = len(dataloader.dataset)  # 测试数据集大小
        num_batch = 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_batch

        return test_acc, test_loss

三、遇到的问题

通过如下代码,加载模型后进行预测时,发现batch_size=1的时候准确率很低,batch_size设置越高预测准确率越高,直到设置成和训练时候一样32,准确率才正常。(目的是想通过一张图片来预测)

# -*- coding: utf-8 -*-
from model import *
from handle import *

# 加载模型
model = Network_bn()
model.load_state_dict(torch.load('./model/model1.pkl'))

# 本地加载图片并处理
img_data = Handle('./test/test3/').handle()
p_data = torch.utils.data.DataLoader(img_data, batch_size=1, shuffle=True)

acc = 0
# 预测结果
for X, y in p_data:
    pred = model(X)
    classNames = ['cloudy', 'rain', 'shine', 'sunrise']
    index = pred.argmax(1) - 1
    predict = classNames[index]
    r_index = y.item()
    print('predict:' + str(predict))
    print('real:' + str(classNames[r_index]))
    print('     ')
    acc += (pred.argmax(1) == y).type(torch.float).item()

print(acc/len(p_data))

在预测前加上model.eval()把问题解决了。
model.eval()
不启用 BatchNormalization 和 Dropout,保证BN和dropout不发生变化,pytorch框架会自动把BN和Dropout固定住,不会取平均,而是用训练好的值,不然的话,一旦test的batch_size过小,很容易就会被BN层影响结果。

你可能感兴趣的:(深度学习实践100例,深度学习,pytorch,python)