{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Process Graham\n\nsource: https://github.com/btgraham/SparseConvNet/blob/kaggle_Diabetic_Retinopathy_competition/Data/kaggleDiabeticRetinopathy/preprocessImages.py\n\nI create dataset that preprocessed using Ben Graham's preprocessing function. I found that this pre-processing is used in several experiments."},{"metadata":{"trusted":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Preprocess training images.\n# Scale 300 seems to be sufficient; 500 and 1000 may be overkill\nimport os\nimport cv2\nimport numpy\nimport fnmatch\nimport shutil\nfrom matplotlib import pyplot as plt\nplt.style.use(\"dark_background\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# create train_graham for output directory first\nif os.path.isdir(\"train_graham\") == False:\n    os.mkdir(\"train_graham\")\n    \n!cp \"../input/aptos2019-blindness-detection/train.csv\" \"./\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"source_dir = \"../input/aptos2019-blindness-detection/train_images\"\ntarget_dir = \"train_graham\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def scaleRadius(img, scale):\n    # sum one row & columns over channels\n    x = img[img.shape[0] // 2, :, :].sum(1)\n    r = (x > x.mean() / 10).sum() / 2\n    s = scale * 1.0 / r\n    return cv2.resize(img, (0, 0), fx=s, fy=s)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# for scale in [300, 500, 1000]:\nscale = 300\ncounter = 0\nfor f in fnmatch.filter(os.listdir(source_dir), \"*.png\"):\n    try:\n        a = cv2.imread(os.path.join(source_dir, f))\n        \n        # scale img to a given radius\n        a = scaleRadius(a, scale)\n        \n        # create masking to remove outer 10%\n        b = numpy.zeros(a.shape)\n        cv2.circle(b, (a.shape[1] // 2, a.shape[0] // 2), int(scale * 0.9),\n                   (1, 1, 1), -1, 8, 0)\n        \n        # subtract local mean color\n        aa = cv2.addWeighted(a, 4, cv2.GaussianBlur(a, (0, 0), scale / 30), -4,\n                             128) * b + 128 * (1 - b)\n        \n        # save the image\n        cv2.imwrite(os.path.join(target_dir, f), aa)\n\n        if counter % 200 == 0:\n            print(\"processed images: \", counter)\n        counter += 1\n\n    except:\n        print(f)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output_img = fnmatch.filter(os.listdir(\"train_graham\"), \"*.png\")\noutput_img[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_output = cv2.imread(os.path.join(target_dir, output_img[0]))\nimg_output = cv2.cvtColor(img_output, cv2.COLOR_BGR2RGB)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(img_output)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"shutil.make_archive(\"train_graham\", 'zip', \"train_graham\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!rm -r \"train_graham\"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Understanding Graham preprocessing"},{"metadata":{"trusted":true},"cell_type":"code","source":"import glob","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fnames = glob.glob(\"../input/aptos2019-blindness-detection/train_images/*.png\")\nfnames[:3]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = cv2.imread(fnames[1])\nscale = 500\nimg.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)\nplt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# let's see, what is x, r, s\n# x is the middle row, contain x columns, 1 channel\n# r is the radius, like column width devided by 2\n# s is scaling factor that will be applied to the image\nx = img[img.shape[0] // 2, :, :].sum(1)\nr = (x > x.mean() / 10).sum() / 2\ns = scale * 1.0 / r\nx, r, s,","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# resize the image according to scaling factor\nimg_rsz = cv2.resize(img, (0, 0), fx=s, fy=s)\nimg_rsz.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# apply gaussian / subtract local mean color\ntemp1 = cv2.GaussianBlur(img_rsz, (0, 0), scale / 30)\nplt.imshow(temp1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# add gaussian weight to the image\ntemp2 = cv2.addWeighted(img_rsz, 4, temp1, -4, 128)\nplt.imshow(temp2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mask = numpy.zeros(img_rsz.shape)\nplt.imshow(mask)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# remove outer 10%\ncv2.circle(mask, (img_rsz.shape[1] // 2, img_rsz.shape[0] // 2),\n           int(scale * 0.9), (1, 1, 1), -1, 8, 0)\nplt.imshow(mask)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# apply the mask\ntemp3 = temp2 * mask + 128 * (1 - mask)\ncv2.imwrite(\"test.png\", temp3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"load_saved = cv2.imread(\"test.png\")\nplt.imshow(load_saved)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}