diff --git a/Run_H5_custom_models.ipynb b/Run_H5_custom_models.ipynb new file mode 100644 index 0000000..4d2f01e --- /dev/null +++ b/Run_H5_custom_models.ipynb @@ -0,0 +1,310 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Demo to create, save, load and run the custom built keras models" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Using TensorFlow backend.\n" + ] + } + ], + "source": [ + "# Importing Libraries\n", + "from keras.models import Model\n", + "from keras.layers import Input,Dense,Dropout\n", + "from keras.callbacks import ModelCheckpoint\n", + "from keras import backend as K\n", + "import tensorflow as tf" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "#Loading mnist data\n", + "mnist=tf.keras.datasets.mnist" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n", + "11493376/11490434 [==============================] - 12s 1us/step\n" + ] + } + ], + "source": [ + "(x_train,y_train),(x_test,y_test)=mnist.load_data()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "x_train,x_test=x_train/255.0,x_test/255.0" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating the model" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "WARNING:tensorflow:From c:\\python36\\lib\\site-packages\\tensorflow\\python\\keras\\layers\\core.py:143: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n", + "Instructions for updating:\n", + "Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n", + "WARNING:tensorflow:From c:\\python36\\lib\\site-packages\\tensorflow\\python\\ops\\math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n", + "Instructions for updating:\n", + "Use tf.cast instead.\n", + "Epoch 1/5\n", + "60000/60000 [==============================] - 23s 390us/sample - loss: 102.5759 - acc: 0.0880\n", + "Epoch 2/5\n", + "60000/60000 [==============================] - 21s 350us/sample - loss: 102.5556 - acc: 0.1142\n", + "Epoch 3/5\n", + "60000/60000 [==============================] - 21s 346us/sample - loss: 102.5556 - acc: 0.1277\n", + "Epoch 4/5\n", + "60000/60000 [==============================] - 21s 350us/sample - loss: 102.5556 - acc: 0.1095\n", + "Epoch 5/5\n", + "60000/60000 [==============================] - 21s 347us/sample - loss: 102.5556 - acc: 0.1332\n", + "10000/10000 [==============================] - 1s 77us/sample - loss: 102.3131 - acc: 0.9808\n" + ] + }, + { + "data": { + "text/plain": [ + "[102.31307451171875, 0.9808]" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "model=tf.keras.models.Sequential([\n", + " tf.keras.layers.Flatten(),\n", + " tf.keras.layers.Dense(512,activation=tf.nn.relu),\n", + " tf.keras.layers.Dropout(0.2),\n", + " tf.keras.layers.Dense(10,activation=tf.nn.softmax)\n", + "])\n", + "\n", + "model.compile(optimizer='adam', loss=\"categorical_crossentropy\", metrics=['accuracy'])\n", + "\n", + "model.fit(x_train,y_train,epochs=5)\n", + "model.evaluate(x_test,y_test)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "_________________________________________________________________\n", + "Layer (type) Output Shape Param # \n", + "=================================================================\n", + "flatten_1 (Flatten) multiple 0 \n", + "_________________________________________________________________\n", + "dense_2 (Dense) multiple 401920 \n", + "_________________________________________________________________\n", + "dropout_1 (Dropout) multiple 0 \n", + "_________________________________________________________________\n", + "dense_3 (Dense) multiple 5130 \n", + "=================================================================\n", + "Total params: 407,050\n", + "Trainable params: 407,050\n", + "Non-trainable params: 0\n", + "_________________________________________________________________\n" + ] + } + ], + "source": [ + "model.summary()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Storing the model architecture and its weight" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Saved model to disk\n" + ] + } + ], + "source": [ + "\n", + "model_json = model.to_json()\n", + "\n", + "# Write the file name of the model\n", + "\n", + "with open(\"mnist.json\", \"w\") as json_file:\n", + " json_file.write(model_json)\n", + " \n", + "# serialize weights to HDF5\n", + "# Write the file name of the weights\n", + "\n", + "model.save_weights(\"mnist.h5\")\n", + "print(\"Saved model to disk\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Loading the model and running it." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Loaded model from disk\n" + ] + } + ], + "source": [ + "\n", + "import time\n", + "import keras\n", + "from keras.models import load_model\n", + "from tensorflow.keras.models import model_from_json\n", + "import json\n", + "\n", + "import pandas as pd\n", + "\n", + "json_file = open('mnist.json', 'r')\n", + "\n", + "loaded_model_json = json_file.read()\n", + "json_file.close()\n", + "loaded_model = model_from_json(loaded_model_json)\n", + "loaded_model.load_weights(\"mnist.h5\")\n", + "print(\"Loaded model from disk\")\n", + "\n", + "#prediction = loaded_model.predict(p)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Doing the prediction" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "prediction = loaded_model.predict(x_test)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [], + "source": [ + "loaded_model.compile(optimizer='adam', loss=\"categorical_crossentropy\", metrics=['accuracy'])" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10000/10000 [==============================] - 1s 79us/sample - loss: 102.3131 - acc: 0.9808\n", + "Accuracy: 98.08\n" + ] + } + ], + "source": [ + "_, accuracy = loaded_model.evaluate(x_test, y_test)\n", + "print('Accuracy: %.2f' % (accuracy*100))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}