Skip to content

Commit

Permalink
Documented code
Browse files Browse the repository at this point in the history
  • Loading branch information
KD-3 committed Jun 30, 2020
1 parent fb78e0a commit 0f6fc28
Showing 1 changed file with 96 additions and 48 deletions.
144 changes: 96 additions & 48 deletions BTP_Final_model_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"name": "BTP_Final_model_1",
"provenance": [],
"mount_file_id": "1BE7Pf9phr7c6uT5F8eS5O58z6_cCyAWc",
"authorship_tag": "ABX9TyOBihmCPRRu06/hUNznXApK"
"authorship_tag": "ABX9TyN5GyVq51tIK3svlzzR+tP+"
},
"kernelspec": {
"name": "python3",
Expand All @@ -15,6 +15,16 @@
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "x1vkYQ4rZwHe",
"colab_type": "text"
},
"source": [
"## Mounting Google Drive "
]
},
{
"cell_type": "code",
"metadata": {
Expand All @@ -30,7 +40,7 @@
"from google.colab import drive\n",
"drive.mount('/gdrive')"
],
"execution_count": 1,
"execution_count": null,
"outputs": [
{
"output_type": "stream",
Expand All @@ -45,6 +55,16 @@
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "OeOZ6HFRaArK",
"colab_type": "text"
},
"source": [
"Extracting Files to the working directory"
]
},
{
"cell_type": "code",
"metadata": {
Expand All @@ -56,13 +76,12 @@
"import os\n",
"import zipfile\n",
"\n",
"local_zip = '/gdrive/My Drive/NEU-DET_new.zip'\n",
"local_zip = '/content/drive/My Drive/NEU-DET_new.zip'\n",
"zip_ref = zipfile.ZipFile(local_zip, 'r')\n",
"zip_ref.extractall('./')\n",
"zip_ref.close()\n",
"mv "
"zip_ref.close()"
],
"execution_count": 2,
"execution_count": null,
"outputs": []
},
{
Expand All @@ -80,7 +99,7 @@
"train_rolledin_scale_dir = os.path.join('/content/NEU-DET/train/images/rolled-in_scale/')\n",
"train_scratches_dir = os.path.join('/content/NEU-DET/train/images/scratches/')"
],
"execution_count": 3,
"execution_count": null,
"outputs": []
},
{
Expand All @@ -98,7 +117,7 @@
"train_rolledin_scale_names = os.listdir(train_rolledin_scale_dir)\n",
"train_scratches_names = os.listdir(train_scratches_dir)"
],
"execution_count": 4,
"execution_count": null,
"outputs": []
},
{
Expand All @@ -110,7 +129,7 @@
"base_uri": "https://localhost:8080/",
"height": 121
},
"outputId": "6cc1db2d-8e04-456c-adee-36d8e83d8864"
"outputId": "f9330a1a-7fb3-4be2-ed9f-8a048bcbf746"
},
"source": [
"print('total training crazing images:', len(train_crazing_names))\n",
Expand All @@ -120,7 +139,7 @@
"print('total training crazing images:', len(train_rolledin_scale_names))\n",
"print('total training crazing images:', len(train_scratches_names))"
],
"execution_count": 5,
"execution_count": null,
"outputs": [
{
"output_type": "stream",
Expand All @@ -146,9 +165,19 @@
"source": [
"import tensorflow as tf"
],
"execution_count": 6,
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "GjO2bTwraOQp",
"colab_type": "text"
},
"source": [
"Defining a simple covnet model"
]
},
{
"cell_type": "code",
"metadata": {
Expand Down Expand Up @@ -178,7 +207,7 @@
"\n",
"])"
],
"execution_count": 26,
"execution_count": null,
"outputs": []
},
{
Expand All @@ -190,39 +219,39 @@
"base_uri": "https://localhost:8080/",
"height": 503
},
"outputId": "6dbe446b-5f15-4c49-fe1d-922a3426c548"
"outputId": "3cabf74b-eeac-48df-a95d-f5ad2dfca669"
},
"source": [
"model.summary()"
],
"execution_count": 27,
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Model: \"sequential_5\"\n",
"Model: \"sequential_1\"\n",
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"conv2d_25 (Conv2D) (None, 222, 222, 32) 896 \n",
"conv2d_3 (Conv2D) (None, 222, 222, 32) 896 \n",
"_________________________________________________________________\n",
"max_pooling2d_25 (MaxPooling (None, 111, 111, 32) 0 \n",
"max_pooling2d_3 (MaxPooling2 (None, 111, 111, 32) 0 \n",
"_________________________________________________________________\n",
"conv2d_26 (Conv2D) (None, 109, 109, 32) 9248 \n",
"conv2d_4 (Conv2D) (None, 109, 109, 32) 9248 \n",
"_________________________________________________________________\n",
"max_pooling2d_26 (MaxPooling (None, 54, 54, 32) 0 \n",
"max_pooling2d_4 (MaxPooling2 (None, 54, 54, 32) 0 \n",
"_________________________________________________________________\n",
"conv2d_27 (Conv2D) (None, 52, 52, 32) 9248 \n",
"conv2d_5 (Conv2D) (None, 52, 52, 32) 9248 \n",
"_________________________________________________________________\n",
"max_pooling2d_27 (MaxPooling (None, 26, 26, 32) 0 \n",
"max_pooling2d_5 (MaxPooling2 (None, 26, 26, 32) 0 \n",
"_________________________________________________________________\n",
"flatten_5 (Flatten) (None, 21632) 0 \n",
"flatten_1 (Flatten) (None, 21632) 0 \n",
"_________________________________________________________________\n",
"dense_10 (Dense) (None, 512) 11076096 \n",
"dense_3 (Dense) (None, 512) 11076096 \n",
"_________________________________________________________________\n",
"dense_11 (Dense) (None, 512) 262656 \n",
"dense_4 (Dense) (None, 512) 262656 \n",
"_________________________________________________________________\n",
"dense_12 (Dense) (None, 6) 3078 \n",
"dense_5 (Dense) (None, 6) 3078 \n",
"=================================================================\n",
"Total params: 11,361,222\n",
"Trainable params: 11,361,222\n",
Expand All @@ -233,6 +262,16 @@
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BWWIEuCeaU22",
"colab_type": "text"
},
"source": [
"Preprocessing through ImageDataGenerator"
]
},
{
"cell_type": "code",
"metadata": {
Expand All @@ -247,7 +286,7 @@
" optimizer= 'adam',\n",
" metrics=['accuracy'])"
],
"execution_count": 30,
"execution_count": null,
"outputs": []
},
{
Expand All @@ -259,7 +298,7 @@
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "8d5ebbfd-adfc-4184-cacc-130b94e5edbd"
"outputId": "0b8a797b-4610-41b4-c874-08ea90ee57a9"
},
"source": [
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
Expand All @@ -274,7 +313,7 @@
" class_mode = 'categorical'\n",
")"
],
"execution_count": 31,
"execution_count": null,
"outputs": [
{
"output_type": "stream",
Expand All @@ -285,61 +324,70 @@
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7VAJepVTab8F",
"colab_type": "text"
},
"source": [
"Training the model"
]
},
{
"cell_type": "code",
"metadata": {
"id": "bjF8FhBihtyM",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 557
"height": 537
},
"outputId": "e93255d7-73e7-41e2-bca8-01fdff35cf4a"
"outputId": "dbc5fa3e-9848-4c99-ea56-31ef29ed078e"
},
"source": [
"history = model.fit(\n",
" train_generator,\n",
" steps_per_epoch=8,\n",
" epochs=15,\n",
" verbose=1,\n",
" validation_data = test_generator \n",
")"
],
"execution_count": 35,
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Epoch 1/15\n",
"8/8 [==============================] - 2s 236ms/step - loss: 0.2769 - accuracy: 0.8854 - val_loss: 0.2789 - val_accuracy: 0.9056\n",
"8/8 [==============================] - 1s 127ms/step - loss: 2.2959 - accuracy: 0.1813\n",
"Epoch 2/15\n",
"8/8 [==============================] - 2s 208ms/step - loss: 0.3255 - accuracy: 0.8979 - val_loss: 0.3401 - val_accuracy: 0.8889\n",
"8/8 [==============================] - 1s 129ms/step - loss: 1.7832 - accuracy: 0.2104\n",
"Epoch 3/15\n",
"8/8 [==============================] - 2s 210ms/step - loss: 0.4343 - accuracy: 0.8417 - val_loss: 1.1395 - val_accuracy: 0.5056\n",
"8/8 [==============================] - 1s 128ms/step - loss: 1.7197 - accuracy: 0.1917\n",
"Epoch 4/15\n",
"8/8 [==============================] - 2s 210ms/step - loss: 0.4073 - accuracy: 0.8500 - val_loss: 1.2126 - val_accuracy: 0.5694\n",
"8/8 [==============================] - 1s 129ms/step - loss: 1.5803 - accuracy: 0.2729\n",
"Epoch 5/15\n",
"8/8 [==============================] - 2s 209ms/step - loss: 0.2937 - accuracy: 0.9021 - val_loss: 0.5899 - val_accuracy: 0.7861\n",
"8/8 [==============================] - 1s 129ms/step - loss: 1.2978 - accuracy: 0.4250\n",
"Epoch 6/15\n",
"8/8 [==============================] - 2s 210ms/step - loss: 0.2017 - accuracy: 0.9375 - val_loss: 0.5628 - val_accuracy: 0.8028\n",
"8/8 [==============================] - 1s 133ms/step - loss: 1.2945 - accuracy: 0.4250\n",
"Epoch 7/15\n",
"8/8 [==============================] - 2s 212ms/step - loss: 0.2230 - accuracy: 0.9229 - val_loss: 0.5277 - val_accuracy: 0.8250\n",
"8/8 [==============================] - 1s 129ms/step - loss: 1.0890 - accuracy: 0.5625\n",
"Epoch 8/15\n",
"8/8 [==============================] - 2s 209ms/step - loss: 0.2095 - accuracy: 0.9229 - val_loss: 0.4542 - val_accuracy: 0.8083\n",
"8/8 [==============================] - 1s 131ms/step - loss: 0.9111 - accuracy: 0.6458\n",
"Epoch 9/15\n",
"8/8 [==============================] - 2s 220ms/step - loss: 0.1725 - accuracy: 0.9500 - val_loss: 0.2405 - val_accuracy: 0.9111\n",
"8/8 [==============================] - 1s 129ms/step - loss: 0.8483 - accuracy: 0.6562\n",
"Epoch 10/15\n",
"8/8 [==============================] - 2s 208ms/step - loss: 0.1565 - accuracy: 0.9542 - val_loss: 0.3334 - val_accuracy: 0.8667\n",
"8/8 [==============================] - 1s 130ms/step - loss: 0.7037 - accuracy: 0.7333\n",
"Epoch 11/15\n",
"8/8 [==============================] - 2s 210ms/step - loss: 0.1187 - accuracy: 0.9667 - val_loss: 0.6420 - val_accuracy: 0.8111\n",
"8/8 [==============================] - 1s 131ms/step - loss: 0.5909 - accuracy: 0.7646\n",
"Epoch 12/15\n",
"8/8 [==============================] - 2s 209ms/step - loss: 0.1547 - accuracy: 0.9521 - val_loss: 0.6191 - val_accuracy: 0.8111\n",
"8/8 [==============================] - 1s 128ms/step - loss: 0.6766 - accuracy: 0.7583\n",
"Epoch 13/15\n",
"8/8 [==============================] - 2s 208ms/step - loss: 0.0972 - accuracy: 0.9708 - val_loss: 0.3362 - val_accuracy: 0.8806\n",
"8/8 [==============================] - 1s 127ms/step - loss: 0.6987 - accuracy: 0.6958\n",
"Epoch 14/15\n",
"8/8 [==============================] - 2s 231ms/step - loss: 0.1216 - accuracy: 0.9625 - val_loss: 0.4381 - val_accuracy: 0.8333\n",
"8/8 [==============================] - 1s 130ms/step - loss: 0.5066 - accuracy: 0.8333\n",
"Epoch 15/15\n",
"8/8 [==============================] - 2s 210ms/step - loss: 0.1018 - accuracy: 0.9708 - val_loss: 0.3516 - val_accuracy: 0.8667\n"
"8/8 [==============================] - 1s 127ms/step - loss: 0.4498 - accuracy: 0.8396\n"
],
"name": "stdout"
}
Expand All @@ -366,7 +414,7 @@
" class_mode = 'categorical'\n",
")"
],
"execution_count": 33,
"execution_count": null,
"outputs": [
{
"output_type": "stream",
Expand Down

0 comments on commit 0f6fc28

Please sign in to comment.