{"cells":[{"metadata":{"_uuid":"a3a93231d926f74c0c4f251999b234a18d466352"},"cell_type":"markdown","source":"# Convert Strokes to RGB Images\n\nThis kernel explains how to pre-process the image to create rbg images, for using in the traditional pretrained networks. \nWe use the opencv package in python to achieve this. \n\nreference: [🐘Greyscale MobileNet [LB=0.892]](https://www.kaggle.com/gaborfodor/greyscale-mobilenet-lb-0-892)"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"%matplotlib inline\nimport pandas as pd\nimport numpy as np\nimport os\nimport ast\nimport cv2\n\nimport matplotlib.pyplot as plt\nplt.rcParams['figure.figsize'] = [16, 10]\nplt.rcParams['font.size'] = 14\n\nfrom tensorflow.keras.applications.resnet50 import preprocess_input","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e5531b4eae4706d511a298cccb278ac927ab8507"},"cell_type":"code","source":"BASE_SIZE = 256\n\nimg_size = 64\nbatchsize = 128\nline_width = 7","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"379434089e331ae7b7f4878ef160ef7d126756e4"},"cell_type":"markdown","source":"## Colors Input\n+ I have used a cyclic progression of colors. We can use different palettes available in [seaborn](https://seaborn.pydata.org/tutorial/color_palettes.html) and [Matplotlib](https://matplotlib.org/users/colormaps.html)\n    + Caution : Do not use palettes with huge variation since it might end up as noise to the model. "},{"metadata":{"trusted":true,"_uuid":"4f11d96203752880b849b511599d76665524c5df"},"cell_type":"code","source":"colors = [(255, 0, 0) , (255, 255, 0),  (128, 255, 0),  (0, 255, 0), (0, 255, 128), (0, 255, 255), \n          (0, 128, 255), (0, 0, 255), (128, 0, 255), (255, 0, 255)]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0ec5fa7cfd2347861447f5c20111d55e2203cb7b"},"cell_type":"markdown","source":"## Strokes to RGB function"},{"metadata":{"trusted":true,"_uuid":"4f11d96203752880b849b511599d76665524c5df"},"cell_type":"code","source":"def draw_cv2(raw_strokes, size=256, lw=7, time_color=True):\n    img = np.zeros((BASE_SIZE, BASE_SIZE, 3), np.uint8)\n    for t, stroke in enumerate(raw_strokes):\n        for i in range(len(stroke[0]) - 1):\n            color = colors[min(t, len(colors)-1)]\n            _ = cv2.line(img, (stroke[0][i], stroke[1][i]),\n                         (stroke[0][i + 1], stroke[1][i + 1]), color, lw, lineType=cv2.LINE_AA)\n    if size != BASE_SIZE:\n        return cv2.resize(img, (size, size))\n    else:\n        return img","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2c5aaa71d46848c2c12da606a4d2831ca8869640"},"cell_type":"markdown","source":"## Test Image Generator "},{"metadata":{"trusted":true,"_uuid":"9299fccde6a7d2dbd005fa3638cedc5389c83873"},"cell_type":"code","source":"def test_generator(img_size, batchsize, lw=6):\n    while True:\n        for df in pd.read_csv('../input/test_simplified.csv', chunksize=batchsize):\n            df['drawing'] = df['drawing'].apply(ast.literal_eval)\n            x = np.zeros((len(df), img_size, img_size, 3))\n            for i, raw_strokes in enumerate(df.drawing.values):\n                x[i, :, :, :] = draw_cv2(raw_strokes, size=img_size, lw=lw)\n            yield x, preprocess_input(x).astype(np.float32)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"231053bc8133ba200a746fff2b3ab815cffc70ef"},"cell_type":"code","source":"test_datagen = test_generator(img_size, batchsize, line_width)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ba0fe9ab6dd7cedde383af2b12a7831a730e221b"},"cell_type":"markdown","source":"## Normal Images "},{"metadata":{"trusted":true,"_uuid":"830fd5d38cee1e47991ef6d0f2c125f80d910ad7"},"cell_type":"code","source":"x, xi = next(test_datagen)\nn = 8\nfig, axs = plt.subplots(nrows=n, ncols=n, sharex=True, sharey=True, figsize=(12, 12))\nfor i in range(n**2):\n    ax = axs[i // n, i % n]\n    ax.imshow(x[i])\n    ax.axis('off')\nplt.tight_layout()\nfig.savefig('gs.png', dpi=300)\nplt.show();","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a8fcfb8e8e702ef97a519867e4582507cf555506"},"cell_type":"markdown","source":"## Normalized Images for Resnet"},{"metadata":{"trusted":true,"_uuid":"7c3992aae7d73b159e2f57b409dd4e34a34623db"},"cell_type":"code","source":"fig, axs = plt.subplots(nrows=n, ncols=n, sharex=True, sharey=True, figsize=(12, 12))\nfor i in range(n**2):\n    ax = axs[i // n, i % n]\n    ax.imshow(xi[i]) # use ax.imshow(xi[i] * 255)\n    ax.axis('off')\nplt.tight_layout()\nfig.savefig('gs.png', dpi=300)\nplt.show();","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"85abe1d628866c943c7b5ca4736ba9319aab8e80"},"cell_type":"markdown","source":"Thanks.\n\nFurther Experimentation:\n+ What should be the colour palette which gives best result ?"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}