Skip to content
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

Shashank Submission: Implementing Hand-digit recognition in Keras #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Binary file not shown.
2 changes: 2 additions & 0 deletions [email protected]/Project-Code/Code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Implement hand digit recognition in Keras with dataset at https://keras.io/examples/mnist_dataset_api/ with a simple 3-layer FFNN with fully connected layers (no convolution or other operations)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"#importing required modules\n",
"\n",
"import keras\n",
"import numpy as np\n",
"from keras.datasets import mnist \n",
"from keras.models import Sequential\n",
"from keras.layers import Flatten,Dense\n",
"from keras.utils import normalize\n",
"import tensorflow as tf\n",
"from tensorflow.nn import relu,softmax"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#spliting dataset into training data and teting data\n",
"\n",
"(xtrain,ytrain),(xtest,ytest) = mnist.load_data()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"#scaling the pixel values between 0-1\n",
"xtrain = normalize(xtrain,axis=1)\n",
"xtest = normalize(xtest,axis=1)\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING: Logging before flag parsing goes to stderr.\n",
"W0730 12:34:40.611266 18928 deprecation_wrapper.py:119] From c:\\users\\ravin\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.\n",
"\n",
"W0730 12:34:40.635203 18928 deprecation_wrapper.py:119] From c:\\users\\ravin\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\keras\\optimizers.py:790: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.\n",
"\n"
]
}
],
"source": [
"#defining model and adding layers\n",
"\n",
"model = Sequential()\n",
"model.add(Flatten())\n",
"model.add(Dense(128,activation=relu))\n",
"model.add(Dense(10,activation=softmax))\n",
"\n",
"model.compile(optimizer='adam',\n",
" loss='sparse_categorical_crossentropy',\n",
" metrics=['accuracy'])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"W0730 12:34:40.713134 18928 deprecation_wrapper.py:119] From c:\\users\\ravin\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.\n",
"\n",
"W0730 12:34:40.723106 18928 deprecation_wrapper.py:119] From c:\\users\\ravin\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.\n",
"\n",
"W0730 12:34:40.764033 18928 deprecation_wrapper.py:119] From c:\\users\\ravin\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:3341: The name tf.log is deprecated. Please use tf.math.log instead.\n",
"\n",
"W0730 12:34:40.850765 18928 deprecation.py:323] From c:\\users\\ravin\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\python\\ops\\math_grad.py:1250: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Use tf.where in 2.0, which has the same broadcast rule as np.where\n",
"W0730 12:34:40.877728 18928 deprecation_wrapper.py:119] From c:\\users\\ravin\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:986: The name tf.assign_add is deprecated. Please use tf.compat.v1.assign_add instead.\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/3\n",
"60000/60000 [==============================] - 5s 89us/step - loss: 0.3119 - acc: 0.9132\n",
"Epoch 2/3\n",
"60000/60000 [==============================] - 6s 105us/step - loss: 0.1420 - acc: 0.9582\n",
"Epoch 3/3\n",
"60000/60000 [==============================] - 6s 105us/step - loss: 0.0977 - acc: 0.9708\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x186d59428d0>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# training the model\n",
"model.fit(xtrain,ytrain,epochs=3)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"flatten_1 (Flatten) (None, 784) 0 \n",
"_________________________________________________________________\n",
"dense_1 (Dense) (None, 128) 100480 \n",
"_________________________________________________________________\n",
"dense_2 (Dense) (None, 10) 1290 \n",
"=================================================================\n",
"Total params: 101,770\n",
"Trainable params: 101,770\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"#trained model summary\n",
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10000/10000 [==============================] - 0s 28us/step\n",
"Loss : 0.106505593823269\n",
"Accuracy : 0.9693\n"
]
}
],
"source": [
"# calculating the accuracy and loss\n",
"\n",
"loss,accuracy= model.evaluate(xtest,ytest) \n",
"print(\"Loss :\",loss)\n",
"print(\"Accuracy : \",accuracy)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"#prediction\n",
"\n",
"predict = model.predict([xtest])\n",
"print(np.argmax(predict[1]))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAOIUlEQVR4nO3dX4xc9XnG8efxencNxgY7gHHATYA6aShtnHZjl1JVRKgpoEoGKWnwBaUSknMRVCLloihVFS5R1CSqqgrJKVbcKiVKlSCohNIgC9VCRRYLcrCNSyFgYPGWDTbExjb79+3FHqrF7PxmmTnzx36/H2k1s+edM+f1eJ85M/Obc36OCAE49y3rdQMAuoOwA0kQdiAJwg4kQdiBJJZ3c2NDHo4VWtnNTQKpvKeTmopJL1ZrK+y2b5L095IGJP1TRNxfuv0KrdQW39jOJgEU7I3dDWstv4y3PSDpHyXdLOkaSdtsX9Pq/QHorHbes2+W9FJEvBwRU5J+JGlrPW0BqFs7Yb9c0usLfh+rln2A7e22R22PTmuyjc0BaEc7YV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FvD5yTtq35u6fVjV+irK48bX5cFkuAbdEAShB1IgrADSRB2IAnCDiRB2IEkCDuQxP8BguwyeA+T5x8AAAAASUVORK5CYII=\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"plt.imshow(xtest[1])\n",
"plt.show()"
]
}
],
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
Loading