Skip to content

MNIST_CNN_Image Classification #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
239 changes: 239 additions & 0 deletions MNIST_CNN.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyPi1NMWMjcq5xT+iIDJb1ha",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/Ishita-Si/deep-learning-keras-tf-tutorial/blob/master/MNIST_CNN.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "9p34bGWawZHo"
},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow.keras import datasets, layers, models\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"source": [
"from tensorflow import keras\n",
"\n",
"(X_train, y_train) , (X_test, y_test) = keras.datasets.mnist.load_data()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Cm6n7mxd24uI",
"outputId": "d8ea3c36-2392-4d13-8c75-aec126a8d7c9"
},
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n",
"\u001b[1m11490434/11490434\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 0us/step\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"X_train = X_train / 255\n",
"X_test = X_test / 255"
],
"metadata": {
"id": "Y32GFU2724wQ"
},
"execution_count": 10,
"outputs": []
},
{
"cell_type": "code",
"source": [
"X_train_flattened = X_train.reshape(len(X_train), 28*28)\n",
"X_test_flattened = X_test.reshape(len(X_test), 28*28)"
],
"metadata": {
"id": "JhDxM0Ci24z2"
},
"execution_count": 11,
"outputs": []
},
{
"cell_type": "code",
"source": [
"model = keras.Sequential([\n",
" layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),\n",
" layers.MaxPooling2D((2, 2)),\n",
"\n",
" layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),\n",
" layers.MaxPooling2D((2, 2)),\n",
"\n",
" keras.layers.Flatten(),\n",
" keras.layers.Dense(100, activation='relu'),\n",
" keras.layers.Dense(10, activation='softmax')\n",
"])\n",
"\n",
"model.compile(optimizer='adam',\n",
" loss='sparse_categorical_crossentropy',\n",
" metrics=['accuracy'])\n",
"\n",
"model.fit(X_train, y_train, epochs=5)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "xiL69z4y8uUN",
"outputId": "cf022306-a8f9-4740-f650-fd8b5ec24dc5"
},
"execution_count": 15,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"/usr/local/lib/python3.11/dist-packages/keras/src/layers/convolutional/base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
" super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"Epoch 1/5\n",
"\u001b[1m1875/1875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m55s\u001b[0m 28ms/step - accuracy: 0.9072 - loss: 0.3004\n",
"Epoch 2/5\n",
"\u001b[1m1875/1875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m53s\u001b[0m 28ms/step - accuracy: 0.9866 - loss: 0.0442\n",
"Epoch 3/5\n",
"\u001b[1m1875/1875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m81s\u001b[0m 28ms/step - accuracy: 0.9905 - loss: 0.0289\n",
"Epoch 4/5\n",
"\u001b[1m1875/1875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m82s\u001b[0m 28ms/step - accuracy: 0.9941 - loss: 0.0197\n",
"Epoch 5/5\n",
"\u001b[1m1875/1875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m52s\u001b[0m 28ms/step - accuracy: 0.9955 - loss: 0.0144\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<keras.src.callbacks.history.History at 0x7925e7557110>"
]
},
"metadata": {},
"execution_count": 15
}
]
},
{
"cell_type": "code",
"source": [
"from sklearn.metrics import confusion_matrix , classification_report\n",
"import numpy as np\n",
"y_pred = model.predict(X_test)\n",
"y_pred_classes = [np.argmax(element) for element in y_pred]\n",
"\n",
"print(\"Classification Report: \\n\", classification_report(y_test, y_pred_classes))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "7UA-puJ28uQl",
"outputId": "d01495a8-6f7d-4932-84b8-d6f6c4ff9a49"
},
"execution_count": 16,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[1m313/313\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m3s\u001b[0m 9ms/step\n",
"Classification Report: \n",
" precision recall f1-score support\n",
"\n",
" 0 0.99 1.00 0.99 980\n",
" 1 1.00 1.00 1.00 1135\n",
" 2 0.99 0.99 0.99 1032\n",
" 3 0.99 1.00 0.99 1010\n",
" 4 0.99 0.99 0.99 982\n",
" 5 0.99 0.99 0.99 892\n",
" 6 1.00 0.98 0.99 958\n",
" 7 0.98 0.99 0.99 1028\n",
" 8 0.99 0.99 0.99 974\n",
" 9 0.99 0.99 0.99 1009\n",
"\n",
" accuracy 0.99 10000\n",
" macro avg 0.99 0.99 0.99 10000\n",
"weighted avg 0.99 0.99 0.99 10000\n",
"\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"model.evaluate(X_test,y_test)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "M5CcUU1L8uOX",
"outputId": "e9f1e805-5974-4da1-e8e4-8ec18aa01f02"
},
"execution_count": 17,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[1m313/313\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 11ms/step - accuracy: 0.9885 - loss: 0.0343\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[0.026914624497294426, 0.9914000034332275]"
]
},
"metadata": {},
"execution_count": 17
}
]
}
]
}