{"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":"none","dataSources":[{"sourceId":62057,"databundleVersionId":6714922,"sourceType":"competition"}],"dockerImageVersionId":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Applications of Deep Learning - Glasses Detection**","metadata":{}},{"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-03-26T19:47:30.175170Z","iopub.execute_input":"2025-03-26T19:47:30.176027Z","iopub.status.idle":"2025-03-26T19:47:39.859377Z","shell.execute_reply.started":"2025-03-26T19:47:30.175991Z","shell.execute_reply":"2025-03-26T19:47:39.858004Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"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","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T19:47:39.860723Z","iopub.execute_input":"2025-03-26T19:47:39.861269Z","iopub.status.idle":"2025-03-26T19:47:55.447122Z","shell.execute_reply.started":"2025-03-26T19:47:39.861230Z","shell.execute_reply":"2025-03-26T19:47:55.446290Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Data Import","metadata":{}},{"cell_type":"code","source":"train_df=pd.read_csv(\"/kaggle/input/applications-of-deep-learning-wustl-fall-2023/train.csv\")\ntest_df=pd.read_csv('/kaggle/input/applications-of-deep-learning-wustl-fall-2023/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T19:50:09.022530Z","iopub.execute_input":"2025-03-26T19:50:09.022895Z","iopub.status.idle":"2025-03-26T19:50:09.099590Z","shell.execute_reply.started":"2025-03-26T19:50:09.022866Z","shell.execute_reply":"2025-03-26T19:50:09.098432Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['file']=\"/kaggle/input/applications-of-deep-learning-wustl-fall-2023/\"+train_df['file']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T19:51:20.027939Z","iopub.execute_input":"2025-03-26T19:51:20.028283Z","iopub.status.idle":"2025-03-26T19:51:20.037895Z","shell.execute_reply.started":"2025-03-26T19:51:20.028258Z","shell.execute_reply":"2025-03-26T19:51:20.037007Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df['file']=\"/kaggle/input/applications-of-deep-learning-wustl-fall-2023/\"+test_df['file']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T21:08:49.174084Z","iopub.execute_input":"2025-03-26T21:08:49.174508Z","iopub.status.idle":"2025-03-26T21:08:49.184494Z","shell.execute_reply.started":"2025-03-26T21:08:49.174476Z","shell.execute_reply":"2025-03-26T21:08:49.183203Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## EDA - Exploratory Data Analysis","metadata":{}},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T19:51:23.799765Z","iopub.execute_input":"2025-03-26T19:51:23.800143Z","iopub.status.idle":"2025-03-26T19:51:23.809125Z","shell.execute_reply.started":"2025-03-26T19:51:23.800114Z","shell.execute_reply":"2025-03-26T19:51:23.808280Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T19:52:16.444453Z","iopub.execute_input":"2025-03-26T19:52:16.444810Z","iopub.status.idle":"2025-03-26T19:52:16.450162Z","shell.execute_reply.started":"2025-03-26T19:52:16.444778Z","shell.execute_reply":"2025-03-26T19:52:16.449314Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T21:08:52.516937Z","iopub.execute_input":"2025-03-26T21:08:52.517407Z","iopub.status.idle":"2025-03-26T21:08:52.529399Z","shell.execute_reply.started":"2025-03-26T21:08:52.517367Z","shell.execute_reply":"2025-03-26T21:08:52.528392Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['glasses'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T19:52:50.674149Z","iopub.execute_input":"2025-03-26T19:52:50.674508Z","iopub.status.idle":"2025-03-26T19:52:50.689684Z","shell.execute_reply.started":"2025-03-26T19:52:50.674475Z","shell.execute_reply":"2025-03-26T19:52:50.688701Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T19:53:36.337942Z","iopub.execute_input":"2025-03-26T19:53:36.338321Z","iopub.status.idle":"2025-03-26T19:53:36.348612Z","shell.execute_reply.started":"2025-03-26T19:53:36.338291Z","shell.execute_reply":"2025-03-26T19:53:36.347673Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Data Visualization","metadata":{}},{"cell_type":"markdown","source":"**Visualize Class Distribution**","metadata":{}},{"cell_type":"code","source":"# Count the occurrences of each label\nlabel_counts = train_df['glasses'].value_counts()\n\n# Create a bar plot\nplt.figure(figsize=(8, 5))\nsns.barplot(x=label_counts.index, y=label_counts.values, palette='viridis')\nplt.title('Distribution of Glasses')\nplt.xlabel('Glasses')\nplt.ylabel('Count')\nplt.xticks(ticks=label_counts.index, labels=['0', '1'], rotation=0)\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T19:59:39.927016Z","iopub.execute_input":"2025-03-26T19:59:39.927396Z","iopub.status.idle":"2025-03-26T19:59:40.088007Z","shell.execute_reply.started":"2025-03-26T19:59:39.927369Z","shell.execute_reply":"2025-03-26T19:59:40.087039Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Display Sample Images**","metadata":{}},{"cell_type":"code","source":"def show_sample_images(df, label, num_images=5):\n    sample_images = train_df[train_df['glasses'] == label]['file'].sample(num_images).values\n    plt.figure(figsize=(15, 5))\n    for i, img_path in enumerate(sample_images):\n        img = cv2.imread(img_path)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        plt.subplot(1, num_images, i + 1)\n        plt.imshow(img)\n        plt.axis('off')\n        plt.title(f'Label: {label}')\n    plt.show()\n\n# Show sample images for both classes\nshow_sample_images(train_df, 0)\nshow_sample_images(train_df, 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T20:01:49.179123Z","iopub.execute_input":"2025-03-26T20:01:49.179440Z","iopub.status.idle":"2025-03-26T20:01:50.362967Z","shell.execute_reply.started":"2025-03-26T20:01:49.179418Z","shell.execute_reply":"2025-03-26T20:01:50.361797Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Data Preparation","metadata":{}},{"cell_type":"markdown","source":"**Split the Data**","metadata":{}},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(train_df['file'], train_df['glasses'], test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T20:03:32.627307Z","iopub.execute_input":"2025-03-26T20:03:32.627645Z","iopub.status.idle":"2025-03-26T20:03:32.636883Z","shell.execute_reply.started":"2025-03-26T20:03:32.627603Z","shell.execute_reply":"2025-03-26T20:03:32.635822Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Image Preprocessing**","metadata":{}},{"cell_type":"code","source":"def load_and_preprocess_image(file_path):\n    img = cv2.imread(file_path)\n    img = cv2.resize(img, (128, 128))  # Resize to a fixed size\n    img = img / 255.0  # Normalize pixel values\n    return img","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T20:04:06.627259Z","iopub.execute_input":"2025-03-26T20:04:06.627634Z","iopub.status.idle":"2025-03-26T20:04:06.632584Z","shell.execute_reply.started":"2025-03-26T20:04:06.627604Z","shell.execute_reply":"2025-03-26T20:04:06.631527Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Build a CNN Model","metadata":{}},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 3)),\n    MaxPooling2D(pool_size=(2, 2)),\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Conv2D(128, (3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Flatten(),\n    Dense(256, activation='relu'),\n    Dropout(0.5),\n    Dense(1, activation='sigmoid')  # Binary classification\n])\n\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T20:05:31.999978Z","iopub.execute_input":"2025-03-26T20:05:32.000411Z","iopub.status.idle":"2025-03-26T20:05:32.231441Z","shell.execute_reply.started":"2025-03-26T20:05:32.000378Z","shell.execute_reply":"2025-03-26T20:05:32.230352Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Train the Model**","metadata":{}},{"cell_type":"code","source":"# Load and preprocess images for training\ntrain_images = np.array([load_and_preprocess_image(img_path) for img_path in X_train])\ntest_images = np.array([load_and_preprocess_image(img_path) for img_path in X_test])\n\nmodel.fit(train_images, y_train, epochs=10, batch_size=32, validation_data=(test_images, y_test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T20:06:09.065370Z","iopub.execute_input":"2025-03-26T20:06:09.065788Z","iopub.status.idle":"2025-03-26T21:00:51.841582Z","shell.execute_reply.started":"2025-03-26T20:06:09.065741Z","shell.execute_reply":"2025-03-26T21:00:51.840328Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save('cnn_model.h5')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T21:02:07.420449Z","iopub.execute_input":"2025-03-26T21:02:07.420884Z","iopub.status.idle":"2025-03-26T21:02:07.598866Z","shell.execute_reply.started":"2025-03-26T21:02:07.420848Z","shell.execute_reply":"2025-03-26T21:02:07.597686Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Model Evaluation","metadata":{}},{"cell_type":"code","source":"test_loss, test_accuracy = model.evaluate(test_images, y_test)\nprint(f'Test Accuracy: {test_accuracy:.2f}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T21:03:56.065804Z","iopub.execute_input":"2025-03-26T21:03:56.066252Z","iopub.status.idle":"2025-03-26T21:04:38.754879Z","shell.execute_reply.started":"2025-03-26T21:03:56.066216Z","shell.execute_reply":"2025-03-26T21:04:38.753877Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Make predictions on the test set\npredictions = model.predict(test_images)\npredictions = (predictions > 0.5).astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T21:06:24.384078Z","iopub.execute_input":"2025-03-26T21:06:24.384422Z","iopub.status.idle":"2025-03-26T21:06:46.998328Z","shell.execute_reply.started":"2025-03-26T21:06:24.384396Z","shell.execute_reply":"2025-03-26T21:06:46.997117Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cm = confusion_matrix(y_test, predictions)\n\nplt.figure(figsize=(5, 5))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=['No Glass', 'Glass'], yticklabels=['No Glass', 'Glass'])\nplt.ylabel('True Label')\nplt.xlabel('Predicted Label')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T21:06:49.936452Z","iopub.execute_input":"2025-03-26T21:06:49.936809Z","iopub.status.idle":"2025-03-26T21:06:50.160006Z","shell.execute_reply.started":"2025-03-26T21:06:49.936781Z","shell.execute_reply":"2025-03-26T21:06:50.159014Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Model Testing","metadata":{}},{"cell_type":"code","source":"def preprocess_images(image_paths):\n    images = []\n    for img_path in image_paths:\n        img = load_and_preprocess_image(img_path)  # Ensure this function resizes and normalizes the image\n        images.append(img)\n    return np.array(images)\ntest_images = preprocess_images(test_df['file'].values)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T21:20:44.519971Z","iopub.execute_input":"2025-03-26T21:20:44.520369Z","iopub.status.idle":"2025-03-26T21:21:09.027165Z","shell.execute_reply.started":"2025-03-26T21:20:44.520339Z","shell.execute_reply":"2025-03-26T21:21:09.026152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Make predictions\npredictions = model.predict(test_images)\npredictions = (predictions > 0.5).astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T21:21:28.787227Z","iopub.execute_input":"2025-03-26T21:21:28.787633Z","iopub.status.idle":"2025-03-26T21:22:08.863316Z","shell.execute_reply.started":"2025-03-26T21:21:28.787600Z","shell.execute_reply":"2025-03-26T21:22:08.862321Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T21:22:13.706464Z","iopub.execute_input":"2025-03-26T21:22:13.706809Z","iopub.status.idle":"2025-03-26T21:22:13.713476Z","shell.execute_reply.started":"2025-03-26T21:22:13.706781Z","shell.execute_reply":"2025-03-26T21:22:13.712428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = predictions.flatten() ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T21:23:56.458335Z","iopub.execute_input":"2025-03-26T21:23:56.458676Z","iopub.status.idle":"2025-03-26T21:23:56.463088Z","shell.execute_reply.started":"2025-03-26T21:23:56.458648Z","shell.execute_reply":"2025-03-26T21:23:56.462070Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission=pd.DataFrame({\n    'id':test_df['id'],\n    'glasses':predictions\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T21:23:58.960985Z","iopub.execute_input":"2025-03-26T21:23:58.961349Z","iopub.status.idle":"2025-03-26T21:23:58.967128Z","shell.execute_reply.started":"2025-03-26T21:23:58.961320Z","shell.execute_reply":"2025-03-26T21:23:58.966027Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T21:24:05.807483Z","iopub.execute_input":"2025-03-26T21:24:05.807885Z","iopub.status.idle":"2025-03-26T21:24:05.817190Z","shell.execute_reply.started":"2025-03-26T21:24:05.807851Z","shell.execute_reply":"2025-03-26T21:24:05.816165Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-26T21:25:35.352274Z","iopub.execute_input":"2025-03-26T21:25:35.352665Z","iopub.status.idle":"2025-03-26T21:25:35.371987Z","shell.execute_reply.started":"2025-03-26T21:25:35.352634Z","shell.execute_reply":"2025-03-26T21:25:35.370931Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}