The easiest --- understand the depth of learning and training process to build

import torch
import torchvision
import matplotlib.pyplot as plt
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision.transforms import transforms
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

#定义ResBlock,见Resnet Learning图
class ResBlock(nn.Module):
    def __init__(self, in_channel, out_channel, stride=1, shortcut=None):
        super(ResBlock, self).__init__()
        self.left = nn.Sequential(
            nn.Conv2d(in_channel, out_channel, 3, stride, 1, bias=False),
            nn.BatchNorm2d(out_channel),
            nn.ReLU(True),
            nn.Conv2d(out_channel, out_channel, 3, 1, 1, bias=False),
            nn.BatchNorm2d(out_channel),
        )
        self.right = shortcut

    def forward(self, x):
        out = self.left(x)
        residual = x if self.right is None else self.right(x)
        out += residual
        return F.relu(out)

#定义make_layer
def make_layer(in_channel, out_channel, block_num, stride=1):
    shortcut = nn.Sequential(
        nn.Conv2d(in_channel, out_channel, 1, stride),
        nn.BatchNorm2d(out_channel))
    layers = list()
    layers.append(ResBlock(in_channel, out_channel, stride, shortcut))

    for i in range(1, block_num):
        layers.append(ResBlock(out_channel, out_channel))
    return nn.Sequential(*layers)

# 堆叠Resnet,见上表所示结构
class Resnet(nn.Module):
    def __init__(self):
        super(Resnet, self).__init__()
        self.pre = nn.Sequential(
            nn.Conv2d(3, 64, 7, 2, 3, bias=False), nn.BatchNorm2d(64),
            nn.ReLU(True), nn.MaxPool2d(3, 2, 1))
        self.layer1 = make_layer(64, 64, 2)
        self.layer2 = make_layer(64, 128, 2, stride=2)
        self.layer3 = make_layer(128, 256, 2, stride=2)
        self.layer4 = make_layer(256, 512, 2, stride=2)
        self.avg = nn.AvgPool2d(7)
        self.classifier = nn.Sequential(nn.Linear(512, 10))

    def forward(self, x):
        x = self.pre(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.avg(x)
        x = x.view(x.size(0), -1)
        OUT Criterion ( outputs, labels)= Self.classifier (X)
         return OUT 

# training function Train 
DEF net_train (): 
    net.train () 
    running_loss = 0.0
     for I, Data in the enumerate (trainloader, 0):
         # input pass the GPU 
        Inputs, Labels = Data 
        Inputs , Labels = inputs.to (Device), labels.to (Device) 

        # gradient zero 
        optimizer.zero_grad () 

        # forward propagation - optimization - calculating an error - backpropagation 
        Outputs = NET (Inputs) 
        Loss =
        loss.backward () 
        optimizer.step () 

        # calculating an error and displays 
        running_loss + = loss.item ()
         IF I 127% == 0:   # Print Every Mini-Batches 
            Print (
                 ' [% D,% 5D] Loss:% .3f ' % (+ Epoch. 1, I +. 1, running_loss / 128 )) 
            running_loss = 0.0 Print ( ' Training Epoch Finished ' ) # test function test DEF net_test (): 
    correct = 0 
    Total = 0
     # Close gradient

    



     with torch.no_grad ():
         for Data in testloader:
            images, labels = data
            images, labels = images.to(device), labels.to(device)
            outputs = net(images)
            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()

    print('Accuracy of the network on the 10000 test images: %d %%' %
          (100 * correct / total))
    return


#数据集函数 data_loader
def net_dataloader(root, train_transform, test_transform):
    trainset = torchvision.datasets.CIFAR10(
        root, train=True, transform=train_transform, download=True)
    testset = torchvision.datasets.CIFAR10(
        root, train=False, transform=test_transform, download=True)
    trainloader = DataLoader(
        trainset, batch_size=128, shuffle=True, num_workers=4)
    testloader = DataLoader(
        testset, batch_size=16, shuffle=False, num_workers=4)
     Print ( ' the Initializing a Dataset ... ' )
     return trainloader, testloader 


# main 
IF  the __name__ == " __main__ " :
     # Create instance and fed to the GPU 
    NET = Resnet () to (Device).
     # Selection Error Loss 
    Criterion = NN. CrossEntropyLoss ()
     # selection optimizer 
    optimizer = torch.optim.Adam (net.parameters (), = 1E-LR. 3 )
     # data position 
    the root = ' ./pydata/data/ ' 
    # data processing Augmentation 
    train_transform = transforms.Compose ([
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])
    test_transform = transforms.Compose([
        transforms.Resize(224),
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])
    # 创建数据loader
    trainloader, testloader = net_dataloader(root, train_transform,
                                             test_transform) 
    # RUN 
    n_epoch. 5 =   #Changing epoch 
    for epoch in the Range (n_epoch):
         Print ( ' Training ... ' ) 
        net_train ()   # each epoch trained once, test once 
        Print ( ' Testing ... ' ) 
        net_test ()

 

Guess you like

Origin www.cnblogs.com/ariel-dreamland/p/12551284.html