{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":70905,"databundleVersionId":7746452,"sourceType":"competition"}],"dockerImageVersionId":31012,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Age Prediction from Stable Diffusion Images**\n\nIn this project, we aim to predict the ages of individuals depicted in images generated using **Stable Diffusion**. Our primary metric of success is **Root Mean Squared Error (RMSE)**, which will guide us in evaluating model performance. \n\nThe workflow includes:\n- Leveraging **transfer learning** to optimize model accuracy and efficiency.\n- Conducting **exploratory data analysis (EDA)** to uncover patterns in the dataset.\n- Creating visually engaging **data visualizations** to enhance interpretability.\n\n**Source**: [Kaggle Competition - Applications of Deep Learning (Spring 2024)](https://www.kaggle.com/competitions/applications-of-deep-learning-wustl-spring-2024)\n\n<div style=\"text-align: center;\">\n    <img src=\"https://data.heatonresearch.com/images/wustl/kaggle/2024-spring/diverse.jpg\" alt=\"Image\">\n</div>\n\n","metadata":{}},{"cell_type":"markdown","source":"# Importing Packages","metadata":{}},{"cell_type":"code","source":"import cv2\nimport os\nimport pandas as pd\nimport numpy as np\nimport math\nimport warnings \nwarnings.filterwarnings('ignore')\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom tensorflow.keras.applications import EfficientNetB0\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error, r2_score\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.utils import Sequence","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T08:36:29.316719Z","iopub.execute_input":"2025-04-12T08:36:29.317069Z","iopub.status.idle":"2025-04-12T08:36:29.323216Z","shell.execute_reply.started":"2025-04-12T08:36:29.317042Z","shell.execute_reply":"2025-04-12T08:36:29.322328Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Importing Data","metadata":{}},{"cell_type":"code","source":"#creating file paths and defining folder structure\nimg_path = '/kaggle/input/applications-of-deep-learning-wustl-spring-2024/faces-age'\n\ndf=pd.read_csv(\"/kaggle/input/applications-of-deep-learning-wustl-spring-2024/faces-age/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T08:47:46.661229Z","iopub.execute_input":"2025-04-12T08:47:46.662209Z","iopub.status.idle":"2025-04-12T08:47:46.686946Z","shell.execute_reply.started":"2025-04-12T08:47:46.662175Z","shell.execute_reply":"2025-04-12T08:47:46.686025Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Exploratory Data Analysis - EDA","metadata":{}},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T07:06:47.474812Z","iopub.execute_input":"2025-04-12T07:06:47.475164Z","iopub.status.idle":"2025-04-12T07:06:47.501340Z","shell.execute_reply.started":"2025-04-12T07:06:47.475140Z","shell.execute_reply":"2025-04-12T07:06:47.500334Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T07:09:17.106844Z","iopub.execute_input":"2025-04-12T07:09:17.107579Z","iopub.status.idle":"2025-04-12T07:09:17.112962Z","shell.execute_reply.started":"2025-04-12T07:09:17.107550Z","shell.execute_reply":"2025-04-12T07:09:17.112167Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#age distribution\nplt.figure(figsize=(10, 6))\nsns.histplot(df['age'], bins=10, kde=True, color='skyblue', stat='density')\nplt.title('Age Distribution', fontsize=16)\nplt.xlabel('Age', fontsize=14)\nplt.ylabel('Density', fontsize=14)\nplt.grid(axis='y')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T07:10:26.052074Z","iopub.execute_input":"2025-04-12T07:10:26.052413Z","iopub.status.idle":"2025-04-12T07:10:26.535472Z","shell.execute_reply.started":"2025-04-12T07:10:26.052388Z","shell.execute_reply":"2025-04-12T07:10:26.534558Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#10 random images\nsample_df = df.sample(10, random_state=42)\nimage_files = sample_df['filename']\nages = sample_df['age']\n\nfig, axes = plt.subplots(2, 5, figsize=(15, 6))\nfig.subplots_adjust(hspace=0.5)\nfor idx, (filename, age) in enumerate(zip(image_files, ages)):\n    img = cv2.imread(os.path.join(img_path, filename))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    axes[idx // 5, idx % 5].imshow(img)\n    axes[idx // 5, idx % 5].set_title(f\"Age: {age}\", fontsize=10)\n    axes[idx // 5, idx % 5].axis(\"off\") \nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T10:26:29.843179Z","iopub.execute_input":"2025-04-12T10:26:29.844197Z","iopub.status.idle":"2025-04-12T10:26:31.290240Z","shell.execute_reply.started":"2025-04-12T10:26:29.844165Z","shell.execute_reply":"2025-04-12T10:26:31.288818Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Feature Engineering","metadata":{}},{"cell_type":"code","source":"#preprocessing function for images\ndef preprocess_image(img_path, img_size=(64, 64)):\n    img = cv2.imread(img_path)\n    img = cv2.resize(img, img_size)\n    img = img / 255.0\n    return img","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T08:47:49.751245Z","iopub.execute_input":"2025-04-12T08:47:49.752138Z","iopub.status.idle":"2025-04-12T08:47:49.757128Z","shell.execute_reply.started":"2025-04-12T08:47:49.752107Z","shell.execute_reply":"2025-04-12T08:47:49.756176Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#creating arrays for images and ages\nimages = []\nages = df['age'].values\n\nfor filename in df['filename']:\n    full_path = os.path.join(img_path, filename)\n    images.append(preprocess_image(full_path))\n\nimages = np.array(images)\nages = np.array(ages)\n\nprint(\"Images shape:\", images.shape)\nprint(\"Ages shape:\", ages.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T08:47:50.022671Z","iopub.execute_input":"2025-04-12T08:47:50.023514Z","iopub.status.idle":"2025-04-12T08:49:02.230921Z","shell.execute_reply.started":"2025-04-12T08:47:50.023485Z","shell.execute_reply":"2025-04-12T08:49:02.229850Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#data augmentation\ndatagen = ImageDataGenerator(\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T08:49:03.304526Z","iopub.execute_input":"2025-04-12T08:49:03.308762Z","iopub.status.idle":"2025-04-12T08:49:03.324771Z","shell.execute_reply.started":"2025-04-12T08:49:03.308704Z","shell.execute_reply":"2025-04-12T08:49:03.321280Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Modelling","metadata":{}},{"cell_type":"code","source":"#EfficientNetB0 as a base model\nbase_model = EfficientNetB0(weights=\"imagenet\", include_top=False, input_shape=(64, 64, 3))\n\nmodel = Sequential([\n    base_model,\n    GlobalAveragePooling2D(),\n    Dense(256, activation=\"relu\"),\n    Dropout(0.5),\n    Dense(128, activation=\"relu\"),\n    Dropout(0.3),\n    Dense(1)\n])\n\n#compiling the model\nmodel.compile(optimizer=Adam(learning_rate=0.0001), loss=\"mean_squared_error\", metrics=[\"mae\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T08:49:35.583185Z","iopub.execute_input":"2025-04-12T08:49:35.583586Z","iopub.status.idle":"2025-04-12T08:49:36.637527Z","shell.execute_reply.started":"2025-04-12T08:49:35.583560Z","shell.execute_reply":"2025-04-12T08:49:36.636443Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(images, ages, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T08:49:38.843378Z","iopub.execute_input":"2025-04-12T08:49:38.843734Z","iopub.status.idle":"2025-04-12T08:49:39.596928Z","shell.execute_reply.started":"2025-04-12T08:49:38.843709Z","shell.execute_reply":"2025-04-12T08:49:39.595795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#training the model\nhistory = model.fit(datagen.flow(x_train, y_train, batch_size=32),validation_data=(x_val, y_val),epochs=12, verbose=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T08:49:40.712711Z","iopub.execute_input":"2025-04-12T08:49:40.713099Z","iopub.status.idle":"2025-04-12T09:32:14.675766Z","shell.execute_reply.started":"2025-04-12T08:49:40.713073Z","shell.execute_reply":"2025-04-12T09:32:14.674279Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#predicting ages and computing RMSE\ny_pred = model.predict(x_val).flatten()\n\nrmse = math.sqrt(mean_squared_error(y_val, y_pred))\nprint(f\"RMSE: {rmse}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T09:35:30.387121Z","iopub.execute_input":"2025-04-12T09:35:30.388133Z","iopub.status.idle":"2025-04-12T09:35:50.089562Z","shell.execute_reply.started":"2025-04-12T09:35:30.388095Z","shell.execute_reply":"2025-04-12T09:35:50.088789Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#actual vs predicted ages\nplt.figure(figsize=(10, 6))\nplt.scatter(y_val, y_pred, color='royalblue')\nplt.plot([min(y_val), max(y_val)], [min(y_val), max(y_val)], color='red', linestyle='dashed', linewidth=2)\nplt.title('Actual vs Predicted Ages', fontsize=16)\nplt.xlabel('Actual Ages', fontsize=14)\nplt.ylabel('Predicted Ages', fontsize=14)\nplt.grid()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T10:19:21.782227Z","iopub.execute_input":"2025-04-12T10:19:21.782599Z","iopub.status.idle":"2025-04-12T10:19:22.037349Z","shell.execute_reply.started":"2025-04-12T10:19:21.782572Z","shell.execute_reply":"2025-04-12T10:19:22.036434Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#distribution of actual and predicted ages\nplt.figure(figsize=(10, 6))\nsns.histplot(y_val, color='green', label='Actual Ages', kde=True, stat='density', bins=8)\nsns.histplot(y_pred, color='orange', label='Predicted Ages', kde=True, stat='density', bins=8)\nplt.title('Distribution of Actual and Predicted Ages', fontsize=16)\nplt.xlabel('Ages', fontsize=14)\nplt.ylabel('Density', fontsize=14)\nplt.legend()\nplt.grid()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-12T10:19:37.286756Z","iopub.execute_input":"2025-04-12T10:19:37.287236Z","iopub.status.idle":"2025-04-12T10:19:37.701571Z","shell.execute_reply.started":"2025-04-12T10:19:37.287198Z","shell.execute_reply":"2025-04-12T10:19:37.700520Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Conclusion**\n\nIn this project, we developed a model to predict ages from Stable Diffusion-generated images, achieving a final **RMSE of 10.11**. This result demonstrates the potential of leveraging computer vision techniques for regression tasks.\n\n### **What We Did**\n- Conducted **Exploratory Data Analysis (EDA)** to understand the dataset and visualize patterns.\n- Built a deep learning model utilizing **transfer learning** for enhanced feature extraction and performance.\n- Trained and validated the model while measuring RMSE as the primary evaluation metric.\n\n### **Future Directions**\nTo further improve the model's accuracy:\n- **Advanced Augmentation**: Apply diverse techniques like brightness adjustments or random cropping for increased robustness.\n- **Hyperparameter Tuning**: Experiment with optimizer settings, learning rates, and architecture modifications.\n- **Ensemble Models**: Combine predictions from multiple models to reduce bias and variance.","metadata":{}}]}