{"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":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport cv2\nimport os\nfrom tqdm import tqdm\nimport pickle\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory\nimport tensorflow_datasets as tfds\nimport shutil\n\nfrom sklearn.preprocessing import LabelEncoder","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-09T07:18:59.404895Z","iopub.execute_input":"2022-04-09T07:18:59.40555Z","iopub.status.idle":"2022-04-09T07:19:06.880997Z","shell.execute_reply.started":"2022-04-09T07:18:59.405433Z","shell.execute_reply":"2022-04-09T07:19:06.880144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('../input/happy-whale-and-dolphin/train.csv')\nle = LabelEncoder()\ntrain_data['species_encode'] = le.fit_transform(train_data['species'])\ntrain_data.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T07:20:36.173383Z","iopub.execute_input":"2022-04-09T07:20:36.173782Z","iopub.status.idle":"2022-04-09T07:20:36.264736Z","shell.execute_reply.started":"2022-04-09T07:20:36.173745Z","shell.execute_reply":"2022-04-09T07:20:36.263639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# shutil.rmtree(r'train')\n# os.makedirs('train')\n# for species in list(train_data.species.value_counts().reset_index()['index']):\n#     os.makedirs(f'train/{species}')\n#     data = train_data[train_data['species'] == species]\n#     for i in range(len(data)):\n#         row = data.iloc[i]\n#         try:\n#             shutil.copy2(f'../input/happy-whale-and-dolphin/train_images/{row[0]}', f'./train/{species}/{row[0]}')\n#         except:\n#             pass","metadata":{"execution":{"iopub.status.busy":"2022-04-09T07:20:37.748053Z","iopub.execute_input":"2022-04-09T07:20:37.748375Z","iopub.status.idle":"2022-04-09T07:20:37.754037Z","shell.execute_reply.started":"2022-04-09T07:20:37.748332Z","shell.execute_reply":"2022-04-09T07:20:37.75324Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# labels = []\n\n# for root, dirs, files in os.walk('../input/happy-whale-and-dolphin/train_images'):\n#     for file in tqdm(files):\n#         labels.append(int(train_data[train_data['image']==file]['species_encode']))","metadata":{"execution":{"iopub.status.busy":"2022-04-09T07:20:37.988939Z","iopub.execute_input":"2022-04-09T07:20:37.989455Z","iopub.status.idle":"2022-04-09T07:20:37.993565Z","shell.execute_reply.started":"2022-04-09T07:20:37.989417Z","shell.execute_reply":"2022-04-09T07:20:37.992604Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_ds = image_dataset_from_directory(\n#     '/kaggle/input/happy-whale-and-dolphin',\n#     labels='inferred',\n#     #label_mode='int',\n#     color_mode='rgb',\n#     batch_size=32,\n#     image_size=(224,224),\n#     shuffle=False,\n#     seed=13\n# )","metadata":{"execution":{"iopub.status.busy":"2022-04-09T07:20:38.28385Z","iopub.execute_input":"2022-04-09T07:20:38.284413Z","iopub.status.idle":"2022-04-09T07:20:38.289054Z","shell.execute_reply.started":"2022-04-09T07:20:38.284376Z","shell.execute_reply":"2022-04-09T07:20:38.287796Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_SIZE = 75\n\nX = []\ny = []\n\nfor i in tqdm(range(len(train_data))):\n    row = list(train_data.iloc[i])\n    img = cv2.imread(os.path.join('../input/happy-whale-and-dolphin/train_images', row[0]))\n    img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE))\n    y.append(row[-1])\n    X.append(img)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T07:20:40.152012Z","iopub.execute_input":"2022-04-09T07:20:40.152562Z","iopub.status.idle":"2022-04-09T07:20:48.004857Z","shell.execute_reply.started":"2022-04-09T07:20:40.152502Z","shell.execute_reply":"2022-04-09T07:20:48.003863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pickle_out = open(f\"X_{IMAGE_SIZE}_arr.pickle\", \"wb\")\npickle.dump(X, pickle_out)\npickle_out.close()\n\npickle_out = open(f\"y_{IMAGE_SIZE}_arr.pickle\", \"wb\")\npickle.dump(y, pickle_out)\npickle_out.close()","metadata":{"execution":{"iopub.status.busy":"2022-04-09T07:20:49.322308Z","iopub.execute_input":"2022-04-09T07:20:49.322612Z","iopub.status.idle":"2022-04-09T07:20:49.48192Z","shell.execute_reply.started":"2022-04-09T07:20:49.32258Z","shell.execute_reply":"2022-04-09T07:20:49.48124Z"},"trusted":true},"execution_count":null,"outputs":[]}]}