{"cells":[{"metadata":{},"cell_type":"markdown","source":"# <center>3rd ML Month - Car Model Classification EDA </center>"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os, shutil\n\nimport numpy as np \nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm_notebook\n\nimport warnings\nwarnings.filterwarnings(action='ignore')\n\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data Path\nDATA_PATH : orignal data\n\nDATA_PATH2 : crop data"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"DATA_PATH =\"../input/2019-3rd-ml-month-with-kakr\"\nDATA_PATH2 =\"../input/car-crop\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 이미지 폴더 경로\nTRAIN_IMG_PATH = os.path.join(DATA_PATH, 'train')\nTEST_IMG_PATH = os.path.join(DATA_PATH, 'test')\n\nTRAIN_IMG_PATH2 = os.path.join(DATA_PATH2, 'train_crop')\nTEST_IMG_PATH2 = os.path.join(DATA_PATH2, 'test_crop')\n\n# CSV 파일 경로\ndf_train = pd.read_csv(os.path.join(DATA_PATH, 'train.csv'))\ndf_test = pd.read_csv(os.path.join(DATA_PATH, 'test.csv'))\ndf_class = pd.read_csv(os.path.join(DATA_PATH, 'class.csv'))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data Merge"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_class2 = df_class.copy()\ndf_class2.rename(columns={'id':\"class\"},inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"comb = pd.merge(df_train, df_class2)\ncomb.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## EDA"},{"metadata":{},"cell_type":"markdown","source":"### random selection of 10\n- 정면 측변 대각등 다양한 각도의 차량 사진이 존재\n- 일부 사진은 차량보다 넓은 범위의 배경을 가짐으로 바운드박스를 이용한 croping가 필요해보임"},{"metadata":{"trusted":true},"cell_type":"code","source":"import PIL\nfrom PIL import ImageDraw\n\n#랜던으로 10개의 file 선택하기\ntmp_imgs = np.random.choice(comb['img_file'],10)\nplt.figure(figsize=(12,15))\n\nfor num, f_name in enumerate(tmp_imgs):\n    img = PIL.Image.open(os.path.join(TRAIN_IMG_PATH, f_name))\n    plt.subplot(5, 2, num + 1)\n    plt.title(comb[comb['img_file']==f_name].reset_index()['name'][0])\n    plt.imshow(img)\n    plt.axis('off')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### EDA of 'Audi S4 Sedan 2012'\n- 다양한 색깔 및 방향을 가지고 있다.\n- 측면의 경우 아우디 로고가 없어 분류가 힘들지 않을까 생각된다.\n- 멋있다.\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"import PIL\nfrom PIL import ImageDraw\n\n# merge를 통해 같은 종류의 차들끼리 묶여짐\ntmp_imgs = comb[comb['name']=='Audi S4 Sedan 2012'].iloc[:10,:]['img_file']\nplt.figure(figsize=(12,15))\n\nfor num, f_name in enumerate(tmp_imgs):\n    img = PIL.Image.open(os.path.join(TRAIN_IMG_PATH, f_name))\n    plt.subplot(5, 2, num + 1)\n    plt.title(comb[comb['img_file']==f_name].reset_index()['name'][0])\n    plt.imshow(img)\n    plt.axis('off')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### bound croping\n허태명님 커널 https://www.kaggle.com/tmheo74/3rd-ml-month-car-image-cropping-updated-7-10 upvote 부탁드려요\n- 위사진과 비교하면 crop가 잘 된것을 볼 수 있다.\n- 오른쪽 아래 네번쨰 아우디 사진을 보면 아우디 로고간판 아래 차가 서있는데 이럴경우 crop를 안하는 것이 판단하는데 있어 좋을까?"},{"metadata":{"trusted":true},"cell_type":"code","source":"import PIL\nfrom PIL import ImageDraw\n\n# merge를 통해 같은 종류의 차들끼리 묶여짐\ntmp_imgs = comb[comb['name']=='Audi S4 Sedan 2012'].iloc[:10,:]['img_file']\nplt.figure(figsize=(12,15))\n\nfor num, f_name in enumerate(tmp_imgs):\n    img = PIL.Image.open(os.path.join(TRAIN_IMG_PATH2, f_name))\n    plt.subplot(5, 2, num + 1)\n    plt.title(comb[comb['img_file']==f_name].reset_index()['name'][0])\n    plt.imshow(img)\n    plt.axis('off')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## augment\n- crop 된 데이터를 통해 데이터증식을 해보자"},{"metadata":{},"cell_type":"markdown","source":"### orignal data"},{"metadata":{"trusted":true},"cell_type":"code","source":"# 이미지를 그대로 읽어온다, 비교를 위해 시드고정\nnp.random.seed(seed=49)\ntmp_imgs =  np.random.choice(comb['img_file'],3,)\nplt.figure(figsize=(15,10))\n\n# 이미지를 시각화한다\nfor i, f_name in enumerate(tmp_imgs):\n    img = PIL.Image.open(os.path.join(TRAIN_IMG_PATH2, f_name))\n    plt.subplot(1, 3, i+1)\n    plt.title(comb[comb['img_file']==f_name].reset_index()['name'][0])\n    plt.imshow(img)\n    plt.axis('off')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### rotate"},{"metadata":{"trusted":true},"cell_type":"code","source":"from scipy.ndimage import rotate\n\n# 임의의 회전 각도(rotate_angle)을 구한 후, 이미지를 회전한다.\nrotate_angle = np.random.randint(40) - 20\nprint('# 이미지 회전 : {}도'.format(rotate_angle))\n\n# 이미지를 시각화한다.\nnp.random.seed(seed=49)\ntmp_imgs =  np.random.choice(comb['img_file'],3,)\nplt.figure(figsize=(15,10))\nfor i, f_name in enumerate(tmp_imgs):\n    img = PIL.Image.open(os.path.join(TRAIN_IMG_PATH2, f_name))\n    img = rotate(img, rotate_angle)\n    plt.subplot(1, 3, i+1)\n    plt.title(comb[comb['img_file']==f_name].reset_index()['name'][0])\n    plt.imshow(img)\n    plt.axis('off')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### blur"},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\n# 10x10 크기의 커널로 이미지를 흐린다\nblur_degree = 10\nprint('{}x{} 커널 크기로 이미지 흐리기'.format(blur_degree, blur_degree))\n\n# 이미지를 시각화한다.\nnp.random.seed(seed=49)\ntmp_imgs =  np.random.choice(comb['img_file'],3,)\nplt.figure(figsize=(15,10))\n\nfor i, f_name in enumerate(tmp_imgs):\n    img = PIL.Image.open(os.path.join(TRAIN_IMG_PATH2, f_name))\n    #cv2사용시 array변경\n    img = np.asarray(img)\n    img = cv2.blur(img,(blur_degree,blur_degree))\n    plt.subplot(1, 3, i+1)\n    plt.title(comb[comb['img_file']==f_name].reset_index()['name'][0])\n    plt.imshow(img)\n    plt.axis('off')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"이것으로 EDA를 마치고, 모델쪽으로 넘어갑니다."},{"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}