{"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 pandas as pd \nimport matplotlib.pyplot as plt\nfrom PIL import Image\nfrom skimage.transform import rescale, resize, downscale_local_mean","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-09T20:54:11.102728Z","iopub.execute_input":"2022-03-09T20:54:11.103039Z","iopub.status.idle":"2022-03-09T20:54:11.107519Z","shell.execute_reply.started":"2022-03-09T20:54:11.103006Z","shell.execute_reply":"2022-03-09T20:54:11.106822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/ultra-mnist/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-09T20:29:24.452956Z","iopub.execute_input":"2022-03-09T20:29:24.453760Z","iopub.status.idle":"2022-03-09T20:29:24.487336Z","shell.execute_reply.started":"2022-03-09T20:29:24.453716Z","shell.execute_reply":"2022-03-09T20:29:24.486694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def view_image(idx, figsize=(10, 10), img_range=None):\n    img = Image.open(f\"../input/ultra-mnist/train/{train['id'][idx]}.jpeg\")\n    fig = plt.figure(figsize=figsize)\n    if img_range:\n        plt.imshow(np.array(img)[img_range], cmap='gray')\n    else:\n        plt.imshow(np.array(img), cmap='gray')\n    plt.title(f\"Sum = {train['digit_sum'][idx]}\")\n    plt.tick_params(left=False, bottom=False, labelleft=False, labelbottom=False)\n    return np.array(img)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T20:30:53.655813Z","iopub.execute_input":"2022-03-09T20:30:53.656253Z","iopub.status.idle":"2022-03-09T20:30:53.662051Z","shell.execute_reply.started":"2022-03-09T20:30:53.656224Z","shell.execute_reply":"2022-03-09T20:30:53.661158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Sample images","metadata":{}},{"cell_type":"code","source":"img = view_image(1234)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T20:30:55.023877Z","iopub.execute_input":"2022-03-09T20:30:55.024499Z","iopub.status.idle":"2022-03-09T20:30:57.632505Z","shell.execute_reply.started":"2022-03-09T20:30:55.024468Z","shell.execute_reply":"2022-03-09T20:30:57.631647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- I could clearly see digit 1 and 4 (sum 5)\n- The remaining 1, is something not clear\n- How to deal with the three perpendicular lines each with a small gap and the other thick one nearby on the left\n- I would count this as 4 + 1 + (three perpendicular 1's) + (one next to the three perpendicular 1's) = 9","metadata":{}},{"cell_type":"code","source":"img = view_image(8531, figsize=(20, 20))","metadata":{"execution":{"iopub.status.busy":"2022-03-09T20:58:14.769918Z","iopub.execute_input":"2022-03-09T20:58:14.770217Z","iopub.status.idle":"2022-03-09T20:58:17.603960Z","shell.execute_reply.started":"2022-03-09T20:58:14.770184Z","shell.execute_reply":"2022-03-09T20:58:17.603141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here the sum is 6\n- However I see 6, 0, 0, 1 in the image (summing up to 7)","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Images with smallest digit (that I found so far)","metadata":{}},{"cell_type":"code","source":"img = view_image(19384, figsize=(20, 20))","metadata":{"execution":{"iopub.status.busy":"2022-03-09T20:31:56.406487Z","iopub.execute_input":"2022-03-09T20:31:56.406773Z","iopub.status.idle":"2022-03-09T20:31:59.233162Z","shell.execute_reply.started":"2022-03-09T20:31:56.406743Z","shell.execute_reply":"2022-03-09T20:31:59.232338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img[2100:2122, 3937:3949])","metadata":{"execution":{"iopub.status.busy":"2022-03-09T20:43:37.092702Z","iopub.execute_input":"2022-03-09T20:43:37.093256Z","iopub.status.idle":"2022-03-09T20:43:37.278530Z","shell.execute_reply.started":"2022-03-09T20:43:37.093221Z","shell.execute_reply":"2022-03-09T20:43:37.277886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The smallest image is hardly 20x10 pixel","metadata":{}},{"cell_type":"code","source":"def rescale_and_view(digit):\n\n    digit_rescaled_20 = rescale(digit, 0.2, anti_aliasing=False)\n    digit_rescaled_40 = rescale(digit, 0.4, anti_aliasing=False)\n    digit_rescaled_50 = rescale(digit, 0.5, anti_aliasing=False)\n    digit_rescaled_60 = rescale(digit, 0.6, anti_aliasing=False)\n    digit_rescaled_80 = rescale(digit, 0.8, anti_aliasing=False)\n\n\n\n    fig, axes = plt.subplots(nrows=1, ncols=6, figsize=(15, 10))\n\n    ax = axes.ravel()\n\n    ax[0].imshow(img[2100:2122, 3937:3949], cmap='gray')\n    ax[0].set_title(\"Original image\")\n\n    ax[1].imshow(digit_rescaled_80, cmap='gray')\n    ax[1].set_title(\"80% rescaled\")\n\n    ax[2].imshow(digit_rescaled_60, cmap='gray')\n    ax[2].set_title(\"60% rescaled\")\n\n    ax[3].imshow(digit_rescaled_50, cmap='gray')\n    ax[3].set_title(\"50% rescaled\")\n\n    ax[4].imshow(digit_rescaled_40, cmap='gray')\n    ax[4].set_title(\"40% rescaled\")\n\n    ax[5].imshow(digit_rescaled_20, cmap='gray')\n    ax[5].set_title(\"20% rescaled\")\n\n\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T20:54:58.832639Z","iopub.execute_input":"2022-03-09T20:54:58.833455Z","iopub.status.idle":"2022-03-09T20:54:58.842185Z","shell.execute_reply.started":"2022-03-09T20:54:58.833418Z","shell.execute_reply":"2022-03-09T20:54:58.841532Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize how much reduction can be made on the original image in order to correctly visualize the smallest digit","metadata":{}},{"cell_type":"code","source":"rescale_and_view(digit = img[2100:2122, 3937:3949])","metadata":{"execution":{"iopub.status.busy":"2022-03-09T20:55:13.259645Z","iopub.execute_input":"2022-03-09T20:55:13.260536Z","iopub.status.idle":"2022-03-09T20:55:13.989506Z","shell.execute_reply.started":"2022-03-09T20:55:13.260494Z","shell.execute_reply":"2022-03-09T20:55:13.988014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Looks like scaling down to almost 50% loses so much information from the original image and anything below 50% is hardly recognisable","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}