{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport cv2\nimport random\nimport math\nimport time, os\nfrom PIL import Image, ImageEnhance\nfrom matplotlib import pyplot as plt\nfrom glob import glob\n\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## What is the purpose of this notebook?\nThere is difference between train images and test images, I will do the following analysis.\n- visualizing train images and test images, comparing their distributions, found a few main differences\n- adding image augmentation methods onto train images\n- comparing augmented train images with test images"},{"metadata":{},"cell_type":"markdown","source":"## visualize train_images"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"nrows, ncols = 4, 8\n\nfig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(20, 20))\n\nfor i,fname in enumerate(np.random.choice(glob('../input/pku-autonomous-driving/train_images/*'), nrows*ncols)):\n    img = Image.open(fname)\n    axes[i//ncols, i%ncols].imshow(img)\n    axes[i//ncols, i%ncols].axis('off')\n\nplt.subplots_adjust(left=0.1, bottom=0.7, right=0.9, top=0.9,  wspace=0.01, hspace=0.1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## visualize test_images\nTune the random seed to randomly browse the test images"},{"metadata":{"trusted":true},"cell_type":"code","source":"nrows, ncols = 4, 8\nseed = np.random.randint(1, 2019)\nprint('seed=', seed)\nnp.random.seed(seed)\n\nfig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(20, 20))\n\nfor i,fname in enumerate(np.random.choice(glob('../input/pku-autonomous-driving/test_images/*'), nrows*ncols)):\n    img = Image.open(fname)\n    axes[i//ncols, i%ncols].imshow(img)\n    axes[i//ncols, i%ncols].axis('off')\n    axes[i//ncols, i%ncols].set_title(fname.split('/')[-1][:-4])\n\nplt.subplots_adjust(left=0.1, bottom=0.7, right=0.9, top=0.9, wspace=0.01, hspace=0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# img = cv2.imread('../input/pku-autonomous-driving/test_images/ID_23495df5e.jpg')\n# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n# fig = plt.figure(figsize=(8,8))\n# plt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## ...found four train-test differences\nThe following are found in the test images and **NOT found in most of the train images**, which cound be manully added.\n1. contrast\n2. brightness\n3. noise\n4. TODO: color jittering"},{"metadata":{},"cell_type":"markdown","source":"### 1. contrast"},{"metadata":{"trusted":true},"cell_type":"code","source":"fname_list = ['../input/pku-autonomous-driving/test_images/ID_73094e19c.jpg', \n              '../input/pku-autonomous-driving/test_images/ID_d4cd0361c.jpg',\n              '../input/pku-autonomous-driving/test_images/ID_ff99f8c4b.jpg', \n              '../input/pku-autonomous-driving/test_images/ID_23495df5e.jpg',  \n             ]\n\nfig, axes = plt.subplots(nrows=1, ncols=4, figsize=(20, 10))\n\nfor i,ax in enumerate(axes):\n    fname = fname_list[i]\n    img = cv2.imread(fname)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    ax.imshow(img)\n    ax.axis('off')\n\nplt.subplots_adjust(left=0.1, bottom=0.7, right=0.7, top=0.9, wspace=0.05, hspace=0.2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 2. brightness"},{"metadata":{"trusted":true},"cell_type":"code","source":"fname_list = ['../input/pku-autonomous-driving/test_images/ID_349fe3d94.jpg', \n              '../input/pku-autonomous-driving/test_images/ID_ea29783ac.jpg',\n              '../input/pku-autonomous-driving/test_images/ID_fec6066ea.jpg', \n              '../input/pku-autonomous-driving/test_images/ID_38aaf4faa.jpg',  \n             ]\n\nfig, axes = plt.subplots(nrows=1, ncols=4, figsize=(20, 10))\n\nfor i,ax in enumerate(axes):\n    fname = fname_list[i]\n    img = cv2.imread(fname)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    ax.imshow(img)\n    ax.axis('off')\n\nplt.subplots_adjust(left=0.1, bottom=0.7, right=0.7, top=0.9, wspace=0.05, hspace=0.2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 3. noise"},{"metadata":{"trusted":true},"cell_type":"code","source":"fname_list = ['../input/pku-autonomous-driving/test_images/ID_b2d1b80e3.jpg', \n              '../input/pku-autonomous-driving/test_images/ID_3970b0062.jpg',\n              '../input/pku-autonomous-driving/test_images/ID_b185c63d9.jpg', \n              '../input/pku-autonomous-driving/test_images/ID_08b7d6f4a.jpg',  \n             ]\n\nfig, axes = plt.subplots(nrows=1, ncols=4, figsize=(20, 10))\n\nfor i,ax in enumerate(axes):\n    fname = fname_list[i]\n    img = cv2.imread(fname)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    ax.imshow(img)\n    ax.axis('off')\n\nplt.subplots_adjust(left=0.1, bottom=0.7, right=0.7, top=0.9, wspace=0.05, hspace=0.2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 4. TODO: color jittering"},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Add augmentations on train images"},{"metadata":{},"cell_type":"markdown","source":"### 1. contrast"},{"metadata":{"trusted":true},"cell_type":"code","source":"seed = np.random.randint(1, 2019)\nprint('seed=', seed)\nnp.random.seed(seed)\n\nfname_list = np.random.choice(glob('../input/pku-autonomous-driving/train_images/*'), 4)\n\nfig, axes = plt.subplots(nrows=1, ncols=4, figsize=(20, 10))\n\nfor i,ax in enumerate(axes):\n    fname = fname_list[i]\n    img = Image.open(fname)\n    ##add contrast here\n    enh = ImageEnhance.Contrast(img)\n    img_enh = enh.enhance(np.random.uniform(1.5, 2))#PIL.Image\n    img_enh = np.asarray(img_enh)\n    img_enh = (img_enh/255).astype('float32')\n    ##plot\n    ax.imshow(img_enh)\n    ax.axis('off')\n\nplt.subplots_adjust(left=0.1, bottom=0.7, right=0.7, top=0.9, wspace=0.05, hspace=0.2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 2. brightness"},{"metadata":{"trusted":true},"cell_type":"code","source":"seed = np.random.randint(1, 2019)\nprint('seed=', seed)\nnp.random.seed(seed)\n\nfname_list = np.random.choice(glob('../input/pku-autonomous-driving/train_images/*'), 4)\n\nfig, axes = plt.subplots(nrows=1, ncols=4, figsize=(20, 10))\n\nfor i,ax in enumerate(axes):\n    fname = fname_list[i]\n    img = Image.open(fname)\n    ##add Brightness here\n    enh = ImageEnhance.Brightness(img)\n    img_enh = enh.enhance(np.random.uniform(0.5, 1.0))#PIL.Image\n    img_enh = np.asarray(img_enh)\n    img_enh = (img_enh/255).astype('float32')\n    ##plot\n    ax.imshow(img_enh)\n    ax.axis('off')\n\nplt.subplots_adjust(left=0.1, bottom=0.7, right=0.7, top=0.9, wspace=0.05, hspace=0.2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 3. add noise"},{"metadata":{"trusted":true},"cell_type":"code","source":"def add_noise(image):\n    \"\"\"gauss noise\"\"\"\n    row,col,ch= image.shape\n    mean = 0\n    var = np.random.random()*0.01 #0.001~0.01\n    sigma = var**0.5\n    gauss = np.random.normal(mean,sigma,(row,col,ch))\n    gauss = gauss.reshape(row,col,ch)\n    noisy = image + gauss\n    noisy = np.clip(noisy, 0, 1)\n    return noisy","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"seed = np.random.randint(1, 2019)\nprint('seed=', seed)\nnp.random.seed(seed)\n\nfname_list = np.random.choice(glob('../input/pku-autonomous-driving/train_images/*'), 4)\n\nfig, axes = plt.subplots(nrows=1, ncols=4, figsize=(20, 10))\n\nfor i,ax in enumerate(axes):\n    fname = fname_list[i]\n    img = plt.imread(fname)\n    img = (img/255).astype('float32')\n    ##add noise here\n    img_enh = add_noise(img)#gauss\n    ##plot\n    ax.imshow(img_enh)\n    ax.axis('off')\n\nplt.subplots_adjust(left=0.1, bottom=0.7, right=0.7, top=0.9, wspace=0.05, hspace=0.2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### What next?\nIt is always helpful to have training data distribution similar to test data, hence we could try training with image augmentations from this notebook and see if it helps model generalization on the testset."}],"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":1}