{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":62057,"databundleVersionId":6714922,"sourceType":"competition"}],"dockerImageVersionId":30699,"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","execution":{"iopub.status.busy":"2024-06-03T19:20:33.106529Z","iopub.execute_input":"2024-06-03T19:20:33.107433Z","iopub.status.idle":"2024-06-03T19:20:41.295027Z","shell.execute_reply.started":"2024-06-03T19:20:33.107395Z","shell.execute_reply":"2024-06-03T19:20:41.293783Z"},"_kg_hide-input":true,"_kg_hide-output":true,"scrolled":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Applications of Deep Learning (WUSTL, Fall 2023)\n<br> Bu projede yapay zeka tarafından üretilen resimler kullanılarak kişinin Gözlüklü olup olmadığını tespit eden model geliştirilmiştir. <br>\n<img src=\"https://www.kaggle.com/competitions/62057/images/header\">","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\n\ndf=pd.read_csv(\"/kaggle/input/applications-of-deep-learning-wustl-fall-2023/train.csv\")\ntest=pd.read_csv(\"/kaggle/input/applications-of-deep-learning-wustl-fall-2023/test.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:21:11.672308Z","iopub.execute_input":"2024-06-03T19:21:11.672924Z","iopub.status.idle":"2024-06-03T19:21:11.748534Z","shell.execute_reply.started":"2024-06-03T19:21:11.672890Z","shell.execute_reply":"2024-06-03T19:21:11.747591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:21:12.895178Z","iopub.execute_input":"2024-06-03T19:21:12.895617Z","iopub.status.idle":"2024-06-03T19:21:14.356911Z","shell.execute_reply.started":"2024-06-03T19:21:12.895581Z","shell.execute_reply":"2024-06-03T19:21:14.355359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path=\"/kaggle/input/applications-of-deep-learning-wustl-fall-2023/\"\nimg=cv2.imread(img_path+str(df[\"file\"][0]))\nimg.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:21:15.588818Z","iopub.execute_input":"2024-06-03T19:21:15.589177Z","iopub.status.idle":"2024-06-03T19:21:15.617859Z","shell.execute_reply.started":"2024-06-03T19:21:15.589151Z","shell.execute_reply":"2024-06-03T19:21:15.616733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:21:16.836932Z","iopub.execute_input":"2024-06-03T19:21:16.837308Z","iopub.status.idle":"2024-06-03T19:21:17.390685Z","shell.execute_reply.started":"2024-06-03T19:21:16.837280Z","shell.execute_reply":"2024-06-03T19:21:17.389596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"size=128\nx=[]\nfor img in df[\"file\"]:\n    img=cv2.imread(img_path+str(img))\n    img=cv2.resize(img, (size,size))\n    img=img/255.0 \n    x.append(img)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:21:18.927096Z","iopub.execute_input":"2024-06-03T19:21:18.927754Z","iopub.status.idle":"2024-06-03T19:23:59.864572Z","shell.execute_reply.started":"2024-06-03T19:21:18.927716Z","shell.execute_reply":"2024-06-03T19:23:59.863250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=np.array(x)\ny=df[\"glasses\"]\nlen(x), y.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:26:20.022660Z","iopub.execute_input":"2024-06-03T19:26:20.023644Z","iopub.status.idle":"2024-06-03T19:26:23.089202Z","shell.execute_reply.started":"2024-06-03T19:26:20.023600Z","shell.execute_reply":"2024-06-03T19:26:23.088048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x[0])","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:26:23.091187Z","iopub.execute_input":"2024-06-03T19:26:23.091515Z","iopub.status.idle":"2024-06-03T19:26:23.447826Z","shell.execute_reply.started":"2024-06-03T19:26:23.091474Z","shell.execute_reply":"2024-06-03T19:26:23.446705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import r2_score, mean_squared_error\nfrom sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.20, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:26:25.691724Z","iopub.execute_input":"2024-06-03T19:26:25.692425Z","iopub.status.idle":"2024-06-03T19:26:30.512812Z","shell.execute_reply.started":"2024-06-03T19:26:25.692391Z","shell.execute_reply":"2024-06-03T19:26:30.511534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, Dense, Flatten, Input, MaxPooling2D, Dropout,BatchNormalization","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:26:30.515235Z","iopub.execute_input":"2024-06-03T19:26:30.515659Z","iopub.status.idle":"2024-06-03T19:26:44.380515Z","shell.execute_reply.started":"2024-06-03T19:26:30.515625Z","shell.execute_reply":"2024-06-03T19:26:44.379323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(64, (3, 3), activation='relu', input_shape=(size, size, 3)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(128, (3, 3), activation='relu', padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(32, activation='relu'))\nmodel.add(Dense(8, activation='relu'))\nmodel.add(Dense(1, activation='sigmoid'))\n\nmodel.compile(optimizer=\"adam\", loss=\"binary_crossentropy\", metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:26:44.381959Z","iopub.execute_input":"2024-06-03T19:26:44.382640Z","iopub.status.idle":"2024-06-03T19:26:45.069629Z","shell.execute_reply.started":"2024-06-03T19:26:44.382607Z","shell.execute_reply":"2024-06-03T19:26:45.068645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=1 #RAM çok fazla dolunca boşaltalım biraz","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:26:45.071923Z","iopub.execute_input":"2024-06-03T19:26:45.072281Z","iopub.status.idle":"2024-06-03T19:26:45.096833Z","shell.execute_reply.started":"2024-06-03T19:26:45.072251Z","shell.execute_reply":"2024-06-03T19:26:45.095575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train, y_train, batch_size=16, epochs=10, validation_data=(x_test, y_test), verbose=1)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:26:45.098413Z","iopub.execute_input":"2024-06-03T19:26:45.098818Z","iopub.status.idle":"2024-06-03T19:30:21.709344Z","shell.execute_reply.started":"2024-06-03T19:26:45.098782Z","shell.execute_reply":"2024-06-03T19:30:21.708177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(history.history).plot(figsize=(8, 5))\nplt.grid(True)\nplt.gca().set_ylim(0, 1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:30:27.754114Z","iopub.execute_input":"2024-06-03T19:30:27.755117Z","iopub.status.idle":"2024-06-03T19:30:28.114780Z","shell.execute_reply.started":"2024-06-03T19:30:27.755064Z","shell.execute_reply":"2024-06-03T19:30:28.113745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, accuracy = model.evaluate(x_test, y_test)\nprint(loss, accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:30:29.788878Z","iopub.execute_input":"2024-06-03T19:30:29.789288Z","iopub.status.idle":"2024-06-03T19:30:33.579758Z","shell.execute_reply.started":"2024-06-03T19:30:29.789253Z","shell.execute_reply":"2024-06-03T19:30:33.578648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import f1_score, confusion_matrix, classification_report","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:30:34.677215Z","iopub.execute_input":"2024-06-03T19:30:34.678029Z","iopub.status.idle":"2024-06-03T19:30:34.683203Z","shell.execute_reply.started":"2024-06-03T19:30:34.677988Z","shell.execute_reply":"2024-06-03T19:30:34.681678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tahmin=model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:30:42.251140Z","iopub.execute_input":"2024-06-03T19:30:42.251745Z","iopub.status.idle":"2024-06-03T19:30:45.225800Z","shell.execute_reply.started":"2024-06-03T19:30:42.251709Z","shell.execute_reply":"2024-06-03T19:30:45.224742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_matrix(y_test, np.round(tahmin).astype(int))","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:30:45.228394Z","iopub.execute_input":"2024-06-03T19:30:45.228911Z","iopub.status.idle":"2024-06-03T19:30:45.240769Z","shell.execute_reply.started":"2024-06-03T19:30:45.228867Z","shell.execute_reply":"2024-06-03T19:30:45.239588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test, np.round(tahmin).astype(int)))","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:30:50.878620Z","iopub.execute_input":"2024-06-03T19:30:50.879458Z","iopub.status.idle":"2024-06-03T19:30:50.898842Z","shell.execute_reply.started":"2024-06-03T19:30:50.879424Z","shell.execute_reply":"2024-06-03T19:30:50.897641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:30:51.815125Z","iopub.execute_input":"2024-06-03T19:30:51.816194Z","iopub.status.idle":"2024-06-03T19:30:51.826734Z","shell.execute_reply.started":"2024-06-03T19:30:51.816152Z","shell.execute_reply":"2024-06-03T19:30:51.825637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=[]\nfor img in test[\"file\"]:\n    img=cv2.imread(img_path+str(img))\n    img=cv2.resize(img, (size,size))\n    img=img/255.0 \n    x.append(img)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:30:52.559572Z","iopub.execute_input":"2024-06-03T19:30:52.560717Z","iopub.status.idle":"2024-06-03T19:31:36.371464Z","shell.execute_reply.started":"2024-06-03T19:30:52.560677Z","shell.execute_reply":"2024-06-03T19:31:36.370466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=np.array(x)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:31:51.180304Z","iopub.execute_input":"2024-06-03T19:31:51.180729Z","iopub.status.idle":"2024-06-03T19:31:51.835840Z","shell.execute_reply.started":"2024-06-03T19:31:51.180696Z","shell.execute_reply":"2024-06-03T19:31:51.833959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=model.predict(x)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:31:53.168449Z","iopub.execute_input":"2024-06-03T19:31:53.169085Z","iopub.status.idle":"2024-06-03T19:31:58.895763Z","shell.execute_reply.started":"2024-06-03T19:31:53.169049Z","shell.execute_reply":"2024-06-03T19:31:58.894580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:31:58.898159Z","iopub.execute_input":"2024-06-03T19:31:58.898601Z","iopub.status.idle":"2024-06-03T19:31:58.907799Z","shell.execute_reply.started":"2024-06-03T19:31:58.898555Z","shell.execute_reply":"2024-06-03T19:31:58.906481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.round(pred).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:31:58.909412Z","iopub.execute_input":"2024-06-03T19:31:58.909888Z","iopub.status.idle":"2024-06-03T19:31:58.919908Z","shell.execute_reply.started":"2024-06-03T19:31:58.909818Z","shell.execute_reply":"2024-06-03T19:31:58.918693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img=cv2.imread(img_path+str(test[\"file\"][2]))\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:32:00.656306Z","iopub.execute_input":"2024-06-03T19:32:00.657405Z","iopub.status.idle":"2024-06-03T19:32:01.098880Z","shell.execute_reply.started":"2024-06-03T19:32:00.657365Z","shell.execute_reply":"2024-06-03T19:32:01.097746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img=cv2.imread(img_path+str(test[\"file\"][3]))\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:32:01.429252Z","iopub.execute_input":"2024-06-03T19:32:01.429679Z","iopub.status.idle":"2024-06-03T19:32:01.845228Z","shell.execute_reply.started":"2024-06-03T19:32:01.429645Z","shell.execute_reply":"2024-06-03T19:32:01.844137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"glasses\"]=np.round(pred).astype(int)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:32:02.188071Z","iopub.execute_input":"2024-06-03T19:32:02.188471Z","iopub.status.idle":"2024-06-03T19:32:02.201358Z","shell.execute_reply.started":"2024-06-03T19:32:02.188437Z","shell.execute_reply":"2024-06-03T19:32:02.200261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[[\"id\", \"glasses\"]].to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:32:03.049474Z","iopub.execute_input":"2024-06-03T19:32:03.049908Z","iopub.status.idle":"2024-06-03T19:32:03.072703Z","shell.execute_reply.started":"2024-06-03T19:32:03.049876Z","shell.execute_reply":"2024-06-03T19:32:03.071534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"glass_model.keras\")","metadata":{"execution":{"iopub.status.busy":"2024-06-03T19:32:08.353068Z","iopub.execute_input":"2024-06-03T19:32:08.353454Z","iopub.status.idle":"2024-06-03T19:32:08.896105Z","shell.execute_reply.started":"2024-06-03T19:32:08.353423Z","shell.execute_reply":"2024-06-03T19:32:08.895101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Geliştirilen model ile en yüksek Kaggle Skoru 0.77984 elde edilmiştir.","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}