{"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 random\nrandom.seed(3001)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-28T18:48:58.94337Z","iopub.execute_input":"2022-05-28T18:48:58.94373Z","iopub.status.idle":"2022-05-28T18:48:58.971164Z","shell.execute_reply.started":"2022-05-28T18:48:58.943645Z","shell.execute_reply":"2022-05-28T18:48:58.970434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# input_image_path = '../input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_images'\ninput_image_path = '../input/hotels-50k-eda-and-image-download/hotels-50k/images/train'","metadata":{"execution":{"iopub.status.busy":"2022-05-28T18:48:58.972467Z","iopub.execute_input":"2022-05-28T18:48:58.973441Z","iopub.status.idle":"2022-05-28T18:48:58.97768Z","shell.execute_reply.started":"2022-05-28T18:48:58.973391Z","shell.execute_reply":"2022-05-28T18:48:58.976718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import PIL\n\ndef rotate_and_crop(image: PIL.Image, angle_deg: int):\n    w_i,h_i = image.size\n    \n    rotated_expanded_image_with_padding = image.rotate(-angle_deg, expand=True)\n    w_p,h_p = rotated_expanded_image_with_padding.size\n    \n    import math\n    angle_rad = np.deg2rad(abs(angle_deg))\n    rem_deg_rad = np.deg2rad(90-(abs(angle_deg) % 90))\n    \n    w_to_remove = int(math.cos(rem_deg_rad)*h_i)\n    h_to_remove = int(math.sin(angle_rad)*w_i)\n    return rotated_expanded_image_with_padding.crop((w_to_remove,h_to_remove,w_p-w_to_remove,h_p-h_to_remove))\n\ndef randomly_rotate_image(image: PIL.Image):\n    def do_nothing(image: PIL.Image):\n        return image, 0\n\n    def rotate_with_max_degrees(image: PIL.Image, max_degrees: int = 10):\n        rotation_deg = random.randrange(-abs(max_degrees), abs(max_degrees) + 1)\n        return (rotate_and_crop(image, rotation_deg), rotation_deg)\n\n    def rotate_about_90_degrees_left(image: PIL.Image):\n        rotated_90_degrees_left = image.rotate(90, expand=True)\n        rotated_image, further_degrees = rotate_with_max_degrees(rotated_90_degrees_left)\n        return (rotated_image, -90 + further_degrees)\n\n    def rotate_about_90_degrees_right(image: PIL.Image):\n        rotated_90_degrees_right = image.rotate(-90, expand=True)\n        rotated_image, further_degrees = rotate_with_max_degrees(rotated_90_degrees_right)\n        return (rotated_image, 90 + further_degrees)\n\n    random_image_rotation_fun = np.random.choice(\n#         [\n# #             do_nothing,\n#             rotate_with_max_degrees,\n#             rotate_about_90_degrees_left,\n#             rotate_about_90_degrees_right\n#         ],\n#         p=[\n# #             0.6,\n#             0.5,\n#             0.25,\n#             0.25\n#         ]\n        [\n            do_nothing,\n            rotate_with_max_degrees,\n            rotate_about_90_degrees_left,\n            rotate_about_90_degrees_right\n        ],\n        p=[\n            0.6,\n            0.20,\n            0.10,\n            0.10\n        ]\n    )\n\n    return random_image_rotation_fun(image)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-28T18:48:58.980467Z","iopub.execute_input":"2022-05-28T18:48:58.980822Z","iopub.status.idle":"2022-05-28T18:48:58.995048Z","shell.execute_reply.started":"2022-05-28T18:48:58.980778Z","shell.execute_reply":"2022-05-28T18:48:58.993974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\ndef count_images(path):\n    total_images = 0\n    for root, dirs, files in os.walk(path):\n        for f in files:\n            total_images += 1\n    return total_images","metadata":{"execution":{"iopub.status.busy":"2022-05-28T18:48:58.996654Z","iopub.execute_input":"2022-05-28T18:48:58.997047Z","iopub.status.idle":"2022-05-28T18:48:59.012374Z","shell.execute_reply.started":"2022-05-28T18:48:58.997009Z","shell.execute_reply":"2022-05-28T18:48:59.011234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(count_images('../input/hotels-50k-eda-and-image-download'))","metadata":{"execution":{"iopub.status.busy":"2022-05-28T18:48:59.01428Z","iopub.execute_input":"2022-05-28T18:48:59.014977Z","iopub.status.idle":"2022-05-28T18:49:45.521462Z","shell.execute_reply.started":"2022-05-28T18:48:59.01494Z","shell.execute_reply":"2022-05-28T18:49:45.520816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import PIL\nimport random\nimport os\nimport shutil\nimport numpy as np\nimport json\n\nif os.path.isdir('data'):\n    shutil.rmtree(\"./data\")\nos.makedirs('./data/images/train/class')\nos.makedirs('./data/images/valid/class')\n\n# n_images_max = 50000#75000#count_images(input_image_path)\n# n_training_images = 40000#60000#n_images_m\n# 2.7 GB zip\n\nn_images_max = 60000#75000#count_images(input_image_path)\nn_training_images = 48000#60000#n_images_max*0.8\n# 8.43 GB zip\n\nrotations = []\ntotal_images = 0\nfor root, dirs, files in os.walk(input_image_path):\n    for f in files:\n        hotel_image_path = os.path.join(root, f)\n        image = PIL.Image.open(hotel_image_path)\n        rotated_image, rotation = randomly_rotate_image(image)\n        rotations.append(rotation)\n        storage_path = './data/images/train/class'\n        if total_images >= n_training_images:\n            storage_path = './data/images/valid/class'\n        rotated_image.save(os.path.join(storage_path, str(total_images)+'.png'))\n        total_images += 1\n    if total_images > n_images_max:#50000:\n        break\nprint(\"Total images: {}\".format(total_images))","metadata":{"execution":{"iopub.status.busy":"2022-05-28T18:49:45.522861Z","iopub.execute_input":"2022-05-28T18:49:45.523252Z","iopub.status.idle":"2022-05-28T20:00:05.810141Z","shell.execute_reply.started":"2022-05-28T18:49:45.523222Z","shell.execute_reply":"2022-05-28T20:00:05.80791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nwith open('./data/label.json', 'w') as f:\n    json.dump(rotations, f, indent=2)","metadata":{"execution":{"iopub.status.busy":"2022-05-28T20:00:05.816223Z","iopub.execute_input":"2022-05-28T20:00:05.816895Z","iopub.status.idle":"2022-05-28T20:00:05.879319Z","shell.execute_reply.started":"2022-05-28T20:00:05.816806Z","shell.execute_reply":"2022-05-28T20:00:05.878652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import shutil\n# shutil.make_archive('data', 'zip', './data')","metadata":{"execution":{"iopub.status.busy":"2022-05-28T20:00:05.882185Z","iopub.execute_input":"2022-05-28T20:00:05.882683Z","iopub.status.idle":"2022-05-28T20:08:23.886641Z","shell.execute_reply.started":"2022-05-28T20:00:05.882638Z","shell.execute_reply":"2022-05-28T20:08:23.886047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# shutil.rmtree('./data')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from IPython.display import FileLink\n# FileLink('./data.zip')","metadata":{"execution":{"iopub.status.busy":"2022-05-28T20:08:23.888805Z","iopub.execute_input":"2022-05-28T20:08:23.889124Z","iopub.status.idle":"2022-05-28T20:08:23.897043Z","shell.execute_reply.started":"2022-05-28T20:08:23.889085Z","shell.execute_reply":"2022-05-28T20:08:23.896354Z"},"trusted":true},"execution_count":null,"outputs":[]}]}