[365 plan-3] pytorch realizes weather recognition

This article is a learning record blog in 365-day deep learning training camp
Reference article address: 365 Days Deep Learning Training Camp – Week P3: Weather Recognition
Author: Classmate K

###This project comes from the online guidance of K students###
Data set download: https://pan.baidu.com/s/1Viq7s2FEtmcQQ3sRTMhszg
Extraction code: hqij

import torch
import torch.nn as nn
import torchvision.transforms as transforms
from torchvision import datasets
import pathlib
##Check if there is a cuda graphics card
device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)

data_dir='./weather_photos/'
data_dir=pathlib.Path(data_dir)
data_paths=list(data_dir.glob('*'))
classNames=[str(path).split('\')[1] for path in data_paths]
print(classNames)


train_transforms = transforms. Compose([
    transforms.Resize([224, 224]), # resize input image
    transforms.ToTensor(), # Convert PIL Image or numpy.ndarray to tensor
    transforms. Normalize(
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225]) # Calculated by random sampling from the data set
])

total_data = datasets. ImageFolder(data_dir, 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])

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

for X,y in test_dl:
    print('Shape of X [N, C, H, W]:', X.shape)
    print('Shape of y:', y.shape)
    break

import torch.nn.functional as F

num_classes = 4 # Number of categories of pictures


class Network_bn(nn.Module):
    def __init__(self):
        super().__init__()
        # feature extraction network
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=12, kernel_size=5, stride=1, padding=0)
        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)
        # classification network
        self.fc1 = nn.Linear(24 * 50 * 50, num_classes)

    # forward pass
    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 = x. view(-1, 24 * 50 * 50)
        x = self.fc1(x)

        return x


model = Network_bn().to(device)

loss_fn = nn.CrossEntropyLoss() # create loss function
learn_rate = 1e-4 # learning rate
opt = torch.optim.SGD(model.parameters(), lr=learn_rate)


# training loop
def train(dataloader, model, loss_fn, optimizer):
    size = len(dataloader.dataset) # The size of the training set, a total of 900 pictures
    num_batches = len(dataloader) # number of batches, 29 (900/32)

    train_loss, train_acc = 0, 0 # Initialize training loss and accuracy

    for X, y in dataloader: # Get pictures and their labels
        X, y = X.to(device), y.to(device)

        # Compute prediction error
        pred = model(X) # network output
        loss = loss_fn(pred, y) # Calculate the gap between the network output and the real value, targets is the real value, and the difference between the two is calculated as the loss

        # Backpropagation
        optimizer.zero_grad() # grad attribute is zeroed
        loss.backward() # backpropagation
        optimizer.step() # Automatically update each step

        # Record acc and 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) # The size of the test set, a total of 10,000 pictures
    num_batches = len(dataloader) # Number of batches, 8 (255/32=8, rounded up)
    test_loss, test_acc = 0, 0

    # When training is not in progress, stop gradient update to save computing memory consumption
    with torch.no_grad():
        for imgs, target in dataloader:
            imgs, target = imgs.to(device), target.to(device)

            # calculate 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 = 100
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
# hide warnings
import warnings

warnings.filterwarnings("ignore") # ignore warnings
plt.rcParams['font.sans-serif'] = ['SimHei'] # used to display Chinese labels normally
plt.rcParams['axes.unicode_minus'] = False # used to display the negative sign normally
plt.rcParams['figure.dpi'] = 100 # resolution

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

Training process:

Epoch: 1, Train_acc: 65.3%, Train_loss: 1.396, Test_acc: 52.4%, Test_loss: 0.886
Epoch: 2, Train_acc: 85.1%, Train_loss: 0.381, Test_acc: 86.7%, Test_loss: 0.345
Epoch: 3, Train_acc: 91.7%, Train_loss: 0.252, Test_acc: 89.8%, Test_loss: 0.257
Epoch: 4, Train_acc: 92.1%, Train_loss: 0.250, Test_acc: 40.4%, Test_loss: 6.429
Epoch: 5, Train_acc: 91.1%, Train_loss: 0.342, Test_acc: 67.1%, Test_loss: 0.966
Epoch: 6, Train_acc: 93.9%, Train_loss: 0.150, Test_acc: 89.8%, Test_loss: 0.223
Epoch: 7, Train_acc: 95.8%, Train_loss: 0.136, Test_acc: 83.6%, Test_loss: 0.820
Epoch: 8, Train_acc: 95.8%, Train_loss: 0.149, Test_acc: 86.7%, Test_loss: 0.291
Epoch: 9, Train_acc: 94.2%, Train_loss: 0.143, Test_acc: 89.8%, Test_loss: 0.264
Epoch: 10, Train_acc: 98.1%, Train_loss: 0.083, Test_acc: 72.9%, Test_loss: 1.241
Epoch: 11, Train_acc: 96.9%, Train_loss: 0.145, Test_acc: 53.3%, Test_loss: 7.011
Epoch: 12, Train_acc: 88.3%, Train_loss: 0.469, Test_acc: 56.0%, Test_loss: 2.457
Epoch: 13, Train_acc: 94.0%, Train_loss: 0.204, Test_acc: 92.0%, Test_loss: 0.211
Epoch: 14, Train_acc: 98.0%, Train_loss: 0.096, Test_acc: 75.1%, Test_loss: 0.686
Epoch: 15, Train_acc: 95.3%, Train_loss: 0.121, Test_acc: 91.6%, Test_loss: 0.244
Epoch: 16, Train_acc: 97.3%, Train_loss: 0.083, Test_acc: 84.9%, Test_loss: 0.305
Epoch: 17, Train_acc: 98.8%, Train_loss: 0.047, Test_acc: 92.0%, Test_loss: 0.184
Epoch: 18, Train_acc: 99.1%, Train_loss: 0.034, Test_acc: 93.3%, Test_loss: 0.210
Epoch: 19, Train_acc: 99.8%, Train_loss: 0.027, Test_acc: 94.2%, Test_loss: 0.174
Epoch: 20, Train_acc: 99.7%, Train_loss: 0.033, Test_acc: 92.4%, Test_loss: 0.182
Done


experience:
(1) There is a certain degree of overfitting
(2) The training process has small fluctuations and is unstable
(3) The training method is relatively simple
Solution:
(1) Add L2 regularization or dropout
(2) Increase the data set and try to increase the number of channels in the second convolutional layer, resulting in a sharp drop in accuracy, and a slight increase in the third layer