{"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":"from pathlib import Path","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:05:50.462024Z","iopub.execute_input":"2022-10-14T13:05:50.463204Z","iopub.status.idle":"2022-10-14T13:05:50.49434Z","shell.execute_reply.started":"2022-10-14T13:05:50.463069Z","shell.execute_reply":"2022-10-14T13:05:50.493148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! ls ","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:05:56.081516Z","iopub.execute_input":"2022-10-14T13:05:56.081941Z","iopub.status.idle":"2022-10-14T13:05:57.172696Z","shell.execute_reply.started":"2022-10-14T13:05:56.081902Z","shell.execute_reply":"2022-10-14T13:05:57.171373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_images = list(Path(\"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n02124075\").rglob(\"*.JPEG\"))","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:07:45.316963Z","iopub.execute_input":"2022-10-14T13:07:45.31812Z","iopub.status.idle":"2022-10-14T13:07:47.380321Z","shell.execute_reply.started":"2022-10-14T13:07:45.318064Z","shell.execute_reply":"2022-10-14T13:07:47.37877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:07:47.382704Z","iopub.execute_input":"2022-10-14T13:07:47.383145Z","iopub.status.idle":"2022-10-14T13:07:47.388876Z","shell.execute_reply.started":"2022-10-14T13:07:47.383109Z","shell.execute_reply":"2022-10-14T13:07:47.387441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:09:10.18031Z","iopub.execute_input":"2022-10-14T13:09:10.180824Z","iopub.status.idle":"2022-10-14T13:09:10.186579Z","shell.execute_reply.started":"2022-10-14T13:09:10.180785Z","shell.execute_reply":"2022-10-14T13:09:10.18553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:10:05.015175Z","iopub.execute_input":"2022-10-14T13:10:05.016146Z","iopub.status.idle":"2022-10-14T13:10:05.021752Z","shell.execute_reply.started":"2022-10-14T13:10:05.016095Z","shell.execute_reply":"2022-10-14T13:10:05.020434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = random.randint(0, len(cat_images))","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:10:16.297667Z","iopub.execute_input":"2022-10-14T13:10:16.298195Z","iopub.status.idle":"2022-10-14T13:10:16.308419Z","shell.execute_reply.started":"2022-10-14T13:10:16.298152Z","shell.execute_reply":"2022-10-14T13:10:16.306792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 1092","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:16:18.793552Z","iopub.execute_input":"2022-10-14T13:16:18.794239Z","iopub.status.idle":"2022-10-14T13:16:18.799695Z","shell.execute_reply.started":"2022-10-14T13:16:18.7942Z","shell.execute_reply":"2022-10-14T13:16:18.798122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(i)\nimage = Image.open(cat_images[i])\nimage.save(\"sharp1.png\")\nimage","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:16:20.429725Z","iopub.execute_input":"2022-10-14T13:16:20.430711Z","iopub.status.idle":"2022-10-14T13:16:20.604866Z","shell.execute_reply.started":"2022-10-14T13:16:20.430665Z","shell.execute_reply":"2022-10-14T13:16:20.603316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"j = random.randint(0, len(cat_images))\nprint(j)\nimage_blurred = cv2.blur(np.array(Image.open(cat_images[j])),\n                         (5, 5))\nimage_blurred = Image.fromarray(image_blurred)\nimage_blurred.save(\"blurry.png\")\nimage_blurred","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:17:03.005824Z","iopub.execute_input":"2022-10-14T13:17:03.006261Z","iopub.status.idle":"2022-10-14T13:17:03.26295Z","shell.execute_reply.started":"2022-10-14T13:17:03.006227Z","shell.execute_reply":"2022-10-14T13:17:03.261461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.filters import laplace","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:17:00.023166Z","iopub.execute_input":"2022-10-14T13:17:00.023624Z","iopub.status.idle":"2022-10-14T13:17:00.028872Z","shell.execute_reply.started":"2022-10-14T13:17:00.023587Z","shell.execute_reply":"2022-10-14T13:17:00.027591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.array(image).dtype","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:22:00.28356Z","iopub.execute_input":"2022-10-14T13:22:00.283999Z","iopub.status.idle":"2022-10-14T13:22:00.292647Z","shell.execute_reply.started":"2022-10-14T13:22:00.283963Z","shell.execute_reply":"2022-10-14T13:22:00.291298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l = np.array(image).astype(\"float64\")\nl -= l.min()\nl /= l.max()\nl = laplace(l)\nprint(l.var())\nl -= l.min()\nl /= l.max()\nl *= 255\nl_sharp = l.astype('uint8')\n\nimage_laplace = Image.fromarray(l_sharp)\nimage_laplace.save(\"laplace_sharp.png\")\nimage_laplace","metadata":{"execution":{"iopub.status.busy":"2022-10-14T14:03:40.266446Z","iopub.execute_input":"2022-10-14T14:03:40.266929Z","iopub.status.idle":"2022-10-14T14:03:40.434676Z","shell.execute_reply.started":"2022-10-14T14:03:40.266893Z","shell.execute_reply":"2022-10-14T14:03:40.43352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l = np.array(image_blurred).astype(\"float64\")\nl -= l.min()\nl /= l.max()\nl = laplace(l)\nprint(l.var())\nl -= l.min()\nl /= l.max()\nl *= 255\nl_blurry = l.astype('uint8')\n\nimage_laplace = Image.fromarray(l_blurry)\nimage_laplace.save(\"laplace_blurred.png\")\nimage_laplace","metadata":{"execution":{"iopub.status.busy":"2022-10-14T14:03:14.891613Z","iopub.execute_input":"2022-10-14T14:03:14.892242Z","iopub.status.idle":"2022-10-14T14:03:15.054403Z","shell.execute_reply.started":"2022-10-14T14:03:14.892187Z","shell.execute_reply":"2022-10-14T14:03:15.052956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = random.sample(cat_images, 30)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:32:08.884327Z","iopub.execute_input":"2022-10-14T13:32:08.884883Z","iopub.status.idle":"2022-10-14T13:32:08.891832Z","shell.execute_reply.started":"2022-10-14T13:32:08.88484Z","shell.execute_reply":"2022-10-14T13:32:08.890133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sharp_image_paths = images[:15]\nblurry_image_paths = images[15:]","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:33:15.119189Z","iopub.execute_input":"2022-10-14T13:33:15.119611Z","iopub.status.idle":"2022-10-14T13:33:15.125508Z","shell.execute_reply.started":"2022-10-14T13:33:15.119576Z","shell.execute_reply":"2022-10-14T13:33:15.123881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"assert len(sharp_image_paths) == len(blurry_image_paths)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:33:15.399007Z","iopub.execute_input":"2022-10-14T13:33:15.399672Z","iopub.status.idle":"2022-10-14T13:33:15.404359Z","shell.execute_reply.started":"2022-10-14T13:33:15.399628Z","shell.execute_reply":"2022-10-14T13:33:15.403014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sharp_images = [np.array(Image.open(p)) for p in sharp_image_paths]\nblurry_images = [cv2.blur(np.array(Image.open(p)), (k, k)) \n                 for p, k in zip(sharp_image_paths,\n                                 [random.randint(3, 5)\n                                  for i in range(len(sharp_image_paths))])]","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:42:42.754118Z","iopub.execute_input":"2022-10-14T13:42:42.754543Z","iopub.status.idle":"2022-10-14T13:42:43.167203Z","shell.execute_reply.started":"2022-10-14T13:42:42.75451Z","shell.execute_reply":"2022-10-14T13:42:43.165892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sharp_images_variance = [laplace(s).var() for s in sharp_images]\nblurry_images_variance = [laplace(b).var() for b in blurry_images]","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:42:43.168998Z","iopub.execute_input":"2022-10-14T13:42:43.169329Z","iopub.status.idle":"2022-10-14T13:42:44.076675Z","shell.execute_reply.started":"2022-10-14T13:42:43.169299Z","shell.execute_reply":"2022-10-14T13:42:44.075656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nfrom matplotlib.ticker import FixedLocator, FixedFormatter","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:42:44.077897Z","iopub.execute_input":"2022-10-14T13:42:44.078212Z","iopub.status.idle":"2022-10-14T13:42:44.083594Z","shell.execute_reply.started":"2022-10-14T13:42:44.078183Z","shell.execute_reply":"2022-10-14T13:42:44.082405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(6, 4), dpi=300)\nax = fig.add_axes([0, 0, 1, 1])\nax.grid(False)\nax.scatter(sharp_images_variance, [1 for i in range(len(blurry_images_variance))], label=\"Blurry\", c=\"#2A64B7\")\nax.scatter(blurry_images_variance, [0 for i in range(len(sharp_images_variance))], label=\"Sharp\", c=\"#327545\")\nax.set_xlabel(\"Laplace Variance\")\nax.set_xscale(\"log\")\nax.set_ylabel(\"Blurriness\")\nax.set_title(\"Blurry or Sharp?\")\n\ny_formatter = FixedFormatter([\"0 %\", \"50 %\", \"100 %\"])\ny_locator = FixedLocator([0, 0.5, 1])\nax.yaxis.set_major_formatter(y_formatter)\nax.yaxis.set_major_locator(y_locator)\n\nplt.savefig(\"blurry_or_sharp.png\", bbox_inches='tight', pad_inches=0, transparent=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T14:08:39.413251Z","iopub.execute_input":"2022-10-14T14:08:39.414595Z","iopub.status.idle":"2022-10-14T14:08:40.429727Z","shell.execute_reply.started":"2022-10-14T14:08:39.414544Z","shell.execute_reply":"2022-10-14T14:08:40.428769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:53:11.907351Z","iopub.execute_input":"2022-10-14T13:53:11.907776Z","iopub.status.idle":"2022-10-14T13:53:12.256274Z","shell.execute_reply.started":"2022-10-14T13:53:11.907733Z","shell.execute_reply":"2022-10-14T13:53:12.254846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.hstack([sharp_images_variance, blurry_images_variance])[:, None]\ny = np.hstack([[1 for i in range(len(sharp_images))],\n               [0 for i in range(len(sharp_images))]])","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:54:48.509265Z","iopub.execute_input":"2022-10-14T13:54:48.510068Z","iopub.status.idle":"2022-10-14T13:54:48.517263Z","shell.execute_reply.started":"2022-10-14T13:54:48.510025Z","shell.execute_reply":"2022-10-14T13:54:48.515613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\nX_transformed = scaler.fit_transform(x)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:54:09.181759Z","iopub.execute_input":"2022-10-14T13:54:09.182652Z","iopub.status.idle":"2022-10-14T13:54:09.190531Z","shell.execute_reply.started":"2022-10-14T13:54:09.182599Z","shell.execute_reply":"2022-10-14T13:54:09.189012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = LogisticRegression(penalty='none').fit(X_transformed, y)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:54:51.771822Z","iopub.execute_input":"2022-10-14T13:54:51.772253Z","iopub.status.idle":"2022-10-14T13:54:51.785804Z","shell.execute_reply.started":"2022-10-14T13:54:51.772219Z","shell.execute_reply":"2022-10-14T13:54:51.784315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w = clf.coef_[0][0]\nb = clf.intercept_[0]","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:55:09.293171Z","iopub.execute_input":"2022-10-14T13:55:09.294709Z","iopub.status.idle":"2022-10-14T13:55:09.300941Z","shell.execute_reply.started":"2022-10-14T13:55:09.294632Z","shell.execute_reply":"2022-10-14T13:55:09.299379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(6, 4), dpi=300)\nax = fig.add_axes([0, 0, 1, 1])\nax.grid(False)\nmodel = np.linspace(x.min(), x.max())\nax.plot(model,\n        1 / (1 + np.exp(-(w * scaler.transform(model[:, None]) + b))), \n        label=\"Prediction\", c=\"#000\", linestyle=\"--\")\nax.scatter(sharp_images_variance, [1 for i in range(len(blurry_images_variance))], label=\"Blurry\", c=\"#2A64B7\")\nax.scatter(blurry_images_variance, [0 for i in range(len(sharp_images_variance))], label=\"Sharp\", c=\"#327545\")\nax.set_xlabel(\"Laplace Variance\")\nax.set_xscale(\"log\")\nax.set_ylabel(\"Blurriness\")\nax.set_title(\"Blurry or Sharp?\")\n\ny_formatter = FixedFormatter([\"0 %\", \"50 %\", \"100 %\"])\ny_locator = FixedLocator([0, 0.5, 1])\nax.yaxis.set_major_formatter(y_formatter)\nax.yaxis.set_major_locator(y_locator)\n\nplt.savefig(\"blurry_or_sharp_prediction.png\", bbox_inches='tight', pad_inches=0, transparent=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T14:07:18.284888Z","iopub.execute_input":"2022-10-14T14:07:18.2854Z","iopub.status.idle":"2022-10-14T14:07:19.396861Z","shell.execute_reply.started":"2022-10-14T14:07:18.285365Z","shell.execute_reply":"2022-10-14T14:07:19.395525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(6, 4), dpi=300)\nax = fig.add_axes([0, 0, 1, 1])\nax.grid(False)\nmodel = np.linspace(x.min(), x.max())\nax.plot(model,\n        1 / (1 + np.exp(-(w * scaler.transform(model[:, None]) + b))), \n        label=\"Prediction\", c=\"#fff\", linestyle=\"--\")\nax.scatter(sharp_images_variance, [1 for i in range(len(blurry_images_variance))], label=\"Blurry\", c=\"#2A64B7\")\nax.scatter(blurry_images_variance, [0 for i in range(len(sharp_images_variance))], label=\"Sharp\", c=\"#327545\")\nax.set_xlabel(\"Laplace Variance\")\nax.set_xscale(\"log\")\nax.set_ylabel(\"Blurriness\")\nax.set_title(\"Blurry or Sharp?\")\n\ny_formatter = FixedFormatter([\"0 %\", \"50 %\", \"100 %\"])\ny_locator = FixedLocator([0, 0.5, 1])\nax.yaxis.set_major_formatter(y_formatter)\nax.yaxis.set_major_locator(y_locator)\n\nplt.savefig(\"blurry_or_sharp_prediction.png\", bbox_inches='tight', pad_inches=0, transparent=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T14:19:02.460987Z","iopub.execute_input":"2022-10-14T14:19:02.461427Z","iopub.status.idle":"2022-10-14T14:19:03.560622Z","shell.execute_reply.started":"2022-10-14T14:19:02.461394Z","shell.execute_reply":"2022-10-14T14:19:03.559536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}