{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "1vuQoR42AF_N", "outputId": "5c473da6-0bf9-42ac-8e7a-993d4522623f" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n", "11490434/11490434 [==============================] - 0s 0us/step\n", "Epoch 1/10\n", "468/468 [==============================] - 100s 210ms/step - loss: 0.3633 - accuracy: 0.8851 - val_loss: 0.0526 - val_accuracy: 0.9829\n", "Epoch 2/10\n", "468/468 [==============================] - 96s 204ms/step - loss: 0.1228 - accuracy: 0.9621 - val_loss: 0.0363 - val_accuracy: 0.9879\n", "Epoch 3/10\n", "468/468 [==============================] - 98s 209ms/step - loss: 0.0898 - accuracy: 0.9730 - val_loss: 0.0298 - val_accuracy: 0.9896\n", "Epoch 4/10\n", "468/468 [==============================] - 99s 210ms/step - loss: 0.0719 - accuracy: 0.9786 - val_loss: 0.0198 - val_accuracy: 0.9935\n", "Epoch 5/10\n", "468/468 [==============================] - 100s 213ms/step - loss: 0.0680 - accuracy: 0.9791 - val_loss: 0.0193 - val_accuracy: 0.9938\n", "Epoch 6/10\n", "468/468 [==============================] - 97s 207ms/step - loss: 0.0590 - accuracy: 0.9816 - val_loss: 0.0175 - val_accuracy: 0.9942\n", "Epoch 7/10\n", "468/468 [==============================] - 101s 216ms/step - loss: 0.0547 - accuracy: 0.9830 - val_loss: 0.0181 - val_accuracy: 0.9941\n", "Epoch 8/10\n", "468/468 [==============================] - 101s 215ms/step - loss: 0.0511 - accuracy: 0.9845 - val_loss: 0.0155 - val_accuracy: 0.9946\n", "Epoch 9/10\n", "468/468 [==============================] - 100s 214ms/step - loss: 0.0476 - accuracy: 0.9858 - val_loss: 0.0188 - val_accuracy: 0.9937\n", "Epoch 10/10\n", "468/468 [==============================] - 100s 213ms/step - loss: 0.0444 - accuracy: 0.9858 - val_loss: 0.0182 - val_accuracy: 0.9948\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": {}, "execution_count": 2 } ], "source": [ "import tensorflow as tf\n", "import numpy as np\n", "\n", "# Load the pre prepared data from TensofFlow\n", "(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()\n", "\n", "# Preprocess the data\n", "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", "x_train = x_train.astype('float32')\n", "x_test = x_test.astype('float32')\n", "x_train /= 255\n", "x_test /= 255\n", "\n", "# Define a CNN model with data augmentation\n", "model = tf.keras.models.Sequential([\n", " tf.keras.layers.Conv2D(64, (3, 3), activation=\"relu\", input_shape=(28, 28, 1)),\n", " tf.keras.layers.MaxPooling2D(2, 2),\n", " tf.keras.layers.Conv2D(64, (3, 3), activation=\"relu\"),\n", " tf.keras.layers.MaxPooling2D(2, 2),\n", " tf.keras.layers.Flatten(),\n", " tf.keras.layers.Dense(512, activation=\"relu\"),\n", " # Dropout to avoid from the overfitting\n", " tf.keras.layers.Dropout(0.5),\n", "\n", " # There are 10 different (0-9) output.\n", " # So use 10 units dense layer with softmax activation\n", " tf.keras.layers.Dense(10, activation=\"softmax\"),\n", "])\n", "\n", "# Compile the model\n", "model.compile(optimizer='adam',\n", " loss='sparse_categorical_crossentropy',\n", " metrics=['accuracy'])\n", "\n", "# Data Augmentation\n", "datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n", " rotation_range=10,\n", " width_shift_range=0.1,\n", " height_shift_range=0.1,\n", " shear_range=0.1,\n", " zoom_range=0.1\n", ")\n", "\n", "datagen.fit(x_train)\n", "\n", "# Train the model with data augmentation\n", "model.fit(datagen.flow(x_train, y_train, batch_size=128),\n", " steps_per_epoch=len(x_train) / 128,\n", " epochs=10,\n", " validation_data=(x_test, y_test))\n" ] }, { "cell_type": "code", "source": [ "loss, acc = model.evaluate(x=x_test, y=y_test)\n", "print(\"Loss:\", loss)\n", "print(\"Accuracy:\", acc)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "3O5b1ShcIE7-", "outputId": "6ef12f6b-eaa2-4c85-97da-db2ee7d81b6e" }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "313/313 [==============================] - 4s 13ms/step - loss: 0.0182 - accuracy: 0.9948\n", "Loss: 0.018189573660492897\n", "Accuracy: 0.9947999715805054\n" ] } ] }, { "cell_type": "markdown", "source": [ "# Save the model and model weights to a avaliable path\n" ], "metadata": { "id": "2pNqtozRN5wj" } }, { "cell_type": "code", "source": [ "model.save(\"/content/model/model.h5\")\n", "model.save_weights(\"/content/model/model_weights.h5\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "7Fp3_wYEKMF-", "outputId": "14213539-b8bf-4774-8916-5a0ea28b23a2" }, "execution_count": 4, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.\n", " saving_api.save_model(\n" ] } ] } ] }