{"cells":[{"metadata":{},"cell_type":"markdown","source":"<h1><center><font size=\"6\">자동차 이미지 데이터셋을 이용한 자동차 차종 분류</font></center></h1>\n\n\n<h2><center><font size=\"4\">2019 3rd ML month with KaKR</font></center></h2>\n<h3><center><font size=\"2\">캐글 코리아와 함께하는 3rd ML 대회</font></center></h3>\n\n<br>\n\n# **<a id='0'>Content</a>**\n\n- <a href='#1'>Introduction</a>  \n- <a href='#2'>Load Package and Data Check</a>\n- <a href='#3'>Data Exploration</a>"},{"metadata":{},"cell_type":"markdown","source":"# **<a id='1'>Introduction</a>**\n\n\n#### **Competition background**\n우리가 살면서 갖고 싶은게 여러 가지가 있지만, 집 다음으로 보통 자동차 일 것입니다. 우리 주위에서 흔히 볼 수 있고, 편리한 교통 수단 중에 하나입니다. 전 세계적으로 그 수는 무려 10억대가 넘고, 그 종류는 수백, 수천 가지 이상 된다고 합니다.\n\n하지만, 이렇게 많은 차종을 구별하는 것은 사람일지라도 쉽지가 않은데요. 만약, 그림만 보고 자동차의 모델(Class)을 쉽게 예측할 수 있다면 CCTV나 블랙박스 속의 차량을 자동으로 분류, 감지해서 범죄예방에 도움을 줄 수 있는 등 다양한 곳에 유익하게 사용될 수 있습니다.\n\n이번 컴페티션에서는, 자동차 이미지 데이터셋을 활용하여 이미지에 해당하는 차종(Class)이 과연 무엇인지 예측해보겠습니다.\n\n#### **Evaluation**\n이번 컴페티션의 평가지표(Metric)은 F1 score 입니다.\n\n#### **Timeline**\n'19.06.14 ~ '19.08.17\n\n#### **Data**\n- train.csv - Train 셋의 이미지 파일명, 바운딩박스 좌표, 차종정보\n- test.csv - Test 셋의 이미지 파일명, 바운딩박스 좌표, 차종정보\n- submission.csv - Test셋과 대응되는 제출 파일\n- class.csv - 데이터 셋의 class 컬럼과 대응되는 차종의 레이블\n- train.zip - Train 이미지 파일\n- test.zip - Test 이미지 파일"},{"metadata":{},"cell_type":"markdown","source":"# **<a id='2'>Load Package and Data Check</a>**\n\n## Load Package"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\n\nimport cv2\nimport json\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\n\nfrom PIL import Image\n\nfrom wordcloud import WordCloud\n\nimport warnings\nwarnings.filterwarnings(action='ignore')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"path = '../input/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(path+'train.csv')\ntest = pd.read_csv(path+'test.csv')\nclass_ = pd.read_csv(path+'class.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data Check - Text"},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_.info()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data Check - Image"},{"metadata":{"trusted":true},"cell_type":"code","source":"def image_show(path, row=0, train_data=True):\n    if train_data:\n        img_name = train.iloc[row, 0]\n        img = np.asarray(Image.open(path + \"train/\" + img_name))\n    else:\n        img_name = test.iloc[row, 0]\n        img = np.asarray(Image.open(path + \"test/\" + img_name))\n    plt.imshow(img)        \n    plt.show()\n    \ndef image_object_show(path, row, train_data=True):\n    if train_data:\n        img_name, x, y, w, h = train.iloc[row, 0], train.iloc[row, 1], train.iloc[row, 2], train.iloc[row, 3], train.iloc[row, 4]\n        img = np.asarray(Image.open(path + \"train/\" + img_name))\n        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)\n    else:\n        img_name, x, y, w, h = test.iloc[row, 0], test.iloc[row, 1], test.iloc[row, 2], test.iloc[row, 3], test.iloc[row, 4]\n        img = np.asarray(Image.open(path + \"test/\" + img_name))\n        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)\n    plt.imshow(img)        \n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_show(path, 0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_show(path, 150)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_object_show(path, 0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_object_show(path, 150)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **<a id='3'>Data Exploration</a>**\n\n## Text"},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.merge(train, class_, how='left', left_on='class', right_on='id')\ntrain['company'] = train['name'].apply(lambda x: x.split(' ')[0])\ntrain['year'] = train['name'].apply(lambda x: x.split(' ')[-1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"text = ''\nfor name in train['company']:\n    text += ' ' + name\n\nwordcloud = WordCloud(max_font_size=50, width=600, height=300, background_color='white').generate(text)\nplt.figure(figsize=(15,8))\nplt.imshow(wordcloud)\nplt.title(\"Company WordCloud\", fontsize=40, color='red')\nplt.axis(\"off\")\nplt.show() ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"여러가지 회사가 보입니다.\n\nChevrolet가 가장 큰 것으로 보아 많은 차량이 train data에 존재하는 듯 합니다. 그 외에도 BMW, Benz, Audi 등이 보입니다."},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(20, 8))\nsns.countplot(train['year'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"2012년과 2007년식이 가장 많은 것을 알 수 있습니다."},{"metadata":{},"cell_type":"markdown","source":"## Image"},{"metadata":{"trusted":true},"cell_type":"code","source":"height = []\nwidth = []\nfor i in range(len(train)):\n    img = np.asarray(Image.open(path + \"train/\" + train.iloc[i, 0]))\n    height.append(img.shape[0])\n    width.append(img.shape[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.subplots(1, 2, figsize=(20, 6))\n\nplt.subplot(1, 2, 1)\nsns.distplot(height)\nplt.title('Height Distribution')\n\nplt.subplot(1, 2, 2)\nsns.distplot(width)\nplt.title('Widht Distribution')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"temp = np.argmax(height)\nimage_show(path, temp)\nprint('maximum height picture \\nheight : {}'.format(height[temp]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"temp = np.argmax(width)\nimage_show(path, temp)\nprint('maximum width picture \\nwidth : {}'.format(width[temp]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"temp = np.argmin(height)\nimage_show(path, temp)\nprint('minimum height picture \\nheight : {}'.format(height[temp]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"temp = np.argmin(width)\nimage_show(path, temp)\nprint('minimum width picture \\nwidth : {}'.format(width[temp]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"temp = np.argmax(np.array(height) * np.array(width))\nimage_show(path, temp)\nprint('maximum picture \\nhight : {}, width : {}'.format(height[temp], width[temp]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"temp = np.argmin(np.array(height) * np.array(width))\nimage_show(path, temp)\nprint('minimum picture \\nhight : {}, width : {}'.format(height[temp], width[temp]))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"to be continue..."},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}