{"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)\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, shutil\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\n\nimport matplotlib.pyplot as plt\nimport PIL\nimport tensorflow as tf\n\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","scrolled":true,"execution":{"iopub.status.busy":"2022-03-16T21:12:18.650513Z","iopub.execute_input":"2022-03-16T21:12:18.65179Z","iopub.status.idle":"2022-03-16T21:12:18.699291Z","shell.execute_reply.started":"2022-03-16T21:12:18.651732Z","shell.execute_reply":"2022-03-16T21:12:18.697462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/happy-whale-and-dolphin/train.csv', index_col='individual_id')\nimgList = df['image'].tolist()\nspecies = df['species'].tolist()\n\npercentage = 1 / 2\nprint(len(imgList) * percentage)\n\nos.makedirs('../outputs/128/train_images', exist_ok=True)\nos.makedirs('../outputs/128/test_images', exist_ok=True)\n\npath = '../input/jpeg-happywhale-128x128/train_images-128-128/train_images-128-128/'\ny=1\nfor x in range(round(len(imgList) * percentage)):\n    path2 = '../outputs/128/train_images/' + species[x] + r'/'\n    path22 = '../outputs/128/test_images/' + species[x] + r'/'\n    if (x % (round(round(len(imgList) * percentage)/20))) == 0:\n        print(str(y)+': ' + str(x))\n        y++\n    if 2 % 2 == 0:\n        if not (os.path.isdir(path2)):\n            os.makedirs(path2, exist_ok=True)\n        shutil.copy(path + str(imgList[x]), path2)\n    else:\n        if not (os.path.isdir(path22)):\n            os.makedirs(path22, exist_ok=True)\n        shutil.copy(path + str(imgList[x]), path22)\nprint(os.listdir('../outputs/128/train_images'))","metadata":{"execution":{"iopub.status.busy":"2022-03-16T21:12:18.700312Z","iopub.status.idle":"2022-03-16T21:12:18.701227Z","shell.execute_reply.started":"2022-03-16T21:12:18.700991Z","shell.execute_reply":"2022-03-16T21:12:18.701017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pathlib\ndata_dir = pathlib.Path('../outputs/holder/train_images')","metadata":{"execution":{"iopub.status.busy":"2022-03-16T21:12:18.702336Z","iopub.status.idle":"2022-03-16T21:12:18.703222Z","shell.execute_reply.started":"2022-03-16T21:12:18.702972Z","shell.execute_reply":"2022-03-16T21:12:18.702998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\nimg_height = 180\nimg_width = 180\n\ntrain_ds = tf.keras.utils.image_dataset_from_directory(\n  data_dir,\n  validation_split=0.2,\n  subset=\"training\",\n  seed=123,\n  image_size=(img_height, img_width),\n  batch_size=batch_size)\n\nval_ds = tf.keras.utils.image_dataset_from_directory(\n  data_dir,\n  validation_split=0.2,\n  subset=\"validation\",\n  seed=123,\n  image_size=(img_height, img_width),\n  batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T21:12:18.704358Z","iopub.status.idle":"2022-03-16T21:12:18.705249Z","shell.execute_reply.started":"2022-03-16T21:12:18.704997Z","shell.execute_reply":"2022-03-16T21:12:18.705023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = train_ds.class_names\nprint(class_names)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T21:12:18.70637Z","iopub.status.idle":"2022-03-16T21:12:18.707251Z","shell.execute_reply.started":"2022-03-16T21:12:18.706998Z","shell.execute_reply":"2022-03-16T21:12:18.707024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTOTUNE = tf.data.AUTOTUNE\n\ntrain_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)\nval_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)\n\nnormalization_layer = layers.Rescaling(1./255)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T21:12:18.708362Z","iopub.status.idle":"2022-03-16T21:12:18.709243Z","shell.execute_reply.started":"2022-03-16T21:12:18.708994Z","shell.execute_reply":"2022-03-16T21:12:18.709021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))\nimage_batch, labels_batch = next(iter(normalized_ds))\nfirst_image = image_batch[0]\n# Notice the pixel values are now in `[0,1]`.\nprint(np.min(first_image), np.max(first_image))","metadata":{"execution":{"iopub.status.busy":"2022-03-16T21:12:18.710364Z","iopub.status.idle":"2022-03-16T21:12:18.711245Z","shell.execute_reply.started":"2022-03-16T21:12:18.710996Z","shell.execute_reply":"2022-03-16T21:12:18.711023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = len(class_names)\n\nmodel = Sequential([\n  layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)),\n  layers.Conv2D(16, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(32, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(64, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Flatten(),\n  layers.Dense(128, activation='relu'),\n  layers.Dense(num_classes)\n])","metadata":{"execution":{"iopub.status.busy":"2022-03-16T21:12:18.71237Z","iopub.status.idle":"2022-03-16T21:12:18.713254Z","shell.execute_reply.started":"2022-03-16T21:12:18.713005Z","shell.execute_reply":"2022-03-16T21:12:18.713032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T21:12:18.714363Z","iopub.status.idle":"2022-03-16T21:12:18.715247Z","shell.execute_reply.started":"2022-03-16T21:12:18.714997Z","shell.execute_reply":"2022-03-16T21:12:18.715024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=10\nhistory = model.fit(\n  train_ds,\n  validation_data=val_ds,\n  epochs=epochs\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T21:12:18.716362Z","iopub.status.idle":"2022-03-16T21:12:18.717248Z","shell.execute_reply.started":"2022-03-16T21:12:18.716991Z","shell.execute_reply":"2022-03-16T21:12:18.717017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(epochs)\n\nplt.figure(figsize=(8, 8))\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T21:12:18.71835Z","iopub.status.idle":"2022-03-16T21:12:18.719223Z","shell.execute_reply.started":"2022-03-16T21:12:18.718974Z","shell.execute_reply":"2022-03-16T21:12:18.719Z"},"trusted":true},"execution_count":null,"outputs":[]}]}