{"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 \n\nimport 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\nPATH = '../input/pku-autonomous-driving/'\nos.listdir(PATH)\ntrain = pd.read_csv(PATH + 'train.csv')\ntest = pd.read_csv(PATH + 'sample_submission.csv')\n# From camera.zip\ncamera_matrix = np.array([[2304.5479, 0,  1686.2379],\n                          [0, 2305.8757, 1354.9849],\n                          [0, 0, 1]], dtype=np.float32)\ncamera_matrix_inv = np.linalg.inv(camera_matrix)\n\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-21T22:12:01.762672Z","iopub.execute_input":"2021-05-21T22:12:01.763081Z","iopub.status.idle":"2021-05-21T22:12:01.828254Z","shell.execute_reply.started":"2021-05-21T22:12:01.763018Z","shell.execute_reply":"2021-05-21T22:12:01.827019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The fuction below is for transform  PredictionString to list of dicts**","metadata":{}},{"cell_type":"code","source":"def str2coords(s, names=['id', 'yaw', 'pitch', 'roll', 'x', 'y', 'z']):\n    '''\n    Input:\n        s: PredictionString (e.g. from train dataframe)\n        names: array of what to extract from the string\n    Output:\n        list of dicts with keys from `names`\n    '''\n    coords = []\n    for l in np.array(s.split()).reshape([-1, 7]):\n        coords.append(dict(zip(names, l.astype('float'))))\n        if 'id' in coords[-1]:\n            coords[-1]['id'] = int(coords[-1]['id'])\n    return coords","metadata":{"execution":{"iopub.status.busy":"2021-05-21T22:12:05.35481Z","iopub.execute_input":"2021-05-21T22:12:05.355404Z","iopub.status.idle":"2021-05-21T22:12:05.365602Z","shell.execute_reply.started":"2021-05-21T22:12:05.35533Z","shell.execute_reply":"2021-05-21T22:12:05.364375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Example of str2coords function:","metadata":{}},{"cell_type":"code","source":"inp = train['PredictionString'][0]\nprint('Example input:\\n', inp)\nprint()\nprint('Output:\\n', str2coords(inp))","metadata":{"execution":{"iopub.status.busy":"2021-05-21T21:46:41.801846Z","iopub.execute_input":"2021-05-21T21:46:41.802422Z","iopub.status.idle":"2021-05-21T21:46:41.813297Z","shell.execute_reply.started":"2021-05-21T21:46:41.802371Z","shell.execute_reply":"2021-05-21T21:46:41.812408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  **Image Augmentation:**\n\nsorce:https://www.kaggle.com/niuddd/image-augmentations-for-pku-self-driving-car\n\nFor image augmentation we do (1)Contrast  (2)Brightness  (3)Add noise\n\nThe fuction will ramdom select images from train\n\nthen create some enhanced images for augmentation\n\nfinally return (1)dataframe of these images (2)list of these PIL Images \n\nnote:   Add noise will take a little long time","metadata":{}},{"cell_type":"code","source":"def Contrast_enhance(img_number):\n    seed = np.random.randint(1, 2019)\n    np.random.seed(seed)\n    Contrast_data=[]\n    fname_list = np.random.choice(glob('../input/pku-autonomous-driving/train_images/*'), img_number)\n    train.set_index(\"ImageId\" , inplace=True)\n    for i,ax in enumerate(fname_list):\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        ##save image \n        img.save('Contrast_'+str(i)+\".jpg\")\n        ##create enhanced image's PredictionString from oringinal image\n        pstring=train.loc[fname.split('/')[-1].split('.jpg')[0]][0]\n        Contrast_data.append([img_enh,str2coords(pstring)])\n      \n    train.reset_index(inplace=True)    \n    return Contrast_data\n        \n    \n    \n    ","metadata":{"execution":{"iopub.status.busy":"2021-05-21T21:54:35.518168Z","iopub.execute_input":"2021-05-21T21:54:35.518786Z","iopub.status.idle":"2021-05-21T21:54:35.530197Z","shell.execute_reply.started":"2021-05-21T21:54:35.518721Z","shell.execute_reply":"2021-05-21T21:54:35.529402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Brightness_enhance(img_number):\n    seed = np.random.randint(1, 2019)\n    np.random.seed(seed)\n    Brightness_data=[]\n    fname_list = np.random.choice(glob('../input/pku-autonomous-driving/train_images/*'), img_number)\n    train.set_index(\"ImageId\" , inplace=True)\n    for i,ax in enumerate(fname_list):\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        ##save image\n        img.save('Brightness_'+str(i)+\".jpg\")\n        ##create enhanced image's PredictionString from oringinal image\n        pstring=train.loc[fname.split('/')[-1].split('.jpg')[0]][0]\n        Brightness_data.append([img_enh,str2coords(pstring)])\n    train.reset_index(inplace=True)    \n    return Brightness_data\n    ","metadata":{"execution":{"iopub.status.busy":"2021-05-21T22:06:46.896087Z","iopub.execute_input":"2021-05-21T22:06:46.89672Z","iopub.status.idle":"2021-05-21T22:06:46.907003Z","shell.execute_reply.started":"2021-05-21T22:06:46.89667Z","shell.execute_reply":"2021-05-21T22:06:46.905949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\ndef Noise_enhance(img_number):\n    seed = np.random.randint(1, 2019)\n    np.random.seed(seed)\n    Noise_data=[]\n    fname_list = np.random.choice(glob('../input/pku-autonomous-driving/train_images/*'),img_number)\n    train.set_index(\"ImageId\" , inplace=True)\n    for i,ax in enumerate(fname_list):\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        ##create enhanced image's PredictionString from oringinal image\n        pstring=train.loc[fname.split('/')[-1].split('.jpg')[0]][0]\n        Noise_data.append([img_enh,str2coords(pstring)])\n    train.reset_index(inplace=True)    \n    return Noise_data\n        ","metadata":{"execution":{"iopub.status.busy":"2021-05-21T22:09:25.496181Z","iopub.execute_input":"2021-05-21T22:09:25.496651Z","iopub.status.idle":"2021-05-21T22:09:25.509237Z","shell.execute_reply.started":"2021-05-21T22:09:25.496604Z","shell.execute_reply":"2021-05-21T22:09:25.508215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Image preprocessing:**","metadata":{}},{"cell_type":"markdown","source":"**Load train data:**\ntrain_data will be a list contains element:   [image , dict of PredictionString]\n\n\nit's takes long time","metadata":{}},{"cell_type":"code","source":"def load_train(directory_name):\n    train_data = [] \n    train.set_index(\"ImageId\" , inplace=True)\n    for filename in os.listdir(r\"./\"+directory_name):\n        img=plt.imread(directory_name+'/'+filename)\n        pstring=train.loc[filename.split('.jpg')[0]]\n        #print(pstring[0])\n        train_data.append([img,str2coords(pstring[0])])\n        #print(img)\n    train.reset_index(inplace=True)   \ntrain_data=load_train('../input/pku-autonomous-driving/train_images')\n\n\n        ","metadata":{"execution":{"iopub.status.busy":"2021-05-21T22:12:29.65179Z","iopub.execute_input":"2021-05-21T22:12:29.652173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Example to augmentation**\n\nit will create enhanced data,then we can merge it with train_data","metadata":{}},{"cell_type":"code","source":"Noise_data=Noise_enhance(5)\nprint(Noise_data[0][1])\nplt.imshow(Noise_data[0][0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-21T22:12:11.117709Z","iopub.execute_input":"2021-05-21T22:12:11.11842Z","iopub.status.idle":"2021-05-21T22:12:24.465414Z","shell.execute_reply.started":"2021-05-21T22:12:11.118344Z","shell.execute_reply":"2021-05-21T22:12:24.46421Z"},"trusted":true},"execution_count":null,"outputs":[]}]}