{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":16880,"databundleVersionId":858837,"sourceType":"competition"},{"sourceId":924245,"sourceType":"datasetVersion","datasetId":464091}],"dockerImageVersionId":29845,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        os.path.join(dirname, filename)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-13T17:41:34.323851Z","iopub.execute_input":"2024-04-13T17:41:34.324169Z","iopub.status.idle":"2024-04-13T17:42:26.048731Z","shell.execute_reply.started":"2024-04-13T17:41:34.324116Z","shell.execute_reply":"2024-04-13T17:42:26.047881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/deepfake-faces/metadata.csv')","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:26.050880Z","iopub.execute_input":"2024-04-13T17:42:26.051122Z","iopub.status.idle":"2024-04-13T17:42:26.203235Z","shell.execute_reply.started":"2024-04-13T17:42:26.051083Z","shell.execute_reply":"2024-04-13T17:42:26.202298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"data : \")\ndf","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:26.204674Z","iopub.execute_input":"2024-04-13T17:42:26.204939Z","iopub.status.idle":"2024-04-13T17:42:26.238607Z","shell.execute_reply.started":"2024-04-13T17:42:26.204889Z","shell.execute_reply":"2024-04-13T17:42:26.237589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"shape : {df.shape}\\n\")\nprint(f\"columns : {df.columns}\\n\")\nprint(f\"df.duplicated().sum() : {df.duplicated().sum()}\\n\")\nprint(f\"df.isnull().sum() : {df.isnull().sum()}\\n\")\nprint(f\"df.info() : {df.info()}\\n\")\nprint(f\"df.nunique() : {df.nunique()}\\n\")","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:26.239682Z","iopub.execute_input":"2024-04-13T17:42:26.240005Z","iopub.status.idle":"2024-04-13T17:42:26.422793Z","shell.execute_reply.started":"2024-04-13T17:42:26.239948Z","shell.execute_reply":"2024-04-13T17:42:26.421699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"object_columns = df.select_dtypes(include=['object']).columns\nprint(\"Object type columns:\")\nprint(object_columns)\n\nnumerical_columns = df.select_dtypes(include=['int64', 'float64']).columns\nprint(\"\\nNumerical type columns:\")\nprint(numerical_columns)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:26.425776Z","iopub.execute_input":"2024-04-13T17:42:26.426118Z","iopub.status.idle":"2024-04-13T17:42:26.445038Z","shell.execute_reply.started":"2024-04-13T17:42:26.426062Z","shell.execute_reply":"2024-04-13T17:42:26.444181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def classify_features(df):\n    categorical_features = []\n    non_categorical_features = []\n    discrete_features = []\n    continuous_features = []\n\n    for column in df.columns:\n        if df[column].dtype == 'object':\n            if df[column].nunique() < 10:\n                categorical_features.append(column)\n            else:\n                non_categorical_features.append(column)\n        elif df[column].dtype in ['int64', 'float64']:\n            if df[column].nunique() < 10:\n                discrete_features.append(column)\n            else:\n                continuous_features.append(column)\n\n    return categorical_features, non_categorical_features, discrete_features, continuous_features","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:26.448058Z","iopub.execute_input":"2024-04-13T17:42:26.448364Z","iopub.status.idle":"2024-04-13T17:42:26.456955Z","shell.execute_reply.started":"2024-04-13T17:42:26.448309Z","shell.execute_reply":"2024-04-13T17:42:26.456105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical, non_categorical, discrete, continuous = classify_features(df)\nprint(\"Categorical Features:\", categorical)\nprint(\"Non-Categorical Features:\", non_categorical)\nprint(\"Discrete Features:\", discrete)\nprint(\"Continuous Features:\", continuous)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:26.458013Z","iopub.execute_input":"2024-04-13T17:42:26.458230Z","iopub.status.idle":"2024-04-13T17:42:26.507340Z","shell.execute_reply.started":"2024-04-13T17:42:26.458193Z","shell.execute_reply":"2024-04-13T17:42:26.506732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.fillna(\"Not Available\")","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:26.508625Z","iopub.execute_input":"2024-04-13T17:42:26.508875Z","iopub.status.idle":"2024-04-13T17:42:26.546176Z","shell.execute_reply.started":"2024-04-13T17:42:26.508821Z","shell.execute_reply":"2024-04-13T17:42:26.545482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in categorical:\n    print(i,':', df[i].unique())\n    print()\nfor i in categorical:\n    print(df[i].value_counts())\n    print()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:26.547511Z","iopub.execute_input":"2024-04-13T17:42:26.547791Z","iopub.status.idle":"2024-04-13T17:42:26.578223Z","shell.execute_reply.started":"2024-04-13T17:42:26.547744Z","shell.execute_reply":"2024-04-13T17:42:26.577606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:26.579355Z","iopub.execute_input":"2024-04-13T17:42:26.579625Z","iopub.status.idle":"2024-04-13T17:42:28.443853Z","shell.execute_reply.started":"2024-04-13T17:42:26.579573Z","shell.execute_reply":"2024-04-13T17:42:28.443099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in categorical:\n    plt.figure(figsize=(15,6))\n    sns.countplot(x = df[i], data = df, palette = 'hls')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:28.445088Z","iopub.execute_input":"2024-04-13T17:42:28.445319Z","iopub.status.idle":"2024-04-13T17:42:28.735485Z","shell.execute_reply.started":"2024-04-13T17:42:28.445280Z","shell.execute_reply":"2024-04-13T17:42:28.734687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in categorical:\n    plt.figure(figsize=(30,20)) \n    plt.pie(df[i].value_counts(), labels=df[i].value_counts().index, \n            autopct='%1.1f%%', textprops={ 'fontsize': 20,\n                                           'color': 'black',\n                                           'weight': 'bold',\n                                           'family': 'serif' }) \n    hfont = {'fontname':'serif', 'weight': 'bold'}\n    plt.title(i, size=20, **hfont) \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:28.736899Z","iopub.execute_input":"2024-04-13T17:42:28.737383Z","iopub.status.idle":"2024-04-13T17:42:29.265943Z","shell.execute_reply.started":"2024-04-13T17:42:28.737328Z","shell.execute_reply":"2024-04-13T17:42:29.265037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in numerical_columns:\n    plt.figure(figsize=(15,6))\n    sns.distplot(df[i], kde = True, bins = 20)\n    plt.xticks(rotation = 90)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:29.267314Z","iopub.execute_input":"2024-04-13T17:42:29.267675Z","iopub.status.idle":"2024-04-13T17:42:29.997552Z","shell.execute_reply.started":"2024-04-13T17:42:29.267616Z","shell.execute_reply":"2024-04-13T17:42:29.996461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in numerical_columns:\n    plt.figure(figsize=(15,6))\n    sns.boxplot(x = df[i],data = df, palette = 'hls')\n    plt.xticks(rotation = 90)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:29.999369Z","iopub.execute_input":"2024-04-13T17:42:29.999997Z","iopub.status.idle":"2024-04-13T17:42:30.662659Z","shell.execute_reply.started":"2024-04-13T17:42:29.999929Z","shell.execute_reply":"2024-04-13T17:42:30.661297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in numerical_columns:\n    plt.figure(figsize=(15,6))\n    sns.violinplot(x = df[i],data = df, palette = 'hls')\n    plt.xticks(rotation = 90)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:30.664664Z","iopub.execute_input":"2024-04-13T17:42:30.665240Z","iopub.status.idle":"2024-04-13T17:42:31.928991Z","shell.execute_reply.started":"2024-04-13T17:42:30.665009Z","shell.execute_reply":"2024-04-13T17:42:31.927854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in categorical:\n    for j in numerical_columns:\n        plt.figure(figsize=(15,6))\n        sns.barplot(x = df[i], y = df[j], data = df, ci = None, palette = 'hls')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:31.930976Z","iopub.execute_input":"2024-04-13T17:42:31.931424Z","iopub.status.idle":"2024-04-13T17:42:32.672158Z","shell.execute_reply.started":"2024-04-13T17:42:31.931345Z","shell.execute_reply":"2024-04-13T17:42:32.671056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in categorical:\n    for j in numerical_columns:\n        plt.figure(figsize=(15,6))\n        sns.boxplot(x = df[i], y = df[j], data = df, palette = 'hls')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:32.674265Z","iopub.execute_input":"2024-04-13T17:42:32.674674Z","iopub.status.idle":"2024-04-13T17:42:33.450811Z","shell.execute_reply.started":"2024-04-13T17:42:32.674610Z","shell.execute_reply":"2024-04-13T17:42:33.447526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in categorical:\n    for j in numerical_columns:\n        plt.figure(figsize=(15,6))\n        sns.violinplot(x = df[i], y = df[j], data = df, palette = 'hls')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:33.453879Z","iopub.execute_input":"2024-04-13T17:42:33.454625Z","iopub.status.idle":"2024-04-13T17:42:35.006502Z","shell.execute_reply.started":"2024-04-13T17:42:33.454551Z","shell.execute_reply":"2024-04-13T17:42:35.005444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in numerical_columns:\n    for j in numerical_columns:\n        if i != j:\n            plt.figure(figsize=(15,6))\n            sns.scatterplot(x = df[j], y = df[i], data = df, palette = 'hls')\n            plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:35.008565Z","iopub.execute_input":"2024-04-13T17:42:35.009303Z","iopub.status.idle":"2024-04-13T17:42:38.227477Z","shell.execute_reply.started":"2024-04-13T17:42:35.009231Z","shell.execute_reply":"2024-04-13T17:42:38.223721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"real_df = df[df[\"label\"] == \"REAL\"]\nfake_df = df[df[\"label\"] == \"FAKE\"]\nsample_size = 10000\n\nreal_df = real_df.sample(sample_size, random_state=42)\nfake_df = fake_df.sample(sample_size, random_state=42)\n\nsample_meta = pd.concat([real_df, fake_df])","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:38.229930Z","iopub.execute_input":"2024-04-13T17:42:38.230520Z","iopub.status.idle":"2024-04-13T17:42:38.300820Z","shell.execute_reply.started":"2024-04-13T17:42:38.230281Z","shell.execute_reply":"2024-04-13T17:42:38.300196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nTrain_set, Test_set = train_test_split(sample_meta,test_size=0.2,random_state=42,stratify=sample_meta['label'])\nTrain_set, Val_set  = train_test_split(Train_set,test_size=0.3,random_state=42,stratify=Train_set['label'])\nTrain_set.shape,Val_set.shape,Test_set.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:38.301841Z","iopub.execute_input":"2024-04-13T17:42:38.302058Z","iopub.status.idle":"2024-04-13T17:42:38.640223Z","shell.execute_reply.started":"2024-04-13T17:42:38.302022Z","shell.execute_reply":"2024-04-13T17:42:38.639500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\n\nimage_path = '/kaggle/input/deepfake-faces/faces_224/'\n\nimage_files = os.listdir(image_path)\n\nimage_files.sort()\n\nselected_images = image_files[:9]\n\nplt.figure(figsize=(10, 10))\n\nfor index, image_file in enumerate(selected_images):\n    image = cv2.imread(os.path.join(image_path, image_file))\n\n    plt.subplot(3, 3, index + 1)\n    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n    plt.title(f'Image {index + 1}')\n    plt.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:38.641363Z","iopub.execute_input":"2024-04-13T17:42:38.641599Z","iopub.status.idle":"2024-04-13T17:42:39.974260Z","shell.execute_reply.started":"2024-04-13T17:42:38.641558Z","shell.execute_reply":"2024-04-13T17:42:39.973287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, image_file in enumerate(image_files[:10]):\n    image = cv2.imread(os.path.join(image_path, image_file))\n    if image is not None:\n        height, width, _ = image.shape\n        print(f\"Resolution of image {i+1}: {width} x {height}\")\n    else:\n        print(f\"Error reading image {i+1}\")\n\nif len(image_files) < 10:\n    print(f\"Only {len(image_files)} images found in the directory.\")","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:39.975735Z","iopub.execute_input":"2024-04-13T17:42:39.976025Z","iopub.status.idle":"2024-04-13T17:42:40.014858Z","shell.execute_reply.started":"2024-04-13T17:42:39.975972Z","shell.execute_reply":"2024-04-13T17:42:40.013967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,15))\nfor cur,i in enumerate(Train_set.index[25:50]):\n    plt.subplot(5,5,cur+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    \n    plt.imshow(cv2.imread('../input/deepfake-faces/faces_224/'+Train_set.loc[i,'videoname'][:-4]+'.jpg'))\n    \n    if(Train_set.loc[i,'label']=='FAKE'):\n        plt.xlabel('FAKE Image')\n    else:\n        plt.xlabel('REAL Image')\n        \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:40.016408Z","iopub.execute_input":"2024-04-13T17:42:40.016734Z","iopub.status.idle":"2024-04-13T17:42:41.807201Z","shell.execute_reply.started":"2024-04-13T17:42:40.016678Z","shell.execute_reply":"2024-04-13T17:42:41.806439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def retreive_dataset(set_name):\n    images,labels=[],[]\n    for (img, imclass) in zip(set_name['videoname'], set_name['label']):\n        images.append(cv2.imread('../input/deepfake-faces/faces_224/'+img[:-4]+'.jpg'))\n        if(imclass=='FAKE'):\n            labels.append(1)\n        else:\n            labels.append(0)\n    \n    return np.array(images),np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:41.808483Z","iopub.execute_input":"2024-04-13T17:42:41.808729Z","iopub.status.idle":"2024-04-13T17:42:41.815111Z","shell.execute_reply.started":"2024-04-13T17:42:41.808689Z","shell.execute_reply":"2024-04-13T17:42:41.814461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,y_train=retreive_dataset(Train_set)\nX_val,y_val=retreive_dataset(Val_set)\nX_test,y_test=retreive_dataset(Test_set)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:42:41.817715Z","iopub.execute_input":"2024-04-13T17:42:41.817956Z","iopub.status.idle":"2024-04-13T17:44:38.692960Z","shell.execute_reply.started":"2024-04-13T17:42:41.817916Z","shell.execute_reply":"2024-04-13T17:44:38.692140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, models\nfrom functools import partial\n\ntf.random.set_seed(42)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:44:38.694183Z","iopub.execute_input":"2024-04-13T17:44:38.694668Z","iopub.status.idle":"2024-04-13T17:44:43.259380Z","shell.execute_reply.started":"2024-04-13T17:44:38.694612Z","shell.execute_reply":"2024-04-13T17:44:43.258618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DefaultConv2D = partial(layers.Conv2D, kernel_size=3, padding=\"same\",\n                        activation=\"relu\", kernel_initializer=\"he_normal\")\n\n# Model Definition\nmodel = models.Sequential([\n    DefaultConv2D(filters=64, kernel_size=7, input_shape=[224, 224, 3]),\n    layers.MaxPooling2D(),\n    layers.BatchNormalization(),\n    DefaultConv2D(filters=128),\n    DefaultConv2D(filters=128),\n    layers.MaxPooling2D(),\n    layers.BatchNormalization(),\n    layers.Flatten(),\n    layers.Dense(units=128, activation=\"relu\",\n                 kernel_initializer=\"he_normal\"),\n    layers.BatchNormalization(),\n    layers.Dropout(0.5),\n    layers.Dense(units=64, activation=\"relu\",\n                 kernel_initializer=\"he_normal\"),\n    layers.BatchNormalization(),\n    layers.Dropout(0.5),\n    layers.Dense(units=1, activation=\"sigmoid\")\n])","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:44:43.260688Z","iopub.execute_input":"2024-04-13T17:44:43.260978Z","iopub.status.idle":"2024-04-13T17:44:47.436105Z","shell.execute_reply.started":"2024-04-13T17:44:43.260924Z","shell.execute_reply":"2024-04-13T17:44:47.435147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"initial_learning_rate = 0.001\nlr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(\n    initial_learning_rate, decay_steps=100000, decay_rate=0.96, staircase=True\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:44:47.437628Z","iopub.execute_input":"2024-04-13T17:44:47.437964Z","iopub.status.idle":"2024-04-13T17:44:47.442916Z","shell.execute_reply.started":"2024-04-13T17:44:47.437904Z","shell.execute_reply":"2024-04-13T17:44:47.442070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=lr_schedule),\n              loss=\"binary_crossentropy\",metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:44:47.444182Z","iopub.execute_input":"2024-04-13T17:44:47.444453Z","iopub.status.idle":"2024-04-13T17:44:47.496204Z","shell.execute_reply.started":"2024-04-13T17:44:47.444386Z","shell.execute_reply":"2024-04-13T17:44:47.495434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:44:47.497404Z","iopub.execute_input":"2024-04-13T17:44:47.497649Z","iopub.status.idle":"2024-04-13T17:44:47.506579Z","shell.execute_reply.started":"2024-04-13T17:44:47.497609Z","shell.execute_reply":"2024-04-13T17:44:47.505656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train,epochs=10,batch_size=32,validation_data=(X_val,y_val),verbose=1)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T17:44:47.518679Z","iopub.execute_input":"2024-04-13T17:44:47.518909Z","iopub.status.idle":"2024-04-13T17:55:46.740872Z","shell.execute_reply.started":"2024-04-13T17:44:47.518871Z","shell.execute_reply":"2024-04-13T17:55:46.740086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, confusion_matrix, classification_report, roc_auc_score","metadata":{"execution":{"iopub.status.busy":"2024-03-30T03:21:32.271636Z","iopub.status.idle":"2024-03-30T03:21:32.272102Z","shell.execute_reply.started":"2024-03-30T03:21:32.271849Z","shell.execute_reply":"2024-03-30T03:21:32.27187Z"}}},{"cell_type":"code","source":"y_test_pred_binary = (y_pred > 0.5).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:23:12.147461Z","iopub.execute_input":"2024-04-13T18:23:12.147771Z","iopub.status.idle":"2024-04-13T18:23:12.156556Z","shell.execute_reply.started":"2024-04-13T18:23:12.147722Z","shell.execute_reply":"2024-04-13T18:23:12.155769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_pred = model.predict(X_train)\ny_train_pred_binary = (y_train_pred > 0.5).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:23:12.157891Z","iopub.execute_input":"2024-04-13T18:23:12.158151Z","iopub.status.idle":"2024-04-13T18:23:30.860263Z","shell.execute_reply.started":"2024-04-13T18:23:12.158101Z","shell.execute_reply":"2024-04-13T18:23:30.859492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, confusion_matrix, classification_report, roc_auc_score\n\ntrain_accuracy = accuracy_score(y_train, y_train_pred_binary)\nprint(f\"Training Accuracy: {train_accuracy * 100:.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:23:30.861528Z","iopub.execute_input":"2024-04-13T18:23:30.861816Z","iopub.status.idle":"2024-04-13T18:23:30.868344Z","shell.execute_reply.started":"2024-04-13T18:23:30.861766Z","shell.execute_reply":"2024-04-13T18:23:30.867664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_accuracy = accuracy_score(y_test, y_test_pred_binary)\nprint(f\"Test Accuracy: {test_accuracy * 100:.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:23:30.869621Z","iopub.execute_input":"2024-04-13T18:23:30.869868Z","iopub.status.idle":"2024-04-13T18:23:30.879014Z","shell.execute_reply.started":"2024-04-13T18:23:30.869827Z","shell.execute_reply":"2024-04-13T18:23:30.878324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f1 = f1_score(y_test, y_test_pred_binary)\nprint(f\"F1 Score: {f1:.4f}\")\n\nprecision = precision_score(y_test, y_test_pred_binary)\nprint(f\"Precison: {precision:.4f}\")\n\nrecall = recall_score(y_test, y_test_pred_binary)\nprint(f\"Recall: {recall:.4f}\")\n\n# Calculate AUC-ROC\nauc_roc = roc_auc_score(y_test, y_test_pred_binary)\nprint(f\"AUC-ROC: {auc_roc:.4f}\")","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:23:30.880459Z","iopub.execute_input":"2024-04-13T18:23:30.880780Z","iopub.status.idle":"2024-04-13T18:23:30.898966Z","shell.execute_reply.started":"2024-04-13T18:23:30.880725Z","shell.execute_reply":"2024-04-13T18:23:30.898120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conf_matrix = confusion_matrix(y_test, y_test_pred_binary)\nprint(\"Confusion Matrix:\")\nprint(conf_matrix)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:23:30.900083Z","iopub.execute_input":"2024-04-13T18:23:30.900352Z","iopub.status.idle":"2024-04-13T18:23:30.912483Z","shell.execute_reply.started":"2024-04-13T18:23:30.900304Z","shell.execute_reply":"2024-04-13T18:23:30.911418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import scikitplot as skplt\nskplt.metrics.plot_confusion_matrix(y_test, y_test_pred_binary, normalize=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:23:30.913766Z","iopub.execute_input":"2024-04-13T18:23:30.914035Z","iopub.status.idle":"2024-04-13T18:23:31.295100Z","shell.execute_reply.started":"2024-04-13T18:23:30.913983Z","shell.execute_reply":"2024-04-13T18:23:31.293954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_report = classification_report(y_test, y_test_pred_binary)\nprint(\"Classification Report:\")\nprint(class_report)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:23:31.297307Z","iopub.execute_input":"2024-04-13T18:23:31.297768Z","iopub.status.idle":"2024-04-13T18:23:31.323164Z","shell.execute_reply.started":"2024-04-13T18:23:31.297696Z","shell.execute_reply":"2024-04-13T18:23:31.321679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:23:31.325063Z","iopub.execute_input":"2024-04-13T18:23:31.325475Z","iopub.status.idle":"2024-04-13T18:23:31.631413Z","shell.execute_reply.started":"2024-04-13T18:23:31.325403Z","shell.execute_reply":"2024-04-13T18:23:31.630349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:23:31.633400Z","iopub.execute_input":"2024-04-13T18:23:31.634151Z","iopub.status.idle":"2024-04-13T18:23:31.964913Z","shell.execute_reply.started":"2024-04-13T18:23:31.634078Z","shell.execute_reply":"2024-04-13T18:23:31.964167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import ResNet50\n\nbase_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224,224,3))\n\nfor layer in base_model.layers:\n    layer.trainable = False\n    \nmodel_resnet50 = models.Sequential()\nmodel_resnet50.add(base_model)\nmodel_resnet50.add(layers.GlobalAveragePooling2D())\nmodel_resnet50.add(layers.Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:23:31.966335Z","iopub.execute_input":"2024-04-13T18:23:31.966679Z","iopub.status.idle":"2024-04-13T18:23:36.260015Z","shell.execute_reply.started":"2024-04-13T18:23:31.966628Z","shell.execute_reply":"2024-04-13T18:23:36.259099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_resnet50.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:23:36.261605Z","iopub.execute_input":"2024-04-13T18:23:36.261959Z","iopub.status.idle":"2024-04-13T18:23:36.285761Z","shell.execute_reply.started":"2024-04-13T18:23:36.261898Z","shell.execute_reply":"2024-04-13T18:23:36.284887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import optimizers\n\nmodel_resnet50.compile(optimizer=optimizers.Adam(lr=0.001), loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:23:36.287275Z","iopub.execute_input":"2024-04-13T18:23:36.287599Z","iopub.status.idle":"2024-04-13T18:23:36.356628Z","shell.execute_reply.started":"2024-04-13T18:23:36.287527Z","shell.execute_reply":"2024-04-13T18:23:36.355946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model_resnet50.fit(\n    X_train, y_train,\n    epochs=10,  \n    validation_data=(X_val, y_val),\n    verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-13T18:23:36.358044Z","iopub.execute_input":"2024-04-13T18:23:36.358334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model_resnet50.predict(X_test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_pred_binary = (y_pred > 0.5).astype(int)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_pred = model_resnet50.predict(X_train)\ny_train_pred_binary = (y_train_pred > 0.5).astype(int)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_accuracy = accuracy_score(y_train, y_train_pred_binary)\nprint(f\"Training Accuracy: {train_accuracy * 100:.2f}%\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_accuracy = accuracy_score(y_test, y_test_pred_binary)\nprint(f\"Test Accuracy: {test_accuracy * 100:.2f}%\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f1 = f1_score(y_test, y_test_pred_binary)\nprint(f\"F1 Score: {f1:.4f}\")\n\nprecision = precision_score(y_test, y_test_pred_binary)\nprint(f\"Precison: {precision:.4f}\")\n\nrecall = recall_score(y_test, y_test_pred_binary)\nprint(f\"Recall: {recall:.4f}\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conf_matrix = confusion_matrix(y_test, y_test_pred_binary)\nprint(\"Confusion Matrix:\")\nprint(conf_matrix)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"skplt.metrics.plot_confusion_matrix(y_test, y_test_pred_binary, normalize=True)\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_report = classification_report(y_test, y_test_pred_binary)\nprint(\"Classification Report:\")\nprint(class_report)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}