{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":70905,"databundleVersionId":7746452,"sourceType":"competition"}],"dockerImageVersionId":31042,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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","trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:50:27.935872Z","iopub.execute_input":"2025-06-10T18:50:27.936171Z","iopub.status.idle":"2025-06-10T18:50:48.987305Z","shell.execute_reply.started":"2025-06-10T18:50:27.936149Z","shell.execute_reply":"2025-06-10T18:50:48.986324Z"},"collapsed":true,"jupyter":{"source_hidden":true,"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train=pd.read_csv(\"/kaggle/input/applications-of-deep-learning-wustl-spring-2024/faces-age/train.csv\")\ntest=pd.read_csv(\"/kaggle/input/applications-of-deep-learning-wustl-spring-2024/faces-age/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:50:48.989194Z","iopub.execute_input":"2025-06-10T18:50:48.989540Z","iopub.status.idle":"2025-06-10T18:50:49.084315Z","shell.execute_reply.started":"2025-06-10T18:50:48.989519Z","shell.execute_reply":"2025-06-10T18:50:49.083503Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:50:49.085198Z","iopub.execute_input":"2025-06-10T18:50:49.085468Z","iopub.status.idle":"2025-06-10T18:50:49.107691Z","shell.execute_reply.started":"2025-06-10T18:50:49.085443Z","shell.execute_reply":"2025-06-10T18:50:49.107013Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:50:49.109386Z","iopub.execute_input":"2025-06-10T18:50:49.109700Z","iopub.status.idle":"2025-06-10T18:50:49.120528Z","shell.execute_reply.started":"2025-06-10T18:50:49.109671Z","shell.execute_reply":"2025-06-10T18:50:49.119873Z"},"_kg_hide-output":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:50:49.121380Z","iopub.execute_input":"2025-06-10T18:50:49.121631Z","iopub.status.idle":"2025-06-10T18:50:51.514534Z","shell.execute_reply.started":"2025-06-10T18:50:49.121607Z","shell.execute_reply":"2025-06-10T18:50:51.513976Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.histplot(train,x='age')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:50:51.515562Z","iopub.execute_input":"2025-06-10T18:50:51.516008Z","iopub.status.idle":"2025-06-10T18:50:51.910512Z","shell.execute_reply.started":"2025-06-10T18:50:51.515978Z","shell.execute_reply":"2025-06-10T18:50:51.909552Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:50:51.911462Z","iopub.execute_input":"2025-06-10T18:50:51.911871Z","iopub.status.idle":"2025-06-10T18:50:51.917247Z","shell.execute_reply.started":"2025-06-10T18:50:51.911832Z","shell.execute_reply":"2025-06-10T18:50:51.916530Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:50:51.918036Z","iopub.execute_input":"2025-06-10T18:50:51.918306Z","iopub.status.idle":"2025-06-10T18:50:51.936558Z","shell.execute_reply.started":"2025-06-10T18:50:51.918284Z","shell.execute_reply":"2025-06-10T18:50:51.935833Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport cv2\nimport os\n\nimages_dir = '/kaggle/input/applications-of-deep-learning-wustl-spring-2024/faces-age/'\ncsv_path = '/kaggle/input/applications-of-deep-learning-wustl-spring-2024/faces-age/train.csv'\n\ndf = pd.read_csv(csv_path)\n\ndata = []\nlabels = []\n\n\nfor index, row in df.iterrows():\n    \n    img_path = os.path.join(images_dir, str(row['filename']))\n   \n    image = cv2.imread(img_path)\n    image = cv2.resize(image, (128, 128))\n    \n    data.append(image)\n    \n    labels.append(row['age'])\n\nprint(f'Total images: {len(data)}')\nprint(f'Total labels: {len(labels)}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:50:51.937328Z","iopub.execute_input":"2025-06-10T18:50:51.937531Z","iopub.status.idle":"2025-06-10T18:53:54.216520Z","shell.execute_reply.started":"2025-06-10T18:50:51.937516Z","shell.execute_reply":"2025-06-10T18:53:54.215737Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_arr=np.array(data)\nlabel_arr=np.array(labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:53:54.219391Z","iopub.execute_input":"2025-06-10T18:53:54.219667Z","iopub.status.idle":"2025-06-10T18:53:54.535969Z","shell.execute_reply.started":"2025-06-10T18:53:54.219622Z","shell.execute_reply":"2025-06-10T18:53:54.535161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y=labels","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:53:54.536795Z","iopub.execute_input":"2025-06-10T18:53:54.537118Z","iopub.status.idle":"2025-06-10T18:53:54.541043Z","shell.execute_reply.started":"2025-06-10T18:53:54.537092Z","shell.execute_reply":"2025-06-10T18:53:54.540457Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x=data_arr/255","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:53:54.541807Z","iopub.execute_input":"2025-06-10T18:53:54.542022Z","iopub.status.idle":"2025-06-10T18:53:56.945024Z","shell.execute_reply.started":"2025-06-10T18:53:54.542005Z","shell.execute_reply":"2025-06-10T18:53:56.944328Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:53:56.945867Z","iopub.execute_input":"2025-06-10T18:53:56.946117Z","iopub.status.idle":"2025-06-10T18:53:56.957293Z","shell.execute_reply.started":"2025-06-10T18:53:56.946096Z","shell.execute_reply":"2025-06-10T18:53:56.956225Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:53:56.958437Z","iopub.execute_input":"2025-06-10T18:53:56.958754Z","iopub.status.idle":"2025-06-10T18:53:57.535236Z","shell.execute_reply.started":"2025-06-10T18:53:56.958726Z","shell.execute_reply":"2025-06-10T18:53:57.534312Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train, x_test, y_train, y_test=train_test_split(x,y,test_size=0.2,random_state=13)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:53:57.536254Z","iopub.execute_input":"2025-06-10T18:53:57.536564Z","iopub.status.idle":"2025-06-10T18:53:59.529880Z","shell.execute_reply.started":"2025-06-10T18:53:57.536537Z","shell.execute_reply":"2025-06-10T18:53:59.529233Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Activation, Conv2D, MaxPooling2D\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:53:59.530715Z","iopub.execute_input":"2025-06-10T18:53:59.530950Z","iopub.status.idle":"2025-06-10T18:54:13.415673Z","shell.execute_reply.started":"2025-06-10T18:53:59.530933Z","shell.execute_reply":"2025-06-10T18:54:13.414987Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.regularizers import l2\n\nl2_reg = 0.001  # biraz daha düşük L2 ile daha esnek öğrenme\n\nmodel = Sequential([\n    layers.Conv2D(32, kernel_size=3, input_shape=(128, 128, 3), activation='relu', padding='same',\n                  kernel_regularizer=l2(l2_reg)),\n    layers.BatchNormalization(),\n    layers.MaxPooling2D(pool_size=(2, 2)),\n    layers.Dropout(0.3),\n\n    layers.Conv2D(64, kernel_size=3, activation='relu', padding='same',\n                  kernel_regularizer=l2(l2_reg)),\n    layers.BatchNormalization(),\n    layers.MaxPooling2D(pool_size=(2, 2)),\n    layers.Dropout(0.3),\n\n    layers.Conv2D(128, kernel_size=3, activation='relu', padding='same',\n                  kernel_regularizer=l2(l2_reg)),\n    layers.BatchNormalization(),\n    layers.MaxPooling2D(pool_size=(2, 2)),\n    layers.Dropout(0.4),\n\n    layers.Flatten(),\n    layers.Dense(128, activation='relu', kernel_regularizer=l2(l2_reg)),\n    layers.BatchNormalization(),\n    layers.Dropout(0.5),\n\n    layers.Dense(1)\n])\n\noptimizer = Adam(learning_rate=0.0002)  # biraz daha yüksek öğrenme oranı\nmodel.compile(optimizer=optimizer, loss='mse', metrics=['mae'])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:54:13.416468Z","iopub.execute_input":"2025-06-10T18:54:13.416995Z","iopub.status.idle":"2025-06-10T18:54:18.277155Z","shell.execute_reply.started":"2025-06-10T18:54:13.416975Z","shell.execute_reply":"2025-06-10T18:54:18.276120Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train=pd.DataFrame(y_train)\ny_test=pd.DataFrame(y_test)\ny_train=y_train.values\ny_test=y_test.values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:54:18.278175Z","iopub.execute_input":"2025-06-10T18:54:18.278764Z","iopub.status.idle":"2025-06-10T18:54:18.287450Z","shell.execute_reply.started":"2025-06-10T18:54:18.278734Z","shell.execute_reply":"2025-06-10T18:54:18.286533Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:54:18.288381Z","iopub.execute_input":"2025-06-10T18:54:18.288609Z","iopub.status.idle":"2025-06-10T18:54:18.322756Z","shell.execute_reply.started":"2025-06-10T18:54:18.288590Z","shell.execute_reply":"2025-06-10T18:54:18.321939Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    x_train, y_train,\n    epochs=50,  # artık 350’ye gerek yok\n    batch_size=64,  # 300 çok büyük, 64 veya 128 daha verimli öğrenir\n    validation_data=(x_test,y_test)\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T18:54:18.323633Z","iopub.execute_input":"2025-06-10T18:54:18.324019Z","iopub.status.idle":"2025-06-10T19:02:44.762117Z","shell.execute_reply.started":"2025-06-10T18:54:18.323998Z","shell.execute_reply":"2025-06-10T19:02:44.759488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nacc = history.history['mae']\nval_acc = history.history['val_mae']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'r', label='Training mae')\nplt.plot(epochs, val_acc, 'b', label='Validation mae')\nplt.title('Training and validation mae')\nplt.legend(loc=0)\nplt.figure()\n\nplt.plot(epochs, loss, 'r', label='Training Loss')\nplt.plot(epochs, val_loss, 'b', label='Validation Loss')\nplt.title('Training and validation loss')\nplt.legend()\n\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T19:02:44.764053Z","iopub.execute_input":"2025-06-10T19:02:44.764399Z","iopub.status.idle":"2025-06-10T19:02:45.171326Z","shell.execute_reply.started":"2025-06-10T19:02:44.764370Z","shell.execute_reply":"2025-06-10T19:02:45.170231Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport cv2\nimport os\n\nimages_dir = '/kaggle/input/applications-of-deep-learning-wustl-spring-2024/faces-age/'\ncsv_path = '/kaggle/input/applications-of-deep-learning-wustl-spring-2024/faces-age/test.csv'\n\ntest_df = pd.read_csv(csv_path)\n\ntest_data = []\nlabels = []\n\n\nfor index, row in test_df.iterrows():\n    \n    img_path = os.path.join(images_dir, str(row['filename']))\n   \n    image = cv2.imread(img_path)\n    image = cv2.resize(image, (128, 128))\n    \n    test_data.append(image)\n\nprint(f'Total images: {len(test_data)}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T19:02:45.172217Z","iopub.execute_input":"2025-06-10T19:02:45.172496Z","iopub.status.idle":"2025-06-10T19:03:01.884530Z","shell.execute_reply.started":"2025-06-10T19:02:45.172476Z","shell.execute_reply":"2025-06-10T19:03:01.883705Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_array=np.array(test_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T19:03:01.885347Z","iopub.execute_input":"2025-06-10T19:03:01.885558Z","iopub.status.idle":"2025-06-10T19:03:01.915035Z","shell.execute_reply.started":"2025-06-10T19:03:01.885542Z","shell.execute_reply":"2025-06-10T19:03:01.914045Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_test=test_array/255","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T19:03:01.916029Z","iopub.execute_input":"2025-06-10T19:03:01.916285Z","iopub.status.idle":"2025-06-10T19:03:02.143274Z","shell.execute_reply.started":"2025-06-10T19:03:01.916266Z","shell.execute_reply":"2025-06-10T19:03:02.142488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T19:03:02.144294Z","iopub.execute_input":"2025-06-10T19:03:02.144671Z","iopub.status.idle":"2025-06-10T19:03:02.150835Z","shell.execute_reply.started":"2025-06-10T19:03:02.144614Z","shell.execute_reply":"2025-06-10T19:03:02.149913Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred=model.predict(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T19:03:02.151986Z","iopub.execute_input":"2025-06-10T19:03:02.152440Z","iopub.status.idle":"2025-06-10T19:03:05.418974Z","shell.execute_reply.started":"2025-06-10T19:03:02.152410Z","shell.execute_reply":"2025-06-10T19:03:05.417943Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T19:03:05.420486Z","iopub.execute_input":"2025-06-10T19:03:05.421159Z","iopub.status.idle":"2025-06-10T19:03:05.427243Z","shell.execute_reply.started":"2025-06-10T19:03:05.421129Z","shell.execute_reply":"2025-06-10T19:03:05.426334Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred=np.round(pred.flatten(),1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T19:03:05.430865Z","iopub.execute_input":"2025-06-10T19:03:05.431215Z","iopub.status.idle":"2025-06-10T19:03:05.441815Z","shell.execute_reply.started":"2025-06-10T19:03:05.431195Z","shell.execute_reply":"2025-06-10T19:03:05.441128Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T19:03:05.442571Z","iopub.execute_input":"2025-06-10T19:03:05.442904Z","iopub.status.idle":"2025-06-10T19:03:05.458115Z","shell.execute_reply.started":"2025-06-10T19:03:05.442877Z","shell.execute_reply":"2025-06-10T19:03:05.457266Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission= pd.DataFrame({\n    \"id\": test_df[\"id\"],\n    \"age\": np.round(pred,1)\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T19:03:05.459013Z","iopub.execute_input":"2025-06-10T19:03:05.459371Z","iopub.status.idle":"2025-06-10T19:03:05.472134Z","shell.execute_reply.started":"2025-06-10T19:03:05.459341Z","shell.execute_reply":"2025-06-10T19:03:05.471236Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T19:03:05.473128Z","iopub.execute_input":"2025-06-10T19:03:05.473417Z","iopub.status.idle":"2025-06-10T19:03:05.492679Z","shell.execute_reply.started":"2025-06-10T19:03:05.473390Z","shell.execute_reply":"2025-06-10T19:03:05.491914Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\",index=False,float_format=\"%.1f\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-10T19:03:05.494184Z","iopub.execute_input":"2025-06-10T19:03:05.494445Z","iopub.status.idle":"2025-06-10T19:03:05.512794Z","shell.execute_reply.started":"2025-06-10T19:03:05.494427Z","shell.execute_reply":"2025-06-10T19:03:05.511939Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}