{"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 os\nimport random\nimport pandas as pd\nimport tensorflow as tf\nimport tensorflow_addons as tfa\nimport seaborn as sns\n\nfrom PIL import Image\nfrom matplotlib import pyplot as plt\nfrom sklearn.model_selection import StratifiedKFold","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":8.09922,"end_time":"2022-05-03T03:04:07.871269","exception":false,"start_time":"2022-05-03T03:03:59.772049","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T03:59:39.12692Z","iopub.execute_input":"2022-05-25T03:59:39.127545Z","iopub.status.idle":"2022-05-25T03:59:47.003669Z","shell.execute_reply.started":"2022-05-25T03:59:39.127452Z","shell.execute_reply":"2022-05-25T03:59:47.002476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta_path = '../input/sorghum-id-fgvc-9/train_cultivar_mapping.csv'\ntrain_meta = pd.read_csv(train_meta_path)\ntrain_meta","metadata":{"papermill":{"duration":0.098258,"end_time":"2022-05-03T03:04:07.994426","exception":false,"start_time":"2022-05-03T03:04:07.896168","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T03:59:47.005311Z","iopub.execute_input":"2022-05-25T03:59:47.00561Z","iopub.status.idle":"2022-05-25T03:59:47.070296Z","shell.execute_reply.started":"2022-05-25T03:59:47.005578Z","shell.execute_reply":"2022-05-25T03:59:47.069592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.value_counts(train_meta['cultivar'])","metadata":{"papermill":{"duration":0.047155,"end_time":"2022-05-03T03:04:08.067231","exception":false,"start_time":"2022-05-03T03:04:08.020076","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T03:59:47.071933Z","iopub.execute_input":"2022-05-25T03:59:47.072517Z","iopub.status.idle":"2022-05-25T03:59:47.090417Z","shell.execute_reply.started":"2022-05-25T03:59:47.072466Z","shell.execute_reply":"2022-05-25T03:59:47.088837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=[24, 6], dpi=200)\nsns.countplot(x=train_meta['cultivar'])\nplt.xticks(rotation=60)\nplt.show()","metadata":{"papermill":{"duration":1.734237,"end_time":"2022-05-03T03:04:09.828024","exception":false,"start_time":"2022-05-03T03:04:08.093787","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T03:59:47.093501Z","iopub.execute_input":"2022-05-25T03:59:47.093743Z","iopub.status.idle":"2022-05-25T03:59:49.065695Z","shell.execute_reply.started":"2022-05-25T03:59:47.093711Z","shell.execute_reply":"2022-05-25T03:59:49.064798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def random_zoom(image, zoom_range):\n    factor = np.random.uniform(low=-zoom_range, high=zoom_range)\n\n    if not isinstance(image, np.ndarray):\n        arr = tf.keras.utils.img_to_array(image)\n\n        tf.image.resize_with_crop_or_pad(tf.image.resize(arr,\n                                                         (int(arr.shape[0] + arr.shape[0] * factor),\n                                                          int(arr.shape[1] + arr.shape[1] * factor))),\n                                         target_height=512,\n                                         target_width=512)\n\n    return tf.image.resize_with_crop_or_pad(tf.image.resize(image,\n                                                            (int(image.shape[0] + image.shape[0] * factor),\n                                                             int(image.shape[1] + image.shape[1] * factor))),\n                                            target_height=512,\n                                            target_width=512)","metadata":{"papermill":{"duration":0.04264,"end_time":"2022-05-03T03:04:09.901362","exception":false,"start_time":"2022-05-03T03:04:09.858722","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T03:59:49.0669Z","iopub.execute_input":"2022-05-25T03:59:49.067279Z","iopub.status.idle":"2022-05-25T03:59:49.076685Z","shell.execute_reply.started":"2022-05-25T03:59:49.067246Z","shell.execute_reply":"2022-05-25T03:59:49.075718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def random_rotate(image, rotation_range):\n    factor = np.random.uniform(low=-rotation_range, high=rotation_range)\n\n    if not isinstance(image, np.ndarray):\n        arr = tf.keras.utils.img_to_array(image)\n\n        return tfa.image.rotate(arr, angles=factor, fill_mode='nearest')\n\n    else:\n        return tfa.image.rotate(image, angles=factor, fill_mode='nearest')\n","metadata":{"papermill":{"duration":0.039148,"end_time":"2022-05-03T03:04:09.97095","exception":false,"start_time":"2022-05-03T03:04:09.931802","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T03:59:49.078099Z","iopub.execute_input":"2022-05-25T03:59:49.078544Z","iopub.status.idle":"2022-05-25T03:59:49.09006Z","shell.execute_reply.started":"2022-05-25T03:59:49.078508Z","shell.execute_reply":"2022-05-25T03:59:49.089172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = '2017-06-01__10-26-27-479.png'\ndir = '../input/sorghum-cultivar-identification-512512/train'\n\nfig, axes = plt.subplots(nrows=1, ncols=3, figsize=[12, 6], dpi=300)\n\nfor i in range(3):\n    zoomed_img = random_zoom(tf.keras.utils.img_to_array(Image.open(os.path.join(dir, filename))), 0.5)\n    axes[i].imshow(tf.keras.utils.array_to_img(zoomed_img))\n\nplt.show()","metadata":{"papermill":{"duration":2.827644,"end_time":"2022-05-03T03:04:12.828751","exception":false,"start_time":"2022-05-03T03:04:10.001107","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T03:59:49.09128Z","iopub.execute_input":"2022-05-25T03:59:49.091712Z","iopub.status.idle":"2022-05-25T03:59:51.44119Z","shell.execute_reply.started":"2022-05-25T03:59:49.091681Z","shell.execute_reply":"2022-05-25T03:59:51.440082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=1, ncols=3, figsize=[12, 6], dpi=300)\n\nfor i in range(3):\n    rotated_img = random_rotate(Image.open(os.path.join(dir, filename)), 20)\n    axes[i].imshow(tf.keras.utils.array_to_img(rotated_img))\n\nplt.show()","metadata":{"papermill":{"duration":2.781779,"end_time":"2022-05-03T03:04:15.724211","exception":false,"start_time":"2022-05-03T03:04:12.942432","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T03:59:51.442701Z","iopub.execute_input":"2022-05-25T03:59:51.443003Z","iopub.status.idle":"2022-05-25T03:59:54.053944Z","shell.execute_reply.started":"2022-05-25T03:59:51.442962Z","shell.execute_reply":"2022-05-25T03:59:54.052464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=1, ncols=3, figsize=[12, 6], dpi=300)\n\nfor i in range(3):\n    zoomed_img = random_zoom(tf.keras.utils.img_to_array(Image.open(os.path.join(dir, filename))), 0.5)\n    rotated_img = random_rotate(zoomed_img, 20)\n    axes[i].imshow(tf.keras.utils.array_to_img(rotated_img))\n\nplt.show()","metadata":{"papermill":{"duration":2.835754,"end_time":"2022-05-03T03:04:18.763248","exception":false,"start_time":"2022-05-03T03:04:15.927494","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T03:59:54.055743Z","iopub.execute_input":"2022-05-25T03:59:54.056355Z","iopub.status.idle":"2022-05-25T03:59:56.542311Z","shell.execute_reply.started":"2022-05-25T03:59:54.056291Z","shell.execute_reply":"2022-05-25T03:59:56.541119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"central_crop_width = (0.35, 0.65, 0.75)\ncentral_crop_height = (0.35, 0.9, 0.75)\n\nfor w_factor, h_factor in zip(central_crop_width, central_crop_height):\n    h, w = tf.keras.utils.img_to_array(Image.open(os.path.join(dir, filename))).shape[:2]\n    print(int(w * w_factor), int(h * h_factor))","metadata":{"papermill":{"duration":0.330012,"end_time":"2022-05-03T03:04:19.387301","exception":false,"start_time":"2022-05-03T03:04:19.057289","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T03:59:56.546543Z","iopub.execute_input":"2022-05-25T03:59:56.546888Z","iopub.status.idle":"2022-05-25T03:59:56.589306Z","shell.execute_reply.started":"2022-05-25T03:59:56.546836Z","shell.execute_reply":"2022-05-25T03:59:56.588427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_to_smaller_side(img,small_side_to=224, crop_window=(128, 128, 3), copies=3):\n    h, w = img.shape[:2]\n    crops = []\n\n    if h < w:\n        resized = tf.image.resize(img, size=(small_side_to, w))\n    elif w < h:\n        resized = tf.image.resize(img, size=(h, small_side_to))\n    else:\n        resized = tf.image.resize(img, (small_side_to, w))\n\n    for _ in range(copies):\n        crops.append(tf.image.random_crop(resized, crop_window))\n\n    return crops","metadata":{"collapsed":false,"pycharm":{"name":"#%%\n"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-05-25T03:59:56.590802Z","iopub.execute_input":"2022-05-25T03:59:56.591055Z","iopub.status.idle":"2022-05-25T03:59:56.59928Z","shell.execute_reply.started":"2022-05-25T03:59:56.591024Z","shell.execute_reply":"2022-05-25T03:59:56.598367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = tf.keras.utils.img_to_array(Image.open(os.path.join(dir,filename)))\ncrops = resize_to_smaller_side(tf.image.resize(img, size=(512, 512)))\n\nfig, axes = plt.subplots(nrows=1, ncols=3, figsize=[12, 6], dpi=300)\n\nfor i, crop in enumerate(crops):\n    axes[i].imshow(tf.keras.utils.array_to_img(crop))\n\nplt.show()","metadata":{"collapsed":false,"pycharm":{"name":"#%%\n"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-05-25T03:59:56.600606Z","iopub.execute_input":"2022-05-25T03:59:56.601173Z","iopub.status.idle":"2022-05-25T03:59:57.523319Z","shell.execute_reply.started":"2022-05-25T03:59:56.601125Z","shell.execute_reply":"2022-05-25T03:59:57.522239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cropping(filename,\n             dir,\n             rotate_range=10,\n             flipping=True,\n             zoom_range=0.5,\n             central_crop_width=(0.35, 0.65, 0.75),\n             central_crop_height=(0.35, 0.9, 0.45),\n             random_crop_window=(128, 128, 3),\n             small_side_to=224,\n             copies=5):\n    arr = tf.keras.utils.img_to_array(Image.open(os.path.join(dir, filename)))\n    crops = []\n\n#     if random.choice([True, False]):\n#         arr = random_zoom(arr, zoom_range)\n\n#     if random.choice([True, False]):\n#         arr = random_rotate(arr, rotate_range)\n\n#     if flipping:\n#         arr = tf.image.random_flip_left_right(arr)\n#         arr = tf.image.random_flip_up_down(arr)\n\n    if isinstance(central_crop_width, (list, tuple, np.ndarray)):\n\n        for w_factor, h_factor in zip(central_crop_width, central_crop_height):\n            h, w = arr.shape[:2]\n            offset_h = (h - h_factor * h) // 2\n            offset_w = (w - w_factor * w) // 2\n\n            crops.append(\n                tf.image.crop_to_bounding_box(arr, int(offset_h), int(offset_w), int(h * h_factor), int(w * w_factor))\n            )\n\n#     h, w = arr.shape[:2]\n#     if h < w:\n#         resized = tf.image.resize(arr, size=(small_side_to, w))\n#     elif w < h:\n#         resized = tf.image.resize(arr, size=(h, small_side_to))\n#     else:\n#         resized = tf.image.resize(arr, (small_side_to, w))\n\n#     for _ in range(copies):\n#         crops.append(tf.image.random_crop(resized, random_crop_window))\n\n    return crops\n","metadata":{"papermill":{"duration":0.295162,"end_time":"2022-05-03T03:04:19.963399","exception":false,"start_time":"2022-05-03T03:04:19.668237","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T04:16:15.097839Z","iopub.execute_input":"2022-05-25T04:16:15.098667Z","iopub.status.idle":"2022-05-25T04:16:15.111355Z","shell.execute_reply.started":"2022-05-25T04:16:15.098587Z","shell.execute_reply":"2022-05-25T04:16:15.110366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"crops = cropping(filename, dir)\n\nfig, axes = plt.subplots(nrows=1, ncols=3, figsize=[12, 10], dpi=300)\naxes = axes.ravel()\n\nfor i, crop in enumerate(crops):\n    axes[i].imshow(tf.keras.utils.array_to_img(crop))\n\nplt.show()","metadata":{"papermill":{"duration":2.309711,"end_time":"2022-05-03T03:04:22.557129","exception":false,"start_time":"2022-05-03T03:04:20.247418","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T04:16:18.406516Z","iopub.execute_input":"2022-05-25T04:16:18.407395Z","iopub.status.idle":"2022-05-25T04:16:20.31871Z","shell.execute_reply.started":"2022-05-25T04:16:18.407339Z","shell.execute_reply":"2022-05-25T04:16:20.317276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.isdir('train'):\n    os.mkdir('train')","metadata":{"papermill":{"duration":0.383605,"end_time":"2022-05-03T03:04:23.288686","exception":false,"start_time":"2022-05-03T03:04:22.905081","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T03:59:59.363302Z","iopub.execute_input":"2022-05-25T03:59:59.363582Z","iopub.status.idle":"2022-05-25T03:59:59.36909Z","shell.execute_reply.started":"2022-05-25T03:59:59.36355Z","shell.execute_reply":"2022-05-25T03:59:59.36781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c = 0\nnew_train_meta = []\n\nfor filename, label in train_meta.values:\n    if filename in os.listdir(dir):\n        crops = cropping(filename, dir)\n\n        for i, crop in enumerate(crops):\n            dst_file = f'pp{i}-{filename}'\n            tf.keras.utils.array_to_img(tf.image.resize(crop, (256, 256))).save(f'train/{dst_file}')\n\n            new_train_meta.append([dst_file, label])\n\n        c += 1\n        print(f'{c}/{len(os.listdir(dir))}', end='\\r')","metadata":{"papermill":{"duration":1386.300701,"end_time":"2022-05-03T03:27:29.934887","exception":false,"start_time":"2022-05-03T03:04:23.634186","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T03:59:59.370824Z","iopub.execute_input":"2022-05-25T03:59:59.371541Z","iopub.status.idle":"2022-05-25T04:16:07.811799Z","shell.execute_reply.started":"2022-05-25T03:59:59.371503Z","shell.execute_reply":"2022-05-25T04:16:07.808968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(Image.open('train/pp2-2017-06-02__16-48-57-866.png'))","metadata":{"papermill":{"duration":2.683927,"end_time":"2022-05-03T03:27:35.050847","exception":false,"start_time":"2022-05-03T03:27:32.36692","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T04:17:40.617515Z","iopub.execute_input":"2022-05-25T04:17:40.617841Z","iopub.status.idle":"2022-05-25T04:17:40.899252Z","shell.execute_reply.started":"2022-05-25T04:17:40.617806Z","shell.execute_reply":"2022-05-25T04:17:40.898056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train_meta_ = pd.DataFrame(new_train_meta, columns=['image', 'cultivar'])\nnew_train_meta_","metadata":{"papermill":{"duration":2.638465,"end_time":"2022-05-03T03:27:40.174847","exception":false,"start_time":"2022-05-03T03:27:37.536382","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T04:17:53.295508Z","iopub.execute_input":"2022-05-25T04:17:53.296223Z","iopub.status.idle":"2022-05-25T04:17:53.325229Z","shell.execute_reply.started":"2022-05-25T04:17:53.296179Z","shell.execute_reply":"2022-05-25T04:17:53.324371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"skf = StratifiedKFold(n_splits=4, shuffle=True, random_state=42)\n\nfor train_idx, valid_idx in skf.split(new_train_meta_['image'], new_train_meta_['cultivar']):\n    df_train = new_train_meta_.iloc[train_idx]\n    df_valid = new_train_meta_.iloc[valid_idx]\n\nprint(f\"train size: {len(df_train)}\")\nprint(f\"valid size: {len(df_valid)}\")\n\nprint(df_train.cultivar.value_counts())\nprint(df_valid.cultivar.value_counts())","metadata":{"papermill":{"duration":2.599259,"end_time":"2022-05-03T03:27:45.269571","exception":false,"start_time":"2022-05-03T03:27:42.670312","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T04:17:59.848774Z","iopub.execute_input":"2022-05-25T04:17:59.849067Z","iopub.status.idle":"2022-05-25T04:17:59.912093Z","shell.execute_reply.started":"2022-05-25T04:17:59.849035Z","shell.execute_reply":"2022-05-25T04:17:59.911063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_valid.to_csv('valid_meta.csv', index=False)","metadata":{"papermill":{"duration":2.496717,"end_time":"2022-05-03T03:27:50.281006","exception":false,"start_time":"2022-05-03T03:27:47.784289","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T04:18:03.706634Z","iopub.execute_input":"2022-05-25T04:18:03.706943Z","iopub.status.idle":"2022-05-25T04:18:03.736078Z","shell.execute_reply.started":"2022-05-25T04:18:03.706903Z","shell.execute_reply":"2022-05-25T04:18:03.73474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image(path, mode='RGB'):\n    return Image.open(path)\n\n\ndef to_array(image):\n    return np.asarray(image)\n\n\ndef to_image(array, mode='RGB'):\n    return Image.fromarray(np.uint8(array), mode=mode)\n\n\ndef resize(image, size):\n    return tf.image.resize(image, size)\n\n\ndef resize_smallest_side_different_scales(image, smallest_side_to=(224, 384)):\n    height, width = to_array(image).shape[:2]\n    scaled_list = []\n\n    if height < width:\n\n        for scale in smallest_side_to:\n            scaled = tf.image.resize(image, (scale, width))\n            scaled_list.append(scaled)\n\n        return scaled_list\n\n    else:\n\n        for scale in smallest_side_to:\n            scaled = tf.image.resize(image, (height, scale))\n            scaled_list.append(scaled)\n\n        return scaled_list\n\n\ndef resize_with_aspect_ratio(image, target_width=(128, 256, 512), input_shape=(224, 224)):\n    h, w = to_array(image).shape[:2]\n    r = h / w\n    resized = []\n\n    for width in target_width:\n        resized_h = int(r * width)\n        resized_img = resize(image, (resized_h, width))\n        resized.append(\n            to_image(resize(tf.image.resize_with_crop_or_pad(resized_img, input_shape[0], input_shape[1]), (128, 128))))\n\n    return resized\n\n\ndef bounding_boxes(offsets, dim):\n    boxes = []\n\n    for i in offsets:\n        offset_height, offset_width = i\n        target_height, target_width = dim\n        boxes.append([offset_height, offset_width, target_height, target_width])\n\n    return boxes\n\n\ndef random_sectioning(image, offsets, dims):\n    boxes = bounding_boxes(offsets, dims)\n    image_sections = []\n    height, width = to_array(image).shape[:2]\n\n    if (height < height // 2 + dims[0]) and (width < width // 2 + dims[1]):\n        image = tf.image.resize(image, (dims[0] * 2, dims[1] * 2))\n\n    if (height > height // 2 + dims[0]) and (width < width // 2 + dims[1]):\n        image = tf.image.resize(image, (height, dims[1] * 2))\n\n    if (height < height // 2 + dims[0]) and (width > width // 2 + dims[1]):\n        image = tf.image.resize(image, (dims[0] * 2, width))\n\n    for box in boxes:\n        if random.choice([True, False]):\n            section = tf.image.crop_to_bounding_box(image, box[0], box[1], box[2], box[3])\n            image_sections.append(resize(section, (128, 128)))\n\n    return image_sections\n\n\ndef aggressive_cropping(image, copies, crop_window, resize_smallest_side=None, output_shape=(128, 128)):\n    global resized_copies\n\n    if resize_smallest_side is not None:\n        if isinstance(resize_smallest_side, int):\n            img = resize(to_array(image), (resize_smallest_side, resize_smallest_side))\n\n        if isinstance(resize_smallest_side, (list, tuple)):\n            resized_copies = [tf.image.resize(to_array(image), (size, size)) for size in resize_smallest_side]\n\n    if isinstance(crop_window, int):\n        crops = [tf.image.random_crop(to_array(image), (crop_window, crop_window)) for _ in range(copies)]\n\n        return [resize(crop_img, output_shape) for crop_img in crops]\n\n    elif isinstance(crop_window, (list, tuple)):\n        crops = [tf.image.random_crop(to_array(image), crop_window) for _ in range(copies)]\n\n        return [resize(crop_img, output_shape) for crop_img in crops]\n\n\ndef change_contrast(image, lower, upper, copies=1):\n    copies = [tf.image.random_contrast(image, lower=lower, upper=upper) for _ in range(copies)]\n    return copies\n\n\ndef change_brightness(image, delta, copies=1):\n    copies = [tf.image.random_brightness(image, max_delta=delta) for _ in range(copies)]\n    return copies\n\n\ndef change_hue(image, delta, copies=1):\n    copies = [tf.image.random_hue(image, max_delta=delta) for _ in range(copies)]\n    return copies\n\n\ndef gamma_transformation(image, gamma=0.3, copies=1):\n    low = 1 - gamma\n    up = 1 + gamma\n    copies = [tf.image.adjust_gamma(image, gamma=np.random.uniform(low, up, 1)) for _ in range(copies)]\n    return copies\n\n\ndef change_staturate(image, delta=0.3, copies=1):\n    copies = [tf.image.adjust_saturation(image, np.round(np.random.uniform(-1 * delta, 1 * delta), 2)) for _ in\n              range(copies)]\n    return copies\n\n\ndef change_sharpness(image, factor=0.3, copies=1):\n    results = []\n\n    for _ in range(copies):\n        change_by = float(np.round(np.random.uniform(0, factor)))\n        results.append(tfa.image.sharpness(image, change_by))\n\n    return results\n\n\ndef apply_blur(image, sigma_range=2, copies=1):\n    result = []\n\n    for _ in range(copies):\n        kernel_size = np.random.randint(1, 7)\n        sigma = np.random.uniform(1, sigma_range)\n        result.append(tfa.image.gaussian_filter2d(image, kernel_size, sigma))\n\n    return result","metadata":{"papermill":{"duration":2.472693,"end_time":"2022-05-03T03:27:55.287997","exception":false,"start_time":"2022-05-03T03:27:52.815304","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T04:16:07.825671Z","iopub.status.idle":"2022-05-25T04:16:07.826265Z","shell.execute_reply.started":"2022-05-25T04:16:07.825966Z","shell.execute_reply":"2022-05-25T04:16:07.825997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# c = 0\n# new_train_meta = []\n# train_batch = df_train.shape[0]\n\n# for filename, label in df_train.values:\n#     if filename in os.listdir('train'):\n#         if random.choice([True, False]):\n#             image = tf.keras.utils.img_to_array(Image.open(os.path.join('train', filename)))\n#             process = change_brightness(image, 0.4, copies=1)[0]\n\n#             if random.choice([True, False]):\n#                 process = change_contrast(process, 0.5, 2, copies=1)[0]\n\n#             if random.choice([True, False]):\n#                 process = change_hue(process, 0.2, copies=1)[0]\n\n#             if random.choice([True, False]):\n#                 process = gamma_transformation(process, 0.3, copies=1)[0]\n\n#             if random.choice([True, False]):\n#                 process = change_staturate(process, 0.3, copies=1)[0]\n\n#             dst_file = f'cm-{filename}'\n#             tf.keras.utils.array_to_img(tf.image.resize(process, (96, 96))).save(f'train/{dst_file}')\n#             new_train_meta.append([dst_file, label])\n\n#             del image\n\n#         c += 1\n#         print(f'{c}/{train_batch}', end='\\r')\n\n# train_df_1 = pd.DataFrame(new_train_meta, columns=['image', 'cultivar'])\n# train_df_1","metadata":{"papermill":{"duration":1788.976027,"end_time":"2022-05-03T03:57:46.703159","exception":false,"start_time":"2022-05-03T03:27:57.727132","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T04:16:07.827669Z","iopub.status.idle":"2022-05-25T04:16:07.828188Z","shell.execute_reply.started":"2022-05-25T04:16:07.827909Z","shell.execute_reply":"2022-05-25T04:16:07.827937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# c = 0\n# new_train_meta = []\n# combine_df = pd.concat([df_train, train_df_1], ignore_index=True).sample(n=45000)\n# train_batch = combine_df.shape[0]\n\n# for filename, label in combine_df.values:\n#     if filename in os.listdir('train'):\n#         if random.choice([True, False]):\n#             image = tf.keras.utils.img_to_array(Image.open(os.path.join('train', filename)))\n\n#             if random.choice([True, False]):\n#                 process = change_sharpness(image, 0.5, copies=1)[0]\n\n#             else:\n#                 process = apply_blur(image, 2.5, copies=1)[0]\n\n#             dst_file = f'cm-1-{filename}'\n#             tf.keras.utils.array_to_img(tf.image.resize(process, (96, 96))).save(f'train/{dst_file}')\n#             new_train_meta.append([dst_file, label])\n\n#             del image\n\n#         c += 1\n#         print(f'{c}/{train_batch}', end='\\r')\n\n# train_df_2 = pd.DataFrame(new_train_meta, columns=['image', 'cultivar'])\n# train_df_2","metadata":{"papermill":{"duration":4058.014931,"end_time":"2022-05-03T05:05:30.176314","exception":false,"start_time":"2022-05-03T03:57:52.161383","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T04:16:07.830564Z","iopub.status.idle":"2022-05-25T04:16:07.831091Z","shell.execute_reply.started":"2022-05-25T04:16:07.830789Z","shell.execute_reply":"2022-05-25T04:16:07.830818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_train = pd.concat([df_train, train_df_1, train_df_2], ignore_index=True)\n# df_train","metadata":{"papermill":{"duration":11.19673,"end_time":"2022-05-03T05:05:52.500531","exception":false,"start_time":"2022-05-03T05:05:41.303801","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T04:16:07.8327Z","iopub.status.idle":"2022-05-25T04:16:07.833278Z","shell.execute_reply.started":"2022-05-25T04:16:07.832927Z","shell.execute_reply":"2022-05-25T04:16:07.832955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.to_csv('train_meta.csv', index=False)","metadata":{"papermill":{"duration":11.341393,"end_time":"2022-05-03T05:06:15.066399","exception":false,"start_time":"2022-05-03T05:06:03.725006","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T04:18:10.535463Z","iopub.execute_input":"2022-05-25T04:18:10.5362Z","iopub.status.idle":"2022-05-25T04:18:10.581676Z","shell.execute_reply.started":"2022-05-25T04:18:10.536117Z","shell.execute_reply":"2022-05-25T04:18:10.580423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=[24, 6], dpi=200)\nsns.countplot(x=new_train_meta_['cultivar'])\nplt.xticks(rotation=60)\nplt.show()","metadata":{"papermill":{"duration":13.017821,"end_time":"2022-05-03T05:06:39.218484","exception":false,"start_time":"2022-05-03T05:06:26.200663","status":"completed"},"tags":[],"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-05-25T04:18:12.974869Z","iopub.execute_input":"2022-05-25T04:18:12.975162Z","iopub.status.idle":"2022-05-25T04:18:14.972159Z","shell.execute_reply.started":"2022-05-25T04:18:12.97513Z","shell.execute_reply":"2022-05-25T04:18:14.971156Z"},"trusted":true},"execution_count":null,"outputs":[]}]}