{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-26T19:48:34.682841Z","iopub.execute_input":"2022-07-26T19:48:34.683462Z","iopub.status.idle":"2022-07-26T19:48:34.695502Z","shell.execute_reply.started":"2022-07-26T19:48:34.683416Z","shell.execute_reply":"2022-07-26T19:48:34.694423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Import Basic Libraries**\n\n**more to be imported as needed**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport seaborn as sns\nplt.style.use('seaborn-whitegrid')\n\nimport tensorflow","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:48:34.700853Z","iopub.execute_input":"2022-07-26T19:48:34.702019Z","iopub.status.idle":"2022-07-26T19:48:34.719227Z","shell.execute_reply.started":"2022-07-26T19:48:34.701976Z","shell.execute_reply":"2022-07-26T19:48:34.718150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Load Data**","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/facial-keypoints-detection/training.zip', compression='zip', header=0, sep=',', quotechar='\"')\ntest = pd.read_csv('../input/facial-keypoints-detection/test.zip', compression='zip', header=0, sep=',', quotechar='\"')\nIdLookupTable = pd.read_csv('../input/facial-keypoints-detection/IdLookupTable.csv',header=0, sep=',', quotechar='\"')\nSampleSubmission = pd.read_csv('../input/facial-keypoints-detection/SampleSubmission.csv',header=0, sep=',', quotechar='\"')","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:48:34.721505Z","iopub.execute_input":"2022-07-26T19:48:34.723291Z","iopub.status.idle":"2022-07-26T19:48:39.095547Z","shell.execute_reply.started":"2022-07-26T19:48:34.722491Z","shell.execute_reply":"2022-07-26T19:48:39.094601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Train**","metadata":{}},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:48:39.098809Z","iopub.execute_input":"2022-07-26T19:48:39.099203Z","iopub.status.idle":"2022-07-26T19:48:39.138535Z","shell.execute_reply.started":"2022-07-26T19:48:39.099167Z","shell.execute_reply":"2022-07-26T19:48:39.137119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train['Image'][1])","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:48:39.140251Z","iopub.execute_input":"2022-07-26T19:48:39.140880Z","iopub.status.idle":"2022-07-26T19:48:39.150725Z","shell.execute_reply.started":"2022-07-26T19:48:39.140835Z","shell.execute_reply":"2022-07-26T19:48:39.149255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:48:39.152547Z","iopub.execute_input":"2022-07-26T19:48:39.153190Z","iopub.status.idle":"2022-07-26T19:48:39.175088Z","shell.execute_reply.started":"2022-07-26T19:48:39.153147Z","shell.execute_reply":"2022-07-26T19:48:39.173994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Test**","metadata":{}},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:48:39.177401Z","iopub.execute_input":"2022-07-26T19:48:39.178168Z","iopub.status.idle":"2022-07-26T19:48:39.195579Z","shell.execute_reply.started":"2022-07-26T19:48:39.178118Z","shell.execute_reply":"2022-07-26T19:48:39.194060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test['Image'][1])","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:48:39.197613Z","iopub.execute_input":"2022-07-26T19:48:39.198306Z","iopub.status.idle":"2022-07-26T19:48:39.207428Z","shell.execute_reply.started":"2022-07-26T19:48:39.198238Z","shell.execute_reply":"2022-07-26T19:48:39.206335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:48:39.208502Z","iopub.execute_input":"2022-07-26T19:48:39.209123Z","iopub.status.idle":"2022-07-26T19:48:39.226295Z","shell.execute_reply.started":"2022-07-26T19:48:39.209068Z","shell.execute_reply":"2022-07-26T19:48:39.224978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **IdLookupTable**","metadata":{}},{"cell_type":"code","source":"IdLookupTable.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:48:39.231959Z","iopub.execute_input":"2022-07-26T19:48:39.232304Z","iopub.status.idle":"2022-07-26T19:48:39.249610Z","shell.execute_reply.started":"2022-07-26T19:48:39.232248Z","shell.execute_reply":"2022-07-26T19:48:39.248466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Missing Data**","metadata":{}},{"cell_type":"markdown","source":"## **Train**","metadata":{}},{"cell_type":"code","source":"import missingno as msno\nmsno.matrix(train.sample(500),color=(1, 0.38, 0.27))","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:48:39.251289Z","iopub.execute_input":"2022-07-26T19:48:39.252469Z","iopub.status.idle":"2022-07-26T19:48:40.188428Z","shell.execute_reply.started":"2022-07-26T19:48:39.252416Z","shell.execute_reply":"2022-07-26T19:48:40.187304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import missingno as msno\nmsno.heatmap(train,cmap=\"RdYlGn\")","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:48:40.190181Z","iopub.execute_input":"2022-07-26T19:48:40.190587Z","iopub.status.idle":"2022-07-26T19:48:42.101767Z","shell.execute_reply.started":"2022-07-26T19:48:40.190548Z","shell.execute_reply":"2022-07-26T19:48:42.097611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Test**","metadata":{}},{"cell_type":"code","source":"import missingno as msno\nmsno.matrix(test.sample(500),color=(1, 0.38, 0.27))","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:48:42.103254Z","iopub.execute_input":"2022-07-26T19:48:42.104252Z","iopub.status.idle":"2022-07-26T19:48:42.423243Z","shell.execute_reply.started":"2022-07-26T19:48:42.104216Z","shell.execute_reply":"2022-07-26T19:48:42.422287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import missingno as msno\nmsno.heatmap(train,cmap=\"RdYlGn\")","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:48:42.424847Z","iopub.execute_input":"2022-07-26T19:48:42.426934Z","iopub.status.idle":"2022-07-26T19:48:44.296937Z","shell.execute_reply.started":"2022-07-26T19:48:42.426894Z","shell.execute_reply":"2022-07-26T19:48:44.295820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**TEST Set doesn't have NANs, but TRAIN Set has many NANs,**\nneeds to be handled","metadata":{}},{"cell_type":"markdown","source":"# **Handle Missing Value**\n\nMissing values are one of the most common problems you can encounter when you try to prepare your data for machine learning. The reason for the missing values might be human errors,interruptions in the data flow, privacy concerns, and so on. Whatever is the reason, missing values affect the performance of the machine learning models.\n\n#### **[missing-value](https://www.kaggle.com/code/pythonkumar/knn-mean-constant-top-6-imputers) Elaboration**\n\n#### **We should NOT Impute here as these are the X & Y coordinates of presence of a facial feature. Imputing Miising values would result in WRONG Identification points on the images, but NAN values might cause our Algorithm to BREAK**","metadata":{}},{"cell_type":"code","source":"train=train.fillna(method = 'ffill')\ntrain.isnull().sum().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:48:44.298821Z","iopub.execute_input":"2022-07-26T19:48:44.299178Z","iopub.status.idle":"2022-07-26T19:48:44.316860Z","shell.execute_reply.started":"2022-07-26T19:48:44.299144Z","shell.execute_reply":"2022-07-26T19:48:44.315523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Visualize**","metadata":{}},{"cell_type":"code","source":"viz = np.array([train['Image'][i].split(' ') for i in range(len(train))],dtype='float')\nviz","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:48:44.318381Z","iopub.execute_input":"2022-07-26T19:48:44.320393Z","iopub.status.idle":"2022-07-26T19:49:01.900570Z","shell.execute_reply.started":"2022-07-26T19:48:44.320349Z","shell.execute_reply":"2022-07-26T19:49:01.899356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Raw & Reshaped Images**","metadata":{}},{"cell_type":"code","source":"raw=np.array([img.reshape(96,96,1) for img in viz])\nraw[:2]","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:01.901989Z","iopub.execute_input":"2022-07-26T19:49:01.903004Z","iopub.status.idle":"2022-07-26T19:49:02.075850Z","shell.execute_reply.started":"2022-07-26T19:49:01.902964Z","shell.execute_reply":"2022-07-26T19:49:02.074333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Gallery using Matplotlib \nfig, ax = plt.subplots(5,5,figsize = (12,12), dpi = 100)\naxes = ax.ravel()\n\nfor idx,ax  in enumerate(axes):\n    ax.imshow(viz[idx].reshape(96,96,1))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:02.077278Z","iopub.execute_input":"2022-07-26T19:49:02.077747Z","iopub.status.idle":"2022-07-26T19:49:04.549936Z","shell.execute_reply.started":"2022-07-26T19:49:02.077704Z","shell.execute_reply":"2022-07-26T19:49:04.549098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keys = train.drop(['Image'], axis=1)\nkeys","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:04.551334Z","iopub.execute_input":"2022-07-26T19:49:04.551868Z","iopub.status.idle":"2022-07-26T19:49:04.594620Z","shell.execute_reply.started":"2022-07-26T19:49:04.551831Z","shell.execute_reply":"2022-07-26T19:49:04.593580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Gallery using Matplotlib \nfig, ax = plt.subplots(5,5,figsize = (12,12), dpi = 100)\naxes = ax.ravel()\n\nfor idx,ax  in enumerate(axes):\n    ax.imshow(viz[idx].reshape(96,96,1))\n    photo_visualize_pnts = keys.iloc[idx].values\n    ax.scatter(photo_visualize_pnts[0::2], photo_visualize_pnts[1::2], c='Red', marker='*')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:04.596137Z","iopub.execute_input":"2022-07-26T19:49:04.596778Z","iopub.status.idle":"2022-07-26T19:49:07.814191Z","shell.execute_reply.started":"2022-07-26T19:49:04.596737Z","shell.execute_reply":"2022-07-26T19:49:07.813045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Reconstructing Images**","metadata":{}},{"cell_type":"code","source":"from  tensorflow.keras.preprocessing.image import array_to_img\n\nimage=[array_to_img(viz[i,:].reshape(96,96,1)) for i in range(len(viz))]\nimage[:5]","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:07.815934Z","iopub.execute_input":"2022-07-26T19:49:07.817802Z","iopub.status.idle":"2022-07-26T19:49:09.995839Z","shell.execute_reply.started":"2022-07-26T19:49:07.817759Z","shell.execute_reply":"2022-07-26T19:49:09.994746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **MakeDir & Save Images**","metadata":{}},{"cell_type":"code","source":"# # Make Dir\n# import os\n# if not os.path.exists('./Images/'):\n#     os.makedirs('./Images/')\n\n# # Save Images\n# for idx,x in enumerate(image):\n#     x.save(f'./image_{idx}.jpg', 'JPEG')","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:09.997709Z","iopub.execute_input":"2022-07-26T19:49:09.998146Z","iopub.status.idle":"2022-07-26T19:49:10.002829Z","shell.execute_reply.started":"2022-07-26T19:49:09.998107Z","shell.execute_reply":"2022-07-26T19:49:10.001874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Image Manipulation**","metadata":{}},{"cell_type":"markdown","source":"## **Inverting-colors**\n\nThe Image's pixel values range from 0 – 225, and We can invert each value by subtracting the value from 255. \n\n**The provided images are already Negated. Further Inverting will Restore original images**","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nfig, ax = plt.subplots(1,5,figsize = (12,12), dpi = 100)\naxes = ax.ravel()\nfor idx,ax  in enumerate(axes):\n    mask=np.full((96,96,1),255)\n    ax.imshow((mask-raw[idx]))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:10.004505Z","iopub.execute_input":"2022-07-26T19:49:10.005228Z","iopub.status.idle":"2022-07-26T19:49:10.547404Z","shell.execute_reply.started":"2022-07-26T19:49:10.005189Z","shell.execute_reply":"2022-07-26T19:49:10.546277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Rotate**\n\n\nPIL provides in-built image.rotate(angle) function to rotate an image by an angle in Python","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nfig, ax = plt.subplots(1,5,figsize = (12,12), dpi = 100)\naxes = ax.ravel()\nfor idx,ax  in enumerate(axes):\n       ax.imshow((image[idx].rotate(45)))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:10.548880Z","iopub.execute_input":"2022-07-26T19:49:10.549503Z","iopub.status.idle":"2022-07-26T19:49:11.066385Z","shell.execute_reply.started":"2022-07-26T19:49:10.549453Z","shell.execute_reply":"2022-07-26T19:49:11.065380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Flip**","metadata":{}},{"cell_type":"code","source":"# FLIP_TOP_BOTTOM \nfrom PIL import Image\nfig, ax = plt.subplots(1,5,figsize = (12,12), dpi = 100)\naxes = ax.ravel()\nfor idx,ax  in enumerate(axes):\n       ax.imshow(image[idx].transpose(method=Image.FLIP_TOP_BOTTOM))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:11.067846Z","iopub.execute_input":"2022-07-26T19:49:11.068694Z","iopub.status.idle":"2022-07-26T19:49:11.588213Z","shell.execute_reply.started":"2022-07-26T19:49:11.068642Z","shell.execute_reply":"2022-07-26T19:49:11.587207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# FLIP_LEFT_RIGHT\nfrom PIL import Image\nfig, ax = plt.subplots(1,5,figsize = (12,12), dpi = 100)\naxes = ax.ravel()\nfor idx,ax  in enumerate(axes):\n       ax.imshow(image[idx].transpose(method=Image.FLIP_LEFT_RIGHT))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:11.589915Z","iopub.execute_input":"2022-07-26T19:49:11.590546Z","iopub.status.idle":"2022-07-26T19:49:12.398482Z","shell.execute_reply.started":"2022-07-26T19:49:11.590512Z","shell.execute_reply":"2022-07-26T19:49:12.397186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Blur**","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nfrom PIL import ImageFilter\nfig, ax = plt.subplots(1,5,figsize = (12,12), dpi = 100)\naxes = ax.ravel()\nfor idx,ax  in enumerate(axes):\n       ax.imshow(image[idx].filter(ImageFilter.BLUR))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:12.400948Z","iopub.execute_input":"2022-07-26T19:49:12.401344Z","iopub.status.idle":"2022-07-26T19:49:12.925307Z","shell.execute_reply.started":"2022-07-26T19:49:12.401308Z","shell.execute_reply":"2022-07-26T19:49:12.924272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Brightness**","metadata":{}},{"cell_type":"code","source":"from PIL import ImageEnhance\nig, ax = plt.subplots(1,5,figsize = (12,12), dpi = 100)\naxes = ax.ravel()\nfor idx,ax  in enumerate(axes):\n    \n    # Creating object of Brightness class\n    im = ImageEnhance.Brightness(image[idx])\n    \n    ax.imshow(im.enhance(2.0))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:12.931818Z","iopub.execute_input":"2022-07-26T19:49:12.932642Z","iopub.status.idle":"2022-07-26T19:49:13.450288Z","shell.execute_reply.started":"2022-07-26T19:49:12.932598Z","shell.execute_reply":"2022-07-26T19:49:13.449210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Sharpness**","metadata":{}},{"cell_type":"code","source":"from PIL import ImageEnhance\nig, ax = plt.subplots(1,5,figsize = (12,12), dpi = 100)\naxes = ax.ravel()\nfor idx,ax  in enumerate(axes):\n    \n    # Creating object of Sharpness class\n    im = ImageEnhance.Sharpness(image[idx])\n    \n    ax.imshow(im.enhance(-2))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:13.451794Z","iopub.execute_input":"2022-07-26T19:49:13.452811Z","iopub.status.idle":"2022-07-26T19:49:13.969189Z","shell.execute_reply.started":"2022-07-26T19:49:13.452766Z","shell.execute_reply":"2022-07-26T19:49:13.968174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Color**","metadata":{}},{"cell_type":"code","source":"from PIL import ImageEnhance\nfig, ax = plt.subplots(1,5,figsize = (12,12), dpi = 100)\naxes = ax.ravel()\nfor idx,ax  in enumerate(axes):\n    \n    # Creating object of Colour class\n    im = ImageEnhance.Color(image[idx])\n    \n    ax.imshow(im.enhance(5))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:13.970801Z","iopub.execute_input":"2022-07-26T19:49:13.971528Z","iopub.status.idle":"2022-07-26T19:49:14.495559Z","shell.execute_reply.started":"2022-07-26T19:49:13.971486Z","shell.execute_reply":"2022-07-26T19:49:14.494604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Contrast**","metadata":{}},{"cell_type":"code","source":"from PIL import ImageEnhance\nfig, ax = plt.subplots(1,5,figsize = (12,12), dpi = 100)\naxes = ax.ravel()\nfor idx,ax  in enumerate(axes):\n    \n    # Creating object of Contrast class\n    im = ImageEnhance.Contrast(image[idx])\n    \n    ax.imshow(im.enhance(5))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:14.497103Z","iopub.execute_input":"2022-07-26T19:49:14.497734Z","iopub.status.idle":"2022-07-26T19:49:15.006203Z","shell.execute_reply.started":"2022-07-26T19:49:14.497694Z","shell.execute_reply":"2022-07-26T19:49:15.005277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Preparation**","metadata":{}},{"cell_type":"markdown","source":"## **Targets**","metadata":{}},{"cell_type":"code","source":"# target=train.drop(['Image'],axis=1).columns\n# target","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:15.008147Z","iopub.execute_input":"2022-07-26T19:49:15.008839Z","iopub.status.idle":"2022-07-26T19:49:15.013637Z","shell.execute_reply.started":"2022-07-26T19:49:15.008793Z","shell.execute_reply":"2022-07-26T19:49:15.012323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Get Data from Dataframe**","metadata":{}},{"cell_type":"code","source":"# from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# datagen=ImageDataGenerator(\n#                             horizontal_flip=True,\n#                             vertical_flip=True,\n#                             rescale=1./255,\n#                             )\n\n# train_images=datagen.flow_from_dataframe(\n#                                     dataframe=train, \n#                                     directory='./Images', \n#                                     x_col='Image', \n#                                     y_col=target,  \n#                                     target_size=(96, 96), \n#                                     color_mode='grayscale', \n#                                     classes=None, \n#                                     class_mode='raw', \n#                                     validate_filenames=False,\n#                                     )\n\n# train_images","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:15.015075Z","iopub.execute_input":"2022-07-26T19:49:15.015578Z","iopub.status.idle":"2022-07-26T19:49:15.024667Z","shell.execute_reply.started":"2022-07-26T19:49:15.015537Z","shell.execute_reply":"2022-07-26T19:49:15.023557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Keys**","metadata":{}},{"cell_type":"code","source":"# keys=np.array(keys)\n# keys","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:15.026387Z","iopub.execute_input":"2022-07-26T19:49:15.027373Z","iopub.status.idle":"2022-07-26T19:49:15.038189Z","shell.execute_reply.started":"2022-07-26T19:49:15.027331Z","shell.execute_reply":"2022-07-26T19:49:15.037167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Train Test Split**\n\nIt splits the train data into 4 parts, X_train, X_test, y_train, y_test.\n\n* X_train, y_train first used to train the algorithm.\n* X_test is used in that trained algorithms to predict outcomes.\n* Once we get the outcomes, we compare it with y_test","metadata":{}},{"cell_type":"code","source":"# from sklearn.model_selection import train_test_split\n# img_train, img_test, keys_train, keys_test = train_test_split(raw, keys, test_size=0.25,shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:15.039650Z","iopub.execute_input":"2022-07-26T19:49:15.040793Z","iopub.status.idle":"2022-07-26T19:49:15.047528Z","shell.execute_reply.started":"2022-07-26T19:49:15.040767Z","shell.execute_reply":"2022-07-26T19:49:15.046489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model**\n\n#### **Inspired by VGG16**\n\n![vgg1](https://viso.ai/wp-content/uploads/2021/10/VGG-16-architecture-of-the-model.jpg)","metadata":{}},{"cell_type":"markdown","source":"## **Build**","metadata":{}},{"cell_type":"code","source":"from keras.layers import Conv2D, MaxPool2D, BatchNormalization, Flatten, Dense, Dropout\nfrom keras.models import Sequential\nfrom tensorflow.keras import regularizers\n\nmodel = Sequential(name='face-recog')\n\n# Convolution Layer 1\nmodel.add(Conv2D(filters=64,kernel_size=3,strides=1,padding='same',activation='relu',input_shape=(96, 96, 1))),\nmodel.add(MaxPool2D(pool_size=(2, 2))),\n\n# Convolution Layer 2\nmodel.add(Conv2D(filters=128,kernel_size=3,strides=1,padding='same',activation='relu',kernel_regularizer='l1_l2')),\nmodel.add(MaxPool2D(pool_size=(2, 2))),\n\n# Convolution Layer 3\nmodel.add(Conv2D(filters=256,kernel_size=3,strides=1,padding='same',activation='relu',kernel_regularizer='l1_l2')),\nmodel.add(MaxPool2D(pool_size=(2, 2))),\n\n# Convolution Layer 4\nmodel.add(Conv2D(filters=512,kernel_size=3,strides=1,padding='same',activation='relu',kernel_regularizer='l1_l2')),\nmodel.add(MaxPool2D(pool_size=(2, 2))),\n\n# flatten output of Convolutions\nmodel.add(Flatten()),\n\n# Hidden Layer 1\nmodel.add(Dense(units=4096,activation='relu',kernel_regularizer='l1_l2')),\nmodel.add(Dropout(0.25)),\n\n# Hidden Layer 2\nmodel.add(Dense(units=1024,activation='relu',kernel_regularizer='l1_l2')),\n\n# Hidden Layer 3\nmodel.add(Dense(units=256,activation='relu',kernel_regularizer='l1_l2')),\nmodel.add(Dropout(0.25)),\n\n# Output Layer\nmodel.add(Dense(units=30,activation='relu'))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:15.048980Z","iopub.execute_input":"2022-07-26T19:49:15.049972Z","iopub.status.idle":"2022-07-26T19:49:17.852334Z","shell.execute_reply.started":"2022-07-26T19:49:15.049923Z","shell.execute_reply":"2022-07-26T19:49:17.851318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Visualize**","metadata":{}},{"cell_type":"code","source":"# !pip install visualkeras\n# import visualkeras\n# visualkeras.layered_view(model, legend=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:17.854002Z","iopub.execute_input":"2022-07-26T19:49:17.854662Z","iopub.status.idle":"2022-07-26T19:49:17.859451Z","shell.execute_reply.started":"2022-07-26T19:49:17.854624Z","shell.execute_reply":"2022-07-26T19:49:17.857999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.vis_utils import plot_model\nplot_model(model, 'model.png',show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:17.861116Z","iopub.execute_input":"2022-07-26T19:49:17.861794Z","iopub.status.idle":"2022-07-26T19:49:19.110379Z","shell.execute_reply.started":"2022-07-26T19:49:17.861755Z","shell.execute_reply":"2022-07-26T19:49:19.109224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Compile**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.losses import MeanSquaredError\nfrom tensorflow.keras.metrics import Accuracy\n\nmodel.compile(optimizer=Adam(learning_rate = 0.003),\n                loss='mae',\n                metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:19.112473Z","iopub.execute_input":"2022-07-26T19:49:19.113175Z","iopub.status.idle":"2022-07-26T19:49:19.135390Z","shell.execute_reply.started":"2022-07-26T19:49:19.113136Z","shell.execute_reply":"2022-07-26T19:49:19.134473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Callbacks**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\n\nes=EarlyStopping(monitor='val_accuracy', patience=10) # This callback will stop the training when there is no improvement in the loss for three consecutive epochs.,\nlrr = ReduceLROnPlateau(monitor='val_accuracy',patience=10) \ncb = [es, lrr]","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:19.137088Z","iopub.execute_input":"2022-07-26T19:49:19.137494Z","iopub.status.idle":"2022-07-26T19:49:19.143822Z","shell.execute_reply.started":"2022-07-26T19:49:19.137454Z","shell.execute_reply":"2022-07-26T19:49:19.142814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Train**","metadata":{}},{"cell_type":"code","source":"history=model.fit(\n#                     train_images,\n                  x=raw,\n                  y=keys,\n                  callbacks=cb,\n                  epochs=50,\n                  validation_split=0.2,\n#                   use_multiprocessing=True,\n#                   steps_per_epoch=1,\n#                   shuffle=True,\n                )","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:49:19.145598Z","iopub.execute_input":"2022-07-26T19:49:19.146429Z","iopub.status.idle":"2022-07-26T19:50:44.108042Z","shell.execute_reply.started":"2022-07-26T19:49:19.146379Z","shell.execute_reply":"2022-07-26T19:50:44.106905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Plotting Accuracy**","metadata":{}},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:50:44.110486Z","iopub.execute_input":"2022-07-26T19:50:44.111254Z","iopub.status.idle":"2022-07-26T19:51:42.781598Z","shell.execute_reply.started":"2022-07-26T19:50:44.111210Z","shell.execute_reply":"2022-07-26T19:51:42.779715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Predict**","metadata":{}},{"cell_type":"code","source":"# Flatten the Test Images\nviz_test = np.array([test['Image'][i].split(' ') for i in range(len(test))],dtype='float')\n# viz_test\n\n# Reconstruct the Test Images\nraw_test=np.array([img.reshape(96,96,1) for img in viz_test])\nraw_test[:2]","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:51:42.782997Z","iopub.execute_input":"2022-07-26T19:51:42.783426Z","iopub.status.idle":"2022-07-26T19:51:47.548731Z","shell.execute_reply.started":"2022-07-26T19:51:42.783383Z","shell.execute_reply":"2022-07-26T19:51:47.547447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(raw_test)\npred","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:51:47.552212Z","iopub.execute_input":"2022-07-26T19:51:47.554035Z","iopub.status.idle":"2022-07-26T19:51:48.293518Z","shell.execute_reply.started":"2022-07-26T19:51:47.553992Z","shell.execute_reply":"2022-07-26T19:51:48.292133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_names = list(IdLookupTable['FeatureName'])\nimage_ids = list(IdLookupTable['ImageId']-1)\nrow_ids = list(IdLookupTable['RowId'])\n\nfeature_list = [feature_names.index(feature) for feature in feature_names]\n    \npredictions = [pred[x][y] for x,y in zip(image_ids, feature_list)]\npredictions[:5]","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:51:48.295243Z","iopub.execute_input":"2022-07-26T19:51:48.295693Z","iopub.status.idle":"2022-07-26T19:51:48.335730Z","shell.execute_reply.started":"2022-07-26T19:51:48.295655Z","shell.execute_reply":"2022-07-26T19:51:48.334351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Suggestions:-**\n* Kaggle - https://www.kaggle.com/pythonkumar\n* GitHub - https://github.com/KumarPython​\n* Twitter - https://twitter.com/KumarPython\n* LinkedIn - https://www.linkedin.com/in/kumarpython/","metadata":{}},{"cell_type":"markdown","source":"# **Submission**","metadata":{}},{"cell_type":"code","source":"submission=pd.DataFrame({'RowId': row_ids,\n                         'Location': predictions,\n                        })\n# submission\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T19:53:03.355532Z","iopub.execute_input":"2022-07-26T19:53:03.355926Z","iopub.status.idle":"2022-07-26T19:53:03.446054Z","shell.execute_reply.started":"2022-07-26T19:53:03.355894Z","shell.execute_reply":"2022-07-26T19:53:03.444967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}