{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":70905,"databundleVersionId":7746452,"sourceType":"competition"}],"dockerImageVersionId":30887,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Age Prediction","metadata":{}},{"cell_type":"markdown","source":"## Aim\n\nThe aim of this project is to develop a deep learning model to predict the age of individuals based on images of their faces. Using Convolutional Neural Networks (CNNs), the model will be trained on a dataset of labeled facial images, where each image corresponds to the age of the individual. By extracting key features from these images, the model will learn to estimate the age of new, unseen individuals, enabling the application of the model in various domains such as healthcare, marketing, and personalized services.","metadata":{}},{"cell_type":"markdown","source":"## Requirements","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom tensorflow.keras.datasets import cifar10\nimport cv2\nimport pandas as pd\nimport os \nimport numpy as np\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, Flatten, Input, MaxPooling2D,Dropout,BatchNormalization,Reshape\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nfrom sklearn.model_selection import train_test_split\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, Flatten, Input, MaxPooling2D,Dropout,BatchNormalization,Reshape\nfrom sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nfrom sklearn.metrics import accuracy_score\nimport math\nfrom tensorflow.keras.models import load_model\nfrom sklearn.metrics import r2_score\nfrom tensorflow.keras import regularizers","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:08:03.427631Z","iopub.execute_input":"2025-02-10T19:08:03.427948Z","iopub.status.idle":"2025-02-10T19:08:03.433603Z","shell.execute_reply.started":"2025-02-10T19:08:03.427923Z","shell.execute_reply":"2025-02-10T19:08:03.432754Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Import Data","metadata":{}},{"cell_type":"code","source":"df_train=pd.read_csv(\"/kaggle/input/applications-of-deep-learning-wustl-spring-2024/faces-age/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:04:43.736142Z","iopub.execute_input":"2025-02-10T19:04:43.736576Z","iopub.status.idle":"2025-02-10T19:04:43.771810Z","shell.execute_reply.started":"2025-02-10T19:04:43.736552Z","shell.execute_reply":"2025-02-10T19:04:43.771221Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## EDA (Exploratory Data Analysis)","metadata":{}},{"cell_type":"code","source":"df_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:04:43.773460Z","iopub.execute_input":"2025-02-10T19:04:43.773694Z","iopub.status.idle":"2025-02-10T19:04:43.789315Z","shell.execute_reply.started":"2025-02-10T19:04:43.773674Z","shell.execute_reply":"2025-02-10T19:04:43.788401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:04:43.790382Z","iopub.execute_input":"2025-02-10T19:04:43.790643Z","iopub.status.idle":"2025-02-10T19:04:43.800851Z","shell.execute_reply.started":"2025-02-10T19:04:43.790623Z","shell.execute_reply":"2025-02-10T19:04:43.799906Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:04:43.801896Z","iopub.execute_input":"2025-02-10T19:04:43.802191Z","iopub.status.idle":"2025-02-10T19:04:43.808037Z","shell.execute_reply.started":"2025-02-10T19:04:43.802160Z","shell.execute_reply":"2025-02-10T19:04:43.807396Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train['filename']=\"/kaggle/input/applications-of-deep-learning-wustl-spring-2024/faces-age/\"+df_train['filename']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:04:43.808825Z","iopub.execute_input":"2025-02-10T19:04:43.809094Z","iopub.status.idle":"2025-02-10T19:04:43.823721Z","shell.execute_reply.started":"2025-02-10T19:04:43.809041Z","shell.execute_reply":"2025-02-10T19:04:43.822882Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:04:43.824562Z","iopub.execute_input":"2025-02-10T19:04:43.824880Z","iopub.status.idle":"2025-02-10T19:04:43.841151Z","shell.execute_reply.started":"2025-02-10T19:04:43.824851Z","shell.execute_reply":"2025-02-10T19:04:43.840154Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train['age'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:04:43.842961Z","iopub.execute_input":"2025-02-10T19:04:43.843196Z","iopub.status.idle":"2025-02-10T19:04:43.858042Z","shell.execute_reply.started":"2025-02-10T19:04:43.843176Z","shell.execute_reply":"2025-02-10T19:04:43.857319Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(15, 5))\n\nfor i in range(10):\n    sample_img_path = df_train.iloc[i]['filename'] \n    img = cv2.imread(sample_img_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) \n    \n    plt.subplot(2, 5, i + 1)\n    plt.imshow(img)\n    plt.axis('off')\n    plt.title(df_train.iloc[i]['age'])\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:04:44.191478Z","iopub.execute_input":"2025-02-10T19:04:44.191770Z","iopub.status.idle":"2025-02-10T19:04:45.464584Z","shell.execute_reply.started":"2025-02-10T19:04:44.191745Z","shell.execute_reply":"2025-02-10T19:04:45.463433Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(35,15))\nax=sns.countplot(x=df_train[\"age\"],palette=\"viridis\",order=df_train['age'].value_counts().index)\nfor p in ax.containers:\n    ax.bar_label(p, fontsize=15, color='black', padding=5)\nplt.xticks(rotation=90);","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:04:48.560840Z","iopub.execute_input":"2025-02-10T19:04:48.561251Z","iopub.status.idle":"2025-02-10T19:04:49.814604Z","shell.execute_reply.started":"2025-02-10T19:04:48.561218Z","shell.execute_reply":"2025-02-10T19:04:49.813553Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Preprocess Image","metadata":{}},{"cell_type":"code","source":"x=[]\nfor img in df_train['filename']:\n    img=cv2.imread(img)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img=cv2.resize(img,(128,128))\n    img=img/255.0\n    x.append(img)\nx=np.array(x)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:04:52.267481Z","iopub.execute_input":"2025-02-10T19:04:52.267802Z","iopub.status.idle":"2025-02-10T19:07:05.063037Z","shell.execute_reply.started":"2025-02-10T19:04:52.267778Z","shell.execute_reply":"2025-02-10T19:07:05.062250Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y=df_train[[\"age\"]]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:07:39.787604Z","iopub.execute_input":"2025-02-10T19:07:39.787982Z","iopub.status.idle":"2025-02-10T19:07:39.793491Z","shell.execute_reply.started":"2025-02-10T19:07:39.787958Z","shell.execute_reply":"2025-02-10T19:07:39.792407Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x.shape,y.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:07:41.276559Z","iopub.execute_input":"2025-02-10T19:07:41.276911Z","iopub.status.idle":"2025-02-10T19:07:41.282645Z","shell.execute_reply.started":"2025-02-10T19:07:41.276879Z","shell.execute_reply":"2025-02-10T19:07:41.281735Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Modelling","metadata":{}},{"cell_type":"code","source":"x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=.2,random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:07:43.423735Z","iopub.execute_input":"2025-02-10T19:07:43.424141Z","iopub.status.idle":"2025-02-10T19:07:45.191659Z","shell.execute_reply.started":"2025-02-10T19:07:43.424095Z","shell.execute_reply":"2025-02-10T19:07:45.190563Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Input(shape=(128, 128, 3)))\n\nmodel.add(Conv2D(32, kernel_size=(3,3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(64, kernel_size=(3,3), activation='relu',kernel_regularizer=regularizers.l1_l2(l1=0.01,l2=0.01)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(128, kernel_size=(3,3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(256, kernel_size=(3,3), activation='relu', padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Dropout(0.5))  \nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(64, activation='relu'))\n\nmodel.add(Dense(1) ) \n\nmodel.compile(optimizer='adam', loss='mse', metrics=['mae', 'mse'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:08:08.256563Z","iopub.execute_input":"2025-02-10T19:08:08.256935Z","iopub.status.idle":"2025-02-10T19:08:08.445369Z","shell.execute_reply.started":"2025-02-10T19:08:08.256909Z","shell.execute_reply":"2025-02-10T19:08:08.444649Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:08:09.290710Z","iopub.execute_input":"2025-02-10T19:08:09.291033Z","iopub.status.idle":"2025-02-10T19:08:09.316903Z","shell.execute_reply.started":"2025-02-10T19:08:09.291008Z","shell.execute_reply":"2025-02-10T19:08:09.316153Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history=model.fit(x_train,y_train,validation_data=(x_test,y_test),epochs=100,batch_size=36,verbose=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:08:14.091146Z","iopub.execute_input":"2025-02-10T19:08:14.091479Z","iopub.status.idle":"2025-02-10T19:24:23.913667Z","shell.execute_reply.started":"2025-02-10T19:08:14.091454Z","shell.execute_reply":"2025-02-10T19:24:23.912767Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save(\"cnn_model.h5\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:24:23.914803Z","iopub.execute_input":"2025-02-10T19:24:23.915153Z","iopub.status.idle":"2025-02-10T19:24:24.109466Z","shell.execute_reply.started":"2025-02-10T19:24:23.915122Z","shell.execute_reply":"2025-02-10T19:24:24.108673Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Model Interpretation","metadata":{}},{"cell_type":"code","source":"history.history['mae'][-1] ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:24:31.959163Z","iopub.execute_input":"2025-02-10T19:24:31.959517Z","iopub.status.idle":"2025-02-10T19:24:31.964751Z","shell.execute_reply.started":"2025-02-10T19:24:31.959485Z","shell.execute_reply":"2025-02-10T19:24:31.963945Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\n\n# Plot Mean Squared Error (Loss)\nplt.subplot(1, 2, 1)\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Loss (MSE)\")\nplt.title(\"Training vs Validation Loss\")\nplt.legend()\n\n# Plot Mean Absolute Error (MAE)\nplt.subplot(1, 2, 2)\nplt.plot(history.history['mae'], label='Training MAE')\nplt.plot(history.history['val_mae'], label='Validation MAE')\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Mean Absolute Error\")\nplt.title(\"Training vs Validation MAE\")\nplt.legend()\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:24:35.523856Z","iopub.execute_input":"2025-02-10T19:24:35.524220Z","iopub.status.idle":"2025-02-10T19:24:35.925465Z","shell.execute_reply.started":"2025-02-10T19:24:35.524186Z","shell.execute_reply":"2025-02-10T19:24:35.924419Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Testing the Model","metadata":{}},{"cell_type":"code","source":"df_test=pd.read_csv(\"/kaggle/input/applications-of-deep-learning-wustl-spring-2024/faces-age/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:24:44.505996Z","iopub.execute_input":"2025-02-10T19:24:44.506375Z","iopub.status.idle":"2025-02-10T19:24:44.519801Z","shell.execute_reply.started":"2025-02-10T19:24:44.506342Z","shell.execute_reply":"2025-02-10T19:24:44.519161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test['filename']=\"/kaggle/input/applications-of-deep-learning-wustl-spring-2024/faces-age/\"+df_test['filename']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:24:45.632677Z","iopub.execute_input":"2025-02-10T19:24:45.633014Z","iopub.status.idle":"2025-02-10T19:24:45.638054Z","shell.execute_reply.started":"2025-02-10T19:24:45.632983Z","shell.execute_reply":"2025-02-10T19:24:45.637037Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:24:46.106519Z","iopub.execute_input":"2025-02-10T19:24:46.106869Z","iopub.status.idle":"2025-02-10T19:24:46.115340Z","shell.execute_reply.started":"2025-02-10T19:24:46.106837Z","shell.execute_reply":"2025-02-10T19:24:46.114391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_test=[]\nfor img in df_test['filename']:\n    img=cv2.imread(img)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img=cv2.resize(img,(128,128))\n    img=img/255.0\n    x_test.append(img)\nx_test=np.array(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:24:48.216984Z","iopub.execute_input":"2025-02-10T19:24:48.217331Z","iopub.status.idle":"2025-02-10T19:25:03.195646Z","shell.execute_reply.started":"2025-02-10T19:24:48.217303Z","shell.execute_reply":"2025-02-10T19:25:03.194722Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = model.predict(x_test)  \npredictions = np.round(predictions).astype(int)  \npredictions = predictions.flatten()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:25:03.197060Z","iopub.execute_input":"2025-02-10T19:25:03.197412Z","iopub.status.idle":"2025-02-10T19:25:04.976487Z","shell.execute_reply.started":"2025-02-10T19:25:03.197380Z","shell.execute_reply":"2025-02-10T19:25:04.975781Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:25:04.977689Z","iopub.execute_input":"2025-02-10T19:25:04.977971Z","iopub.status.idle":"2025-02-10T19:25:04.983166Z","shell.execute_reply.started":"2025-02-10T19:25:04.977948Z","shell.execute_reply":"2025-02-10T19:25:04.982296Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission=pd.DataFrame({\n    'id':df_test['id'],\n    'age':predictions\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:25:04.984015Z","iopub.execute_input":"2025-02-10T19:25:04.984303Z","iopub.status.idle":"2025-02-10T19:25:04.998099Z","shell.execute_reply.started":"2025-02-10T19:25:04.984279Z","shell.execute_reply":"2025-02-10T19:25:04.997392Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:25:04.998898Z","iopub.execute_input":"2025-02-10T19:25:04.999159Z","iopub.status.idle":"2025-02-10T19:25:05.016768Z","shell.execute_reply.started":"2025-02-10T19:25:04.999125Z","shell.execute_reply":"2025-02-10T19:25:05.015719Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-10T19:25:05.017862Z","iopub.execute_input":"2025-02-10T19:25:05.018185Z","iopub.status.idle":"2025-02-10T19:25:05.034843Z","shell.execute_reply.started":"2025-02-10T19:25:05.018153Z","shell.execute_reply":"2025-02-10T19:25:05.034160Z"}},"outputs":[],"execution_count":null}]}