{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "Digit Classification.ipynb", "provenance": [], "authorship_tag": "ABX9TyP3/IRBCx57G89m6QaSm4OT" }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "code", "metadata": { "id": "zimHZN7LMF2s", "colab_type": "code", "colab": {} }, "source": [ "import tensorflow as tf\n", "(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "Dosq-8VRUJk4", "colab_type": "code", "outputId": "a680ffa8-676c-4e84-fa31-446932dd8b4d", "colab": { "base_uri": "https://localhost:8080/", "height": 299 } }, "source": [ "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "image_index = 7777\n", "print(y_train[image_index])\n", "plt.imshow(x_train[image_index], cmap='Greys')" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "8\n" ], "name": "stdout" }, { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": { "tags": [] }, "execution_count": 28 }, { "output_type": "display_data", "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAOTklEQVR4nO3dfYxUZZbH8d9ZHCLpGQ1I0xCHLDjpRM3GZTodYsRM2EycSMcE+UOF6AQTkx61SZg4JktYk0H9h2x2ZjRxJWGUwOrYBDMo/GFGFMcXEh0tkEVAXV+ADIhQYGDANxTO/tEX0mLfp5q69Uaf7yepVNU99dQ9Kf1xq+9TVY+5uwCMfP/U7AYANAZhB4Ig7EAQhB0IgrADQVzQyJ2NHz/ep0yZ0shdAqHs3r1bhw4dsqFqhcJuZtdLeljSKEmPufvS1OOnTJmiUqlUZ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FKy3gPtcy4WuC1a+by580I+1uSOs1sqpmNljRX0vom9PE9ZtaWnTiRmbVJ+oVabynq9ZLmZ7fnS1rXxF6+o1WW8c5bZlxNfu2avvy5uzf8IqlHA2fkP5L0H83oIaevyyT9b3bZ0ezeJPVr4G3dNxo4t3GHpEskbZT0gaQXJY1rod6ekPSOpG0aCNakJvV2rQbeom+TtDW79DT7tUv01ZDXjY/LAkFwgg4IgrADQRB2IAjCDgRB2IEgCDsQBGEHgvh/9T5QU2WpHjQAAAAASUVORK5CYII=\n", "text/plain": [ "
" ] }, "metadata": { "tags": [], "needs_background": "light" } } ] }, { "cell_type": "code", "metadata": { "id": "3yvMzB0JUTUd", "colab_type": "code", "outputId": "5e03c021-6ed4-40d7-ef35-e22f1c8bbce9", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "source": [ "print(type(x_train[7777]))" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "BezMk6DWUmBp", "colab_type": "code", "outputId": "72e48cb3-e77d-41bf-99b7-50f759ab189f", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "source": [ "x_train.shape" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(60000, 28, 28, 1)" ] }, "metadata": { "tags": [] }, "execution_count": 24 } ] }, { "cell_type": "code", "metadata": { "id": "MZ0_cHcpUsoE", "colab_type": "code", "outputId": "95f9bce5-efd2-492a-e290-356b7a41ae0a", "colab": { "base_uri": "https://localhost:8080/", "height": 68 } }, "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)\n", "input_shape = (28, 28, 1)\n", "\n", "x_train = x_train.astype('float32')\n", "x_test = x_test.astype('float32')\n", "\n", "x_train /= 255\n", "x_test /= 255\n", "print('x_train shape:', x_train.shape)\n", "print('Number of images in x_train', x_train.shape[0])\n", "print('Number of images in x_test', x_test.shape[0])" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "x_train shape: (60000, 28, 28, 1)\n", "Number of images in x_train 60000\n", "Number of images in x_test 10000\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "-wfTj5-YXyhL", "colab_type": "code", "outputId": "681db0be-38f1-4052-8511-6dbd2de28335", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "source": [ "from keras.models import Sequential\n", "from keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D\n", "\n", "model = Sequential()\n", "model.add(Conv2D(28, kernel_size=(3,3), input_shape=input_shape))\n", "model.add(MaxPooling2D(pool_size=(2, 2)))\n", "model.add(Flatten())\n", "model.add(Dense(128, activation=tf.nn.relu))\n", "model.add(Dropout(0.2))\n", "model.add(Dense(10,activation=tf.nn.softmax))" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "Using TensorFlow backend.\n" ], "name": "stderr" } ] }, { "cell_type": "code", "metadata": { "id": "ncg-uWsaX4qN", "colab_type": "code", "outputId": "7af6ce96-9197-4926-c51d-b89cfa031599", "colab": { "base_uri": "https://localhost:8080/", "height": 374 } }, "source": [ "model.compile(optimizer='adam', \n", " loss='sparse_categorical_crossentropy', \n", " metrics=['accuracy'])\n", "model.fit(x=x_train,y=y_train, epochs=10)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "Epoch 1/10\n", "60000/60000 [==============================] - 34s 562us/step - loss: 0.2101 - accuracy: 0.9368\n", "Epoch 2/10\n", "60000/60000 [==============================] - 34s 560us/step - loss: 0.0849 - accuracy: 0.9744\n", "Epoch 3/10\n", "60000/60000 [==============================] - 34s 566us/step - loss: 0.0582 - accuracy: 0.9815\n", "Epoch 4/10\n", "60000/60000 [==============================] - 34s 561us/step - loss: 0.0446 - accuracy: 0.9858\n", "Epoch 5/10\n", "60000/60000 [==============================] - 34s 560us/step - loss: 0.0359 - accuracy: 0.9878\n", "Epoch 6/10\n", "60000/60000 [==============================] - 34s 561us/step - loss: 0.0287 - accuracy: 0.9908\n", "Epoch 7/10\n", "60000/60000 [==============================] - 34s 569us/step - loss: 0.0239 - accuracy: 0.9919\n", "Epoch 8/10\n", "60000/60000 [==============================] - 34s 559us/step - loss: 0.0214 - accuracy: 0.9927\n", "Epoch 9/10\n", "60000/60000 [==============================] - 34s 560us/step - loss: 0.0195 - accuracy: 0.9934\n", "Epoch 10/10\n", "60000/60000 [==============================] - 33s 552us/step - loss: 0.0184 - accuracy: 0.9939\n" ], "name": "stdout" }, { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": { "tags": [] }, "execution_count": 8 } ] }, { "cell_type": "code", "metadata": { "id": "8jzedE40X_3F", "colab_type": "code", "outputId": "620c876e-b73f-4d01-9add-710ccf28d86a", "colab": { "base_uri": "https://localhost:8080/", "height": 51 } }, "source": [ "model.evaluate(x_test, y_test)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "10000/10000 [==============================] - 2s 187us/step\n" ], "name": "stdout" }, { "output_type": "execute_result", "data": { "text/plain": [ "[0.06076548418843795, 0.9858999848365784]" ] }, "metadata": { "tags": [] }, "execution_count": 9 } ] }, { "cell_type": "code", "metadata": { "id": "H8TFf3YDbtMs", "colab_type": "code", "outputId": "458fd988-0dc7-4388-dca9-ea21fce37bd5", "colab": { "resources": { "http://localhost:8080/nbextensions/google.colab/files.js": { "data": "Ly8gQ29weXJpZ2h0IDIwMTcgR29vZ2xlIExMQwovLwovLyBMaWNlbnNlZCB1bmRlciB0aGUgQXBhY2hlIExpY2Vuc2UsIFZlcnNpb24gMi4wICh0aGUgIkxpY2Vuc2UiKTsKLy8geW91IG1heSBub3QgdXNlIHRoaXMgZmlsZSBleGNlcHQgaW4gY29tcGxpYW5jZSB3aXRoIHRoZSBMaWNlbnNlLgovLyBZb3UgbWF5IG9idGFpbiBhIGNvcHkgb2YgdGhlIExpY2Vuc2UgYXQKLy8KLy8gICAgICBodHRwOi8vd3d3LmFwYWNoZS5v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Qcm9taXNlLCBjYW5jZWxQcm9taXNlXSksCiAgICByZXNwb25zZTogewogICAgICBhY3Rpb246ICdzdGFydGluZycsCiAgICB9CiAgfTsKCiAgaWYgKCFmaWxlcykgewogICAgcmV0dXJuIHsKICAgICAgcmVzcG9uc2U6IHsKICAgICAgICBhY3Rpb246ICdjb21wbGV0ZScsCiAgICAgIH0KICAgIH07CiAgfQoKICBjYW5jZWwucmVtb3ZlKCk7CgogIC8vIERpc2FibGUgdGhlIGlucHV0IGVsZW1lbnQgc2luY2UgZnVydGhlciBwaWNrcyBhcmUgbm90IGFsbG93ZWQuCiAgaW5wdXRFbGVtZW50LmRpc2FibGVkID0gdHJ1ZTsKCiAgZm9yIChjb25zdCBmaWxlIG9mIGZpbGVzKSB7CiAgICBjb25zdCBsaSA9IGRvY3VtZW50LmNyZWF0ZUVsZW1lbnQoJ2xpJyk7CiAgICBsaS5hcHBlbmQoc3BhbihmaWxlLm5hbWUsIHtmb250V2VpZ2h0OiAnYm9sZCd9KSk7CiAgICBsaS5hcHBlbmQoc3BhbigKICAgICAgICBgKCR7ZmlsZS50eXBlIHx8ICduL2EnfSkgLSAke2ZpbGUuc2l6ZX0gYnl0ZXMsIGAgKwogICAgICAgIGBsYXN0IG1vZGlmaWVkOiAkewogICAgICAgICAgICBmaWxlLmxhc3RNb2RpZmllZERhdGUgPyBmaWxlLmxhc3RNb2RpZmllZERhdGUudG9Mb2NhbGVEYXRlU3RyaW5nKCkgOgogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAnbi9hJ30gLSBgKSk7CiAgICBjb25zdCBwZXJjZW50ID0gc3BhbignMCUgZG9uZScpOwogICAgbGkuYXBwZW5kQ2hpbGQocGVyY2VudCk7CgogICAgb3V0cHV0RWxlbWVudC5hcHBlbmRDaGlsZChsaSk7CgogICAgY29uc3QgZmlsZURhdGFQcm9taXNlID0gbmV3IFByb21pc2UoKHJlc29sdmUpID0+IHsKICAgICAgY29uc3QgcmVhZGVyID0gbmV3IEZpbGVSZWFkZXIoKTsKICAgICAgcmVhZGVyLm9ubG9hZCA9IChlKSA9PiB7CiAgICAgICAgcmVzb2x2ZShlLnRhcmdldC5yZXN1bHQpOwogICAgICB9OwogICAgICByZWFkZXIucmVhZEFzQXJyYXlCdWZmZXIoZmlsZSk7CiAgICB9KTsKICAgIC8vIFdhaXQgZm9yIHRoZSBkYXRhIHRvIGJlIHJlYWR5LgogICAgbGV0IGZpbGVEYXRhID0geWllbGQgewogICAgICBwcm9taXNlOiBmaWxlRGF0YVByb21pc2UsCiAgICAgIHJlc3BvbnNlOiB7CiAgICAgICAgYWN0aW9uOiAnY29udGludWUnLAogICAgICB9CiAgICB9OwoKICAgIC8vIFVzZSBhIGNodW5rZWQgc2VuZGluZyB0byBhdm9pZCBtZXNzYWdlIHNpemUgbGltaXRzLiBTZWUgYi82MjExNTY2MC4KICAgIGxldCBwb3NpdGlvbiA9IDA7CiAgICB3aGlsZSAocG9zaXRpb24gPCBmaWxlRGF0YS5ieXRlTGVuZ3RoKSB7CiAgICAgIGNvbnN0IGxlbmd0aCA9IE1hdGgubWluKGZpbGVEYXRhLmJ5dGVMZW5ndGggLSBwb3NpdGlvbiwgTUFYX1BBWUxPQURfU0laRSk7CiAgICAgIGNvbnN0IGNodW5rID0gbmV3IFVpbnQ4QXJyYXkoZmlsZURhdGEsIHBvc2l0aW9uLCBsZW5ndGgpOwogICAgICBwb3NpdGlvbiArPSBsZW5ndGg7CgogICAgICBjb25zdCBiYXNlNjQgPSBidG9hKFN0cmluZy5mcm9tQ2hhckNvZGUuYXBwbHkobnVsbCwgY2h1bmspKTsKICAgICAgeWllbGQgewogICAgICAgIHJlc3BvbnNlOiB7CiAgICAgICAgICBhY3Rpb246ICdhcHBlbmQnLAogICAgICAgICAgZmlsZTogZmlsZS5uYW1lLAogICAgICAgICAgZGF0YTogYmFzZTY0LAogICAgICAgIH0sCiAgICAgIH07CiAgICAgIHBlcmNlbnQudGV4dENvbnRlbnQgPQogICAgICAgICAgYCR7TWF0aC5yb3VuZCgocG9zaXRpb24gLyBmaWxlRGF0YS5ieXRlTGVuZ3RoKSAqIDEwMCl9JSBkb25lYDsKICAgIH0KICB9CgogIC8vIEFsbCBkb25lLgogIHlpZWxkIHsKICAgIHJlc3BvbnNlOiB7CiAgICAgIGFjdGlvbjogJ2NvbXBsZXRlJywKICAgIH0KICB9Owp9CgpzY29wZS5nb29nbGUgPSBzY29wZS5nb29nbGUgfHwge307CnNjb3BlLmdvb2dsZS5jb2xhYiA9IHNjb3BlLmdvb2dsZS5jb2xhYiB8fCB7fTsKc2NvcGUuZ29vZ2xlLmNvbGFiLl9maWxlcyA9IHsKICBfdXBsb2FkRmlsZXMsCiAgX3VwbG9hZEZpbGVzQ29udGludWUsCn07Cn0pKHNlbGYpOwo=", "ok": true, "headers": [ [ "content-type", "application/javascript" ] ], "status": 200, "status_text": "" } }, "base_uri": "https://localhost:8080/", "height": 74 } }, "source": [ "from google.colab import files\n", "image = files.upload()" ], "execution_count": 0, "outputs": [ { "output_type": "display_data", "data": { "text/html": [ "\n", " \n", " \n", " Upload widget is only available when the cell has been executed in the\n", " current browser session. Please rerun this cell to enable.\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": { "tags": [] } }, { "output_type": "stream", "text": [ "Saving image2.png to image2.png\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "i9WMU9xXcejh", "colab_type": "code", "outputId": "bcbf3244-28f3-47fc-d964-257e35e5267d", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "source": [ "ls" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "image2.png \u001b[0m\u001b[01;34msample_data\u001b[0m/\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "8mYioYp-doC_", "colab_type": "code", "colab": {} }, "source": [ "import matplotlib.image as mpimg\n", "img = mpimg.imread('image2.png')" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "576Gf3fcdK2F", "colab_type": "code", "outputId": "67d924ba-f24e-4ed3-de33-73bbde38542d", "colab": { "base_uri": "https://localhost:8080/", "height": 282 } }, "source": [ "plt.imshow(img.reshape(28, 28),cmap='Greys')" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": { "tags": [] }, "execution_count": 33 }, { "output_type": "display_data", "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAL2klEQVR4nO3dX4hc5R3G8edxNSIxYJIdlhCjsTU3UmiUIQiKWKSiuYl6IeZCUhDihYKCFxV7oXdKqUovihBrMC1WEVTMhVjTIIgXiqOkSTS0sbJi1vyZGPwTRNT468WeyBp3z2zmnJkz2d/3A8PMnHd2z8PokzNz3tl5HRECsPCd1XQAAMNB2YEkKDuQBGUHkqDsQBJnD3Nn4+PjsXr16mHuEkhlcnJSR48e9Wxjlcpu+wZJf5Y0JumvEfFI2eNXr16tTqdTZZcASrTb7TnH+n4Zb3tM0l8k3SjpMkkbbV/W7+8DMFhV3rOvk/RhRHwUEd9Kek7ShnpiAahblbKvlPTJjPsHim0/YXuz7Y7tTrfbrbA7AFUM/Gx8RGyJiHZEtFut1qB3B2AOVco+JWnVjPsXFtsAjKAqZX9H0hrbl9heJOk2SdvriQWgbn1PvUXE97bvlvRPTU+9bY2I92tLBqBWlebZI+IVSa/UlAXAAPFxWSAJyg4kQdmBJCg7kARlB5Kg7EASlB1IgrIDSVB2IAnKDiRB2YEkKDuQBGUHkqDsQBKUHUiCsgNJUHYgCcoOJEHZgSQoO5AEZQeSoOxAEpQdSIKyA0lQdiAJyg4kQdmBJCg7kARlB5KotIorznxffPFF6fh5551XOr5o0aI642CAKpXd9qSkrySdkPR9RLTrC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pl3Xfafrcxh2SlkvaKWm/pH9JWjZC2f4uaY+k3Zou1oqGsl2t6ZfouyXtKi7rm37uSnIN5Xnj47JAEpygA5Kg7EASlB1IgrIDSVB2IAnKDiRB2YEk/g+MVZJ0dp6uRgAAAABJRU5ErkJggg==\n", "text/plain": [ "
" ] }, "metadata": { "tags": [], "needs_background": "light" } } ] }, { "cell_type": "code", "metadata": { "id": "0B9anNRmdimm", "colab_type": "code", "colab": {} }, "source": [ "pred = model.predict(img.reshape(1, 28, 28, 1))" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "4-yMBMJ6hISM", "colab_type": "code", "outputId": "ca61cd9b-3f0f-4a8c-db59-734e0d355474", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "source": [ "print(pred.argmax())" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "1\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "__wEP5HXhrQw", "colab_type": "code", "colab": {} }, "source": [ "model.save('network.h5')" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "_VsVijHHlvpZ", "colab_type": "code", "colab": {} }, "source": [ "files.download('network.h5')" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "jEwKOA6hl5xK", "colab_type": "code", "colab": {} }, "source": [ "" ], "execution_count": 0, "outputs": [] } ] }