{"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":87323,"databundleVersionId":9934427,"sourceType":"competition"}],"dockerImageVersionId":30822,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:22:45.012636Z","iopub.execute_input":"2024-12-30T11:22:45.013110Z","iopub.status.idle":"2024-12-30T11:22:45.019111Z","shell.execute_reply.started":"2024-12-30T11:22:45.013075Z","shell.execute_reply":"2024-12-30T11:22:45.017803Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dir='/kaggle/input/cidaut-ai-fake-scene-classification-2024/Train'\ntrain_csv_dir='/kaggle/input/cidaut-ai-fake-scene-classification-2024/train.csv'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:22:47.027277Z","iopub.execute_input":"2024-12-30T11:22:47.027732Z","iopub.status.idle":"2024-12-30T11:22:47.032876Z","shell.execute_reply.started":"2024-12-30T11:22:47.027702Z","shell.execute_reply":"2024-12-30T11:22:47.031441Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_csv = pd.read_csv(train_csv_dir)\ntrain_csv['image_path'] = train_csv['image'].apply(lambda x: os.path.join(train_dir, x))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:22:49.014892Z","iopub.execute_input":"2024-12-30T11:22:49.015249Z","iopub.status.idle":"2024-12-30T11:22:49.027292Z","shell.execute_reply.started":"2024-12-30T11:22:49.015222Z","shell.execute_reply":"2024-12-30T11:22:49.025970Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_csv.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:22:51.239755Z","iopub.execute_input":"2024-12-30T11:22:51.240196Z","iopub.status.idle":"2024-12-30T11:22:51.251252Z","shell.execute_reply.started":"2024-12-30T11:22:51.240166Z","shell.execute_reply":"2024-12-30T11:22:51.250011Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:22:53.595483Z","iopub.execute_input":"2024-12-30T11:22:53.595948Z","iopub.status.idle":"2024-12-30T11:22:53.601529Z","shell.execute_reply.started":"2024-12-30T11:22:53.595916Z","shell.execute_reply":"2024-12-30T11:22:53.599948Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Function to compute histogram\ndef compute_histogram(image_path):\n    image = cv2.imread(image_path)\n    # Convert image to RGB if it's not\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    # Compute histogram for each channel\n    r_hist = cv2.calcHist([image], [0], None, [256], [0, 256]).flatten()\n    g_hist = cv2.calcHist([image], [1], None, [256], [0, 256]).flatten()\n    b_hist = cv2.calcHist([image], [2], None, [256], [0, 256]).flatten()\n    \n    # Concatenate the histograms of each channel into one feature vector\n    hist = np.concatenate([r_hist, g_hist, b_hist])\n    return hist\n\n# Apply the function to compute histograms for each image\ntrain_csv['histogram'] = train_csv['image_path'].apply(compute_histogram)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:22:55.995910Z","iopub.execute_input":"2024-12-30T11:22:55.996245Z","iopub.status.idle":"2024-12-30T11:23:04.150572Z","shell.execute_reply.started":"2024-12-30T11:22:55.996222Z","shell.execute_reply":"2024-12-30T11:23:04.149428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_csv.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:23:07.646976Z","iopub.execute_input":"2024-12-30T11:23:07.647322Z","iopub.status.idle":"2024-12-30T11:23:07.663512Z","shell.execute_reply.started":"2024-12-30T11:23:07.647297Z","shell.execute_reply":"2024-12-30T11:23:07.662417Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Prepare features and labels\nX = np.array(train_csv['histogram'].tolist())\ny = train_csv['label'].values  # Assuming 'label' column exists for classification\n\n# Split the data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:23:10.312711Z","iopub.execute_input":"2024-12-30T11:23:10.313141Z","iopub.status.idle":"2024-12-30T11:23:10.322621Z","shell.execute_reply.started":"2024-12-30T11:23:10.313110Z","shell.execute_reply":"2024-12-30T11:23:10.321464Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train a classifier (e.g., Random Forest)\nclf = RandomForestClassifier(n_estimators=100, random_state=42)\nclf.fit(X_train, y_train)\n\n# Evaluate the model\naccuracy = clf.score(X_val, y_val)\nprint(f'Accuracy: {accuracy}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:03:12.538002Z","iopub.execute_input":"2024-12-30T11:03:12.538406Z","iopub.status.idle":"2024-12-30T11:03:13.754233Z","shell.execute_reply.started":"2024-12-30T11:03:12.538370Z","shell.execute_reply":"2024-12-30T11:03:13.753055Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\n\nclf = LogisticRegression(random_state=42)\nclf.fit(X_train, y_train)\n\naccuracy = clf.score(X_val, y_val)\nprint(f'Accuracy: {accuracy}')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:15:53.580781Z","iopub.execute_input":"2024-12-30T11:15:53.581202Z","iopub.status.idle":"2024-12-30T11:15:53.696191Z","shell.execute_reply.started":"2024-12-30T11:15:53.581171Z","shell.execute_reply":"2024-12-30T11:15:53.693129Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.svm import SVC\n\nclf = SVC(kernel='linear', random_state=42)\nclf.fit(X_train, y_train)\n\naccuracy = clf.score(X_val, y_val)\nprint(f'Accuracy: {accuracy}')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:16:02.677747Z","iopub.execute_input":"2024-12-30T11:16:02.678147Z","iopub.status.idle":"2024-12-30T11:16:02.898998Z","shell.execute_reply.started":"2024-12-30T11:16:02.678118Z","shell.execute_reply":"2024-12-30T11:16:02.897910Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\n\nclf = KNeighborsClassifier(n_neighbors=5)\nclf.fit(X_train, y_train)\n\naccuracy = clf.score(X_val, y_val)\nprint(f'Accuracy: {accuracy}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:16:55.758430Z","iopub.execute_input":"2024-12-30T11:16:55.759282Z","iopub.status.idle":"2024-12-30T11:16:55.841472Z","shell.execute_reply.started":"2024-12-30T11:16:55.759245Z","shell.execute_reply":"2024-12-30T11:16:55.840264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier\n\nclf = DecisionTreeClassifier(random_state=42)\nclf.fit(X_train, y_train)\n\naccuracy = clf.score(X_val, y_val)\nprint(f'Accuracy: {accuracy}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:17:06.219212Z","iopub.execute_input":"2024-12-30T11:17:06.219716Z","iopub.status.idle":"2024-12-30T11:17:06.647573Z","shell.execute_reply.started":"2024-12-30T11:17:06.219683Z","shell.execute_reply":"2024-12-30T11:17:06.646192Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.naive_bayes import GaussianNB\n\nclf = GaussianNB()\nclf.fit(X_train, y_train)\n\naccuracy = clf.score(X_val, y_val)\nprint(f'Accuracy: {accuracy}')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:17:19.362830Z","iopub.execute_input":"2024-12-30T11:17:19.363218Z","iopub.status.idle":"2024-12-30T11:17:19.388195Z","shell.execute_reply.started":"2024-12-30T11:17:19.363190Z","shell.execute_reply":"2024-12-30T11:17:19.387107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import AdaBoostClassifier\n\nclf = AdaBoostClassifier(n_estimators=100, random_state=42)\nclf.fit(X_train, y_train)\n\naccuracy = clf.score(X_val, y_val)\nprint(f'Accuracy: {accuracy}')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:17:28.593074Z","iopub.execute_input":"2024-12-30T11:17:28.593469Z","iopub.status.idle":"2024-12-30T11:17:34.371991Z","shell.execute_reply.started":"2024-12-30T11:17:28.593428Z","shell.execute_reply":"2024-12-30T11:17:34.370790Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import lightgbm as lgb\n\nclf = lgb.LGBMClassifier(random_state=42)\nclf.fit(X_train, y_train)\n\naccuracy = clf.score(X_val, y_val)\nprint(f'Accuracy: {accuracy}')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:23:19.402012Z","iopub.execute_input":"2024-12-30T11:23:19.402542Z","iopub.status.idle":"2024-12-30T11:23:22.225955Z","shell.execute_reply.started":"2024-12-30T11:23:19.402498Z","shell.execute_reply":"2024-12-30T11:23:22.224300Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Now, load the test images and make predictions\ntest_dir = '/kaggle/input/cidaut-ai-fake-scene-classification-2024/Test'\ntest_images = os.listdir(test_dir)\n\n# Initialize an empty list to store the predictions\npredictions = []\n\n\n# Loop through the test images and make predictions\nfor img_name in test_images:\n    img_path = os.path.join(test_dir, img_name)\n    \n    # Compute the histogram for the test image\n    hist = compute_histogram(img_path)\n    \n    # Predict the label for the image using the trained model\n    label = clf.predict([hist])[0]\n    \n    # Convert label: 1 for real, 0 for fake (editable)\n    if label == 'real':\n        label = 1  # Real is 1\n    else:\n        label = 0  # Editable is 0\n    \n    # Append the result to the predictions list\n    predictions.append({'image': img_name, 'label': label})\n\n# Convert the predictions list to a DataFrame\npred_df = pd.DataFrame(predictions)\n\n# Save the predictions to a CSV file\nsubmission_file = '/kaggle/working/submission.csv'\npred_df.to_csv(submission_file, index=False)\n\nprint(f\"Predictions saved successfully to {submission_file}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:23:39.073783Z","iopub.execute_input":"2024-12-30T11:23:39.074264Z","iopub.status.idle":"2024-12-30T11:23:41.453182Z","shell.execute_reply.started":"2024-12-30T11:23:39.074230Z","shell.execute_reply":"2024-12-30T11:23:41.451976Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\n# Initialize the label encoder\nlabel_encoder = LabelEncoder()\n\n# Fit and transform the 'label' column\ntrain_csv['label'] = label_encoder.fit_transform(train_csv['label'])\n\n# Now, proceed with feature extraction and training\nX = np.array(train_csv['histogram'].tolist())\ny = train_csv['label'].values\n\n# Split the data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n\nfrom catboost import CatBoostClassifier\n\nclf = CatBoostClassifier(random_state=42, verbose=0)\nclf.fit(X_train, y_train)\n\n# Evaluate the model\naccuracy = clf.score(X_val, y_val)\nprint(f'Accuracy: {accuracy}')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T11:20:33.719253Z","iopub.execute_input":"2024-12-30T11:20:33.719672Z","iopub.status.idle":"2024-12-30T11:22:13.735874Z","shell.execute_reply.started":"2024-12-30T11:20:33.719641Z","shell.execute_reply":"2024-12-30T11:22:13.734516Z"}},"outputs":[],"execution_count":null}]}