TensorFlow實戰卷積神經網路之LeNet

磐石001發表於2018-04-03

歡迎大家關注我們的網站和系列教程:http://www.tensorflownews.com/,學習更多的機器學習、深度學習的知識!

LeNet

專案簡介

1994 年深度學習三巨頭之一的 Yan LeCun 提出了 LeNet 神經網路,這是最早的卷積神經網路。1998 年 Yan LeCun 在論文 “Gradient-Based Learning Applied to Document Recognition” 中將這種卷積神經網路命名為 “LeNet-5”。LeNet 已經包含了現在卷積神經網路中的卷積層,池化層,全連線層,已經具備了卷積神經網路必須的基本元件。

Gradient-Based Learning Applied to Document Recognition
http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=726791

Architecture of LeNet-5 (Convolutional Neural Networks) for digit recognition

資料處理

同卷積神經網路中的 MNIST 資料集處理方法。

TensorFlow 卷積神經網路手寫數字識別資料集介紹

http://www.tensorflownews.com/2018/03/26/tensorflow-mnist/

模型實現

經典的卷積神經網路,TensorFlow 官方已經實現,並且封裝在了 tensorflow 庫中,以下內容擷取自 TensorFlow 官方 Github。

models/research/slim/nets/lenet.py
https://github.com/tensorflow/models/blob/master/research/slim/nets/lenet.py

import tensorflow as tf

slim = tf.contrib.slim


def lenet(images, num_classes=10, is_training=False,
          dropout_keep_prob=0.5,
          prediction_fn=slim.softmax,
          scope=`LeNet`):
  end_points = {}
  with tf.variable_scope(scope, `LeNet`, [images]):
    net = end_points[`conv1`] = slim.conv2d(images, 32, [5, 5], scope=`conv1`)
    net = end_points[`pool1`] = slim.max_pool2d(net, [2, 2], 2, scope=`pool1`)
    net = end_points[`conv2`] = slim.conv2d(net, 64, [5, 5], scope=`conv2`)
    net = end_points[`pool2`] = slim.max_pool2d(net, [2, 2], 2, scope=`pool2`)
    net = slim.flatten(net)
    end_points[`Flatten`] = net

    net = end_points[`fc3`] = slim.fully_connected(net, 1024, scope=`fc3`)
    if not num_classes:
      return net, end_points
    net = end_points[`dropout3`] = slim.dropout(
        net, dropout_keep_prob, is_training=is_training, scope=`dropout3`)
    logits = end_points[`Logits`] = slim.fully_connected(
        net, num_classes, activation_fn=None, scope=`fc4`)

  end_points[`Predictions`] = prediction_fn(logits, scope=`Predictions`)

  return logits, end_points
lenet.default_image_size = 28


def lenet_arg_scope(weight_decay=0.0):
  """Defines the default lenet argument scope.
  Args:
    weight_decay: The weight decay to use for regularizing the model.
  Returns:
    An `arg_scope` to use for the inception v3 model.
  """
  with slim.arg_scope(
      [slim.conv2d, slim.fully_connected],
      weights_regularizer=slim.l2_regularizer(weight_decay),
      weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
      activation_fn=tf.nn.relu) as sc:
    return sc

模型優化

歡迎大家關注我們的網站和系列教程:http://www.tensorflownews.com/,學習更多的機器學習、深度學習的知識!


相關文章