{"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":"# Whale Dolphin Species Classification","metadata":{}},{"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)\nfrom tqdm import tqdm\nimport cv2\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.utils import image_dataset_from_directory\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom matplotlib.pyplot import imshow\nimport pathlib\nimport PIL\n\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\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\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":{"execution":{"iopub.status.busy":"2022-02-08T16:00:33.864181Z","iopub.execute_input":"2022-02-08T16:00:33.864853Z","iopub.status.idle":"2022-02-08T16:00:39.853296Z","shell.execute_reply.started":"2022-02-08T16:00:33.864726Z","shell.execute_reply":"2022-02-08T16:00:39.852367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '../input/happy-whale-and-dolphin/train_images/'\ntest_dir = '../input/happy-whale-and-dolphin/train_images/'","metadata":{"execution":{"iopub.status.busy":"2022-02-08T17:28:52.713268Z","iopub.execute_input":"2022-02-08T17:28:52.713943Z","iopub.status.idle":"2022-02-08T17:28:52.7189Z","shell.execute_reply.started":"2022-02-08T17:28:52.713894Z","shell.execute_reply":"2022-02-08T17:28:52.717702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"species = pd.read_csv('../input/happy-whale-and-dolphin/train.csv',usecols = ['image','species'])\nfor i in range(len(species)):\n    if 'dolphin' in species.species[i]:\n        species.species[i] = 'dolphin'\n    else:\n        species.species[i] = 'whale'\n        \ndf = species.copy()","metadata":{"execution":{"iopub.status.busy":"2022-02-08T16:00:39.862917Z","iopub.execute_input":"2022-02-08T16:00:39.863176Z","iopub.status.idle":"2022-02-08T16:00:48.773663Z","shell.execute_reply.started":"2022-02-08T16:00:39.863141Z","shell.execute_reply":"2022-02-08T16:00:48.772861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# img_path = '../input/happy-whale-and-dolphin/train_images/000c3d63069748.jpg'\n# heights = []\n# img = cv2.imread(img_path)\n# for image in os.listdir('../input/happy-whale-and-dolphin/train_images'):\n#     i = cv2.imread(os.path.join('../input/happy-whale-and-dolphin/train_images',image)).shape[0]\n#     heights.append(i)\ndf.loc[df['image'] == '0007c33415ce37.jpg','species'].values[0]\n\n    \n","metadata":{"execution":{"iopub.status.busy":"2022-02-08T17:17:42.125058Z","iopub.execute_input":"2022-02-08T17:17:42.125353Z","iopub.status.idle":"2022-02-08T17:17:42.141286Z","shell.execute_reply.started":"2022-02-08T17:17:42.12532Z","shell.execute_reply":"2022-02-08T17:17:42.140585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = pathlib.Path(train_dir)\ntest_path = pathlib.Path(test_dir)\ntraining_images = []\ntesting_images = []\nfor img_path in list(train_path.glob('*')):\n    img = cv2.imread(str(img_path))\n    if df.loc[df['image'] == img,'species'].values[0] == 'dolphin':\n        class_num = 1\n    else:\n        class_num = 0\n    training_images.append([img,class_num])\n    \n","metadata":{"execution":{"iopub.status.busy":"2022-02-08T17:29:42.228511Z","iopub.execute_input":"2022-02-08T17:29:42.229256Z","iopub.status.idle":"2022-02-08T17:29:42.605516Z","shell.execute_reply.started":"2022-02-08T17:29:42.229211Z","shell.execute_reply":"2022-02-08T17:29:42.604541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"\ntrain_ds = tf.keras.utils.image_dataset_from_directory(\n  train_path,\n  validation_split=0.2,\n  subset=\"training\",\n  seed=123,\n  image_size=(50, 50),\n  batch_size=32)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-08T17:29:03.27801Z","iopub.execute_input":"2022-02-08T17:29:03.27826Z","iopub.status.idle":"2022-02-08T17:29:23.007426Z","shell.execute_reply.started":"2022-02-08T17:29:03.278236Z","shell.execute_reply":"2022-02-08T17:29:23.005894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = []\ny_train = []\nfor features,label in training_images:\n    x_train.append(features)\n    y_train.append(label)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\npickle_out = open(\"X.pickle\",'wb')\npickle.dump(training_images,pickle_out)\npickle_out.close()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pickle_in = open(\"X.pickle\",'rb')\nX = pickle.load(pickle_in)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}