{"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":"# Resizing HappyWhale Images","metadata":{}},{"cell_type":"markdown","source":"This notebook helps is resizing images of [Happywhale - Whale and Dolphin Identification](https://www.kaggle.com/c/happy-whale-and-dolphin) competition. I have tried it using tensorflow and cv2 (commented it) both. Also, visualized images after resizing it.\n\nThis is second notebook for this competition. The first notebook, [Visuals of HappyWhale](https://www.kaggle.com/rajankumar/visuals-of-happywhale), provides detailed explorartory analysis on nomenclature, type of species, various modes and shapes of images.   ","metadata":{}},{"cell_type":"markdown","source":"# Load Packages","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom termcolor import colored\nfrom PIL import Image","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-15T07:26:13.542228Z","iopub.execute_input":"2022-03-15T07:26:13.542843Z","iopub.status.idle":"2022-03-15T07:26:18.938479Z","shell.execute_reply.started":"2022-03-15T07:26:13.542718Z","shell.execute_reply":"2022-03-15T07:26:18.937292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_IMAGE_DIR = '/kaggle/input/happy-whale-and-dolphin/train_images'\nTEST_IMAGE_DIR = '/kaggle/input/happy-whale-and-dolphin/test_images'\n\nTRAIN_IMAGE_SAVE_DIR = '/kaggle/working/resized_train_images'\nTEST_IMAGE_SAVE_DIR = '/kaggle/working/resized_test_images'","metadata":{"execution":{"iopub.status.busy":"2022-03-15T07:26:18.94039Z","iopub.execute_input":"2022-03-15T07:26:18.940642Z","iopub.status.idle":"2022-03-15T07:26:18.945631Z","shell.execute_reply.started":"2022-03-15T07:26:18.940612Z","shell.execute_reply":"2022-03-15T07:26:18.944818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = os.listdir(TRAIN_IMAGE_DIR)\ntest_images = os.listdir(TEST_IMAGE_DIR)\n\nn_train_images = len(train_images)\nn_test_images = len(test_images)\n\nprint('# of training images = {}'.format(n_train_images))\nprint('# of testing images = {}'.format(n_test_images))","metadata":{"execution":{"iopub.status.busy":"2022-03-15T07:26:18.947408Z","iopub.execute_input":"2022-03-15T07:26:18.947636Z","iopub.status.idle":"2022-03-15T07:26:20.177187Z","shell.execute_reply.started":"2022-03-15T07:26:18.947608Z","shell.execute_reply":"2022-03-15T07:26:20.176265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Resize training images","metadata":{}},{"cell_type":"code","source":"# print(colored('Processing training images...', 'red'))\n# if not os.path.exists(TRAIN_IMAGE_SAVE_DIR):\n#     os.makedirs(TRAIN_IMAGE_SAVE_DIR)\n#     print('Training images saving directory created')\n# else:\n#     print('Training images saving directory exists')\n\n# for i, train_image in enumerate(train_images):\n#     if (i+1) % 1000 == 0:\n#         print('{}/{} images processed'.format(i+1, n_train_images))\n#     image = cv2.imread(os.path.join(TRAIN_IMAGE_DIR, train_image), 1)\n#     resized_img = cv2.resize(image, (128, 128))\n#     cv2.imwrite(os.path.join(TRAIN_IMAGE_SAVE_DIR, train_image), resized_img)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T07:26:20.179441Z","iopub.execute_input":"2022-03-15T07:26:20.179827Z","iopub.status.idle":"2022-03-15T07:26:20.184749Z","shell.execute_reply.started":"2022-03-15T07:26:20.179788Z","shell.execute_reply":"2022-03-15T07:26:20.183634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_images(path):\n    img = tf.io.read_file(path)\n    img = tf.image.decode_jpeg(img, channels=3)\n    img = tf.image.resize(img, [128, 128])\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-03-15T07:26:20.186375Z","iopub.execute_input":"2022-03-15T07:26:20.186645Z","iopub.status.idle":"2022-03-15T07:26:20.196638Z","shell.execute_reply.started":"2022-03-15T07:26:20.186612Z","shell.execute_reply":"2022-03-15T07:26:20.195783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(colored('Processing training images...', 'red'))\nif not os.path.exists(TRAIN_IMAGE_SAVE_DIR):\n    os.makedirs(TRAIN_IMAGE_SAVE_DIR)\n    print('Training images saving directory created')\nelse:\n    print('Training images saving directory exists')\n    \nfor i, train_image in enumerate(train_images):\n    if (i+1) % 1000 == 0:\n        print('{}/{} images processed'.format(i+1, n_train_images))\n    resized_img = resize_images(os.path.join(TRAIN_IMAGE_DIR, train_image))\n    tf.keras.utils.save_img(os.path.join(TRAIN_IMAGE_SAVE_DIR, train_image), resized_img)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T07:26:20.19799Z","iopub.execute_input":"2022-03-15T07:26:20.198689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_images_plot = 50\nn_col_plot = 10\nfigsize = (20,10)\nfontsize = 20\n\ndef plot_images(images, cmap = None):\n    fig, axs = plt.subplots(int(n_images_plot/n_col_plot), n_col_plot, figsize = figsize)\n    for i, train_image in enumerate(images[0:n_images_plot]):\n        img = Image.open(os.path.join(TRAIN_IMAGE_SAVE_DIR, train_image))\n        axs.ravel()[i].imshow(img, cmap = cmap)\n        axs.ravel()[i].set_axis_off()\n        axs.ravel()[i].set_title('Sample No. {}'.format(i+1))\n    return fig, axs\n\nfig, axs = plot_images(train_images)\nplt.suptitle('Sample resized training images', fontsize = fontsize)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T11:06:59.830732Z","iopub.execute_input":"2022-03-15T11:06:59.831045Z","iopub.status.idle":"2022-03-15T11:06:59.854375Z","shell.execute_reply.started":"2022-03-15T11:06:59.831011Z","shell.execute_reply":"2022-03-15T11:06:59.853183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(colored('Processing testing images...', 'red'))\n# if not os.path.exists(TEST_IMAGE_SAVE_DIR):\n#     os.makedirs(TEST_IMAGE_SAVE_DIR)\n#     print('Testing images saving directory created')\n# else:\n#     print('Testing images saving directory exists')\n\n# for i, test_image in enumerate(test_images):\n#     if (i+1) % 1000 == 0:\n#         print('{}/{} images processed'.format(i+1, n_test_images))\n#     image = cv2.imread(os.path.join(TEST_IMAGE_DIR, test_image), 1)\n#     resized_img = cv2.resize(image, (128, 128))\n#     cv2.imwrite(os.path.join(TEST_IMAGE_SAVE_DIR, test_image), resized_img)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(colored('Processing training images...', 'red'))\nif not os.path.exists(TEST_IMAGE_SAVE_DIR):\n    os.makedirs(TEST_IMAGE_SAVE_DIR)\n    print('Testing images saving directory created')\nelse:\n    print('Testing images saving directory exists')\n    \nfor i, test_image in enumerate(test_images):\n    if (i+1) % 1000 == 0:\n        print('{}/{} images processed'.format(i+1, n_test_images))\n    resized_img = resize_images(os.path.join(TEST_IMAGE_DIR, test_image))\n    tf.keras.utils.save_img(os.path.join(TEST_IMAGE_SAVE_DIR, test_image), resized_img)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_images_plot = 50\nn_col_plot = 10\nfigsize = (20,10)\nfontsize = 20\n\ndef plot_images(images, cmap = None):\n    fig, axs = plt.subplots(int(n_images_plot/n_col_plot), n_col_plot, figsize = figsize)\n    for i, test_image in enumerate(images[0:n_images_plot]):\n        img = Image.open(os.path.join(TEST_IMAGE_SAVE_DIR, test_image))\n        axs.ravel()[i].imshow(img, cmap = cmap)\n        axs.ravel()[i].set_axis_off()\n        axs.ravel()[i].set_title('Sample No. {}'.format(i+1))\n    return fig, axs\n\nfig, axs = plot_images(test_images)\nplt.suptitle('Sample resized testing images', fontsize = fontsize)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T11:05:52.981227Z","iopub.execute_input":"2022-03-15T11:05:52.981554Z","iopub.status.idle":"2022-03-15T11:05:53.082417Z","shell.execute_reply.started":"2022-03-15T11:05:52.981471Z","shell.execute_reply":"2022-03-15T11:05:53.081069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = os.listdir(TRAIN_IMAGE_SAVE_DIR)\ntest_images = os.listdir(TEST_IMAGE_SAVE_DIR)\n\nn_train_images = len(train_images)\nn_test_images = len(test_images)\n\nprint('# of training images = {}'.format(n_train_images))\nprint('# of testing images = {}'.format(n_test_images))","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}