{"metadata":{"colab":{"provenance":[],"gpuType":"T4"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"},"accelerator":"GPU","kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"#Imports","metadata":{"id":"4gbQATxYioxs"}},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"id":"2P-vN1aSiudy"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Load the Dataset","metadata":{"id":"5TqpaTq4iLpQ"}},{"cell_type":"code","source":"# Load the pre prepared data from TensofFlow\n(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()","metadata":{"id":"VJMMrRuQiLT5","outputId":"94681eff-1979-4223-fe4b-1a7882cf6198"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Preprocessing","metadata":{"id":"JOWssbqYiY76"}},{"cell_type":"code","source":"# Preprocess the data\nx_train = x_train.reshape(x_train.shape[0], 28, 28, 1)\nx_test = x_test.reshape(x_test.shape[0], 28, 28, 1)\nx_train = x_train.astype(\"float32\")\nx_test = x_test.astype(\"float32\")\nx_train /= 255\nx_test /= 255","metadata":{"id":"cLIJnuw2ijlL"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define a Callback to Avoid Overfitting","metadata":{"id":"-e9nA7Pji6hf"}},{"cell_type":"code","source":"class Callback(tf.keras.callbacks.Callback):\n  def __init__(self, threshold):\n        super(Callback, self).__init__()\n        self.threshold = threshold\n\n  def on_epoch_end(self, epoch, logs=None):\n      if logs is not None and logs.get(\"val_accuracy\") > self.threshold:\n          print(f\"\\nReached {self.threshold * 100}% validation accuracy so cancelling training!\")\n          self.model.stop_training = True","metadata":{"id":"PpP2vuM2jBSW"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create a Model","metadata":{"id":"XuXZouKOjFhO"}},{"cell_type":"code","source":"# Define a model with functional API\ndef build_model():\n  input_layer = tf.keras.Input(shape=(28, 28, 1))\n\n  x = tf.keras.layers.Conv2D(128, (3, 3), activation=\"relu\", padding=\"same\", input_shape=(28, 28, 1))(input_layer)\n  x = tf.keras.layers.MaxPooling2D(2, 2)(x)\n\n  x = tf.keras.layers.Conv2D(128, (3, 3), activation=\"relu\", padding=\"same\")(x)\n  x = tf.keras.layers.MaxPooling2D(2, 2)(x)\n\n  x = tf.keras.layers.Flatten()(x)\n  x = tf.keras.layers.Dense(512, activation=\"relu\")(x)\n  x = tf.keras.layers.Dropout(0.2)(x)\n  x = tf.keras.layers.Dense(128, activation=\"relu\")(x)\n  x = tf.keras.layers.Dropout(0.2)(x)\n\n  output_layer = tf.keras.layers.Dense(10, activation=\"softmax\")(x)\n\n  model = tf.keras.models.Model(inputs=input_layer, outputs=output_layer)\n  return model\n\nmodel = build_model()","metadata":{"id":"py958Nsfjv4j"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Summary","metadata":{"id":"hlsx9WnIDG1t"}},{"cell_type":"code","source":"model.summary()","metadata":{"id":"ZEKy7iLXDOuL","outputId":"0e8cfcef-87b6-4e81-c4f3-73e5c0a4f857"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train the Model","metadata":{"id":"quMqzvO0j6fg"}},{"cell_type":"code","source":"# Data Augmentation\ndatagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rotation_range=10,\n    width_shift_range=0.15,\n    height_shift_range=0.15,\n    shear_range=0.1,\n    zoom_range=0.15,\n)\n\ndatagen.fit(x_train)\n\n# Compile the model\nmodel.compile(optimizer=tf.keras.optimizers.Adam(),\n              loss=\"sparse_categorical_crossentropy\",\n              metrics=[\"accuracy\"])\n\nepochs = 20\ncallbacks = Callback(0.9950)\n\nhistory = model.fit(datagen.flow(x_train, y_train, batch_size=128),\n          steps_per_epoch=len(x_train) // 128,\n          epochs=epochs,\n          verbose=1,\n          validation_data=(x_test, y_test),\n          callbacks=[callbacks])","metadata":{"id":"1vuQoR42AF_N","outputId":"7360b37b-cf83-4504-c954-3f8d12a7aa32"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Show Training Chart","metadata":{"id":"N9TZhZF8jhWU"}},{"cell_type":"code","source":"acc=history.history[\"accuracy\"]\nval_acc=history.history[\"val_accuracy\"]\nloss=history.history[\"loss\"]\nval_loss=history.history[\"val_loss\"]\n\nepochs=range(len(acc)) # Get number of epochs\n\n#------------------------------------------------\n# Plot training and validation accuracy per epoch\n#------------------------------------------------\nplt.plot(epochs, acc, \"r\", label=\"Training accuracy\")\nplt.plot(epochs, val_acc, \"b\", label=\"Validation accuracy\")\nplt.title(\"Training and validation accuracy\")\nplt.legend(loc=0)\nplt.show()\nprint()\n\n#------------------------------------------------\n# Plot training and validation loss per epoch\n#------------------------------------------------\nplt.plot(epochs, loss, \"r\", label=\"Training Loss\")\nplt.plot(epochs, val_loss, \"b\", label=\"Validation Loss\")\nplt.title(\"Training and Validation Loss\")\nplt.legend(loc=0)\nplt.show()","metadata":{"id":"Yb7eUsRiTo6N","outputId":"01db2f85-a908-40ec-ca87-5dde3107b5a4"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluate the Model","metadata":{"id":"W1LAAZHeEeLF"}},{"cell_type":"code","source":"loss, acc = model.evaluate(x=x_test, y=y_test)\nprint(\"Loss:\", loss)\nprint(\"Accuracy:\", acc)","metadata":{"id":"3O5b1ShcIE7-","outputId":"065af96f-86a4-48bf-f7a1-a09aa2d419bb"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save the Model and Model Weights\n","metadata":{"id":"2pNqtozRN5wj"}},{"cell_type":"code","source":"model.save(\"/content/model/model2.h5\")\nmodel.save_weights(\"/content/model/model2_weights.h5\")","metadata":{"id":"7Fp3_wYEKMF-"},"execution_count":null,"outputs":[]}]}