forked from aribis369/ML-Starter-Pack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Iceberg Recognition from Satellite Images using Kears Deep NN (aribis…
…369#57) * Create spam_data.csv * Add files via upload * Delete spam_data.csv * Add files via upload * Rename Spam_filter (1).ipynb to Spam_filter.ipynb * Create Spam_Filter * Delete Spam_Filter * Add files via upload * Rename MNIST.ipynb to Digit_Recognition.ipynb * Delete Digit_Recognition.ipynb * Add files via upload * Add files via upload * Rename BiomechanicalPatients.csv.csv to examples-pytorch/BiomechanicalPatients.csv.csv * Rename Pytorch_Tutorial_1.ipynb to examples-pytorch/Pytorch_Tutorial_1.ipynb * Rename BiomechanicalPatients.csv.csv to BiomechanicalPatients.csv * Add Pytorch_Tutorial_2_nn.ipynb * Added Pytorch_CNN.ipynb * Delete Pytorch_CNN.ipynb * Add Pytorch_CNN_updated.ipynb * Delete Pytorch_CNN_updated.ipynb * Add Pytorch_CNN_Tutorial.ipynb * Add Hand_Sign_Pytorch.ipynb * Add Keras_Iceberg.ipynb * Move Keras_Iceberg.ipnb to examples-keras
- Loading branch information
1 parent
cbf4629
commit 298a819
Showing
1 changed file
with
302 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,302 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"metadata": { | ||
"_uuid": "a1b81d4cb2e75a6e2ef83433ca3fd86786a7d072" | ||
}, | ||
"cell_type": "markdown", | ||
"source": "# Iceberg Recognition from Salelite Images Using Keras Deep NN" | ||
}, | ||
{ | ||
"metadata": { | ||
"_uuid": "a728862ccf216b4c8e43178e91a22653206780c8" | ||
}, | ||
"cell_type": "markdown", | ||
"source": "In this notebook I am going to develop a Deep Neural Network to recognise Iceberg in ocean from satelite images . For this image recognition problem I am going to use Keras deep learning features . Keras is high level deep NN library which can be built over a tensorflow or theano backend . Keras provides much more abstraction and flexibilty to built deep networks in a very simple way . " | ||
}, | ||
{ | ||
"metadata": { | ||
"_cell_guid": "4f18337e-24ff-479b-9983-c462cf195701", | ||
"_uuid": "84e8fb50043642e4df9025319e87d0d54c405418", | ||
"trusted": true | ||
}, | ||
"cell_type": "code", | ||
"source": "# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output.", | ||
"execution_count": 1, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"text": "sample_submission.csv\ntest.json\ntrain.json\n\n", | ||
"name": "stdout" | ||
} | ||
] | ||
}, | ||
{ | ||
"metadata": { | ||
"_uuid": "9ad1ccdab3de414f4e42520493646172c7f30157" | ||
}, | ||
"cell_type": "markdown", | ||
"source": "The data for this problem is a image data in json format . We have to read the json data using pd.read_json( ) and we see it has two bands or channels and 1604 images in the training data . The data can be downloaded form this [link](https://www.kaggle.com/c/statoil-iceberg-classifier-challenge/data) . " | ||
}, | ||
{ | ||
"metadata": { | ||
"_cell_guid": "cfc162c8-8dc7-44ec-b50b-8f7cf6e737b1", | ||
"_uuid": "c8c595a2cdfba21f1617a0116bc42babf4de3370", | ||
"trusted": true | ||
}, | ||
"cell_type": "code", | ||
"source": "train_df = pd.read_json('../input/train.json')\ntest_df = pd.read_json('../input/test.json')\ntrain_df.head()", | ||
"execution_count": 2, | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"execution_count": 2, | ||
"data": { | ||
"text/plain": " band_1 \\\n0 [-27.878360999999998, -27.15416, -28.668615, -... \n1 [-12.242375, -14.920304999999999, -14.920363, ... \n2 [-24.603676, -24.603714, -24.871029, -23.15277... \n3 [-22.454607, -23.082819, -23.998013, -23.99805... \n4 [-26.006956, -23.164886, -23.164886, -26.89116... \n\n band_2 id inc_angle \\\n0 [-27.154118, -29.537888, -31.0306, -32.190483,... dfd5f913 43.9239 \n1 [-31.506321, -27.984554, -26.645678, -23.76760... e25388fd 38.1562 \n2 [-24.870956, -24.092632, -20.653963, -19.41104... 58b2aaa0 45.2859 \n3 [-27.889421, -27.519794, -27.165262, -29.10350... 4cfc3a18 43.8306 \n4 [-27.206915, -30.259186, -30.259186, -23.16495... 271f93f4 35.6256 \n\n is_iceberg \n0 0 \n1 0 \n2 1 \n3 0 \n4 0 ", | ||
"text/html": "<div>\n<style>\n .dataframe thead tr:only-child th {\n text-align: right;\n }\n\n .dataframe thead th {\n text-align: left;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>band_1</th>\n <th>band_2</th>\n <th>id</th>\n <th>inc_angle</th>\n <th>is_iceberg</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>[-27.878360999999998, -27.15416, -28.668615, -...</td>\n <td>[-27.154118, -29.537888, -31.0306, -32.190483,...</td>\n <td>dfd5f913</td>\n <td>43.9239</td>\n <td>0</td>\n </tr>\n <tr>\n <th>1</th>\n <td>[-12.242375, -14.920304999999999, -14.920363, ...</td>\n <td>[-31.506321, -27.984554, -26.645678, -23.76760...</td>\n <td>e25388fd</td>\n <td>38.1562</td>\n <td>0</td>\n </tr>\n <tr>\n <th>2</th>\n <td>[-24.603676, -24.603714, -24.871029, -23.15277...</td>\n <td>[-24.870956, -24.092632, -20.653963, -19.41104...</td>\n <td>58b2aaa0</td>\n <td>45.2859</td>\n <td>1</td>\n </tr>\n <tr>\n <th>3</th>\n <td>[-22.454607, -23.082819, -23.998013, -23.99805...</td>\n <td>[-27.889421, -27.519794, -27.165262, -29.10350...</td>\n <td>4cfc3a18</td>\n <td>43.8306</td>\n <td>0</td>\n </tr>\n <tr>\n <th>4</th>\n <td>[-26.006956, -23.164886, -23.164886, -26.89116...</td>\n <td>[-27.206915, -30.259186, -30.259186, -23.16495...</td>\n <td>271f93f4</td>\n <td>35.6256</td>\n <td>0</td>\n </tr>\n </tbody>\n</table>\n</div>" | ||
}, | ||
"metadata": {} | ||
} | ||
] | ||
}, | ||
{ | ||
"metadata": { | ||
"_uuid": "10a60a58d768649672587a8e17cf1a99461f892a" | ||
}, | ||
"cell_type": "markdown", | ||
"source": "We need to preprocess the data to reshape it to make it in shape (1604,75,75,2) . We get the the training data in X_band ." | ||
}, | ||
{ | ||
"metadata": { | ||
"_cell_guid": "c0cf60e9-f6fa-46a8-b209-584cd17e58e3", | ||
"_uuid": "9448794c7a226f1c61c75b31178121782babb47d", | ||
"trusted": true, | ||
"collapsed": true | ||
}, | ||
"cell_type": "code", | ||
"source": "X_band_1=np.array([np.array(band).astype(np.float32).reshape(75, 75) for band in train_df[\"band_1\"]])\nX_band_2=np.array([np.array(band).astype(np.float32).reshape(75, 75) for band in train_df[\"band_2\"]])", | ||
"execution_count": 3, | ||
"outputs": [] | ||
}, | ||
{ | ||
"metadata": { | ||
"_cell_guid": "1359c2ae-d42c-4071-bbc0-077a0a1276e0", | ||
"_uuid": "06e801c07554537979ffc9648de9da34f5bb77c6", | ||
"trusted": true, | ||
"collapsed": true | ||
}, | ||
"cell_type": "code", | ||
"source": "X_band = np.zeros([1604,75,75,2])\nfor t in range(1604):\n X_band[t,:,:,0] = X_band_1[t]\n X_band[t,:,:,1] = X_band_2[t]", | ||
"execution_count": 4, | ||
"outputs": [] | ||
}, | ||
{ | ||
"metadata": { | ||
"_uuid": "7055906ebdc1fe1cc5a8858b7f5be07554f1fd70" | ||
}, | ||
"cell_type": "markdown", | ||
"source": "Now we import all required keras packages . " | ||
}, | ||
{ | ||
"metadata": { | ||
"_cell_guid": "8e29480a-875f-41ab-8c33-baf597221998", | ||
"_uuid": "e1cc3d6f0cd75b6b715f66901f20a7a0e9beb186", | ||
"trusted": true | ||
}, | ||
"cell_type": "code", | ||
"source": "from keras import layers\nfrom keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D\nfrom keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D\nfrom keras.models import Model\nfrom keras.preprocessing import image\nfrom keras.utils import layer_utils\nfrom keras.utils.data_utils import get_file\nfrom keras.applications.imagenet_utils import preprocess_input\n\nfrom IPython.display import SVG\nfrom keras.utils.vis_utils import model_to_dot\nfrom keras.utils import plot_model\n\nimport keras.backend as K\nK.set_image_data_format('channels_last')\nimport matplotlib.pyplot as plt\nfrom matplotlib.pyplot import imshow", | ||
"execution_count": 5, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"text": "Using TensorFlow backend.\n/opt/conda/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6\n return f(*args, **kwds)\n", | ||
"name": "stderr" | ||
} | ||
] | ||
}, | ||
{ | ||
"metadata": { | ||
"_uuid": "95ef7d7e362fffffaacebafebcf70a662650a726" | ||
}, | ||
"cell_type": "markdown", | ||
"source": "The deep NN model in keras can be built in 2 ways . One is using Sequential API and the other is Model API . I am using Model API . So we built the model thatconsistes of 3 layers of convolution layer and two fully connected layer . Each conv layer is made of (Conv2D,BatchNormalization,ReLU,MaxPooling2D) block. " | ||
}, | ||
{ | ||
"metadata": { | ||
"_cell_guid": "cfc32109-12f5-471f-8fad-462cd91ee1ea", | ||
"_uuid": "7eb2c775a705d742e8d27b8d34b29eca2583aa3c", | ||
"collapsed": true, | ||
"trusted": true | ||
}, | ||
"cell_type": "code", | ||
"source": "def Iceberg_model(input_shape):\n X_in = Input(input_shape)\n \n X = Conv2D(10,kernel_size=(5,5),input_shape=(75,75,2))(X_in)\n X = BatchNormalization()(X)\n X = Activation('relu')(X)\n X = MaxPooling2D(pool_size=(2,2))(X)\n \n X = Conv2D(10,kernel_size=(5,5))(X)\n X = BatchNormalization()(X)\n X = Activation('relu')(X)\n X = MaxPooling2D(pool_size=(2,2))(X)\n \n X = Conv2D(10,kernel_size=(5,5))(X)\n X = BatchNormalization()(X)\n X = Activation('relu')(X)\n X = MaxPooling2D(pool_size=(2,2))(X)\n \n X = Flatten()(X)\n X = Dense(50)(X)\n X = Activation('relu')(X)\n \n X = Dense(1)(X)\n X = Activation('sigmoid')(X)\n \n model = Model(inputs=X_in,outputs=X,name='Iceberg_model')\n return model", | ||
"execution_count": 6, | ||
"outputs": [] | ||
}, | ||
{ | ||
"metadata": { | ||
"_uuid": "b1d8e2c16c34c34b6252503e7afab3fb9cd083d2" | ||
}, | ||
"cell_type": "markdown", | ||
"source": "In Keras we have follow 4 simple steps to built a NN model . \n1. create the model .\n2. compile the model .\n3. fit the model on the training data. \n4. evaluate the model on the test data . " | ||
}, | ||
{ | ||
"metadata": { | ||
"_uuid": "be0e3034710115db7a088842375f1735f9a813b5" | ||
}, | ||
"cell_type": "markdown", | ||
"source": "For creating the model we provide the shape of each training data (75,75,2) ." | ||
}, | ||
{ | ||
"metadata": { | ||
"_cell_guid": "5f738ef2-a41b-49d7-a462-6ed8d4f4005f", | ||
"_uuid": "7ae316e5773a2ac422d73e6e38e08080528fc443", | ||
"trusted": true, | ||
"collapsed": true | ||
}, | ||
"cell_type": "code", | ||
"source": "IcebergModel = Iceberg_model((75,75,2))", | ||
"execution_count": 15, | ||
"outputs": [] | ||
}, | ||
{ | ||
"metadata": { | ||
"_uuid": "858018401b56401390458f143ed6508b462e34e7" | ||
}, | ||
"cell_type": "markdown", | ||
"source": "Compile the model using 'adam' optimizer , 'binary_crossentropy' loss and accuracy metric ." | ||
}, | ||
{ | ||
"metadata": { | ||
"_cell_guid": "c263a7ab-8cfd-47ca-993f-13041f44e925", | ||
"_uuid": "ce1bdee690b623e2ffe95b5a67bea56745ba8caf", | ||
"collapsed": true, | ||
"trusted": true | ||
}, | ||
"cell_type": "code", | ||
"source": "IcebergModel.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])", | ||
"execution_count": 16, | ||
"outputs": [] | ||
}, | ||
{ | ||
"metadata": { | ||
"_uuid": "cafe997e2d89ea1ff13b22a63f10dd31117cae83" | ||
}, | ||
"cell_type": "markdown", | ||
"source": "Now we fit the model with the training data having epochs = 20 and random batch_size = 128 . " | ||
}, | ||
{ | ||
"metadata": { | ||
"_cell_guid": "09cd8290-8bc4-4568-b366-03286d2bdc47", | ||
"_uuid": "6c45909f5afa43d1f791f76ac9bdb03a5a31ad7b", | ||
"trusted": true | ||
}, | ||
"cell_type": "code", | ||
"source": "target = train_df['is_iceberg'].values\nIcebergModel.fit(x=X_band,y=target,epochs=20,batch_size=128)", | ||
"execution_count": 17, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"text": "Epoch 1/20\n1604/1604 [==============================] - 13s 8ms/step - loss: 0.6654 - acc: 0.6241\nEpoch 2/20\n1604/1604 [==============================] - 12s 7ms/step - loss: 0.4797 - acc: 0.7550\nEpoch 3/20\n1604/1604 [==============================] - 11s 7ms/step - loss: 0.3634 - acc: 0.8416\nEpoch 4/20\n1604/1604 [==============================] - 11s 7ms/step - loss: 0.3080 - acc: 0.8622\nEpoch 5/20\n1604/1604 [==============================] - 12s 7ms/step - loss: 0.2684 - acc: 0.8840\nEpoch 6/20\n1604/1604 [==============================] - 11s 7ms/step - loss: 0.2278 - acc: 0.9146\nEpoch 7/20\n1604/1604 [==============================] - 12s 7ms/step - loss: 0.1981 - acc: 0.9296\nEpoch 8/20\n1604/1604 [==============================] - 12s 7ms/step - loss: 0.1671 - acc: 0.9458\nEpoch 9/20\n1604/1604 [==============================] - 12s 7ms/step - loss: 0.1536 - acc: 0.9445\nEpoch 10/20\n1604/1604 [==============================] - 12s 7ms/step - loss: 0.1179 - acc: 0.9688\nEpoch 11/20\n1604/1604 [==============================] - 12s 8ms/step - loss: 0.1008 - acc: 0.9713\nEpoch 12/20\n1604/1604 [==============================] - 12s 7ms/step - loss: 0.0986 - acc: 0.9782\nEpoch 13/20\n1604/1604 [==============================] - 11s 7ms/step - loss: 0.0722 - acc: 0.9869\nEpoch 14/20\n1604/1604 [==============================] - 12s 7ms/step - loss: 0.0492 - acc: 0.9963\nEpoch 15/20\n1604/1604 [==============================] - 11s 7ms/step - loss: 0.0361 - acc: 0.9988\nEpoch 16/20\n1604/1604 [==============================] - 13s 8ms/step - loss: 0.0293 - acc: 0.9988\nEpoch 17/20\n1604/1604 [==============================] - 15s 9ms/step - loss: 0.0219 - acc: 1.0000\nEpoch 18/20\n1604/1604 [==============================] - 13s 8ms/step - loss: 0.0166 - acc: 1.0000\nEpoch 19/20\n1604/1604 [==============================] - 12s 7ms/step - loss: 0.0132 - acc: 1.0000\nEpoch 20/20\n1604/1604 [==============================] - 12s 7ms/step - loss: 0.0110 - acc: 1.0000\n", | ||
"name": "stdout" | ||
}, | ||
{ | ||
"output_type": "execute_result", | ||
"execution_count": 17, | ||
"data": { | ||
"text/plain": "<keras.callbacks.History at 0x7fc375d37da0>" | ||
}, | ||
"metadata": {} | ||
} | ||
] | ||
}, | ||
{ | ||
"metadata": { | ||
"_uuid": "b5066ec82f92036add6e54bf9a64442c3b2ca513" | ||
}, | ||
"cell_type": "markdown", | ||
"source": "Evaluating the model on the traning data and it gets a 96.5 % accuarcy . " | ||
}, | ||
{ | ||
"metadata": { | ||
"_cell_guid": "cbbb7dd6-4e55-4eac-931a-91e7ceea04d1", | ||
"_uuid": "2cb5a693c5c4d90f265aafec81cf2f5b91386512", | ||
"trusted": true | ||
}, | ||
"cell_type": "code", | ||
"source": "IcebergModel.evaluate(x=X_band,y=target)", | ||
"execution_count": 18, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"text": "1604/1604 [==============================] - 6s 4ms/step\n", | ||
"name": "stdout" | ||
}, | ||
{ | ||
"output_type": "execute_result", | ||
"execution_count": 18, | ||
"data": { | ||
"text/plain": "[0.090052791218507913, 0.96508728179551118]" | ||
}, | ||
"metadata": {} | ||
} | ||
] | ||
}, | ||
{ | ||
"metadata": { | ||
"_uuid": "73b609c2f543f0893ab9f2b82ed52c6950b5c485" | ||
}, | ||
"cell_type": "markdown", | ||
"source": "Classification report on the training data . " | ||
}, | ||
{ | ||
"metadata": { | ||
"trusted": true, | ||
"_uuid": "2893f9043ead5d440d43e1e67fc028d3949a562d" | ||
}, | ||
"cell_type": "code", | ||
"source": "from sklearn.metrics import classification_report\npred_label = IcebergModel.predict(x=X_band)\npred_label[pred_label>0.5]=1\npred_label[pred_label<=0.5]=0\nprint(classification_report(target,pred_label))", | ||
"execution_count": 25, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"text": " precision recall f1-score support\n\n 0 1.00 0.93 0.97 851\n 1 0.93 1.00 0.96 753\n\navg / total 0.97 0.97 0.97 1604\n\n", | ||
"name": "stdout" | ||
} | ||
] | ||
}, | ||
{ | ||
"metadata": { | ||
"_cell_guid": "0a55a653-c526-4b4f-b477-66d03ce2dfc7", | ||
"_uuid": "9ca13c023d0c9846fc25b30ae80c2b9e1bc5d8dd", | ||
"collapsed": true, | ||
"trusted": true | ||
}, | ||
"cell_type": "code", | ||
"source": "", | ||
"execution_count": null, | ||
"outputs": [] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "python", | ||
"version": "3.6.3", | ||
"mimetype": "text/x-python", | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"pygments_lexer": "ipython3", | ||
"nbconvert_exporter": "python", | ||
"file_extension": ".py" | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 1 | ||
} |