{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":21755,"databundleVersionId":1475600,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport math\nimport random\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\nimport albumentations as A","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-17T18:35:20.645265Z","iopub.execute_input":"2023-11-17T18:35:20.646676Z","iopub.status.idle":"2023-11-17T18:35:20.654063Z","shell.execute_reply.started":"2023-11-17T18:35:20.646623Z","shell.execute_reply":"2023-11-17T18:35:20.652544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed):\n    random.seed(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    np.random.seed(seed)\n    \n    \nSEED = 42\nset_seed(SEED)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T18:35:20.658747Z","iopub.execute_input":"2023-11-17T18:35:20.659550Z","iopub.status.idle":"2023-11-17T18:35:20.670443Z","shell.execute_reply.started":"2023-11-17T18:35:20.659501Z","shell.execute_reply":"2023-11-17T18:35:20.668621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_PATH = \"../input/gan-getting-started/\"\nMONET_PATH = os.path.join(BASE_PATH, \"monet_jpg\")\nPHOTO_PATH = os.path.join(BASE_PATH, \"photo_jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-11-17T18:35:20.673044Z","iopub.execute_input":"2023-11-17T18:35:20.674073Z","iopub.status.idle":"2023-11-17T18:35:20.684195Z","shell.execute_reply.started":"2023-11-17T18:35:20.674035Z","shell.execute_reply":"2023-11-17T18:35:20.682851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def  print_folder_statistics(path):\n    d_image_sizes = {}\n    for image_name in os.listdir(path):\n        image = cv2.imread(os.path.join(path, image_name))\n        d_image_sizes[image.shape] = d_image_sizes.get(image.shape, 0) + 1\n    for size, count in d_image_sizes.items():\n        print(f\"shape: {size} \\tcount: {count}\")\n        \nprint(\"Monet images:\")\nprint(\" \")\nprint_folder_statistics(MONET_PATH)\nprint(\" \")\nprint(\"-\" * 10)\nprint(\" \")\nprint(\"Photo images:\")\nprint(\" \")\nprint_folder_statistics(PHOTO_PATH)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T18:36:57.919968Z","iopub.execute_input":"2023-11-17T18:36:57.920603Z","iopub.status.idle":"2023-11-17T18:37:14.763138Z","shell.execute_reply.started":"2023-11-17T18:36:57.920564Z","shell.execute_reply":"2023-11-17T18:37:14.761879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def batch_visualization(path, n_images, is_random=True, figsize=(16, 16)):\n    plt.figure(figsize=figsize)\n    \n    w = int(n_images ** .5)\n    h = math.ceil(n_images / w)\n    \n    all_names = os.listdir(path)\n    \n    image_names = all_names[:n_images]\n    if is_random:\n        image_names = random.sample(all_names, n_images)\n    \n    for ind, image_name in enumerate(image_names):\n        img = cv2.imread(os.path.join(path, image_name))\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) \n        plt.subplot(h, w, ind + 1)\n        plt.imshow(img)\n        plt.axis(\"off\")\n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:26:17.113594Z","iopub.execute_input":"2023-11-17T19:26:17.114108Z","iopub.status.idle":"2023-11-17T19:26:17.124492Z","shell.execute_reply.started":"2023-11-17T19:26:17.114073Z","shell.execute_reply":"2023-11-17T19:26:17.123068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_visualization(MONET_PATH, 1, is_random=True, figsize=(5, 5))","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:26:38.590322Z","iopub.execute_input":"2023-11-17T19:26:38.591625Z","iopub.status.idle":"2023-11-17T19:26:38.854586Z","shell.execute_reply.started":"2023-11-17T19:26:38.591578Z","shell.execute_reply":"2023-11-17T19:26:38.853351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_visualization(MONET_PATH, 4, is_random=True, figsize=(10, 10))","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:26:49.049079Z","iopub.execute_input":"2023-11-17T19:26:49.049594Z","iopub.status.idle":"2023-11-17T19:26:49.821487Z","shell.execute_reply.started":"2023-11-17T19:26:49.049557Z","shell.execute_reply":"2023-11-17T19:26:49.820002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_visualization(MONET_PATH, 9, is_random=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:27:03.069386Z","iopub.execute_input":"2023-11-17T19:27:03.069797Z","iopub.status.idle":"2023-11-17T19:27:04.691942Z","shell.execute_reply.started":"2023-11-17T19:27:03.069767Z","shell.execute_reply":"2023-11-17T19:27:04.690529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_visualization(MONET_PATH, 16, is_random=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:27:24.078983Z","iopub.execute_input":"2023-11-17T19:27:24.079392Z","iopub.status.idle":"2023-11-17T19:27:25.830568Z","shell.execute_reply.started":"2023-11-17T19:27:24.079351Z","shell.execute_reply":"2023-11-17T19:27:25.826999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_visualization(MONET_PATH, 300, is_random=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:27:51.723709Z","iopub.execute_input":"2023-11-17T19:27:51.724161Z","iopub.status.idle":"2023-11-17T19:28:07.652789Z","shell.execute_reply.started":"2023-11-17T19:27:51.724127Z","shell.execute_reply":"2023-11-17T19:28:07.650757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_visualization(PHOTO_PATH, 16, is_random=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:38:17.959428Z","iopub.execute_input":"2023-11-17T19:38:17.959931Z","iopub.status.idle":"2023-11-17T19:38:19.701276Z","shell.execute_reply.started":"2023-11-17T19:38:17.959897Z","shell.execute_reply":"2023-11-17T19:38:19.699974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def color_hist_visualization(image_path, figsize=(16, 4)):\n    plt.figure(figsize=figsize)\n    \n    img = cv2.imread(image_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) \n    plt.subplot(1, 4, 1)\n    plt.imshow(img)\n    plt.axis(\"off\")\n    \n    colors = ['red', 'green', 'blue']\n    for i in range(len(colors)):\n        plt.subplot(1, 4, i + 2)\n        plt.hist(\n            img[:, :, i].reshape(-1),\n            bins=25,\n            alpha=0.5,\n            color=colors[i],\n            density=True\n        )\n        plt.xlim(0, 255)\n        plt.xticks([])\n        plt.yticks([])\n    \n    \n    plt.show()        ","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:41:04.643732Z","iopub.execute_input":"2023-11-17T19:41:04.644313Z","iopub.status.idle":"2023-11-17T19:41:04.654974Z","shell.execute_reply.started":"2023-11-17T19:41:04.644267Z","shell.execute_reply":"2023-11-17T19:41:04.653693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = '../input/gan-getting-started/monet_jpg/000c1e3bff.jpg'\ncolor_hist_visualization(img_path)\n\nimg_path = '../input/gan-getting-started/monet_jpg/05144e306f.jpg'\ncolor_hist_visualization(img_path)\n\nimg_path = '../input/gan-getting-started/monet_jpg/16dabe418c.jpg'\ncolor_hist_visualization(img_path)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:41:56.642214Z","iopub.execute_input":"2023-11-17T19:41:56.642633Z","iopub.status.idle":"2023-11-17T19:41:57.954009Z","shell.execute_reply.started":"2023-11-17T19:41:56.642604Z","shell.execute_reply":"2023-11-17T19:41:57.952586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def channels_visualization(image_path, figsize=(16, 4)):\n    plt.figure(figsize=figsize)\n    \n    img = cv2.imread(image_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) \n    plt.subplot(1, 4, 1)\n    plt.imshow(np.mean(img, axis=2), cmap=\"gray\")\n    plt.axis('off')\n    \n    for i in range(3):\n        plt.subplot(1, 4, i + 2)\n        tmp_img = np.full_like(img, 0)\n        tmp_img[:, :, i] = img[:, :, i]\n        plt.imshow(tmp_img)\n        plt.xlim(0, 255)\n        plt.xticks([])\n        plt.yticks([])\n    \n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:43:09.504534Z","iopub.execute_input":"2023-11-17T19:43:09.505380Z","iopub.status.idle":"2023-11-17T19:43:09.515925Z","shell.execute_reply.started":"2023-11-17T19:43:09.505298Z","shell.execute_reply":"2023-11-17T19:43:09.514480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = '../input/gan-getting-started/monet_jpg/51db3fc011.jpg'\ncolor_hist_visualization(img_path)\nchannels_visualization(img_path)\n\nimg_path = '../input/gan-getting-started/monet_jpg/1814cc6632.jpg'\ncolor_hist_visualization(img_path)\nchannels_visualization(img_path)\n\nimg_path = '../input/gan-getting-started/monet_jpg/4995c04b1a.jpg'\ncolor_hist_visualization(img_path)\nchannels_visualization(img_path)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:43:23.656073Z","iopub.execute_input":"2023-11-17T19:43:23.656509Z","iopub.status.idle":"2023-11-17T19:43:27.690094Z","shell.execute_reply.started":"2023-11-17T19:43:23.656471Z","shell.execute_reply":"2023-11-17T19:43:27.688406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def grayscale_visualization(image_path, figsize=(8, 4)):\n    plt.figure(figsize=figsize)\n    \n    img = cv2.imread(image_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) \n    plt.subplot(1, 2, 1)\n    plt.imshow(img)\n    plt.axis('off')\n    \n    plt.subplot(1, 2, 2)\n    tmp_img = np.full_like(img, 0)\n    for i in range(3):\n        tmp_img[:, :, i] = img.mean(axis=-1)\n    plt.imshow(tmp_img)\n    plt.axis('off')\n    \n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:43:42.072008Z","iopub.execute_input":"2023-11-17T19:43:42.072482Z","iopub.status.idle":"2023-11-17T19:43:42.081859Z","shell.execute_reply.started":"2023-11-17T19:43:42.072429Z","shell.execute_reply":"2023-11-17T19:43:42.080829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = '../input/gan-getting-started/monet_jpg/5c79cfe0b3.jpg'\ngrayscale_visualization(img_path)\n\nimg_path = '../input/gan-getting-started/monet_jpg/990ed28f62.jpg'\ngrayscale_visualization(img_path)\n\nimg_path = '../input/gan-getting-started/monet_jpg/fd63a333f1.jpg'\ngrayscale_visualization(img_path)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:43:53.932206Z","iopub.execute_input":"2023-11-17T19:43:53.932634Z","iopub.status.idle":"2023-11-17T19:43:54.786198Z","shell.execute_reply.started":"2023-11-17T19:43:53.932604Z","shell.execute_reply":"2023-11-17T19:43:54.784775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_simple_augmentation(image_path, transform):\n    img = cv2.imread(image_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) \n    \n    plt.figure(figsize=(10, 5))\n    \n    plt.subplot(1, 2, 1)\n    plt.imshow(img)\n    plt.axis(\"off\")\n    \n    plt.subplot(1, 2, 2)\n    x = transform(image=img)[\"image\"]\n    plt.imshow(x)\n    plt.axis(\"off\")\n\n    plt.show()\n    \ndef plot_multiple_augmentation(image_path, transform):\n    img = cv2.imread(image_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) \n    \n    plt.figure(figsize=(10, 10))\n    \n    plt.subplot(2, 2, 1)\n    plt.imshow(img)\n    plt.axis(\"off\")\n    \n    plt.subplot(2, 2, 2)\n    x = transform(image=img)[\"image\"]\n    plt.imshow(x)\n    plt.axis(\"off\")\n    \n    plt.subplot(2, 2, 3)\n    x = transform(image=img)[\"image\"]\n    plt.imshow(x)\n    plt.axis(\"off\")\n    \n    plt.subplot(2, 2, 4)\n    x = transform(image=img)[\"image\"]\n    plt.imshow(x)\n    plt.axis(\"off\")\n\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:48:19.507515Z","iopub.execute_input":"2023-11-17T19:48:19.507961Z","iopub.status.idle":"2023-11-17T19:48:19.522219Z","shell.execute_reply.started":"2023-11-17T19:48:19.507930Z","shell.execute_reply":"2023-11-17T19:48:19.520825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = A.Compose(\n    [\n        A.Blur(p=1.0, blur_limit=(5, 5)),\n    ]\n)\n\nplot_simple_augmentation(\"../input/gan-getting-started/monet_jpg/990ed28f62.jpg\", transform)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:48:27.754912Z","iopub.execute_input":"2023-11-17T19:48:27.755391Z","iopub.status.idle":"2023-11-17T19:48:28.183517Z","shell.execute_reply.started":"2023-11-17T19:48:27.755359Z","shell.execute_reply":"2023-11-17T19:48:28.181165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = A.CLAHE(p=1.0, clip_limit=(10, 10), tile_grid_size=(3, 3))\n\nplot_simple_augmentation(\"../input/gan-getting-started/monet_jpg/990ed28f62.jpg\", transform)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:48:57.416271Z","iopub.execute_input":"2023-11-17T19:48:57.417712Z","iopub.status.idle":"2023-11-17T19:48:57.976382Z","shell.execute_reply.started":"2023-11-17T19:48:57.417641Z","shell.execute_reply":"2023-11-17T19:48:57.974919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = A.CenterCrop(p=1.0, height=100, width=150)\n\nplot_simple_augmentation(\"../input/gan-getting-started/monet_jpg/990ed28f62.jpg\", transform)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:49:08.231963Z","iopub.execute_input":"2023-11-17T19:49:08.232386Z","iopub.status.idle":"2023-11-17T19:49:08.625800Z","shell.execute_reply.started":"2023-11-17T19:49:08.232354Z","shell.execute_reply":"2023-11-17T19:49:08.624277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = A.ChannelDropout(p=1.0, channel_drop_range=(1, 2), fill_value=0)\n\nplot_multiple_augmentation(\"../input/gan-getting-started/monet_jpg/990ed28f62.jpg\", transform)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:49:30.518031Z","iopub.execute_input":"2023-11-17T19:49:30.518513Z","iopub.status.idle":"2023-11-17T19:49:31.142820Z","shell.execute_reply.started":"2023-11-17T19:49:30.518448Z","shell.execute_reply":"2023-11-17T19:49:31.141586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = A.ChannelShuffle(p=1.0)\n\nplot_multiple_augmentation(\"../input/gan-getting-started/monet_jpg/990ed28f62.jpg\", transform)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:49:50.429793Z","iopub.execute_input":"2023-11-17T19:49:50.430330Z","iopub.status.idle":"2023-11-17T19:49:51.122087Z","shell.execute_reply.started":"2023-11-17T19:49:50.430292Z","shell.execute_reply":"2023-11-17T19:49:51.120548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = A.ChannelShuffle(p=1.0)\n\nplot_multiple_augmentation(\"../input/gan-getting-started/monet_jpg/990ed28f62.jpg\", transform)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:50:00.174196Z","iopub.execute_input":"2023-11-17T19:50:00.174714Z","iopub.status.idle":"2023-11-17T19:50:00.879365Z","shell.execute_reply.started":"2023-11-17T19:50:00.174677Z","shell.execute_reply":"2023-11-17T19:50:00.878279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = A.RandomCrop(p=1.0, height=100, width=100)\n\nplot_multiple_augmentation(\"../input/gan-getting-started/monet_jpg/990ed28f62.jpg\", transform)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T19:50:12.117221Z","iopub.execute_input":"2023-11-17T19:50:12.117733Z","iopub.status.idle":"2023-11-17T19:50:12.861155Z","shell.execute_reply.started":"2023-11-17T19:50:12.117697Z","shell.execute_reply":"2023-11-17T19:50:12.859544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}