LeNet卷积神经网络


前言

LeNet是由Yann LeCun在1988年提出的用于数字识别的模型,可以说是深度卷积神经网络的基石,接下来深入理解其结构,并且用pytorch框架实现对CIFAR10数据集进行训练和预测过程。


一、LeNet简介

LeNet网络结构
输入:32x32
C1卷积层:6个5x5大小卷积核的卷积层,stride=1,输出大小6x28x28
S2池化层:最大池化,size=2,输出6x14x14
C3卷积层:16个5x5大小卷积核的卷积层,stride=1,输出大小16x10x10
S4池化层:最大池化,size=2,输出尺寸16x5x5
C5卷积层:120个5x5大小卷积核的卷积层,stride=1
F6层:全连接层,输出84张特征图
输出层:10个类别

二、使用步骤

1.模型搭建

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


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 x

2.模型训练

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张训练图片
    # 第一次使用时要将download设置为True才会自动去下载数据集
    train_set = torchvision.datasets.CIFAR10(root='./data', train=True,
                                             download=False, transform=transform)
    train_loader = torch.utils.data.DataLoader(train_set, batch_size=36,
                                               shuffle=True, num_workers=0)

    # 10000张验证图片
    # 第一次使用时要将download设置为True才会自动去下载数据集
    val_set = torchvision.datasets.CIFAR10(root='./data', train=False,
                                           download=False, transform=transform)
    val_loader = torch.utils.data.DataLoader(val_set, batch_size=5000,
                                             shuffle=False, num_workers=0)
    val_data_iter = iter(val_loader)
    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()
    optimizer = optim.Adam(net.parameters(), lr=0.001)

    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

            # zero the parameter gradients
            optimizer.zero_grad()
            # 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
                with torch.no_grad():
                    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()

模型预测

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()
    net.load_state_dict(torch.load('Lenet.pth'))

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

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


if __name__ == '__main__':
    main()


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于LeNet卷积神经网络的遥感影像分类处理的代码实现,其中使用了Tensorflow框架: ```python import tensorflow as tf import numpy as np import os from PIL import Image # 数据路径 data_path = './data/' # 训练集、验证集、测试集的比例 train_percent = 0.8 val_percent = 0.1 test_percent = 0.1 # 图像大小 image_width = 28 image_height = 28 image_channels = 3 # 分类数 num_classes = 6 # LeNet-5模型 def LeNet5(x, num_classes): # 第一个卷积层 conv1 = tf.layers.conv2d(x, filters=6, kernel_size=[5, 5], strides=1, padding='same', activation=tf.nn.relu) # 第一个池化层 pool1 = tf.layers.max_pooling2d(conv1, pool_size=[2, 2], strides=2) # 第二个卷积层 conv2 = tf.layers.conv2d(pool1, filters=16, kernel_size=[5, 5], strides=1, padding='valid', activation=tf.nn.relu) # 第二个池化层 pool2 = tf.layers.max_pooling2d(conv2, pool_size=[2, 2], strides=2) # 将特征图变成向量 flatten = tf.reshape(pool2, shape=[-1, 5 * 5 * 16]) # 第一个全连接层 fc1 = tf.layers.dense(flatten, units=120, activation=tf.nn.relu) # 第二个全连接层 fc2 = tf.layers.dense(fc1, units=84, activation=tf.nn.relu) # 输出层 logits = tf.layers.dense(fc2, units=num_classes) return logits # 加载数据 def load_data(): x_data = [] y_data = [] for label, name in enumerate(os.listdir(data_path)): dir_path = os.path.join(data_path, name) for file_name in os.listdir(dir_path): file_path = os.path.join(dir_path, file_name) # 读取图像并缩放到指定大小 image = Image.open(file_path).resize((image_width, image_height)) # 将图像转换成numpy数组 image_data = np.array(image) # 将图像的像素值归一化到0-1之间 image_data = image_data / 255.0 # 将图像和标签加入数据集 x_data.append(image_data) y_data.append(label) # 将数据集转换成numpy数组 x_data = np.array(x_data) y_data = np.array(y_data) # 将标签转换成one-hot编码 y_data_one_hot = np.eye(num_classes)[y_data] # 将数据集打乱顺序 perm = np.random.permutation(len(x_data)) x_data = x_data[perm] y_data_one_hot = y_data_one_hot[perm] # 将数据集分成训练集、验证集、测试集 train_size = int(train_percent * len(x_data)) val_size = int(val_percent * len(x_data)) test_size = len(x_data) - train_size - val_size x_train = x_data[:train_size] y_train = y_data_one_hot[:train_size] x_val = x_data[train_size:train_size+val_size] y_val = y_data_one_hot[train_size:train_size+val_size] x_test = x_data[-test_size:] y_test = y_data_one_hot[-test_size:] return x_train, y_train, x_val, y_val, x_test, y_test # 训练模型 def train_model(): # 加载数据 x_train, y_train, x_val, y_val, x_test, y_test = load_data() # 创建占位符 x = tf.placeholder(tf.float32, [None, image_width, image_height, image_channels]) y = tf.placeholder(tf.float32, [None, num_classes]) # 创建模型 logits = LeNet5(x, num_classes) # 定义损失函数和优化器 loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=y)) optimizer = tf.train.AdamOptimizer().minimize(loss) # 定义评估指标 correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # 创建会话并开始训练 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for epoch in range(10): for i in range(len(x_train) // 100): batch_x = x_train[i*100:(i+1)*100] batch_y = y_train[i*100:(i+1)*100] sess.run(optimizer, feed_dict={x: batch_x, y: batch_y}) train_acc = sess.run(accuracy, feed_dict={x: x_train, y: y_train}) val_acc = sess.run(accuracy, feed_dict={x: x_val, y: y_val}) print('epoch %d, train accuracy %g, validation accuracy %g' % (epoch+1, train_acc, val_acc)) test_acc = sess.run(accuracy, feed_dict={x: x_test, y: y_test}) print('test accuracy %g' % test_acc) if __name__ == '__main__': train_model() ``` 这个代码实现了一个基于LeNet-5卷积神经网络的遥感影像分类处理,其中使用了一个包含6个类别的数据集。代码中使用了Tensorflow框架,并将数据集分成了训练集、验证集和测试集。训练过程中使用了Adam优化器和交叉熵损失函数,并在每个epoch结束时计算了训练集和验证集的准确率。最终在测试集上计算了模型的准确率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值