Skip to content

Commit

Permalink
🔥update
Browse files Browse the repository at this point in the history
  • Loading branch information
wmpscc committed Feb 27, 2019
1 parent 8e26fed commit 74a2af0
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 13 deletions.
34 changes: 34 additions & 0 deletions LeNet/LeNet.py
@@ -0,0 +1,34 @@
import tensorflow as tf
from tensorflow._api.v1.keras import layers
from tensorflow.examples.tutorials.mnist import input_data

# 准备数据集
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
X_train = mnist.train.images
y_train = mnist.train.labels
X_test = mnist.test.images
y_test = mnist.test.labels

X_train = X_train.reshape(-1, 28, 28, 1)
X_test = X_test.reshape(-1, 28, 28, 1)

# LeNet模型
model = tf.keras.Sequential()
model.add(layers.Conv2D(filters=6, kernel_size=(5, 5), padding='valid', strides=(1, 1)))
model.add(layers.AvgPool2D(strides=(2, 2), padding='same'))
model.add(layers.Conv2D(filters=16, kernel_size=(5, 5), padding='valid', strides=(1, 1)))
model.add(layers.AvgPool2D(strides=(2, 2), padding='same'))
model.add(layers.Flatten())
model.add(layers.Dense(units=120))
model.add(layers.Dense(units=84))
model.add(layers.Dense(units=10, activation='softmax'))

model.compile(optimizer=tf.train.AdamOptimizer(0.001),
loss=tf.keras.losses.categorical_crossentropy,
metrics=[tf.keras.metrics.categorical_accuracy])

model.fit(X_train, y_train, epochs=10, batch_size=32,
validation_data=(X_test, y_test))

