{"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-25T09:31:16.157185Z","iopub.execute_input":"2024-04-25T09:31:16.157526Z","iopub.status.idle":"2024-04-25T09:31:49.971318Z","shell.execute_reply.started":"2024-04-25T09:31:16.157477Z","shell.execute_reply":"2024-04-25T09:31:49.970535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/deepfake-faces/metadata.csv')\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-25T09:31:49.973354Z","iopub.execute_input":"2024-04-25T09:31:49.973613Z","iopub.status.idle":"2024-04-25T09:31:50.120034Z","shell.execute_reply.started":"2024-04-25T09:31:49.973570Z","shell.execute_reply":"2024-04-25T09:31:50.119403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"data : \")\ndf","metadata":{"execution":{"iopub.status.busy":"2024-04-25T09:31:50.121367Z","iopub.execute_input":"2024-04-25T09:31:50.121599Z","iopub.status.idle":"2024-04-25T09:31:50.156413Z","shell.execute_reply.started":"2024-04-25T09:31:50.121559Z","shell.execute_reply":"2024-04-25T09:31:50.155645Z"},"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-25T09:31:50.157814Z","iopub.execute_input":"2024-04-25T09:31:50.158155Z","iopub.status.idle":"2024-04-25T09:31:50.345734Z","shell.execute_reply.started":"2024-04-25T09:31:50.158070Z","shell.execute_reply":"2024-04-25T09:31:50.345004Z"},"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-25T09:31:50.349367Z","iopub.execute_input":"2024-04-25T09:31:50.349613Z","iopub.status.idle":"2024-04-25T09:31:50.368561Z","shell.execute_reply.started":"2024-04-25T09:31:50.349574Z","shell.execute_reply":"2024-04-25T09:31:50.367912Z"},"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-25T09:31:50.371139Z","iopub.execute_input":"2024-04-25T09:31:50.371414Z","iopub.status.idle":"2024-04-25T09:31:50.379631Z","shell.execute_reply.started":"2024-04-25T09:31:50.371363Z","shell.execute_reply":"2024-04-25T09:31:50.378733Z"},"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-25T09:31:50.381034Z","iopub.execute_input":"2024-04-25T09:31:50.381386Z","iopub.status.idle":"2024-04-25T09:31:50.428035Z","shell.execute_reply.started":"2024-04-25T09:31:50.381322Z","shell.execute_reply":"2024-04-25T09:31:50.427355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.fillna(\"Not Available\")","metadata":{"execution":{"iopub.status.busy":"2024-04-25T09:31:50.429151Z","iopub.execute_input":"2024-04-25T09:31:50.429382Z","iopub.status.idle":"2024-04-25T09:31:50.468691Z","shell.execute_reply.started":"2024-04-25T09:31:50.429341Z","shell.execute_reply":"2024-04-25T09:31:50.468164Z"},"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-25T09:31:50.470376Z","iopub.execute_input":"2024-04-25T09:31:50.470671Z","iopub.status.idle":"2024-04-25T09:31:50.514441Z","shell.execute_reply.started":"2024-04-25T09:31:50.470618Z","shell.execute_reply":"2024-04-25T09:31:50.513389Z"},"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-25T09:31:50.515837Z","iopub.execute_input":"2024-04-25T09:31:50.516200Z","iopub.status.idle":"2024-04-25T09:31:52.392421Z","shell.execute_reply.started":"2024-04-25T09:31:50.516112Z","shell.execute_reply":"2024-04-25T09:31:52.391725Z"},"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-25T09:31:52.393771Z","iopub.execute_input":"2024-04-25T09:31:52.394054Z","iopub.status.idle":"2024-04-25T09:31:52.783540Z","shell.execute_reply.started":"2024-04-25T09:31:52.393995Z","shell.execute_reply":"2024-04-25T09:31:52.782229Z"},"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-25T09:31:52.785864Z","iopub.execute_input":"2024-04-25T09:31:52.786310Z","iopub.status.idle":"2024-04-25T09:31:53.193820Z","shell.execute_reply.started":"2024-04-25T09:31:52.786230Z","shell.execute_reply":"2024-04-25T09:31:53.193041Z"},"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-25T09:31:53.195281Z","iopub.execute_input":"2024-04-25T09:31:53.195805Z","iopub.status.idle":"2024-04-25T09:31:53.733321Z","shell.execute_reply.started":"2024-04-25T09:31:53.195752Z","shell.execute_reply":"2024-04-25T09:31:53.732204Z"},"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-25T09:31:53.734985Z","iopub.execute_input":"2024-04-25T09:31:53.735547Z","iopub.status.idle":"2024-04-25T09:31:54.172846Z","shell.execute_reply.started":"2024-04-25T09:31:53.735309Z","shell.execute_reply":"2024-04-25T09:31:54.171795Z"},"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-25T09:31:54.174804Z","iopub.execute_input":"2024-04-25T09:31:54.175300Z","iopub.status.idle":"2024-04-25T09:31:55.206072Z","shell.execute_reply.started":"2024-04-25T09:31:54.175094Z","shell.execute_reply":"2024-04-25T09:31:55.205241Z"},"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-25T09:31:55.207571Z","iopub.execute_input":"2024-04-25T09:31:55.208099Z","iopub.status.idle":"2024-04-25T09:31:55.696929Z","shell.execute_reply.started":"2024-04-25T09:31:55.208025Z","shell.execute_reply":"2024-04-25T09:31:55.696151Z"},"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-25T09:31:55.698314Z","iopub.execute_input":"2024-04-25T09:31:55.698623Z","iopub.status.idle":"2024-04-25T09:31:56.201144Z","shell.execute_reply.started":"2024-04-25T09:31:55.698572Z","shell.execute_reply":"2024-04-25T09:31:56.199222Z"},"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-25T09:31:56.202548Z","iopub.execute_input":"2024-04-25T09:31:56.203020Z","iopub.status.idle":"2024-04-25T09:31:57.479999Z","shell.execute_reply.started":"2024-04-25T09:31:56.202966Z","shell.execute_reply":"2024-04-25T09:31:57.479212Z"},"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-25T09:31:57.481481Z","iopub.execute_input":"2024-04-25T09:31:57.481976Z","iopub.status.idle":"2024-04-25T09:32:00.440127Z","shell.execute_reply.started":"2024-04-25T09:31:57.481921Z","shell.execute_reply":"2024-04-25T09:32:00.438985Z"},"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-25T09:32:00.441696Z","iopub.execute_input":"2024-04-25T09:32:00.442341Z","iopub.status.idle":"2024-04-25T09:32:00.515208Z","shell.execute_reply.started":"2024-04-25T09:32:00.441987Z","shell.execute_reply":"2024-04-25T09:32:00.513437Z"},"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-25T09:32:00.517809Z","iopub.execute_input":"2024-04-25T09:32:00.518596Z","iopub.status.idle":"2024-04-25T09:32:00.860451Z","shell.execute_reply.started":"2024-04-25T09:32:00.518538Z","shell.execute_reply":"2024-04-25T09:32:00.859570Z"},"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-25T09:32:00.861861Z","iopub.execute_input":"2024-04-25T09:32:00.862127Z","iopub.status.idle":"2024-04-25T09:32:02.153464Z","shell.execute_reply.started":"2024-04-25T09:32:00.862061Z","shell.execute_reply":"2024-04-25T09:32:02.152694Z"},"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-25T09:32:02.155181Z","iopub.execute_input":"2024-04-25T09:32:02.155512Z","iopub.status.idle":"2024-04-25T09:32:02.188112Z","shell.execute_reply.started":"2024-04-25T09:32:02.155458Z","shell.execute_reply":"2024-04-25T09:32:02.187236Z"},"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-25T09:32:02.189439Z","iopub.execute_input":"2024-04-25T09:32:02.189699Z","iopub.status.idle":"2024-04-25T09:32:03.679721Z","shell.execute_reply.started":"2024-04-25T09:32:02.189650Z","shell.execute_reply":"2024-04-25T09:32:03.678884Z"},"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-25T09:32:03.680924Z","iopub.execute_input":"2024-04-25T09:32:03.681156Z","iopub.status.idle":"2024-04-25T09:32:03.688033Z","shell.execute_reply.started":"2024-04-25T09:32:03.681117Z","shell.execute_reply":"2024-04-25T09:32:03.687182Z"},"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-25T09:32:03.689458Z","iopub.execute_input":"2024-04-25T09:32:03.689763Z","iopub.status.idle":"2024-04-25T09:33:36.186696Z","shell.execute_reply.started":"2024-04-25T09:32:03.689714Z","shell.execute_reply":"2024-04-25T09:33:36.185979Z"},"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-25T09:33:36.188464Z","iopub.execute_input":"2024-04-25T09:33:36.188815Z","iopub.status.idle":"2024-04-25T09:33:40.748260Z","shell.execute_reply.started":"2024-04-25T09:33:36.188749Z","shell.execute_reply":"2024-04-25T09:33:40.747349Z"},"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-25T09:33:40.750911Z","iopub.execute_input":"2024-04-25T09:33:40.751372Z","iopub.status.idle":"2024-04-25T09:33:44.924559Z","shell.execute_reply.started":"2024-04-25T09:33:40.751305Z","shell.execute_reply":"2024-04-25T09:33:44.923682Z"},"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-25T09:33:44.926173Z","iopub.execute_input":"2024-04-25T09:33:44.926412Z","iopub.status.idle":"2024-04-25T09:33:44.930949Z","shell.execute_reply.started":"2024-04-25T09:33:44.926371Z","shell.execute_reply":"2024-04-25T09:33:44.930149Z"},"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-25T09:33:44.932811Z","iopub.execute_input":"2024-04-25T09:33:44.933322Z","iopub.status.idle":"2024-04-25T09:33:44.984138Z","shell.execute_reply.started":"2024-04-25T09:33:44.933100Z","shell.execute_reply":"2024-04-25T09:33:44.983458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-25T09:33:44.985238Z","iopub.execute_input":"2024-04-25T09:33:44.985502Z","iopub.status.idle":"2024-04-25T09:33:44.994053Z","shell.execute_reply.started":"2024-04-25T09:33:44.985452Z","shell.execute_reply":"2024-04-25T09:33:44.993107Z"},"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-25T09:33:45.004145Z","iopub.execute_input":"2024-04-25T09:33:45.004399Z","iopub.status.idle":"2024-04-25T09:44:46.590211Z","shell.execute_reply.started":"2024-04-25T09:33:45.004352Z","shell.execute_reply":"2024-04-25T09:44:46.589411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-04-25T09:44:46.592288Z","iopub.execute_input":"2024-04-25T09:44:46.592575Z","iopub.status.idle":"2024-04-25T09:44:53.299553Z","shell.execute_reply.started":"2024-04-25T09:44:46.592516Z","shell.execute_reply":"2024-04-25T09:44:53.298478Z"},"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-25T09:44:53.301940Z","iopub.execute_input":"2024-04-25T09:44:53.302211Z","iopub.status.idle":"2024-04-25T09:44:53.306282Z","shell.execute_reply.started":"2024-04-25T09:44:53.302166Z","shell.execute_reply":"2024-04-25T09:44:53.305465Z"},"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-25T09:44:53.307521Z","iopub.execute_input":"2024-04-25T09:44:53.307794Z","iopub.status.idle":"2024-04-25T09:45:11.650328Z","shell.execute_reply.started":"2024-04-25T09:44:53.307741Z","shell.execute_reply":"2024-04-25T09:45:11.649598Z"},"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-25T09:45:11.651569Z","iopub.execute_input":"2024-04-25T09:45:11.651801Z","iopub.status.idle":"2024-04-25T09:45:11.658353Z","shell.execute_reply.started":"2024-04-25T09:45:11.651762Z","shell.execute_reply":"2024-04-25T09:45:11.657626Z"},"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-25T09:45:11.659499Z","iopub.execute_input":"2024-04-25T09:45:11.659711Z","iopub.status.idle":"2024-04-25T09:45:11.671191Z","shell.execute_reply.started":"2024-04-25T09:45:11.659675Z","shell.execute_reply":"2024-04-25T09:45:11.670506Z"},"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-25T09:45:11.672543Z","iopub.execute_input":"2024-04-25T09:45:11.672805Z","iopub.status.idle":"2024-04-25T09:45:11.692767Z","shell.execute_reply.started":"2024-04-25T09:45:11.672756Z","shell.execute_reply":"2024-04-25T09:45:11.692066Z"},"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-25T09:45:11.693814Z","iopub.execute_input":"2024-04-25T09:45:11.694019Z","iopub.status.idle":"2024-04-25T09:45:11.706519Z","shell.execute_reply.started":"2024-04-25T09:45:11.693983Z","shell.execute_reply":"2024-04-25T09:45:11.705616Z"},"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-25T09:45:11.707635Z","iopub.execute_input":"2024-04-25T09:45:11.707841Z","iopub.status.idle":"2024-04-25T09:45:12.224200Z","shell.execute_reply.started":"2024-04-25T09:45:11.707805Z","shell.execute_reply":"2024-04-25T09:45:12.223048Z"},"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-25T09:45:12.226421Z","iopub.execute_input":"2024-04-25T09:45:12.226929Z","iopub.status.idle":"2024-04-25T09:45:12.251977Z","shell.execute_reply.started":"2024-04-25T09:45:12.226775Z","shell.execute_reply":"2024-04-25T09:45:12.250604Z"},"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-25T09:45:12.254099Z","iopub.execute_input":"2024-04-25T09:45:12.254497Z","iopub.status.idle":"2024-04-25T09:45:12.559222Z","shell.execute_reply.started":"2024-04-25T09:45:12.254428Z","shell.execute_reply":"2024-04-25T09:45:12.558104Z"},"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-25T09:45:12.561195Z","iopub.execute_input":"2024-04-25T09:45:12.561831Z","iopub.status.idle":"2024-04-25T09:45:12.954712Z","shell.execute_reply.started":"2024-04-25T09:45:12.561760Z","shell.execute_reply":"2024-04-25T09:45:12.953578Z"},"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-25T09:45:12.956624Z","iopub.execute_input":"2024-04-25T09:45:12.957283Z","iopub.status.idle":"2024-04-25T09:45:18.243322Z","shell.execute_reply.started":"2024-04-25T09:45:12.957210Z","shell.execute_reply":"2024-04-25T09:45:18.242475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_resnet50.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-25T09:45:18.244513Z","iopub.execute_input":"2024-04-25T09:45:18.244771Z","iopub.status.idle":"2024-04-25T09:45:18.276298Z","shell.execute_reply.started":"2024-04-25T09:45:18.244729Z","shell.execute_reply":"2024-04-25T09:45:18.274990Z"},"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-25T09:45:18.277964Z","iopub.execute_input":"2024-04-25T09:45:18.278224Z","iopub.status.idle":"2024-04-25T09:45:18.364026Z","shell.execute_reply.started":"2024-04-25T09:45:18.278180Z","shell.execute_reply":"2024-04-25T09:45:18.363271Z"},"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-25T09:45:18.365245Z","iopub.execute_input":"2024-04-25T09:45:18.365496Z","iopub.status.idle":"2024-04-25T09:54:43.417983Z","shell.execute_reply.started":"2024-04-25T09:45:18.365448Z","shell.execute_reply":"2024-04-25T09:54:43.416997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model_resnet50.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-04-25T09:54:43.419505Z","iopub.execute_input":"2024-04-25T09:54:43.419773Z","iopub.status.idle":"2024-04-25T09:54:57.785729Z","shell.execute_reply.started":"2024-04-25T09:54:43.419722Z","shell.execute_reply":"2024-04-25T09:54:57.784984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_pred_binary = (y_pred > 0.5).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-04-25T09:54:57.787148Z","iopub.execute_input":"2024-04-25T09:54:57.787385Z","iopub.status.idle":"2024-04-25T09:54:57.791166Z","shell.execute_reply.started":"2024-04-25T09:54:57.787345Z","shell.execute_reply":"2024-04-25T09:54:57.790472Z"},"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":{"execution":{"iopub.status.busy":"2024-04-25T09:54:57.792499Z","iopub.execute_input":"2024-04-25T09:54:57.792769Z","iopub.status.idle":"2024-04-25T09:55:34.644193Z","shell.execute_reply.started":"2024-04-25T09:54:57.792708Z","shell.execute_reply":"2024-04-25T09:55:34.643228Z"},"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":{"execution":{"iopub.status.busy":"2024-04-25T09:55:34.645641Z","iopub.execute_input":"2024-04-25T09:55:34.645892Z","iopub.status.idle":"2024-04-25T09:55:34.652445Z","shell.execute_reply.started":"2024-04-25T09:55:34.645851Z","shell.execute_reply":"2024-04-25T09:55:34.651456Z"},"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-25T09:55:34.653850Z","iopub.execute_input":"2024-04-25T09:55:34.654171Z","iopub.status.idle":"2024-04-25T09:55:34.662867Z","shell.execute_reply.started":"2024-04-25T09:55:34.654111Z","shell.execute_reply":"2024-04-25T09:55:34.662149Z"},"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":{"execution":{"iopub.status.busy":"2024-04-25T09:55:34.664290Z","iopub.execute_input":"2024-04-25T09:55:34.664624Z","iopub.status.idle":"2024-04-25T09:55:34.681788Z","shell.execute_reply.started":"2024-04-25T09:55:34.664534Z","shell.execute_reply":"2024-04-25T09:55:34.680933Z"},"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-25T09:55:34.683235Z","iopub.execute_input":"2024-04-25T09:55:34.683564Z","iopub.status.idle":"2024-04-25T09:55:34.698593Z","shell.execute_reply.started":"2024-04-25T09:55:34.683504Z","shell.execute_reply":"2024-04-25T09:55:34.697810Z"},"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":{"execution":{"iopub.status.busy":"2024-04-25T09:55:34.699651Z","iopub.execute_input":"2024-04-25T09:55:34.700017Z","iopub.status.idle":"2024-04-25T09:55:35.098517Z","shell.execute_reply.started":"2024-04-25T09:55:34.699829Z","shell.execute_reply":"2024-04-25T09:55:35.096971Z"},"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-25T09:55:35.102350Z","iopub.execute_input":"2024-04-25T09:55:35.102758Z","iopub.status.idle":"2024-04-25T09:55:35.128731Z","shell.execute_reply.started":"2024-04-25T09:55:35.102687Z","shell.execute_reply":"2024-04-25T09:55:35.126386Z"},"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-25T09:55:35.130673Z","iopub.execute_input":"2024-04-25T09:55:35.131042Z","iopub.status.idle":"2024-04-25T09:55:35.455683Z","shell.execute_reply.started":"2024-04-25T09:55:35.130982Z","shell.execute_reply":"2024-04-25T09:55:35.454260Z"},"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-25T09:55:35.457856Z","iopub.execute_input":"2024-04-25T09:55:35.458475Z","iopub.status.idle":"2024-04-25T09:55:35.806568Z","shell.execute_reply.started":"2024-04-25T09:55:35.458240Z","shell.execute_reply":"2024-04-25T09:55:35.805383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_resnet50.save(\"video.h5\")","metadata":{"execution":{"iopub.status.busy":"2024-04-25T09:56:53.912470Z","iopub.execute_input":"2024-04-25T09:56:53.912886Z","iopub.status.idle":"2024-04-25T09:56:54.572143Z","shell.execute_reply.started":"2024-04-25T09:56:53.912820Z","shell.execute_reply":"2024-04-25T09:56:54.571236Z"},"trusted":true},"execution_count":null,"outputs":[]}]}