{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np \nimport matplotlib.pyplot as plt \nimport pandas as pd \nfrom IPython.display import clear_output \nfrom time import sleep \nimport os ","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_data = pd.read_csv('../input/training/training.csv')\ntest_data = pd.read_csv('../input/test/test.csv')\nlookid_data = pd.read_csv('../input/IdLookupTable.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"924eac64ecad9323bd1bb2579eb810d2c780ce6a"},"cell_type":"code","source":"train_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d5c95cba0c96b01e551864da304f1eaada1cc25b"},"cell_type":"code","source":"train_data.isnull().any().value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c56cb85998c12774ba0745b1795faad223b29628"},"cell_type":"code","source":"train_data.fillna(method = 'ffill', inplace =True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e8e30a0e959850a7b3fbd8fa44736238649b2b49"},"cell_type":"code","source":"train_data.isnull().any().value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a3c0fd68064b1b504c0b5cb35be0182e0af997fd"},"cell_type":"code","source":"train_data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"78c3be7ffa774eaa61034739e612d0401e69fcbc"},"cell_type":"code","source":"train_data.tail(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ae69897c14e449c17b4ebf6bdcfa34fb87c65af9"},"cell_type":"code","source":"len(train_data), len(test_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"7980735dfe2ace7d5da31f176a21d30417355db3"},"cell_type":"code","source":"len(train_data['Image'][0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"15af20dee4b60053558ba252b46b1cefb16e9960"},"cell_type":"code","source":"len(test_data.Image[0].split(' '))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9de2d51981e05a4e14a7839b6776366ebe2eaf8f"},"cell_type":"code","source":"def append_X(X):\n    image_list = []\n    for i in range(len(X)):\n        image_list.append(np.asarray(X.Image[i].split(' '),dtype = 'float').reshape(96,96))                        \n    image = image_list\n    return image\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4cca33e4b55a82a457ea279af2a16d5382d0dede","scrolled":false},"cell_type":"code","source":"X_train = append_X(train_data) \nplt.imshow(X_train[0],cmap='gray')\nplt.show()\n\nX_test = append_X(test_data) \nplt.imshow(X_test[0],cmap='gray')\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"badcf7db02c38ee04a0dbaf5a5d023c37a7f6fef"},"cell_type":"code","source":"y = train_data.iloc[:, :-1].values\ny[1,:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2cf60ebcc35041d6e8af80203b738f599335deb5"},"cell_type":"code","source":"print(len(X_train))\n\n\ndef keypoints_show(x, y=None):\n    plt.imshow(x, 'gray')\n    if y is not None:\n        points = np.vstack(np.split(y, 15)).T\n        plt.plot(points[0], points[1], 'o', color='red')\n    plt.axis('off')   \n\n    \nsample_idx = np.random.choice(len(X_train))\n\ny[sample_idx]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ccd2485f840b2671d06faec3229b6d701544d0c2"},"cell_type":"code","source":"X_train[sample_idx]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e7d79e2a7a0a63acf38a19d134dec9fb9aa853b5"},"cell_type":"code","source":"keypoints_show(X_train[sample_idx], y[sample_idx])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8dfa5abf9f722aa7f6ede068bb7a7df98b662855"},"cell_type":"code","source":"y.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8c356d9a66cd27a531b1ee7225175dde5b9754f4"},"cell_type":"code","source":"np.array(X_train).shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"facdfd8d59f7a32e6f8e1176341caee630c5472e"},"cell_type":"code","source":"X = np.stack(np.array(X_train))[...,None]\nX_t = np.stack(np.array(X_test))[...,None]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7ab686392845784a0beb31fcbed761dd997acc29"},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Activation, Dropout, Conv2D, MaxPool2D, Flatten, LeakyReLU, ELU\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom keras.utils.vis_utils import model_to_dot","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d942c58b28b6485519469fc5e6e1faebaf9ff2f0"},"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters = 256, kernel_size = (6,6), padding = 'Same', activation = 'relu', input_shape = (96,96,1)))\nmodel.add(MaxPool2D(pool_size=(4,4), strides=(4,4)))\n\nmodel.add(Conv2D(filters = 128, kernel_size = (4,4), padding = 'Same', activation = 'relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\n\n\nmodel.add(Conv2D(filters = 64, kernel_size = (4,4), padding = 'Same', activation = 'relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\n\nmodel.add(Conv2D(filters = 32, kernel_size = (3,3), padding = 'Same', activation = 'relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\n\n\nmodel.add(Conv2D(filters = 16, kernel_size = (2,2), padding = 'Same', activation = 'relu'))\nmodel.add(MaxPool2D(pool_size=(1,1), strides=(1,1)))\nmodel.add(Dropout(0.6))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = 'relu'))\nmodel.add(Dense(128, activation = 'relu'))\nmodel.add(Dense(64, activation = 'relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(30, activation = 'relu'))\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7f3680ee338f076cd4157e14c8bd52ecdcb15725"},"cell_type":"code","source":"model.compile(loss='mse', optimizer='adam', metrics = ['mae'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"939e8ff540331ae4bb392ce5d2d05d45cb943b61"},"cell_type":"code","source":"model.fit(X,y,epochs = 50,batch_size = 128,validation_split = 0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"421d3f04d5114ca00ba10577e53a960b5f3f58f3"},"cell_type":"code","source":"pred = model.predict(X_t)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9a14249f689f41eed564db165d48f8881fe6b494"},"cell_type":"code","source":"lookid_list = list(lookid_data['FeatureName'])\nimageID = list(lookid_data['ImageId']-1)\npre_list = list(pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8e0e7c858137e39f5be970b8375e05d25dbb3346"},"cell_type":"code","source":"rowid = lookid_data['RowId']\nrowid=list(rowid)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"61e6e258f96cd666d4bed0437e0129fb492e77e1"},"cell_type":"code","source":"feature = []\nfor f in list(lookid_data['FeatureName']):\n    feature.append(lookid_list.index(f))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"be4d30e575619f537d37671832a48e4d564ccb71"},"cell_type":"code","source":"preded = []\nfor x,y in zip(imageID,feature):\n    preded.append(pre_list[x][y])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6728cf572c3fac4f4340f644c31c36ab6c937580"},"cell_type":"code","source":"rowid = pd.Series(rowid,name = 'RowId')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d9f587a8652157f2a6f21eccaf95505084350cf0"},"cell_type":"code","source":"loc = pd.Series(preded,name = 'Location')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"00d18d1176fd60f90ca68d4e34c6b1bf1048839f"},"cell_type":"code","source":"submission = pd.concat([rowid,loc],axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8c6d33dd2d9b105daddbbf7d339e18c8fdca3a54"},"cell_type":"code","source":"submission.to_csv('face_key_detection_submission.csv',index = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"54f4ab81ffdca0294f9ac12cc5711b640794420e"},"cell_type":"code","source":"y_t = model.predict(X_t)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3dc9a0322ff80940f20655eed1f93a940332c686"},"cell_type":"code","source":"np.array(X_test).shape\nsample_idx = np.random.choice(len(X_test))\nkeypoints_show(X_test[sample_idx], y_t[sample_idx])","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}