Skip to content

Instantly share code, notes, and snippets.

@ia35
Created June 2, 2020 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ia35/05839fe6edfe0b7ece7212577a2e3d4b to your computer and use it in GitHub Desktop.
Save ia35/05839fe6edfe0b7ece7212577a2e3d4b to your computer and use it in GitHub Desktop.
MNIST_Conv2D.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "MNIST_Conv2D.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyOpT8gT3jbA8dT5M0c37yes",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/ia35/05839fe6edfe0b7ece7212577a2e3d4b/mnist_conv2d.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "v6npsJ2jRWZg",
"colab_type": "text"
},
"source": [
"[![](http://bec552ebfe.url-de-test.ws/ml/buttonBackProp.png)](https://www.backprop.fr)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ivVc2pFWRbKX",
"colab_type": "text"
},
"source": [
"[![](https://raw.githubusercontent.com/BackProp-fr/meetup/master/images/LogoBackPropTranspSmall.png)](https://www.backprop.fr)\n",
"Le logo BackProp est présenté chaque fois qu'un commentaire doit être signalé"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1p5HDASMRf4W",
"colab_type": "text"
},
"source": [
"## <font color=\"teal\">Références</font>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "c7Cde7i4RkoU",
"colab_type": "text"
},
"source": [
"- § MNIST Classification with Eager Execution - Livre **Advanced Applied Deep Learning** de: Umberto Michelucci\n",
"\n",
"- Source [Code](https://github.com/Apress/advanced-applied-deep-learning) for 'Advanced Applied Deep Learning' by Umberto Michelucci (^1)\n",
"\n",
"- Source [Code](https://github.com/Apress/applied-deep-learning) for 'Applied Deep Learning' by Umberto Michelucci (^2)\n",
"\n",
"- How to Develop a CNN for MNIST [Handwritten](https://machinelearningmastery.com/how-to-develop-a-convolutional-neural-network-from-scratch-for-mnist-handwritten-digit-classification/) Digit Classification\n",
"\n",
"- [Ensure](https://tensorflow.backprop.fr/build-and-train-neural-network-models-using-tensorflow-2-x/ensure-that-inputs-to-a-model-are-in-the-correct-shape/) that inputs to a model are in the correct shape\n",
"\n",
"- - [CNS1](https://colab.research.google.com/drive/1Er19AXtk2heJQGbN3QmynKXFlkmmrZiT?usp=sharing)-MNIST-Classification.ipynb"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "43-Yg4RvRo3G",
"colab_type": "text"
},
"source": [
"## <font color=\"teal\">Imports</font>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "YNTyamguBidZ",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 54
},
"outputId": "000594d0-0ad9-4519-bb96-15ab01e22ebb"
},
"source": [
"import tensorflow as tf\n",
"print (tf.__version__)"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"2.2.0\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "uLzL8crtB1lp",
"colab_type": "code",
"colab": {}
},
"source": [
"from tensorflow import keras"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "VGJ-83-VRu_F",
"colab_type": "text"
},
"source": [
"## <font color=\"teal\">Données</font>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "O011-nipBlsx",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 71
},
"outputId": "0450eee1-cbe7-4773-d06b-4107df43f71d"
},
"source": [
"# Lecture du dataset\n",
"mnist = tf.keras.datasets.mnist\n",
"(x_train, y_train), (x_test, y_test) = mnist.load_data()"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n",
"11493376/11490434 [==============================] - 0s 0us/step\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "qfmtnrc3BpGf",
"colab_type": "code",
"colab": {}
},
"source": [
"x_train, x_test = x_train / 255.0, x_test / 255.0"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "9-l9U39EBrbP",
"colab_type": "code",
"colab": {}
},
"source": [
"from functools import partial"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "u73MS_HyBv_3",
"colab_type": "code",
"colab": {}
},
"source": [
"DefaultConv2D = partial(keras.layers.Conv2D, kernel_size=3, activation='relu', padding=\"SAME\")"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "1KxH4u0kFP9R",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 54
},
"outputId": "8446c669-81ab-4393-e35c-98b825f7d0a9"
},
"source": [
"x_train.shape[0]"
],
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"60000"
]
},
"metadata": {
"tags": []
},
"execution_count": 7
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "TYuM5_OhDGit",
"colab_type": "code",
"colab": {}
},
"source": [
"X_train = x_train.reshape(x_train.shape[0], 28, 28, 1)\n",
"X_test = x_test.reshape(x_test.shape[0], 28, 28, 1)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "MRI9LkCiR3eH",
"colab_type": "text"
},
"source": [
"## <font color=\"teal\">Architecture</font>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Go5St6MCBzjX",
"colab_type": "code",
"colab": {}
},
"source": [
"model = keras.models.Sequential([\n",
" DefaultConv2D(filters=64, kernel_size=7, input_shape=[28, 28, 1]),\n",
" keras.layers.MaxPooling2D(pool_size=2),\n",
" DefaultConv2D(filters=128),\n",
" DefaultConv2D(filters=128),\n",
" keras.layers.MaxPooling2D(pool_size=2),\n",
" DefaultConv2D(filters=256),\n",
" DefaultConv2D(filters=256),\n",
" keras.layers.MaxPooling2D(pool_size=2),\n",
" keras.layers.Flatten(),\n",
" keras.layers.Dense(units=128, activation='relu'),\n",
" keras.layers.Dropout(0.5),\n",
" keras.layers.Dense(units=64, activation='relu'),\n",
" keras.layers.Dropout(0.5),\n",
" keras.layers.Dense(units=10, activation='softmax'),\n",
"])"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "kMV0xypNB89F",
"colab_type": "code",
"colab": {}
},
"source": [
"loss_fn = tf.keras.losses.SparseCategoricalCrossentropy()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "AT5QNMttCiqZ",
"colab_type": "code",
"colab": {}
},
"source": [
"model.compile(optimizer='adam',loss=loss_fn, metrics=['accuracy'])"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "OreDiRa8CqdB",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 633
},
"outputId": "beb20d41-99e3-429d-ebf1-1042d1f6c8fe"
},
"source": [
"model.summary()"
],
"execution_count": 13,
"outputs": [
{
"output_type": "stream",
"text": [
"Model: \"sequential\"\n",
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"conv2d (Conv2D) (None, 28, 28, 64) 3200 \n",
"_________________________________________________________________\n",
"max_pooling2d (MaxPooling2D) (None, 14, 14, 64) 0 \n",
"_________________________________________________________________\n",
"conv2d_1 (Conv2D) (None, 14, 14, 128) 73856 \n",
"_________________________________________________________________\n",
"conv2d_2 (Conv2D) (None, 14, 14, 128) 147584 \n",
"_________________________________________________________________\n",
"max_pooling2d_1 (MaxPooling2 (None, 7, 7, 128) 0 \n",
"_________________________________________________________________\n",
"conv2d_3 (Conv2D) (None, 7, 7, 256) 295168 \n",
"_________________________________________________________________\n",
"conv2d_4 (Conv2D) (None, 7, 7, 256) 590080 \n",
"_________________________________________________________________\n",
"max_pooling2d_2 (MaxPooling2 (None, 3, 3, 256) 0 \n",
"_________________________________________________________________\n",
"flatten (Flatten) (None, 2304) 0 \n",
"_________________________________________________________________\n",
"dense (Dense) (None, 128) 295040 \n",
"_________________________________________________________________\n",
"dropout (Dropout) (None, 128) 0 \n",
"_________________________________________________________________\n",
"dense_1 (Dense) (None, 64) 8256 \n",
"_________________________________________________________________\n",
"dropout_1 (Dropout) (None, 64) 0 \n",
"_________________________________________________________________\n",
"dense_2 (Dense) (None, 10) 650 \n",
"=================================================================\n",
"Total params: 1,413,834\n",
"Trainable params: 1,413,834\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "E7q6qF_WCs_K",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 220
},
"outputId": "d960c4aa-ef82-4047-ad28-a923542a9f92"
},
"source": [
"model.fit(X_train, y_train, epochs=5)"
],
"execution_count": 15,
"outputs": [
{
"output_type": "stream",
"text": [
"Epoch 1/5\n",
"1875/1875 [==============================] - 30s 16ms/step - loss: 0.3651 - accuracy: 0.8864\n",
"Epoch 2/5\n",
"1875/1875 [==============================] - 30s 16ms/step - loss: 0.1169 - accuracy: 0.9722\n",
"Epoch 3/5\n",
"1875/1875 [==============================] - 30s 16ms/step - loss: 0.0821 - accuracy: 0.9798\n",
"Epoch 4/5\n",
"1875/1875 [==============================] - 30s 16ms/step - loss: 0.0765 - accuracy: 0.9830\n",
"Epoch 5/5\n",
"1875/1875 [==============================] - 31s 16ms/step - loss: 0.0626 - accuracy: 0.9856\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<tensorflow.python.keras.callbacks.History at 0x7f29f052bba8>"
]
},
"metadata": {
"tags": []
},
"execution_count": 15
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "jUxMxN2aCv5C",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment