{"metadata": {"language_info": {"nbconvert_exporter": "python", "codemirror_mode": {"version": 3, "name": "ipython"}, "mimetype": "text/x-python", "file_extension": ".py", "pygments_lexer": "ipython3", "version": "3.6.1", "name": "python"}, "kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}}, "nbformat_minor": 0, "nbformat": 4, "cells": [{"metadata": {"_uuid": "d5c2d01e63290cfb1b255dbc5ac5096a6586b1b8", "_execution_state": "idle", "collapsed": false}, "outputs": [], "source": "Counting Sea Lions -- Understanding Keras", "execution_count": null, "cell_type": "markdown"}, {"metadata": {"_uuid": "ecba996f4b964efd88e97d7213b6ed095398b642", "_execution_state": "idle", "trusted": false}, "outputs": [], "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": null, "cell_type": "code"}, {"outputs": [], "metadata": {"_uuid": "5ab9bee720250f327938f5e0af91c655f10c4238", "_execution_state": "idle", "trusted": false, "collapsed": false}, "source": "import pip\n\ndef install(package):\n    pip.main(['install', package])\n    \ninstall(\"keras\")", "execution_count": null, "cell_type": "code"}, {"metadata": {"_uuid": "5d2cb570646ee6ddf0538433d9286bd34200ca1b", "_execution_state": "idle", "trusted": false, "collapsed": false}, "outputs": [], "source": "import numpy as np\nimport pandas as pd\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport skimage.feature\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelBinarizer\nimport keras\nfrom keras.models import Sequential, load_model\nfrom keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D, Lambda, Cropping2D\nfrom keras.utils import np_utils\n%matplotlib inline", "execution_count": null, "cell_type": "code"}, {"outputs": [], "metadata": {"_uuid": "1f2ce96d5cf785dd718274015391b1cef69a6d68", "_execution_state": "idle", "collapsed": false}, "source": "\nInitialise variables\n--------------------\n\n", "execution_count": null, "cell_type": "markdown"}, {"outputs": [], "metadata": {"_uuid": "e193251cebdeffc1e8515077d61e01c9443abb63", "_execution_state": "idle", "trusted": false, "collapsed": false}, "source": "classes = [\"adult_males\", \"subadult_males\", \"adult_females\", \"juveniles\", \"pups\"]\n\nfile_names = os.listdir(\"../input/Train/\")\nprint(file_names)\nfile_names = sorted(file_names, key=lambda\n                   item: (int(item.partition('.')[0]) if item[0].isdigit() else float('inf'), item))\n\nfile_names = file_names[0:1]\nprint(file_names)\ncoordinated_df = pd.DataFrame(index = file_names, columns = classes)\ncoordinated_df.head()", "execution_count": null, "cell_type": "code"}, {"outputs": [], "metadata": {"_uuid": "4017ae57cd133149130fe4bf783630506f305256", "_execution_state": "busy", "trusted": false, "collapsed": false}, "source": "for filename in file_names:\n    image_1 = cv2.imread(\"../input/TrainDotted/\" + filename)\n    image_2 = cv2.imread(\"../input/Train/\" + filename)\n    #print(image_1)\n    #taking absolute difference\n    image_3 = cv2.absdiff(image_1, image_2)\n    #print(image_3)\n    \n    #mask out blackened regions in both the images\n    mask_1 = cv2.cvtColor(image_1, cv2.COLOR_BGR2GRAY)\n    mask_1[mask_1 < 20] = 0\n    mask_1[mask_1 > 0] = 255\n    #print(mask_1)\n    \n    mask_2 = cv2.cvtColor(image_2, cv2.COLOR_BGR2GRAY)\n    mask_2[mask_2 < 20] = 0\n    mask_2[mask_2 > 0] = 255\n    #print(type(mask_1))\n    \n    image_3 = cv2.bitwise_or(image_3, image_3, mask=mask_1)\n    image_3 = cv2.bitwise_or(image_3, image_3, mask=mask_2)\n    \n    image_3 = cv2.cvtColor(image_3, cv2.COLOR_BGR2GRAY)\n    blobs = skimage.feature.blob_log(image_3, min_sigma=3, max_sigma=4, num_sigma=1, threshold=0.02)\n    \n    adult_males = []\n    subadult_males = []\n    pups = []\n    juveniles = []\n    adult_females = []\n    #print(blobs[0:5])\n    for blob in blobs:\n        #finding coordinates for each blob\n        y, x, s = blob\n        #getting color of image from the original imgae with coordinates\n        g, b, r = image_1[int(y)][int(x)][:]\n        # decision tree to pick the class of the blob by looking at the color in Train Dotted\n        if r > 200 and g < 50 and b < 50: # RED\n            adult_males.append((int(x),int(y)))        \n        elif r > 200 and g > 200 and b < 50: # MAGENTA\n            subadult_males.append((int(x),int(y)))         \n        elif r < 100 and g < 100 and 150 < b < 200: # GREEN\n            pups.append((int(x),int(y)))\n        elif r < 100 and  100 < g and b < 100: # BLUE\n            juveniles.append((int(x),int(y))) \n        elif r < 150 and g < 50 and b < 100:  # BROWN\n            adult_females.append((int(x),int(y)))\n            \n    coordinated_df['adult_males'][filename] = adult_males\n    coordinated_df['adult_females'][filename] = adult_females\n    coordinated_df['pups'][filename] = adult_males\n    coordinated_df['juveniles'][filename] = adult_males\n    coordinated_df['subadult_males'][filename] = adult_males\ncoordinated_df.head()", "execution_count": null, "cell_type": "code"}]}