{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"#Load Libraries\n\nimport gc\nimport os\nimport glob\nimport zipfile\nimport warnings\nfrom tqdm import tqdm\n\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom keras import backend as K","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"DATA_PATH = '../input/2019-3rd-ml-month-with-kakr/'\nos.listdir(DATA_PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TRAIN_IMG_PATH = os.path.join(DATA_PATH, 'train')\nTEST_IMG_PATH = os.path.join(DATA_PATH, 'test')\n\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":{"trusted":true},"cell_type":"code","source":"if set(list(df_train['img_file'])) == set(os.listdir(TRAIN_IMG_PATH)):\n    print('trainset is well match')\nelse:\n    print('trainset is no match')\n    \nif set(list(df_test['img_file'])) == set(os.listdir(TEST_IMG_PATH)):\n    print('testset is well match')\nelse:\n    print('testset is no match')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#class distribution\nplt.figure(figsize=(20,10))\nsns.countplot(df_train['class'], order=df_train['class'].value_counts(ascending=True).index)\nplt.title('Number of data per each class')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"numOfClass = df_train['class'].value_counts(ascending=False)\nprint('class with most count: {}'.format(numOfClass.index[0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import PIL\nfrom PIL import ImageDraw, ImageFilter, ImageOps\n\nplt.figure(figsize=(12,20))\ntmp_imgs = df_train['img_file'][100:110]\n\nfor idx, fn in enumerate(tmp_imgs):\n    img = PIL.Image.open(os.path.join(TRAIN_IMG_PATH, fn))\n    plt.subplot(5, 2, idx+1)\n    plt.title(fn)\n    plt.axis('off')\n    plt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def draw_rect(drawcontext, pos, outline=None, width=0):\n    (x1, y1, x2, y2) = (pos[0], pos[1], pos[2], pos[3])\n    points = (x1,y1), (x2,y1), (x2,y2), (x1,y2), (x1,y1)\n    drawcontext.line(points, fill=outline, width=width)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def make_box_img(img_name):\n    if img_name.split('_')[0] == 'train':\n        PATH = TRAIN_IMG_PATH\n        data = df_train\n    elif img_name.split('_')[0] == 'test':\n        PATH = TEST_IMG_PATH\n        data = df_test\n        \n    img = PIL.Image.open(os.path.join(PATH, img_name))\n    pos = data.loc[data['img_file'] == img_name, ['bbox_x1', 'bbox_y1', 'bbox_x2', 'bbox_y2']].values.reshape(-1)\n    draw = ImageDraw.Draw(img)\n    draw_rect(draw, pos, outline='red', width=10)\n    \n    return img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def crop_boxing_img(img_name, margin=16):\n    if img_name.split('_')[0] == \"train\":\n        PATH = TRAIN_IMG_PATH\n        data = df_train\n    elif img_name.split('_')[0] == \"test\":\n        PATH = TEST_IMG_PATH\n        data = df_test\n        \n    img = PIL.Image.open(os.path.join(PATH, img_name))\n    pos = data.loc[data[\"img_file\"] == img_name, ['bbox_x1','bbox_y1', 'bbox_x2', 'bbox_y2']].values.reshape(-1)\n\n    width, height = img.size\n    x1 = max(0, pos[0] - margin)\n    y1 = max(0, pos[1] - margin)\n    x2 = min(pos[2] + margin, width)\n    y2 = min(pos[3] + margin, height)\n\n    return img.crop((x1,y1,x2,y2))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TRAIN_CROP_PATH = \"../train_crop\"\nVAL_CROP_PATH = \"../val_crop\"\nTEST_CROP_PATH = \"../test_crop\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if (os.path.isdir(TRAIN_CROP_PATH) == False):\n    os.mkdir(TRAIN_CROP_PATH)\n\nif (os.path.isdir(VAL_CROP_PATH) == False):\n    os.mkdir(VAL_CROP_PATH)\n\nif (os.path.isdir(TEST_CROP_PATH) == False):\n    os.mkdir(TEST_CROP_PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i, row in df_train.iterrows():\n    cropped = crop_boxing_img(row['img_file'])\n    cropped.save(os.path.join(TRAIN_CROP_PATH, row['img_file']))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i, row in df_test.iterrows():\n    cropped = crop_boxing_img(row['img_file'])\n    cropped.save(os.path.join(TEST_CROP_PATH, row['img_file']))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls ../train_crop","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f_name = 'train_00102.jpg'\n\nplt.figure(figsize=(20,10))\nplt.subplot(1, 2, 1)\n# Original Image\norigin_img = PIL.Image.open(os.path.join(TRAIN_IMG_PATH, f_name))\nplt.title(\"Original Image - {}\".format(f_name))\nplt.imshow(origin_img)\nplt.axis('off')\n\n# Image included bounding box\nplt.subplot(1, 2, 2)\ncrop_img = PIL.Image.open(os.path.join(TRAIN_CROP_PATH, f_name))\nplt.title(\"Boxing Image - {}\".format(f_name))\nplt.imshow(crop_img)\nplt.axis('off')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls ../train_crop","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f_name = 'train_00102.jpg'\n\nplt.figure(figsize=(20,10))\nplt.subplot(1, 2, 1)\n# Original Image\norigin_img = PIL.Image.open(os.path.join(TRAIN_IMG_PATH, f_name))\nplt.title(\"Original Image - {}\".format(f_name))\nplt.imshow(origin_img)\nplt.axis('off')\n\n# Image included bounding box\nplt.subplot(1, 2, 2)\nboxing = make_box_img(f_name)\nplt.title(\"Boxing Image - {}\".format(f_name))\nplt.imshow(boxing)\nplt.axis('off')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ndf_train['class'] = df_train['class'].astype('str')\n\ndf_train = df_train[['img_file', 'class']]\ndf_test = df_test[['img_file']]\n\nx_train, x_val = train_test_split(df_train, test_size=0.2, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.applications.resnet50 import ResNet50, preprocess_input\nfrom keras.preprocessing.image import ImageDataGenerator\n\nimg_size = (224, 224)\nnum_train = len(x_train)\nnum_val = len(x_val)\nnum_test = len(df_test)\nepochs = 20\nbatch_size = 32\n\ntrain_datagen = ImageDataGenerator(horizontal_flip=True,\n                                  vertical_flip=False,\n                                   rotation_range=20,\n                                   width_shift_range=0.2,\n                                   height_shift_range=0.2,\n                                  zoom_range=0.1)\n\nval_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)\ntest_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls ../val_crop","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator = train_datagen.flow_from_dataframe(dataframe=x_train,\n                                                   directory=TRAIN_CROP_PATH,\n                                                   x_col='img_file',\n                                                   y_col='class',\n                                                   target_size=img_size,\n                                                   color_mode='rgb',\n                                                   class_mode='categorical',\n                                                   batch_size=batch_size)\n\nval_generator = val_datagen.flow_from_dataframe(dataframe=x_val,\n                                                   directory=TRAIN_CROP_PATH,\n                                                   x_col='img_file',\n                                                   y_col='class',\n                                                   target_size=img_size,\n                                                   color_mode='rgb',\n                                                   class_mode='categorical',\n                                                   batch_size=batch_size,\n                                                   shuffle=False)\n\ntest_generator = test_datagen.flow_from_dataframe(dataframe=df_test,\n                                                   directory=TEST_CROP_PATH,\n                                                   x_col='img_file',\n                                                   y_col=None,\n                                                   target_size=img_size,\n                                                   color_mode='rgb',\n                                                   class_mode=None,\n                                                   batch_size=batch_size,\n                                                 shuffle=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls ../input/keras-pretrained-models/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"resNet_model = ResNet50(weights=None,include_top=False, input_shape=(224,224,3))\nresNet_model.load_weights('../input/keras-pretrained-models/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import Sequential, Model\nfrom keras.layers import Dense, Dropout, Activation, Conv2D, GlobalAveragePooling2D, Flatten\n\nmodel = Sequential()\nmodel.add(resNet_model)\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Dense(196, activation='softmax', kernel_initializer='he_normal'))\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint, EarlyStopping\n\nfilepath = 'my_resnet_model_{val_acc:.2f}_{val_loss:.4f}.h5'\n\nes = EarlyStopping(monitor='val_acc', min_delta=0, patience=3, verbose=1, mode='auto')\n\ncallbackList = [es]\nepochs = 10\nhistory = model.fit_generator(train_generator,\n                             steps_per_epoch = (num_train // batch_size),\n                             epochs = epochs,\n                             validation_data = val_generator,\n                             validation_steps = (num_val // batch_size),\n                             callbacks = callbackList)\n\n#gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(history.history['acc'])\nplt.plot(history.history['val_acc'])\nplt.title('Model Accuracy')\nplt.xlabel('Accuracy')\nplt.ylabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model loss')\nplt.xlabel('Loss')\nplt.ylabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_generator.reset()\nprediction = model.predict_generator(generator = test_generator,\n                                    steps = (num_test//batch_size),\n                                    verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predicted_class_indices = np.argmax(prediction, axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predicted_class_indices","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predicted_class_indices=np.argmax(prediction, axis=1)\n\n# Generator class dictionary mapping\nlabels = (train_generator.class_indices)\nlabels = dict((v,k) for k,v in labels.items())\npredictions = [labels[k] for k in predicted_class_indices]\n\nsubmission = pd.read_csv(os.path.join(DATA_PATH, 'sample_submission.csv'))\nsubmission[\"class\"] = predictions\nsubmission.to_csv(\"submission.csv\", index=False)\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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}