第P3周:天气识别

本文详细介绍了使用PyTorch进行彩色图片天气识别的项目,包括数据预处理、模型构建(含网络结构、Dropout和小卷积核)、训练与测试函数以及结果可视化。作者尝试了不同网络设置但效果不理想,暗示了对更深入网络知识的需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的环境

语言环境:Python3.8
●编译器:Jupyter Lab
●深度学习环境:
○torch1.12.1+cu113
○torchvision
0.13.1+cu113

一、前期准备

import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision
from torchvision import transforms, datasets

import os,PIL,pathlib

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

device

二、数据处理

1.导入数据集
import os,PIL,random,pathlib

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]), 
    transforms.ToTensor(),         
    transforms.Normalize(          
        mean=[0.485, 0.456, 0.406], 
        std=[0.229, 0.224, 0.225]) 
])

total_data = datasets.ImageFolder(total_datadir,transform=train_transforms)
total_data
2.数据划分
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)

三、网络

1.网络构建
import torch.nn.functional as F

class Network_bn_with_dropout_and_small_kernel(nn.Module):
    def __init__(self):
        super(Network_bn_with_dropout_and_small_kernel, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=12, kernel_size=3, stride=1, padding=0) # Changed kernel_size to 3x3
        self.bn1 = nn.BatchNorm2d(12)
        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.conv3 = nn.Conv2d(in_channels=12, out_channels=24, kernel_size=5, stride=1, padding=0)
        self.bn3 = nn.BatchNorm2d(24)
        self.conv4 = nn.Conv2d(in_channels=24, out_channels=24, kernel_size=5, stride=1, padding=0)
        self.bn4 = nn.BatchNorm2d(24)
        self.conv5 = nn.Conv2d(in_channels=24, out_channels=48, kernel_size=5, stride=1, padding=0)
        self.bn5 = nn.BatchNorm2d(48)
        self.conv6 = nn.Conv2d(in_channels=48, out_channels=48, kernel_size=5, stride=1, padding=0)
        self.bn6 = nn.BatchNorm2d(48)
        self.fc1 = nn.Linear(48 * 12 * 12, 256)
        self.dropout = nn.Dropout(0.5)
        self.fc2 = nn.Linear(256, len(classeNames))

    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.bn3(self.conv3(x)))
        x = F.relu(self.bn4(self.conv4(x)))
        x = self.pool(x)
        x = F.relu(self.bn5(self.conv5(x)))
        x = F.relu(self.bn6(self.conv6(x)))
        x = self.pool(x)
        x = x.view(-1, 48 * 12 * 12)
        x = F.relu(self.fc1(x))
        x = self.dropout(x)
        x = self.fc2(x)

        return x


device = "cuda" if torch.cuda.is_available() else "cpu"
2.网络加载打印
from torchinfo import summary
model = Network_bn().to(device)
summary(model)

在这里插入图片描述

三、训练与测试函数

1.超参数
loss_fn    = nn.CrossEntropyLoss() 
learn_rate = 1e-4 
opt        = torch.optim.SGD(model.parameters(),lr=learn_rate)

learn_rate:学习率是优化器在更新模型权重时调整权重的步长大小。较小的学习率可能导致收敛较慢,而较大的学习率可能导致优化过程不稳定。

2.训练函数
# 训练循环
def train(dataloader, model, loss_fn, optimizer):
    size = len(dataloader.dataset)  
    num_batches = len(dataloader)   

    train_loss, train_acc = 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()     
        
    
        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
3.测试函数
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)
            
            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_loss  = []
test_acc   = []

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

在这里插入图片描述

五、结果可视化

import matplotlib.pyplot as plt
#隐藏警告
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=(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()

在这里插入图片描述

六、总结

这次试图改进网络,分别尝试了增加卷积层、dropout、调整卷积核大小,但训练结果都不算特别好,还需要更加深入了解网络构建。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值