loss, accuracy = model.evaluate(X_test, y_test)
print("loss:%f, accuracy:%f" % (loss, accuracy))
38 changes: 38 additions & 0 deletions LeNet/README.md
@@ -0,0 +1,38 @@
> 注意:论文中使用的输入图片的shape是`32*32*1`,而代码中使用的是`28*28*1`
# LeNet
LeNet是一种典型的卷积神经网络的结构,由Yann LeCun发明。它的网路结构如下图:
![LeNet](http://daweiwong.com/2017/03/07/MNIST%20LeNet-5/LeNet-5-structure.png)

代码实现参考下图结构:
![LeNet model](https://ask.qcloudimg.com/http-save/yehe-1881084/f3xo7y48br.png?imageView2/2/w/1620)
LeNet-5网络是针对灰度图进行训练的,输入图像大小为`32*32*1`

# Result
``` python
Train on 55000 samples, validate on 10000 samples
Epoch 1/10
55000/55000 [==============================] - 6s 112us/step - loss: 0.4190 - categorical_accuracy: 0.8775 - val_loss: 0.3363 - val_categorical_accuracy: 0.9057
Epoch 2/10
55000/55000 [==============================] - 5s 86us/step - loss: 0.3558 - categorical_accuracy: 0.8982 - val_loss: 0.3273 - val_categorical_accuracy: 0.9066
Epoch 3/10
55000/55000 [==============================] - 5s 83us/step - loss: 0.3455 - categorical_accuracy: 0.9012 - val_loss: 0.3228 - val_categorical_accuracy: 0.9058
Epoch 4/10
55000/55000 [==============================] - 5s 82us/step - loss: 0.3394 - categorical_accuracy: 0.9030 - val_loss: 0.3241 - val_categorical_accuracy: 0.9103
Epoch 5/10
55000/55000 [==============================] - 5s 83us/step - loss: 0.3324 - categorical_accuracy: 0.9050 - val_loss: 0.3210 - val_categorical_accuracy: 0.9087
Epoch 6/10
55000/55000 [==============================] - 5s 83us/step - loss: 0.3303 - categorical_accuracy: 0.9046 - val_loss: 0.3125 - val_categorical_accuracy: 0.9119
Epoch 7/10
55000/55000 [==============================] - 5s 83us/step - loss: 0.3265 - categorical_accuracy: 0.9071 - val_loss: 0.3005 - val_categorical_accuracy: 0.9158
Epoch 8/10
55000/55000 [==============================] - 5s 83us/step - loss: 0.3229 - categorical_accuracy: 0.9087 - val_loss: 0.3090 - val_categorical_accuracy: 0.9114
Epoch 9/10
55000/55000 [==============================] - 5s 83us/step - loss: 0.3199 - categorical_accuracy: 0.9092 - val_loss: 0.3102 - val_categorical_accuracy: 0.9128
Epoch 10/10
55000/55000 [==============================] - 5s 83us/step - loss: 0.3189 - categorical_accuracy: 0.9101 - val_loss: 0.2883 - val_categorical_accuracy: 0.9173
10000/10000 [==============================] - 0s 36us/step
loss:0.288287, accuracy:0.917300
```

### 参考文章
- @BookThief:[卷积神经网络 LeNet-5各层参数详解](https://www.jianshu.com/p/ce609f9b5910)
3 changes: 2 additions & 1 deletion MLP/MLP.py
Expand Up @@ -15,8 +15,9 @@
loss=tf.keras.losses.categorical_crossentropy,
metrics=[tf.keras.metrics.categorical_accuracy])


model.fit(mnist.train.images, mnist.train.labels, epochs=10, batch_size=32,
validation_data=(mnist.test.images, mnist.test.labels))

loss, accuracy = model.evaluate(mnist.test.images, mnist.test.labels)
print("loss:%f, accuracy:%f" % (loss, accuracy))

22 changes: 12 additions & 10 deletions MLP/README.md
Expand Up @@ -11,25 +11,27 @@
```python
Train on 55000 samples, validate on 10000 samples
Epoch 1/10
55000/55000 [==============================] - 4s 80us/step - loss: 0.3947 - categorical_accuracy: 0.8772 - val_loss: 0.2011 - val_categorical_accuracy: 0.9406
55000/55000 [==============================] - 4s 77us/step - loss: 0.3974 - categorical_accuracy: 0.8776 - val_loss: 0.2439 - val_categorical_accuracy: 0.9309
Epoch 2/10
55000/55000 [==============================] - 4s 74us/step - loss: 0.1759 - categorical_accuracy: 0.9474 - val_loss: 0.1594 - val_categorical_accuracy: 0.9515
55000/55000 [==============================] - 3s 62us/step - loss: 0.1687 - categorical_accuracy: 0.9495 - val_loss: 0.1498 - val_categorical_accuracy: 0.9557
Epoch 3/10
55000/55000 [==============================] - 4s 68us/step - loss: 0.1340 - categorical_accuracy: 0.9598 - val_loss: 0.1454 - val_categorical_accuracy: 0.9573
55000/55000 [==============================] - 3s 62us/step - loss: 0.1315 - categorical_accuracy: 0.9602 - val_loss: 0.1331 - val_categorical_accuracy: 0.9607
Epoch 4/10
55000/55000 [==============================] - 4s 65us/step - loss: 0.1109 - categorical_accuracy: 0.9655 - val_loss: 0.1270 - val_categorical_accuracy: 0.9635
55000/55000 [==============================] - 3s 62us/step - loss: 0.1092 - categorical_accuracy: 0.9675 - val_loss: 0.1478 - val_categorical_accuracy: 0.9576
Epoch 5/10
55000/55000 [==============================] - 3s 63us/step - loss: 0.0942 - categorical_accuracy: 0.9713 - val_loss: 0.1233 - val_categorical_accuracy: 0.9651
55000/55000 [==============================] - 3s 62us/step - loss: 0.0938 - categorical_accuracy: 0.9714 - val_loss: 0.1253 - val_categorical_accuracy: 0.9636
Epoch 6/10
55000/55000 [==============================] - 4s 64us/step - loss: 0.0825 - categorical_accuracy: 0.9744 - val_loss: 0.1190 - val_categorical_accuracy: 0.9667
55000/55000 [==============================] - 4s 64us/step - loss: 0.0823 - categorical_accuracy: 0.9748 - val_loss: 0.1307 - val_categorical_accuracy: 0.9623
Epoch 7/10
55000/55000 [==============================] - 3s 63us/step - loss: 0.0717 - categorical_accuracy: 0.9774 - val_loss: 0.1205 - val_categorical_accuracy: 0.9668
55000/55000 [==============================] - 4s 71us/step - loss: 0.0721 - categorical_accuracy: 0.9773 - val_loss: 0.1364 - val_categorical_accuracy: 0.9638
Epoch 8/10
55000/55000 [==============================] - 3s 61us/step - loss: 0.0630 - categorical_accuracy: 0.9801 - val_loss: 0.1256 - val_categorical_accuracy: 0.9662
55000/55000 [==============================] - 3s 62us/step - loss: 0.0627 - categorical_accuracy: 0.9804 - val_loss: 0.1169 - val_categorical_accuracy: 0.9664
Epoch 9/10
55000/55000 [==============================] - 3s 62us/step - loss: 0.0573 - categorical_accuracy: 0.9813 - val_loss: 0.1226 - val_categorical_accuracy: 0.9676
55000/55000 [==============================] - 4s 64us/step - loss: 0.0547 - categorical_accuracy: 0.9830 - val_loss: 0.1265 - val_categorical_accuracy: 0.9668
Epoch 10/10
55000/55000 [==============================] - 3s 61us/step - loss: 0.0519 - categorical_accuracy: 0.9837 - val_loss: 0.1188 - val_categorical_accuracy: 0.9691
55000/55000 [==============================] - 4s 69us/step - loss: 0.0504 - categorical_accuracy: 0.9840 - val_loss: 0.1222 - val_categorical_accuracy: 0.9697
10000/10000 [==============================] - 0s 30us/step
loss:0.122211, accuracy:0.969700
```

# tf.keras
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -3,7 +3,7 @@
> 本项目目的是使用TF高级API实现从2012年LeNet到2016年的Inception ResNet神经网络。
# Image Classification
- ### Multi-Layer Perceptron
- ### [Multi-Layer Perceptron](https://github.com/wmpscc/CNN-Series-Getting-Started-and-TensorFlow-Implementation/blob/master/MLP/README.md)
- ### LeNet
- ### AlexNet
- ### VGG
Expand All @@ -13,4 +13,4 @@
- ### Inception ResNet


> 实现方法可能与原始论文有出入,如有错误欢迎指正!
> 实现方法可能与原始论文有出入,如有错误欢迎指正!

0 comments on commit 74a2af0

Please sign in to comment.