{"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":"markdown","source":"# Welcome to Happy Whale notebook!","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"# What is the problem?\n\n**In this competition, we’ll develop a model to match individual whales and dolphins by unique—but often subtle—characteristics of their natural markings. We'll pay particular attention to dorsal fins and lateral body views in image sets from a multi-species dataset built by 28 research institutions. The best submissions will suggest photo-ID solutions that are fast and accurate.**","metadata":{}},{"cell_type":"markdown","source":"# Method of solving\n\n\n1. ~Learn about Dataset\n2. ~EDA & Visualization\n3. ~Data Cleaning\n4. ~Model Selection\n5. ~Prediction & Submission","metadata":{}},{"cell_type":"markdown","source":"**If you like it , please upvote this notebook**","metadata":{}},{"cell_type":"markdown","source":"# Importing Different Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport cv2\nimport os\nfrom PIL import Image\nfrom torchvision import transforms\nfrom matplotlib.pyplot import imshow\nfrom IPython.display import HTML\nimport torch\nimport torch.nn.functional as F\nfrom torchvision import datasets, transforms\nfrom torch import nn\nfrom torch import optim\nfrom torch.utils.data import Dataset, DataLoader\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport pickle\nfrom keras import layers\nfrom keras.models import Sequential\nfrom keras.preprocessing import image\nfrom keras.layers import Input, Dense, Activation, Dropout\nfrom keras.layers import Flatten, BatchNormalization, Conv2D\nfrom keras.layers import MaxPooling2D, AveragePooling2D\nfrom keras.applications.imagenet_utils import preprocess_input\nfrom PIL import Image\nfrom tqdm import tqdm\nimport random as rnd\nimport cv2\n!pip install livelossplot\nfrom livelossplot import PlotLossesKeras\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:45:29.753460Z","iopub.execute_input":"2022-02-21T15:45:29.754292Z","iopub.status.idle":"2022-02-21T15:45:46.134745Z","shell.execute_reply.started":"2022-02-21T15:45:29.754240Z","shell.execute_reply":"2022-02-21T15:45:46.133687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Checking Working Directory","metadata":{}},{"cell_type":"code","source":"#Checking current working directory!\n\ncwd = os.getcwd()\nprint(\"Your current working directory is : \" , cwd)","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:45:46.136792Z","iopub.execute_input":"2022-02-21T15:45:46.137828Z","iopub.status.idle":"2022-02-21T15:45:46.143938Z","shell.execute_reply.started":"2022-02-21T15:45:46.137782Z","shell.execute_reply":"2022-02-21T15:45:46.142943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Print Data Location**","metadata":{}},{"cell_type":"code","source":"print(os.listdir('../input/happy-whale-and-dolphin'))","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:45:46.145803Z","iopub.execute_input":"2022-02-21T15:45:46.146499Z","iopub.status.idle":"2022-02-21T15:45:46.163247Z","shell.execute_reply.started":"2022-02-21T15:45:46.146448Z","shell.execute_reply":"2022-02-21T15:45:46.162196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading data from kernel","metadata":{}},{"cell_type":"code","source":"img_train_path = os.path.abspath('../input/happy-whale-and-dolphin/train_images')\ntrainedData = pd.read_csv('../input/happy-whale-and-dolphin/train.csv')\ntrainedData['path'] = '../input/happy-whale-and-dolphin/train_images/' + trainedData['image']\nimg_test_path = os.path.abspath('../input/happy-whale-and-dolphin/test_images')\ncsv_train_path = os.path.abspath('../input/happy-whale-and-dolphin/train.csv')\ncsv_train_path","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:45:46.165965Z","iopub.execute_input":"2022-02-21T15:45:46.166656Z","iopub.status.idle":"2022-02-21T15:45:46.306328Z","shell.execute_reply.started":"2022-02-21T15:45:46.166599Z","shell.execute_reply":"2022-02-21T15:45:46.305171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(csv_train_path)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:45:46.307772Z","iopub.execute_input":"2022-02-21T15:45:46.308033Z","iopub.status.idle":"2022-02-21T15:45:46.395827Z","shell.execute_reply.started":"2022-02-21T15:45:46.308000Z","shell.execute_reply":"2022-02-21T15:45:46.394810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Statistics of Datasets","metadata":{}},{"cell_type":"code","source":"#Print total counts\nprint('Train samples count: ', len(trainedData))\ntrainedData.columns","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:45:46.397163Z","iopub.execute_input":"2022-02-21T15:45:46.397517Z","iopub.status.idle":"2022-02-21T15:45:46.405030Z","shell.execute_reply.started":"2022-02-21T15:45:46.397386Z","shell.execute_reply":"2022-02-21T15:45:46.404261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Print different species\nprint('Species Count: ',len(trainedData['species'].value_counts()))\ntrainedData['species'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:45:46.406379Z","iopub.execute_input":"2022-02-21T15:45:46.406914Z","iopub.status.idle":"2022-02-21T15:45:46.444241Z","shell.execute_reply.started":"2022-02-21T15:45:46.406852Z","shell.execute_reply":"2022-02-21T15:45:46.442951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Visualization","metadata":{}},{"cell_type":"markdown","source":"**Number with species**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(8,8))\nsns.countplot(data=trainedData, y = 'species',  palette='crest', dodge=False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:45:46.445733Z","iopub.execute_input":"2022-02-21T15:45:46.446252Z","iopub.status.idle":"2022-02-21T15:45:47.010153Z","shell.execute_reply.started":"2022-02-21T15:45:46.446202Z","shell.execute_reply":"2022-02-21T15:45:47.009209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plt.figure(figsize = (15,12))\n#for idx,i in enumerate(trainedData.species.unique()):\n#    plt.subplot(4,7,idx+1)\n#    df = trainedData[trainedData['species'] ==i].reset_index(drop = True)\n  #  image_path = df.loc[rnd.randint(0, len(df))-1,'path']\n#    img = Image.open(image_path)\n#    img = img.resize((224,224))\n#    plt.imshow(img)\n #   plt.axis('off')\n#    plt.title(i)\n#plt.tight_layout()\n#plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:45:47.011563Z","iopub.execute_input":"2022-02-21T15:45:47.011827Z","iopub.status.idle":"2022-02-21T15:45:47.017489Z","shell.execute_reply.started":"2022-02-21T15:45:47.011794Z","shell.execute_reply":"2022-02-21T15:45:47.016334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visualization of Unique Species**","metadata":{}},{"cell_type":"code","source":"def plot_species(df,species_name):\n    plt.figure(figsize = (12,12))\n    species_df = df[df['species'] ==species_name].reset_index(drop = True)\n    plt.suptitle(species_name)\n    for idx,i in enumerate(np.random.choice(species_df['path'],32)):\n        plt.subplot(8,8,idx+1)\n        image_path = i\n        img = Image.open(image_path)\n        img = img.resize((224,224))\n        plt.imshow(img)\n        plt.axis('off')\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:45:47.020710Z","iopub.execute_input":"2022-02-21T15:45:47.020974Z","iopub.status.idle":"2022-02-21T15:45:47.039905Z","shell.execute_reply.started":"2022-02-21T15:45:47.020943Z","shell.execute_reply":"2022-02-21T15:45:47.038737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for species in trainedData['species'].unique():\n    plot_species(trainedData , species)","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:45:47.041579Z","iopub.execute_input":"2022-02-21T15:45:47.042301Z","iopub.status.idle":"2022-02-21T15:48:35.348932Z","shell.execute_reply.started":"2022-02-21T15:45:47.042256Z","shell.execute_reply":"2022-02-21T15:48:35.347699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Individual visualization**","metadata":{}},{"cell_type":"code","source":"def plot_individual(df,individual_id):\n    plt.figure(figsize = (12,12))\n    species_df = df[df['individual_id'] ==individual_id].reset_index(drop = True)\n    plt.suptitle(individual_id)\n    for idx,i in enumerate(np.random.choice(species_df['path'],24)):\n        plt.subplot(8,8,idx+1)\n        image_path = i\n        img = Image.open(image_path)\n        img = img.resize((224,224))\n        plt.imshow(img)\n        plt.axis('off')\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:48:35.350850Z","iopub.execute_input":"2022-02-21T15:48:35.351189Z","iopub.status.idle":"2022-02-21T15:48:35.360988Z","shell.execute_reply.started":"2022-02-21T15:48:35.351148Z","shell.execute_reply":"2022-02-21T15:48:35.360186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numShow = trainedData.individual_id.value_counts().tail(10)\nfor i in numShow.index:\n    plot_individual(trainedData , i)","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:48:35.362378Z","iopub.execute_input":"2022-02-21T15:48:35.362763Z","iopub.status.idle":"2022-02-21T15:49:12.795318Z","shell.execute_reply.started":"2022-02-21T15:48:35.362730Z","shell.execute_reply":"2022-02-21T15:49:12.794099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Image Path Selecting**","metadata":{}},{"cell_type":"code","source":"df['Image_path'] = [os.path.join(img_train_path,whale) for whale in df['image']]\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:49:12.797180Z","iopub.execute_input":"2022-02-21T15:49:12.797520Z","iopub.status.idle":"2022-02-21T15:49:12.923788Z","shell.execute_reply.started":"2022-02-21T15:49:12.797474Z","shell.execute_reply":"2022-02-21T15:49:12.922736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_path_random_whales = np.random.choice(df['Image_path'],5)\nfull_path_random_whales","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:49:12.925817Z","iopub.execute_input":"2022-02-21T15:49:12.926210Z","iopub.status.idle":"2022-02-21T15:49:12.934585Z","shell.execute_reply.started":"2022-02-21T15:49:12.926158Z","shell.execute_reply":"2022-02-21T15:49:12.933525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\nfor whale in full_path_random_whales:\n    img = Image.open(whale)\n    plt.imshow(img)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:49:12.936432Z","iopub.execute_input":"2022-02-21T15:49:12.936796Z","iopub.status.idle":"2022-02-21T15:49:17.474054Z","shell.execute_reply.started":"2022-02-21T15:49:12.936748Z","shell.execute_reply":"2022-02-21T15:49:17.473091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread(full_path_random_whales[0])\nimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\nres = cv2.resize(img, dsize=(128, 128), interpolation=cv2.INTER_CUBIC)\nplt.imshow(res,cmap='gray')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:49:17.475487Z","iopub.execute_input":"2022-02-21T15:49:17.476475Z","iopub.status.idle":"2022-02-21T15:49:17.759346Z","shell.execute_reply.started":"2022-02-21T15:49:17.476415Z","shell.execute_reply":"2022-02-21T15:49:17.758228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Normalization of data","metadata":{}},{"cell_type":"code","source":"normalize = transforms.Normalize(\n   mean=[0.485, 0.456, 0.406],\n   std=[0.229, 0.224, 0.225]\n)\npreprocess = transforms.Compose([\n   transforms.Grayscale(num_output_channels=3),\n   transforms.Resize(128),\n   transforms.CenterCrop(128),\n   transforms.ToTensor(),\n   normalize\n])\nimgs = [Image.open(whale) for whale in full_path_random_whales]\nimgs_tensor = [preprocess(whale) for whale in imgs]","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:49:17.760998Z","iopub.execute_input":"2022-02-21T15:49:17.761354Z","iopub.status.idle":"2022-02-21T15:49:18.597587Z","shell.execute_reply.started":"2022-02-21T15:49:17.761307Z","shell.execute_reply":"2022-02-21T15:49:18.596715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs_tensor[0].shape","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:49:18.598707Z","iopub.execute_input":"2022-02-21T15:49:18.599521Z","iopub.status.idle":"2022-02-21T15:49:18.606663Z","shell.execute_reply.started":"2022-02-21T15:49:18.599473Z","shell.execute_reply":"2022-02-21T15:49:18.605592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = imgs_tensor[0]\nplt.imshow(img[0],cmap='gray')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:49:18.608003Z","iopub.execute_input":"2022-02-21T15:49:18.608382Z","iopub.status.idle":"2022-02-21T15:49:18.830926Z","shell.execute_reply.started":"2022-02-21T15:49:18.608344Z","shell.execute_reply":"2022-02-21T15:49:18.829797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Cleaning!","metadata":{}},{"cell_type":"markdown","source":"**Counting number using ID**","metadata":{}},{"cell_type":"code","source":"df.individual_id.value_counts().head()","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:49:18.832183Z","iopub.execute_input":"2022-02-21T15:49:18.832419Z","iopub.status.idle":"2022-02-21T15:49:18.856665Z","shell.execute_reply.started":"2022-02-21T15:49:18.832390Z","shell.execute_reply":"2022-02-21T15:49:18.855418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dolWhale = df['individual_id'] != 'new_whale'\ndf = df[dolWhale]\ndf.individual_id.value_counts().head()","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:49:18.858312Z","iopub.execute_input":"2022-02-21T15:49:18.858867Z","iopub.status.idle":"2022-02-21T15:49:18.903855Z","shell.execute_reply.started":"2022-02-21T15:49:18.858819Z","shell.execute_reply":"2022-02-21T15:49:18.902709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Null Checking**","metadata":{}},{"cell_type":"code","source":"trainedData.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:49:18.905555Z","iopub.execute_input":"2022-02-21T15:49:18.905796Z","iopub.status.idle":"2022-02-21T15:49:18.937906Z","shell.execute_reply.started":"2022-02-21T15:49:18.905769Z","shell.execute_reply":"2022-02-21T15:49:18.936954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Cleaning & Displaying workflow**","metadata":{}},{"cell_type":"code","source":"print(\"Number of unique species : \", trainedData['species'].nunique())\n\ntrainedData['species'].replace({\n    'bottlenose_dolpin' : 'bottlenose_dolphin',\n    'kiler_whale' : 'killer_whale',\n    'beluga' : 'beluga_whale',\n    'globis' : 'short_finned_pilot_whale',\n    'pilot_whale' : 'short_finned_pilot_whale'\n},inplace =True)\n\nprint('\\nAfter Removing duplicate labels : ')\nprint(\"Total unique species : \", trainedData['species'].nunique())\n\n\ntrainedData['class'] = trainedData['species'].apply(lambda x: x.split('_')[-1])\ntrainedData.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:49:18.939142Z","iopub.execute_input":"2022-02-21T15:49:18.939736Z","iopub.status.idle":"2022-02-21T15:49:19.016235Z","shell.execute_reply.started":"2022-02-21T15:49:18.939695Z","shell.execute_reply":"2022-02-21T15:49:19.015219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Encoding ","metadata":{}},{"cell_type":"code","source":"unique_classes = pd.unique(df['individual_id'])\nencoding = dict(enumerate(unique_classes))\nencoding = {value: key for key, value in encoding.items()}\ndf = df.replace(encoding)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:49:19.017791Z","iopub.execute_input":"2022-02-21T15:49:19.018027Z","iopub.status.idle":"2022-02-21T15:52:25.796417Z","shell.execute_reply.started":"2022-02-21T15:49:19.017998Z","shell.execute_reply":"2022-02-21T15:52:25.795222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**If you like it , please support me by upvoting!**","metadata":{}},{"cell_type":"markdown","source":"# Modeling & Conclusion","metadata":{}},{"cell_type":"code","source":"test = df['Image_path'][:600]\nimgs = [Image.open(whale) for whale in test]\nimgs_tensor = torch.stack([preprocess(whale) for whale in imgs])","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:52:25.798014Z","iopub.execute_input":"2022-02-21T15:52:25.798700Z","iopub.status.idle":"2022-02-21T15:53:56.689147Z","shell.execute_reply.started":"2022-02-21T15:52:25.798650Z","shell.execute_reply":"2022-02-21T15:53:56.688030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = torch.tensor(df['individual_id'][:600].values)\nmax_label = int(max(labels)) +1\nmax_label","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:53:56.690958Z","iopub.execute_input":"2022-02-21T15:53:56.691323Z","iopub.status.idle":"2022-02-21T15:53:56.710701Z","shell.execute_reply.started":"2022-02-21T15:53:56.691276Z","shell.execute_reply":"2022-02-21T15:53:56.709881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Building Model**","metadata":{}},{"cell_type":"code","source":"model = nn.Sequential(nn.Linear(128*128, 256),\n                      nn.Sigmoid(),\n                      nn.Linear(256, 128),\n                      nn.Sigmoid(),\n                      nn.Linear(128, max_label),\n                      nn.LogSoftmax(dim=1))\n\noptimizer = optim.SGD(model.parameters(), lr=0.01)\ncriterion = nn.NLLLoss()\n\nmodel","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:54:12.886638Z","iopub.execute_input":"2022-02-21T15:54:12.887312Z","iopub.status.idle":"2022-02-21T15:54:12.932706Z","shell.execute_reply.started":"2022-02-21T15:54:12.887274Z","shell.execute_reply":"2022-02-21T15:54:12.932038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Finding Loss**","metadata":{}},{"cell_type":"code","source":"#epochs = 5\n#batch_size = 125\n#iters = int(len(imgs_tensor)/batch_size)\n#next_batch = 0\n#for e in range(epochs):\n #   running_loss = 0\n #   next_batch = 0\n  #  for n in range(iters):\n    #    batch_images = imgs_tensor[next_batch:next_batch+batch_size] \n     #   batch_images = batch_images.view(batch_images.shape[0], -1)\n       # batch_labels = labels[next_batch:next_batch+batch_size]       \n       # optimizer.zero_grad()       \n       # output = model(batch_images)\n        #loss = criterion(output, batch_labels)   \n        #loss.backward()\n        #optimizer.step()      \n        #running_loss += loss.item()       \n        #next_batch += batch_size\n        \n    #print(running_loss)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-02-21T15:57:02.291253Z","iopub.execute_input":"2022-02-21T15:57:02.291838Z","iopub.status.idle":"2022-02-21T15:57:02.296546Z","shell.execute_reply.started":"2022-02-21T15:57:02.291782Z","shell.execute_reply":"2022-02-21T15:57:02.295850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Under Construction ! If you like it please upvote this notebook**","metadata":{}},{"cell_type":"markdown","source":"# Continue..\n\n# This notebook will be modified more , stay with me & support me by Upvoting !","metadata":{}},{"cell_type":"markdown","source":"References -\n1.https://www.kaggle.com/jhonatansilva31415/whales-a-simple-guide/notebook","metadata":{}}]}