# Pytorch示例demo LeNet()-torch

## Pytorch示例demo LeNet()

### Pytorch 安装

pytorch tensor的通道顺序[batch,channel,height,width]

Conv2d(self,
in_channels, # 输入通道
out_channels, # 卷积核的个数
kernel_size, # 卷积核大小
stride， # 步距默认1
bias # 偏置
)

``````  import torch.nn as nn
import torch.nn.functional as F
``````

### model.py

``````class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv1 = nn.Conv2d(3, 16, 5)
self.pool1 = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(16, 32, 5)
self.pool2 = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(32*5*5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = F.relu(self.conv1(x))    # input(3, 32, 32) output(16, 28, 28)
x = self.pool1(x)            # output(16, 14, 14)
x = F.relu(self.conv2(x))    # output(32, 10, 10)
x = self.pool2(x)            # output(32, 5, 5)
x = x.view(-1, 32*5*5)       # output(32*5*5)
x = F.relu(self.fc1(x))      # output(120)
x = F.relu(self.fc2(x))      # output(84)
x = self.fc3(x)              # output(10)
return
``````

### train.py

``````import torch
import torchvision
import torch.nn as nn
from model import LeNet
import torch.optim as optim
import torchvision.transforms as transforms

def main():
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

# 50000张训练图片
train_set = torchvision.datasets.CIFAR10(root='./data', train=True,
shuffle=True, num_workers=0)

# 10000张验证图片
val_set = torchvision.datasets.CIFAR10(root='./data', train=False,
shuffle=False, num_workers=0)
val_image, val_label = val_data_iter.next()

# classes = ('plane', 'car', 'bird', 'cat',
#            'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

net = LeNet()
loss_function = nn.CrossEntropyLoss()

for epoch in range(5):  # loop over the dataset multiple times

running_loss = 0.0
for step, data in enumerate(train_loader, start=0):
# get the inputs; data is a list of [inputs, labels]
inputs, labels = data

# forward + backward + optimize
outputs = net(inputs)
loss = loss_function(outputs, labels)
loss.backward()
optimizer.step()

# print statistics
running_loss += loss.item()
if step % 500 == 499:    # print every 500 mini-batches
outputs = net(val_image)  # [batch, 10]
predict_y = torch.max(outputs, dim=1)[1]
accuracy = torch.eq(predict_y, val_label).sum().item() / val_label.size(0)

print('[%d, %5d] train_loss: %.3f  test_accuracy: %.3f' %
(epoch + 1, step + 1, running_loss / 500, accuracy))
running_loss = 0.0

print('Finished Training')

save_path = './Lenet.pth'
torch.save(net.state_dict(), save_path)
if __name__ == '__main__':
main()
``````

### predict.py

``````import torch
import torchvision.transforms as transforms
from PIL import Image

from model import LeNet

def main():
transform = transforms.Compose(
[transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

net = LeNet()

im = Image.open('1.jpg')
im = transform(im)  # [C, H, W]
im = torch.unsqueeze(im, dim=0)  # [N, C, H, W]

outputs = net(im)
predict = torch.max(outputs, dim=1)[1].numpy()
print(classes[int(predict)])

if __name__ == '__main__':
main()``````
————————

### Pytorch 安装

pytorch tensor的通道顺序[batch,channel,height,width]

Conv2d(self,
in_channels, # 输入通道
out_channels, # 卷积核的个数
kernel_size, # 卷积核大小
stride， # 步距默认1
bias # 偏置
)

``````  import torch.nn as nn
import torch.nn.functional as F
``````

### model.py

``````class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv1 = nn.Conv2d(3, 16, 5)
self.pool1 = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(16, 32, 5)
self.pool2 = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(32*5*5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = F.relu(self.conv1(x))    # input(3, 32, 32) output(16, 28, 28)
x = self.pool1(x)            # output(16, 14, 14)
x = F.relu(self.conv2(x))    # output(32, 10, 10)
x = self.pool2(x)            # output(32, 5, 5)
x = x.view(-1, 32*5*5)       # output(32*5*5)
x = F.relu(self.fc1(x))      # output(120)
x = F.relu(self.fc2(x))      # output(84)
x = self.fc3(x)              # output(10)
return
``````

### train.py

``````import torch
import torchvision
import torch.nn as nn
from model import LeNet
import torch.optim as optim
import torchvision.transforms as transforms

def main():
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

# 50000张训练图片
train_set = torchvision.datasets.CIFAR10(root='./data', train=True,
shuffle=True, num_workers=0)

# 10000张验证图片
val_set = torchvision.datasets.CIFAR10(root='./data', train=False,
shuffle=False, num_workers=0)
val_image, val_label = val_data_iter.next()

# classes = ('plane', 'car', 'bird', 'cat',
#            'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

net = LeNet()
loss_function = nn.CrossEntropyLoss()

for epoch in range(5):  # loop over the dataset multiple times

running_loss = 0.0
for step, data in enumerate(train_loader, start=0):
# get the inputs; data is a list of [inputs, labels]
inputs, labels = data

# forward + backward + optimize
outputs = net(inputs)
loss = loss_function(outputs, labels)
loss.backward()
optimizer.step()

# print statistics
running_loss += loss.item()
if step % 500 == 499:    # print every 500 mini-batches
outputs = net(val_image)  # [batch, 10]
predict_y = torch.max(outputs, dim=1)[1]
accuracy = torch.eq(predict_y, val_label).sum().item() / val_label.size(0)

print('[%d, %5d] train_loss: %.3f  test_accuracy: %.3f' %
(epoch + 1, step + 1, running_loss / 500, accuracy))
running_loss = 0.0

print('Finished Training')

save_path = './Lenet.pth'
torch.save(net.state_dict(), save_path)
if __name__ == '__main__':
main()
``````

### predict.py

``````import torch
import torchvision.transforms as transforms
from PIL import Image

from model import LeNet

def main():
transform = transforms.Compose(
[transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

net = LeNet()