Skip to content

Commit

Permalink
Adding File
Browse files Browse the repository at this point in the history
  • Loading branch information
ADJason committed Aug 4, 2018
0 parents commit dd4cb52
Show file tree
Hide file tree
Showing 7 changed files with 655 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.ipynb_checkpoints/
/WEIGHTS
309 changes: 309 additions & 0 deletions CNN_LeNet.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,309 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Seed for reproducibility"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"np.random.seed(42)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Import Libraries"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import keras\n",
"import matplotlib.pyplot as plt\n",
"from keras.datasets import mnist\n",
"from keras.models import Sequential, model_from_json\n",
"from keras.layers import Conv2D, Flatten, Dense, MaxPooling2D, Dropout\n",
"from keras.optimizers import SGD, Adam"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Load data"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"(X_train, y_train),(X_test, y_test) = mnist.load_data()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Preprocessing Data"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"X_train = X_train.reshape(60000, 28, 28, 1).astype('float32')\n",
"X_test = X_test.reshape(10000, 28, 28, 1).astype('float32')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"X_train /= 255\n",
"X_test /= 255"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"n_classes = 10"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"y_train = keras.utils.to_categorical(y_train, n_classes)\n",
"y_test = keras.utils.to_categorical(y_test, n_classes)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Defining Neural Network Architecture"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"model = Sequential()\n",
"model.add(Conv2D(32,kernel_size=(3,3),activation='relu', input_shape=(28,28,1)))\n",
"model.add(Conv2D(64,kernel_size=(3,3),activation='relu'))\n",
"model.add(MaxPooling2D(pool_size=(2,2)))\n",
"model.add(Dropout(0.25))\n",
"model.add(Flatten())\n",
"model.add(Dense(128, activation='relu'))\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(n_classes, activation='softmax'))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"conv2d_1 (Conv2D) (None, 26, 26, 32) 320 \n",
"_________________________________________________________________\n",
"conv2d_2 (Conv2D) (None, 24, 24, 64) 18496 \n",
"_________________________________________________________________\n",
"max_pooling2d_1 (MaxPooling2 (None, 12, 12, 64) 0 \n",
"_________________________________________________________________\n",
"dropout_1 (Dropout) (None, 12, 12, 64) 0 \n",
"_________________________________________________________________\n",
"flatten_1 (Flatten) (None, 9216) 0 \n",
"_________________________________________________________________\n",
"dense_1 (Dense) (None, 128) 1179776 \n",
"_________________________________________________________________\n",
"dropout_2 (Dropout) (None, 128) 0 \n",
"_________________________________________________________________\n",
"dense_2 (Dense) (None, 10) 1290 \n",
"=================================================================\n",
"Total params: 1,199,882\n",
"Trainable params: 1,199,882\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"model.summary()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Compile Model"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"model.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['accuracy'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Train Model"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train on 60000 samples, validate on 10000 samples\n",
"Epoch 1/10\n",
"60000/60000 [==============================] - 156s 3ms/step - loss: 0.2395 - acc: 0.9271 - val_loss: 0.0507 - val_acc: 0.9840\n",
"Epoch 2/10\n",
"60000/60000 [==============================] - 161s 3ms/step - loss: 0.0848 - acc: 0.9753 - val_loss: 0.0422 - val_acc: 0.9864\n",
"Epoch 3/10\n",
"60000/60000 [==============================] - 1925s 32ms/step - loss: 0.0638 - acc: 0.9803 - val_loss: 0.0369 - val_acc: 0.9881\n",
"Epoch 4/10\n",
"60000/60000 [==============================] - 160s 3ms/step - loss: 0.0528 - acc: 0.9839 - val_loss: 0.0315 - val_acc: 0.9894\n",
"Epoch 5/10\n",
"60000/60000 [==============================] - 163s 3ms/step - loss: 0.0454 - acc: 0.9855 - val_loss: 0.0287 - val_acc: 0.9912\n",
"Epoch 6/10\n",
"60000/60000 [==============================] - 169s 3ms/step - loss: 0.0397 - acc: 0.9873 - val_loss: 0.0313 - val_acc: 0.9903\n",
"Epoch 7/10\n",
"60000/60000 [==============================] - 163s 3ms/step - loss: 0.0352 - acc: 0.9889 - val_loss: 0.0269 - val_acc: 0.9915\n",
"Epoch 8/10\n",
"60000/60000 [==============================] - 169s 3ms/step - loss: 0.0297 - acc: 0.9905 - val_loss: 0.0250 - val_acc: 0.9929\n",
"Epoch 9/10\n",
"60000/60000 [==============================] - 180s 3ms/step - loss: 0.0299 - acc: 0.9904 - val_loss: 0.0274 - val_acc: 0.9916\n",
"Epoch 10/10\n",
"60000/60000 [==============================] - 178s 3ms/step - loss: 0.0252 - acc: 0.9919 - val_loss: 0.0290 - val_acc: 0.9913\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x7f149fe21278>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.fit(X_train, y_train, batch_size=128, epochs=10, verbose=1, validation_data=[X_test, y_test])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Saving Model data"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"model.save_weights('WEIGHTS/Model_LeNet_weights.h5')\n",
"with open('WEIGHTS/MODEL_ARCHITECTURE.json','w') as m:\n",
" m.write(model.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Loading Model data"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"with open('WEIGHTS/MODEL_ARCHITECTURE.json','r') as m:\n",
" mo = model_from_json(m.read())"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"mo.load_weights('WEIGHTS/Model_LeNet_weights.h5')"
]
},
{
"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
}
73 changes: 73 additions & 0 deletions Digits.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"from keras.datasets import mnist\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"(X_train, y_train),(X_test, y_test) = mnist.load_data()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def getDigit(no):\n",
" index = np.argmax(y_train == no)\n",
" sample_img = X_train[index]\n",
" plt.imsave(f'images/sample_{no}.jpg',sample_img,cmap='gray')"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"getDigit(9)"
]
},
{
"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
}
Loading

0 comments on commit dd4cb52

Please sign in to comment.