{"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":"%%bash\nunzip /kaggle/input/facial-keypoints-detection/training.zip -d /train\nunzip /kaggle/input/facial-keypoints-detection/test.zip -d /test","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-13T10:13:05.118368Z","iopub.execute_input":"2022-08-13T10:13:05.118946Z","iopub.status.idle":"2022-08-13T10:13:07.939755Z","shell.execute_reply.started":"2022-08-13T10:13:05.118907Z","shell.execute_reply":"2022-08-13T10:13:07.938713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport gc\nimport sys\nimport pandas as pd\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Dense, Conv2D, GlobalAvgPool2D\nfrom tensorflow.keras import activations, Model, Input\nimport albumentations as A\nfrom functools import partial\nfrom tqdm import tqdm\nAUTOTUNE = tf.data.experimental.AUTOTUNE","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:39:56.548946Z","iopub.execute_input":"2022-08-13T10:39:56.549492Z","iopub.status.idle":"2022-08-13T10:39:56.556563Z","shell.execute_reply.started":"2022-08-13T10:39:56.549455Z","shell.execute_reply":"2022-08-13T10:39:56.555619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = pd.read_csv('/train/training.csv')\ntest_csv = pd.read_csv('/test/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:13:10.482791Z","iopub.execute_input":"2022-08-13T10:13:10.483068Z","iopub.status.idle":"2022-08-13T10:13:13.856227Z","shell.execute_reply.started":"2022-08-13T10:13:10.483035Z","shell.execute_reply":"2022-08-13T10:13:13.855152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.head(n=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T08:48:20.476787Z","iopub.execute_input":"2022-08-02T08:48:20.478933Z","iopub.status.idle":"2022-08-02T08:48:20.509026Z","shell.execute_reply.started":"2022-08-02T08:48:20.478903Z","shell.execute_reply":"2022-08-02T08:48:20.508274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T08:48:20.510914Z","iopub.execute_input":"2022-08-02T08:48:20.516646Z","iopub.status.idle":"2022-08-02T08:48:20.844297Z","shell.execute_reply.started":"2022-08-02T08:48:20.516393Z","shell.execute_reply":"2022-08-02T08:48:20.843580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Preperation","metadata":{}},{"cell_type":"markdown","source":"### ⚠️ There are some examples that have 15 keypoints and some have just 4 keypoints\nI'll train 2 seperate models but since these 15 keypoints also include other 4 keypoints, I'll remove those 4 keypoints from 15 ones.","metadata":{}},{"cell_type":"code","source":"eleven_columns = ['left_eye_inner_corner_x','left_eye_inner_corner_y', 'left_eye_outer_corner_x',\n       'left_eye_outer_corner_y', 'right_eye_inner_corner_x','right_eye_inner_corner_y',\n       'right_eye_outer_corner_x','right_eye_outer_corner_y', 'left_eyebrow_inner_end_x',\n       'left_eyebrow_inner_end_y', 'left_eyebrow_outer_end_x','left_eyebrow_outer_end_y',\n       'right_eyebrow_inner_end_x', 'right_eyebrow_inner_end_y', 'right_eyebrow_outer_end_x',\n       'right_eyebrow_outer_end_y', 'mouth_left_corner_x', 'mouth_left_corner_y',\n       'mouth_right_corner_x', 'mouth_right_corner_y', 'mouth_center_top_lip_x',\n       'mouth_center_top_lip_y', 'Image']\n\nfour_columns = ['left_eye_center_x', 'left_eye_center_y', 'right_eye_center_x',\n                 'right_eye_center_y','nose_tip_x', 'nose_tip_y',\n                'mouth_center_bottom_lip_x','mouth_center_bottom_lip_y','Image']","metadata":{"execution":{"iopub.status.busy":"2022-08-13T14:10:49.992757Z","iopub.execute_input":"2022-08-13T14:10:49.993575Z","iopub.status.idle":"2022-08-13T14:10:50.025696Z","shell.execute_reply.started":"2022-08-13T14:10:49.993490Z","shell.execute_reply":"2022-08-13T14:10:50.024660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eleven_csv = train_csv[eleven_columns].dropna()\n\nfour_csv = train_csv[four_columns].dropna()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:13:21.141715Z","iopub.execute_input":"2022-08-13T10:13:21.142300Z","iopub.status.idle":"2022-08-13T10:13:21.166640Z","shell.execute_reply.started":"2022-08-13T10:13:21.142264Z","shell.execute_reply":"2022-08-13T10:13:21.165900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#First 100 data used for validation\neleven_train = eleven_csv.iloc[100:,:]\neleven_val = eleven_csv.iloc[:100,:]\n\nfour_train = four_csv.iloc[100:,:]\nfour_val = four_csv.iloc[:100,:]","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:14:48.487653Z","iopub.execute_input":"2022-08-13T10:14:48.487934Z","iopub.status.idle":"2022-08-13T10:14:48.493734Z","shell.execute_reply.started":"2022-08-13T10:14:48.487903Z","shell.execute_reply":"2022-08-13T10:14:48.493033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eleven_train.shape, four_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:17:55.021254Z","iopub.execute_input":"2022-08-13T10:17:55.021914Z","iopub.status.idle":"2022-08-13T10:17:55.029764Z","shell.execute_reply.started":"2022-08-13T10:17:55.021873Z","shell.execute_reply":"2022-08-13T10:17:55.028945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def to_image(img):\n    img = np.array(img.split(' '), dtype = np.uint8).reshape(96,96)\n    return img\ndef to_keypoint(array):\n    keypoints = []\n    for i in range(0,len(array),2):\n        keypoints.append((array[i],array[i+1]))\n    return keypoints\n\ndef get_data(data):\n    images = np.array([to_image(data.iloc[i,-1]) for i in range(len(data))])\n    keypoints = np.array([to_keypoint(data.iloc[:,:-1].values[i]) for i in range(len(data))])\n\n    return images, keypoints.astype('float32')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:16:31.368757Z","iopub.execute_input":"2022-08-13T10:16:31.369499Z","iopub.status.idle":"2022-08-13T10:16:31.377468Z","shell.execute_reply.started":"2022-08-13T10:16:31.369452Z","shell.execute_reply":"2022-08-13T10:16:31.376768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train11_x, train11_y = get_data(eleven_train)\nval11_x, val11_y = get_data(eleven_val)\n\ntrain4_x, train4_y = get_data(four_train)\nval4_x, val4_y = get_data(four_val)","metadata":{"_kg_hide-input":false,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-08-13T10:17:14.132060Z","iopub.execute_input":"2022-08-13T10:17:14.132679Z","iopub.status.idle":"2022-08-13T10:17:29.272888Z","shell.execute_reply.started":"2022-08-13T10:17:14.132642Z","shell.execute_reply":"2022-08-13T10:17:29.272134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"four_dataset = tf.data.Dataset.from_tensor_slices((train4_x, train4_y))\neleven_dataset = tf.data.Dataset.from_tensor_slices((train11_x, train11_y))","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:18:22.585177Z","iopub.execute_input":"2022-08-13T10:18:22.585461Z","iopub.status.idle":"2022-08-13T10:18:25.396472Z","shell.execute_reply.started":"2022-08-13T10:18:22.585428Z","shell.execute_reply":"2022-08-13T10:18:25.394996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data Augmentation with Albumentation","metadata":{}},{"cell_type":"code","source":"transforms = A.Compose([\n        A.Rotate(limit=15, border_mode=cv2.BORDER_CONSTANT, p=0.8),\n        A.IAAAffine(shear=15, scale=1.0, mode=\"constant\", p=0.2),\n        A.RandomBrightnessContrast(contrast_limit=0.5, brightness_limit=0.5, p=0.2),\n        A.OneOf([\n            A.GaussNoise(p=0.8),\n            A.ImageCompression(p=0.8),\n            A.RandomGamma(p=0.8),\n            A.Posterize(p=0.8),\n            A.Blur(p=0.8),\n        ], p=1.0),\n        A.OneOf([\n            A.GaussNoise(p=0.8),\n            A.ImageCompression(p=0.8),\n            A.RandomGamma(p=0.8),\n            A.Posterize(p=0.8),\n            A.Blur(p=0.8),\n        ], p=1.0),\n        A.ShiftScaleRotate(shift_limit=0.1, scale_limit=0.1, rotate_limit=0, p=0.2, border_mode=cv2.BORDER_CONSTANT),\n],\n    keypoint_params = A.KeypointParams(format = 'xy', remove_invisible=False)\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:20:26.243980Z","iopub.execute_input":"2022-08-13T10:20:26.244894Z","iopub.status.idle":"2022-08-13T10:20:26.254956Z","shell.execute_reply.started":"2022-08-13T10:20:26.244853Z","shell.execute_reply":"2022-08-13T10:20:26.254020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Augmentate Validation Dataset","metadata":{}},{"cell_type":"code","source":"def get_augmented_data(image_data, point_data, aug_size):\n  print(f\"Length: {len(point_data)}\")\n  images = []\n  keypoints = []\n  def albumentate_image(img, kp):\n      images.append(np.array(img, dtype = np.float32))\n      keypoints.append(np.array(kp).reshape(-1))\n      for _ in range(aug_size):\n          res = transforms(image = img, keypoints = kp)\n          aug_image = res['image']\n          aug_image = np.array(aug_image, dtype = np.float32)\n\n          aug_keypoints = res['keypoints']\n          aug_keypoints = np.array(aug_keypoints).reshape(-1)\n\n          images.append(aug_image)\n          keypoints.append(aug_keypoints)\n\n  for img,kp in tqdm(zip(image_data,point_data)):\n      albumentate_image(img,kp)\n\n  images = np.array(images).astype('float32')\n  keypoints = np.array(keypoints).astype('float32')\n\n  return images/255, keypoints","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:23:35.719083Z","iopub.execute_input":"2022-08-13T10:23:35.719682Z","iopub.status.idle":"2022-08-13T10:23:35.726971Z","shell.execute_reply.started":"2022-08-13T10:23:35.719645Z","shell.execute_reply":"2022-08-13T10:23:35.726233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val11_img, val11_kp = get_augmented_data(val11_x, val11_y, 3)\nval4_img, val4_kp = get_augmented_data(val4_x, val4_y, 3)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:23:39.192669Z","iopub.execute_input":"2022-08-13T10:23:39.192926Z","iopub.status.idle":"2022-08-13T10:23:40.528113Z","shell.execute_reply.started":"2022-08-13T10:23:39.192895Z","shell.execute_reply":"2022-08-13T10:23:40.527389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create Datasets","metadata":{}},{"cell_type":"code","source":"def aug_fn(image, keypoints):\n    aug_data = transforms(image = image, keypoints = keypoints)\n    aug_img = aug_data[\"image\"]\n    aug_img = aug_img/255\n    aug_keypoints = aug_data['keypoints']\n    aug_keypoints = np.array(aug_keypoints).reshape(-1)\n    return aug_img.astype('float32'), aug_keypoints","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:24:31.559671Z","iopub.execute_input":"2022-08-13T10:24:31.560199Z","iopub.status.idle":"2022-08-13T10:24:31.565038Z","shell.execute_reply.started":"2022-08-13T10:24:31.560161Z","shell.execute_reply":"2022-08-13T10:24:31.564361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_data(image, keypoints):\n    data = tf.numpy_function(func=aug_fn, inp=[image, keypoints], Tout=[tf.float32, tf.float32])\n    return data","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:25:09.866746Z","iopub.execute_input":"2022-08-13T10:25:09.867002Z","iopub.status.idle":"2022-08-13T10:25:09.873983Z","shell.execute_reply.started":"2022-08-13T10:25:09.866973Z","shell.execute_reply":"2022-08-13T10:25:09.873214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_shapes(img, keypoint, img_shape = (96,96), kp_shape = 8):\n    img.set_shape(img_shape)\n    keypoint.set_shape(kp_shape)\n    return img, keypoint","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:25:18.753155Z","iopub.execute_input":"2022-08-13T10:25:18.753588Z","iopub.status.idle":"2022-08-13T10:25:18.761854Z","shell.execute_reply.started":"2022-08-13T10:25:18.753535Z","shell.execute_reply":"2022-08-13T10:25:18.759424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_data(data_type = 'four', batch_size = 64):\n    dataset = four_dataset if data_type == 'four' else eleven_dataset\n    kp_shape = 8 if data_type == 'four' else 22\n    \n    aug_dataset = dataset.map(process_data,\n                  num_parallel_calls=AUTOTUNE).prefetch(AUTOTUNE)\n    shaped_dataset = aug_dataset.map(partial(set_shapes, img_shape=(96,96), kp_shape = kp_shape), \n                          num_parallel_calls=AUTOTUNE).batch(batch_size).prefetch(AUTOTUNE)\n    \n    return shaped_dataset","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:32:42.381431Z","iopub.execute_input":"2022-08-13T10:32:42.382233Z","iopub.status.idle":"2022-08-13T10:32:42.387873Z","shell.execute_reply.started":"2022-08-13T10:32:42.382189Z","shell.execute_reply":"2022-08-13T10:32:42.386946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def view_image(ds):\n    image, keypoints = next(iter(ds))\n\n    image = image.numpy()\n    keypoints = keypoints.numpy()\n    \n    fig = plt.figure(figsize=(22, 22))\n    for idx in range(20):\n        ax = fig.add_subplot(4, 5, idx+1, xticks=[], yticks=[])\n        ax.imshow(image[idx])\n        ax.scatter([keypoints[idx][i] for i in range(0, len(keypoints[idx]), 2)], \n                   [keypoints[idx][i] for i in range(1, len(keypoints[idx]), 2)], color = 'r')\n        ","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:29:27.096776Z","iopub.execute_input":"2022-08-13T10:29:27.097725Z","iopub.status.idle":"2022-08-13T10:29:27.105055Z","shell.execute_reply.started":"2022-08-13T10:29:27.097674Z","shell.execute_reply":"2022-08-13T10:29:27.103828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eleven_train = get_data(data_type = 'eleven', batch_size = 32)\nview_image(eleven_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:32:49.312400Z","iopub.execute_input":"2022-08-13T10:32:49.312972Z","iopub.status.idle":"2022-08-13T10:32:51.373640Z","shell.execute_reply.started":"2022-08-13T10:32:49.312936Z","shell.execute_reply":"2022-08-13T10:32:51.372396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train","metadata":{}},{"cell_type":"markdown","source":"## Create Model","metadata":{}},{"cell_type":"code","source":"def get_model(output_size = 8):\n    \n    img_input = Input(shape = (96,96,1))\n    \n    # 1 channel to 3 channel\n    stack_block = Conv2D(3, (1,1), padding = 'same', activation = 'linear',\n                          kernel_initializer = tf.keras.initializers.Ones())(img_input)\n    \n    conv_base = tf.keras.applications.ResNet50(\n            include_top = False, input_shape = (96,96,3),\n             weights = 'imagenet'\n        )(stack_block)\n    \n    avg_pool = GlobalAvgPool2D()(conv_base)\n    output = Dense(units = output_size)(avg_pool)\n\n    model = Model(inputs = img_input, outputs = output)\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:40:20.971602Z","iopub.execute_input":"2022-08-13T10:40:20.972140Z","iopub.status.idle":"2022-08-13T10:40:20.986482Z","shell.execute_reply.started":"2022-08-13T10:40:20.972075Z","shell.execute_reply":"2022-08-13T10:40:20.985629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train Four Model","metadata":{}},{"cell_type":"code","source":"four_model = get_model(output_size=8)\nfour_train = get_data(data_type = 'four', batch_size = 64)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:41:12.960301Z","iopub.execute_input":"2022-08-13T10:41:12.961007Z","iopub.status.idle":"2022-08-13T10:41:14.592253Z","shell.execute_reply.started":"2022-08-13T10:41:12.960963Z","shell.execute_reply":"2022-08-13T10:41:14.591409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"four_model.compile(optimizer = 'adam',\n                loss = 'mse',\n                metrics = [tf.keras.metrics.RootMeanSquaredError(name='rmse'), 'acc']\n                )","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = tf.keras.callbacks.EarlyStopping(\n    monitor=\"val_rmse\", patience=50, restore_best_weights=True\n)\n\nrlp = tf.keras.callbacks.ReduceLROnPlateau(\n    monitor='val_rmse', factor=0.5, patience=5, min_lr=1e-15, mode='min', verbose=1\n)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"four_history = four_model.fit(\n    four_train,\n    epochs = 100,\n    workers = 4,\n    validation_data = (val4_img, val4_kp),\n    callbacks = [early_stopping, rlp]\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T10:41:37.164552Z","iopub.execute_input":"2022-08-13T10:41:37.164814Z","iopub.status.idle":"2022-08-13T10:41:37.190462Z","shell.execute_reply.started":"2022-08-13T10:41:37.164784Z","shell.execute_reply":"2022-08-13T10:41:37.189730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"four_history = four_history.history","metadata":{"execution":{"iopub.status.busy":"2022-08-01T14:41:47.920370Z","iopub.execute_input":"2022-08-01T14:41:47.920621Z","iopub.status.idle":"2022-08-01T14:41:47.924562Z","shell.execute_reply.started":"2022-08-01T14:41:47.920591Z","shell.execute_reply":"2022-08-01T14:41:47.923872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(four_history['val_rmse'])\nplt.plot(four_history['rmse'])\nplt.legend(['val_rmse','rmse'])\nplt.title(\"RMSE\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(four_history['val_acc'])\nplt.plot(four_history['acc'])\nplt.legend(['val_acc','acc'])\nplt.title(\"ACCURACY\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train Eleven Model","metadata":{}},{"cell_type":"code","source":"eleven_model = get_model(output_size = 22)\neleven_train = get_data(data_type = 'eleven', batch_size = 32)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eleven_model.compile(optimizer = 'adam',\n                loss = 'mse',\n                metrics = [tf.keras.metrics.RootMeanSquaredError(name='rmse'), 'acc']\n                )","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = tf.keras.callbacks.EarlyStopping(\n    monitor=\"val_rmse\", patience=50, restore_best_weights=True\n)\n\nrlp = tf.keras.callbacks.ReduceLROnPlateau(\n    monitor='val_rmse', factor=0.7, patience=5, min_lr=1e-15, mode='min', verbose=1\n)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eleven_history = eleven_model.fit(\n    eleven_train,\n    epochs = 100,\n    workers = 4,\n    validation_data = (val11_img, val11_kp),\n    callbacks = [early_stopping, rlp]\n)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eleven_history = eleven_history.history","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(eleven_history['val_rmse'])\nplt.plot(eleven_history['rmse'])\nplt.legend(['val_rmse','rmse'])\nplt.title(\"RMSE\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(eleven_history['val_acc'])\nplt.plot(eleven_history['acc'])\nplt.legend(['val_acc','acc'])\nplt.title(\"ACCURACY\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create Submission File","metadata":{}},{"cell_type":"code","source":"test_len = 1783\n\ntest_data = np.array([to_image(test_csv['Image'][i]) for i in range(test_len)]).astype('float32')","metadata":{"execution":{"iopub.status.busy":"2022-08-01T05:56:45.591229Z","iopub.execute_input":"2022-08-01T05:56:45.591790Z","iopub.status.idle":"2022-08-01T05:56:50.038424Z","shell.execute_reply.started":"2022-08-01T05:56:45.591752Z","shell.execute_reply":"2022-08-01T05:56:50.037667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IdLookupTable = pd.read_csv('../input/facial-keypoints-detection/IdLookupTable.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-01T05:58:40.761553Z","iopub.execute_input":"2022-08-01T05:58:40.762137Z","iopub.status.idle":"2022-08-01T05:58:40.814165Z","shell.execute_reply.started":"2022-08-01T05:58:40.762099Z","shell.execute_reply":"2022-08-01T05:58:40.813473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"four_point_features = np.array(four_columns[:-1])\neleven_point_features = np.array(eleven_columns[:-1])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = []\nfor i in tqdm(range(len(test_data))):\n    features = IdLookupTable[IdLookupTable['ImageId'] == i+1]['FeatureName'].values\n    \n    test = test_data[i].reshape(1,96,96,1)/255\n    four_pred = four_model.predict(test)\n    full_pred = eleven_model.predict(test)\n    for feature in features:\n        if feature in  four_point_features:\n            index = np.where(four_point_features == feature)[0][0]\n            result.append(four_pred[0][index])\n        else:\n            index = np.where(eleven_point_features == feature)[0][0]\n            result.append(full_pred[0][index])\n           ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T05:58:45.699501Z","iopub.execute_input":"2022-08-01T05:58:45.700185Z","iopub.status.idle":"2022-08-01T06:01:39.186013Z","shell.execute_reply.started":"2022-08-01T05:58:45.700148Z","shell.execute_reply":"2022-08-01T06:01:39.185184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({\"RowId\": np.arange(1, len(IdLookupTable)+1), \"Location\": result})\ndf.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T06:01:44.386277Z","iopub.execute_input":"2022-08-01T06:01:44.386960Z","iopub.status.idle":"2022-08-01T06:01:44.490597Z","shell.execute_reply.started":"2022-08-01T06:01:44.386915Z","shell.execute_reply":"2022-08-01T06:01:44.489818Z"},"trusted":true},"execution_count":null,"outputs":[]}]}