{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        os.path.join(dirname, filename)\n\n\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-29T11:06:44.678847Z","iopub.execute_input":"2022-07-29T11:06:44.679536Z","iopub.status.idle":"2022-07-29T11:08:12.865398Z","shell.execute_reply.started":"2022-07-29T11:06:44.679489Z","shell.execute_reply":"2022-07-29T11:08:12.864394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Data Exploration**","metadata":{}},{"cell_type":"code","source":"start = \"\\033[1m\"\nend = \"\\033[0;0m\"\npath = \"../input/happy-whale-and-dolphin/\"\ntrain = pd.read_csv(path + \"train.csv\")\nsubmission = pd.read_csv(path + \"sample_submission.csv\")\nplt.figure(figsize = (15,10))\ncat = np.unique(train['species'])\nprint(\"Total Unique Categories \" + start + str(len(cat)) + end)\nsns.countplot(train['species'])\nplt.xticks(rotation = 90)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T11:08:12.867353Z","iopub.execute_input":"2022-07-29T11:08:12.867714Z","iopub.status.idle":"2022-07-29T11:08:13.493598Z","shell.execute_reply.started":"2022-07-29T11:08:12.867680Z","shell.execute_reply":"2022-07-29T11:08:13.492726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def render_categories(df):\n    x = df.groupby('species')\n    plt.figure(figsize = (7,8))\n    for c in cat:\n        image_path = path + 'train_images/'+ x.get_group(c)[:1]['image'].to_string(index = False)\n        print(start + c + end)\n        print(image_path)\n        image_path = cv2.imread(image_path)[... ,::-1]\n        plt.imshow(image_path)\n        plt.show()\n        \nrender_categories(train)    ","metadata":{"execution":{"iopub.status.busy":"2022-07-29T11:08:13.495087Z","iopub.execute_input":"2022-07-29T11:08:13.495455Z","iopub.status.idle":"2022-07-29T11:08:34.687746Z","shell.execute_reply.started":"2022-07-29T11:08:13.495410Z","shell.execute_reply":"2022-07-29T11:08:34.686653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['image_path'] =  path + 'train_images/'+ train['image']","metadata":{"execution":{"iopub.status.busy":"2022-07-29T11:08:34.690945Z","iopub.execute_input":"2022-07-29T11:08:34.691862Z","iopub.status.idle":"2022-07-29T11:08:34.705318Z","shell.execute_reply.started":"2022-07-29T11:08:34.691800Z","shell.execute_reply":"2022-07-29T11:08:34.704308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T11:08:34.706599Z","iopub.execute_input":"2022-07-29T11:08:34.707039Z","iopub.status.idle":"2022-07-29T11:08:34.725863Z","shell.execute_reply.started":"2022-07-29T11:08:34.707002Z","shell.execute_reply":"2022-07-29T11:08:34.724878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Data Augmentation**","metadata":{}},{"cell_type":"code","source":"def create_mask(image):\n    image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)\n\n    sensitivity = 35\n    lower_hsv = np.array([60 - sensitivity, 100, 50])\n    upper_hsv = np.array([60 + sensitivity, 255, 255])\n\n    mask = cv2.inRange(image_hsv, lower_hsv, upper_hsv)\n    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11,11))\n    mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)\n    \n    return mask\n\ndef segment(image):\n    mask = create_mask(image)\n    output = cv2.bitwise_and(image, image, mask = mask)\n    return output\n\ndef sharpen_image(image):\n    image_blurred = cv2.GaussianBlur(image, (0, 0), 3)\n    image_sharp = cv2.addWeighted(image, 1.5, image_blurred, -0.5, 0)\n    return image_sharp","metadata":{"execution":{"iopub.status.busy":"2022-07-29T11:08:34.727465Z","iopub.execute_input":"2022-07-29T11:08:34.727935Z","iopub.status.idle":"2022-07-29T11:08:34.738165Z","shell.execute_reply.started":"2022-07-29T11:08:34.727901Z","shell.execute_reply":"2022-07-29T11:08:34.736947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = '../input/happy-whale-and-dolphin/train_images/009642068626f9.jpg'\nimage_path = cv2.imread(sample)\nimage_mask = create_mask(image_path)\nimage_segmented = segment(image_path)\nimage_sharpen = sharpen_image(image_path)\n\n\nfig, axs = plt.subplots(1, 4, figsize=(20, 20))\naxs[0].imshow(image_path)\naxs[1].imshow(image_mask)\naxs[2].imshow(image_segmented)\naxs[3].imshow(image_sharpen)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T11:08:34.739727Z","iopub.execute_input":"2022-07-29T11:08:34.740240Z","iopub.status.idle":"2022-07-29T11:08:35.527636Z","shell.execute_reply.started":"2022-07-29T11:08:34.740205Z","shell.execute_reply":"2022-07-29T11:08:35.526715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nimport random","metadata":{"execution":{"iopub.status.busy":"2022-07-29T11:08:35.528901Z","iopub.execute_input":"2022-07-29T11:08:35.530072Z","iopub.status.idle":"2022-07-29T11:08:40.214644Z","shell.execute_reply.started":"2022-07-29T11:08:35.530035Z","shell.execute_reply":"2022-07-29T11:08:40.213680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = '../input/happy-whale-and-dolphin/train_images/009642068626f9.jpg'\nimg = cv2.imread(sample)\n\nplt.imshow(img)\nplt.show()\n\ndef fill(img, h, w):\n    img = cv2.resize(img, (h, w), cv2.INTER_CUBIC)\n    return img\n        \ndef horizontal_shift(img, ratio=0.0):\n    if ratio > 1 or ratio < 0:\n        print('Value should be less than 1 and greater than 0')\n        return img\n    ratio = random.uniform(-ratio, ratio)\n    h, w = img.shape[:2]\n    to_shift = w*ratio\n    if ratio > 0:\n        img = img[:, :int(w-to_shift), :]\n    if ratio < 0:\n        img = img[:, int(-1*to_shift):, :]\n    img = fill(img, h, w)\n    return img\n\nimg = horizontal_shift(img, 0.7)\nplt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T11:08:40.215915Z","iopub.execute_input":"2022-07-29T11:08:40.216583Z","iopub.status.idle":"2022-07-29T11:08:40.776616Z","shell.execute_reply.started":"2022-07-29T11:08:40.216545Z","shell.execute_reply":"2022-07-29T11:08:40.775713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nfrom numpy import expand_dims\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2022-07-29T11:08:40.779920Z","iopub.execute_input":"2022-07-29T11:08:40.780582Z","iopub.status.idle":"2022-07-29T11:08:40.785890Z","shell.execute_reply.started":"2022-07-29T11:08:40.780543Z","shell.execute_reply":"2022-07-29T11:08:40.784794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_augimages(paths, datagen):\n    plt.figure(figsize = (14,28))\n    plt.suptitle('Augmented Images')\n    \n    midx = 0\n    for path in paths:\n        data = Image.open(path)\n        data = data.resize((224,224))\n        samples = expand_dims(data, 0)\n        it = datagen.flow(samples, batch_size=1)\n    \n        # Show Original Image\n        plt.subplot(10,5, midx+1)\n        plt.imshow(data)\n        plt.axis('off')\n    \n        # Show Augmented Images\n        for idx, i in enumerate(range(4)):\n            midx += 1\n            plt.subplot(10,5, midx+1)\n            \n            batch = it.next()\n            image = batch[0].astype('uint8')\n            plt.imshow(image)\n            plt.axis('off')\n        midx += 1\n    \n    plt.tight_layout()\n    plt.show()\n\n    \ndatagen = ImageDataGenerator(\n    rotation_range=20,\n    zoom_range=0.10,\n    brightness_range=[0.6,1.4],\n    channel_shift_range=0.7,\n    width_shift_range=0.15,\n    height_shift_range=0.15,\n    shear_range=0.15,\n    horizontal_flip=True,\n    fill_mode='nearest'\n) \n\nplot_augimages(np.random.choice(train['image_path'],10), datagen)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T11:52:13.335650Z","iopub.execute_input":"2022-07-29T11:52:13.336317Z","iopub.status.idle":"2022-07-29T11:52:17.506384Z","shell.execute_reply.started":"2022-07-29T11:52:13.336278Z","shell.execute_reply":"2022-07-29T11:52:17.504901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-29T11:52:33.149519Z","iopub.execute_input":"2022-07-29T11:52:33.150169Z","iopub.status.idle":"2022-07-29T11:52:33.171105Z","shell.execute_reply.started":"2022-07-29T11:52:33.150136Z","shell.execute_reply":"2022-07-29T11:52:33.169887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = '../input/happy-whale-and-dolphin/train_images/009642068626f9.jpg'\ndata = Image.open(sample)\ndata = data.resize((224,224))\nsmp = np.expand_dims(data, axis = 0)\nsmp.size\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T11:57:30.829445Z","iopub.execute_input":"2022-07-29T11:57:30.829890Z","iopub.status.idle":"2022-07-29T11:57:30.881058Z","shell.execute_reply.started":"2022-07-29T11:57:30.829851Z","shell.execute_reply":"2022-07-29T11:57:30.880102Z"},"trusted":true},"execution_count":null,"outputs":[]}]}