{"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 pandas as pd\nimport plotly.express as px\n\nimport os\n\nimport math\nimport random\n\nimport matplotlib.pyplot as plt\nimport matplotlib.gridspec as gridspec\n\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2023-04-05T06:05:14.371335Z","iopub.execute_input":"2023-04-05T06:05:14.372409Z","iopub.status.idle":"2023-04-05T06:05:17.385009Z","shell.execute_reply.started":"2023-04-05T06:05:14.372333Z","shell.execute_reply":"2023-04-05T06:05:17.383457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 데이터 확인","metadata":{}},{"cell_type":"code","source":"data_path = '/kaggle/input/state-farm-distracted-driver-detection/'\n\nimgs_list = pd.read_csv(data_path + 'driver_imgs_list.csv')\nsubmission = pd.read_csv(data_path + 'sample_submission.csv')\n\nimgs_list.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-05T06:05:24.229029Z","iopub.execute_input":"2023-04-05T06:05:24.229463Z","iopub.status.idle":"2023-04-05T06:05:24.472403Z","shell.execute_reply.started":"2023-04-05T06:05:24.229420Z","shell.execute_reply":"2023-04-05T06:05:24.471040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_map = {'c0': 'Safe driving', \n                'c1': 'Texting - right', \n                'c2': 'Talking on the phone - right', \n                'c3': 'Texting - left', \n                'c4': 'Talking on the phone - left', \n                'c5': 'Operating the radio', \n                'c6': 'Drinking', \n                'c7': 'Reaching behind', \n                'c8': 'Hair and makeup', \n                'c9': 'Talking to passenger'}","metadata":{"execution":{"iopub.status.busy":"2023-04-05T06:05:25.499515Z","iopub.execute_input":"2023-04-05T06:05:25.500621Z","iopub.status.idle":"2023-04-05T06:05:25.507056Z","shell.execute_reply.started":"2023-04-05T06:05:25.500568Z","shell.execute_reply":"2023-04-05T06:05:25.506058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(imgs_list), len(submission)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T06:05:26.811234Z","iopub.execute_input":"2023-04-05T06:05:26.811678Z","iopub.status.idle":"2023-04-05T06:05:26.818860Z","shell.execute_reply.started":"2023-04-05T06:05:26.811636Z","shell.execute_reply":"2023-04-05T06:05:26.817596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 타깃 분포 확인","metadata":{}},{"cell_type":"code","source":"# 클래스 의미\nimgs_list[\"classmean\"] = imgs_list[\"classname\"].replace(class_map)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T06:05:27.754144Z","iopub.execute_input":"2023-04-05T06:05:27.754637Z","iopub.status.idle":"2023-04-05T06:05:27.773652Z","shell.execute_reply.started":"2023-04-05T06:05:27.754595Z","shell.execute_reply":"2023-04-05T06:05:27.772490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 클래스 별 분포확인\npx.histogram(imgs_list, x=\"classmean\", color=\"classmean\", title=\"Number of images by categories \")","metadata":{"execution":{"iopub.status.busy":"2023-04-05T06:05:28.216120Z","iopub.execute_input":"2023-04-05T06:05:28.216567Z","iopub.status.idle":"2023-04-05T06:05:29.909746Z","shell.execute_reply.started":"2023-04-05T06:05:28.216529Z","shell.execute_reply":"2023-04-05T06:05:29.908525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 운전자 별 분포확인\npx.histogram(imgs_list, x=\"subject\", color=\"subject\", title=\"Number of images by subjects \")","metadata":{"execution":{"iopub.status.busy":"2023-04-05T06:05:29.911707Z","iopub.execute_input":"2023-04-05T06:05:29.912091Z","iopub.status.idle":"2023-04-05T06:05:30.165529Z","shell.execute_reply.started":"2023-04-05T06:05:29.912052Z","shell.execute_reply":"2023-04-05T06:05:30.164341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 운전자 별 분포확인 - 데이터 크기 순 정렬\ndrivers_id = pd.DataFrame((imgs_list['subject'].value_counts()).reset_index())\ndrivers_id.columns = ['driver_id', 'Counts']\n# px.histogram(drivers_id, x=\"driver_id\",y=\"Counts\" ,color=\"driver_id\", title=\"Number of images by subjects \")","metadata":{"execution":{"iopub.status.busy":"2023-04-05T06:05:30.166841Z","iopub.execute_input":"2023-04-05T06:05:30.167214Z","iopub.status.idle":"2023-04-05T06:05:30.185781Z","shell.execute_reply.started":"2023-04-05T06:05:30.167177Z","shell.execute_reply":"2023-04-05T06:05:30.184436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"클래스 10개 - 각 클래스별 숫자가 똑같지는 않지만 크게 차이나지도 않음.<br>\n운전자 26명 - 운전자별 최대 1237개 부터 최소 346장의 이미지 보유","metadata":{}},{"cell_type":"markdown","source":"## 이미지 확인","metadata":{}},{"cell_type":"code","source":"def show_image(imgs_list, cols=3):\n    img_num = len(imgs_list)\n    \n    rows = math.ceil(img_num / cols)\n    grid = gridspec.GridSpec(rows, cols)\n    plt.figure(figsize=(15, 12 // cols * rows))\n\n    for idx in range(img_num):\n        img_id = imgs_list.iloc[idx]['img']\n        classname = imgs_list.iloc[idx]['classname']\n        img_path = f'{data_path}imgs/train/{classname}/{img_id}'\n        \n        image = cv2.imread(img_path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n        ax = plt.subplot(grid[idx])\n        ax.imshow(image)\n        plt.title(class_map[classname])\n        # plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2023-04-05T06:05:31.931441Z","iopub.execute_input":"2023-04-05T06:05:31.931896Z","iopub.status.idle":"2023-04-05T06:05:31.940760Z","shell.execute_reply.started":"2023-04-05T06:05:31.931853Z","shell.execute_reply":"2023-04-05T06:05:31.939172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"임의로 뽑은 6개 이미지 확인","metadata":{}},{"cell_type":"code","source":"order = list(range(len(imgs_list)))\nrandom.shuffle(order)\n\nshow_image(imgs_list.iloc[order[:6]])","metadata":{"execution":{"iopub.status.busy":"2023-04-05T06:05:33.671580Z","iopub.execute_input":"2023-04-05T06:05:33.671992Z","iopub.status.idle":"2023-04-05T06:05:35.538359Z","shell.execute_reply.started":"2023-04-05T06:05:33.671952Z","shell.execute_reply":"2023-04-05T06:05:35.536940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"클래스 당 1개의 이미지를 확인","metadata":{}},{"cell_type":"code","source":"class_img = pd.DataFrame(columns=imgs_list.columns)\n\nfor key, value in class_map.items():\n    class_img_all = imgs_list[imgs_list['classname']==key]\n    order = list(range(len(class_img_all)))\n    random.shuffle(order)  \n    \n    class_img = class_img.append(imgs_list[imgs_list['classname']==key].iloc[order[0]])\n\nshow_image(class_img, 4)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T06:05:35.540482Z","iopub.execute_input":"2023-04-05T06:05:35.541188Z","iopub.status.idle":"2023-04-05T06:05:37.879483Z","shell.execute_reply.started":"2023-04-05T06:05:35.541142Z","shell.execute_reply":"2023-04-05T06:05:37.878405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"클래스 당 6개의 이미지를 확인","metadata":{}},{"cell_type":"code","source":"class_img = pd.DataFrame(columns=imgs_list.columns)\n\nfor key, value in class_map.items():\n    class_img_all = imgs_list[imgs_list['classname']==key]\n    order = list(range(len(class_img_all)))\n    random.shuffle(order)  \n    \n    class_img = class_img.append(imgs_list[imgs_list['classname']==key].iloc[order[:6]])\n    \nshow_image(class_img)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T06:05:37.881210Z","iopub.execute_input":"2023-04-05T06:05:37.881606Z","iopub.status.idle":"2023-04-05T06:05:54.269131Z","shell.execute_reply.started":"2023-04-05T06:05:37.881568Z","shell.execute_reply":"2023-04-05T06:05:54.268039Z"},"trusted":true},"execution_count":null,"outputs":[]}]}