{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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":"2023-10-06T13:21:51.726283Z","iopub.execute_input":"2023-10-06T13:21:51.726733Z","iopub.status.idle":"2023-10-06T13:33:03.878707Z","shell.execute_reply.started":"2023-10-06T13:21:51.726685Z","shell.execute_reply":"2023-10-06T13:33:03.8764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten\n","metadata":{"execution":{"iopub.status.busy":"2023-10-06T15:32:24.812494Z","iopub.execute_input":"2023-10-06T15:32:24.812848Z","iopub.status.idle":"2023-10-06T15:32:24.817655Z","shell.execute_reply.started":"2023-10-06T15:32:24.81282Z","shell.execute_reply":"2023-10-06T15:32:24.816601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# LOAD DATASET","metadata":{"execution":{"iopub.status.busy":"2023-10-06T15:33:36.507193Z","iopub.execute_input":"2023-10-06T15:33:36.508347Z","iopub.status.idle":"2023-10-06T15:33:36.51231Z","shell.execute_reply.started":"2023-10-06T15:33:36.508314Z","shell.execute_reply":"2023-10-06T15:33:36.511367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport cv2\nimport random\nimport matplotlib.pyplot as plt\nimport pandas as pd  # Add pandas for creating the ground truth table\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.utils import shuffle\n\n# Define constraints for image dimensions\nIMAGE_WIDTH = 128\nIMAGE_HEIGHT = 128\nIMAGE_CHANNELS = 3\n\n# Define the base paths for 'Cover' and 'UERD' folders\nCOVER_PATH = '/kaggle/input/alaska2-image-steganalysis/Cover'\nUERD_PATH = '/kaggle/input/alaska2-image-steganalysis/UERD'\n\n# Initialize lists to store image data and labels\nimages = []\nlabels = []\n\n# Create an empty DataFrame for the ground truth table\nground_truth = pd.DataFrame(columns=[\"Image_Path\", \"Label\"])\n\n# Set random seed for reproducibility\nnp.random.seed(57)\n\n# Load and preprocess images from 'Cover' folder\ncover_image_filenames = os.listdir(COVER_PATH)\nselected_cover_images = np.random.choice(cover_image_filenames, 1000, replace=False)\n\nfor image_filename in selected_cover_images:\n    image_path = os.path.join(COVER_PATH, image_filename)\n    image = cv2.imread(image_path)\n    image = cv2.resize(image, (IMAGE_WIDTH, IMAGE_HEIGHT))\n    images.append(image)\n    labels.append(0)  # Label 0 for 'normal image'\n    ground_truth = pd.concat([ground_truth, pd.DataFrame({\"Image_Path\": [image_path], \"Label\": [\"normal\"]})], ignore_index=True)  # Update ground truth table\n\n# Load and preprocess images from 'UERD' folder\nuerd_image_filenames = os.listdir(UERD_PATH)\nselected_uerd_images = np.random.choice(uerd_image_filenames, 1000, replace=False)\n\nfor image_filename in selected_uerd_images:\n    image_path = os.path.join(UERD_PATH, image_filename)\n    image = cv2.imread(image_path)\n    image = cv2.resize(image, (IMAGE_WIDTH, IMAGE_HEIGHT))\n    images.append(image)\n    labels.append(1)  # Label 1 for 'stegano image'\n    ground_truth = pd.concat([ground_truth, pd.DataFrame({\"Image_Path\": [image_path], \"Label\": [\"stegano\"]})], ignore_index=True)  # Update ground truth table\n\n# Combine data and labels\ndata = np.array(images)\nlabels = np.array(labels)\n\n# Shuffle the data\ndata, labels = shuffle(data, labels, random_state=42)\n\n# Split data into training and testing sets (80% train, 20% test)\nX_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.2, random_state=42)\n\n# Divide the values of X_train and X_test data by 255.0 for scaling\nX_train_scaled = X_train / 255.0\nX_test_scaled = X_test / 255.0\n\n# Save the ground truth table to a CSV file\nground_truth.to_csv(\"ground_truth.csv\", index=False)\n\n# Verify data shape\nprint(\"Shape of X_train:\", X_train.shape)\nprint(\"Shape of y_train:\", y_train.shape)\nprint(\"Shape of X_test:\", X_test.shape)\nprint(\"Shape of y_test:\", y_test.shape)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-06T16:54:42.943914Z","iopub.execute_input":"2023-10-06T16:54:42.944769Z","iopub.status.idle":"2023-10-06T16:55:01.557037Z","shell.execute_reply.started":"2023-10-06T16:54:42.944734Z","shell.execute_reply":"2023-10-06T16:55:01.55604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random  # Import the random module\nimport matplotlib.pyplot as plt\n\n# Generate a random index\nidx = random.randint(0, len(X_train) - 1)\n\n# Display the image at the randomly selected index\nplt.imshow(X_train[idx])\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-06T15:47:58.462043Z","iopub.execute_input":"2023-10-06T15:47:58.462399Z","iopub.status.idle":"2023-10-06T15:47:58.731094Z","shell.execute_reply.started":"2023-10-06T15:47:58.462366Z","shell.execute_reply":"2023-10-06T15:47:58.730011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#MODEL\n","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization\nfrom tensorflow.keras.optimizers import RMSprop\n\nmodel = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 3)),\n    MaxPooling2D((2, 2)),\n    BatchNormalization(),\n\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    BatchNormalization(),\n\n    Conv2D(128, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    BatchNormalization(),\n\n    Flatten(),\n    Dense(256, activation='relu'),\n    Dropout(0.5),\n    Dense(1, activation='sigmoid')\n])\n\n# Using RMSprop optimizer with a lower learning rate\noptimizer = RMSprop(learning_rate=0.0001)\nmodel.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])\n\nmodel.fit(X_train, y_train, epochs=20, batch_size=64)  # Increase the number of epochs\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-06T16:13:57.54387Z","iopub.execute_input":"2023-10-06T16:13:57.54431Z","iopub.status.idle":"2023-10-06T16:23:20.763176Z","shell.execute_reply.started":"2023-10-06T16:13:57.544277Z","shell.execute_reply":"2023-10-06T16:23:20.761974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(X_test , y_test)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T16:23:34.851181Z","iopub.execute_input":"2023-10-06T16:23:34.851563Z","iopub.status.idle":"2023-10-06T16:23:36.865465Z","shell.execute_reply.started":"2023-10-06T16:23:34.851532Z","shell.execute_reply":"2023-10-06T16:23:36.86449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport cv2\nimport random\nimport matplotlib.pyplot as plt\nimport pandas as pd  # Import pandas for reading the ground truth table\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.utils import shuffle\n\n# ... (previous code for loading and preprocessing data)\n\n# Load the ground truth table from the CSV file\nground_truth = pd.read_csv(\"ground_truth.csv\")\n\n# ... (previous code for training and testing the model)\n\n# Assuming you have ground truth labels for your test data\nidx2 = np.random.randint(0, len(y_test))\nground_truth_label = y_test[idx2]  # Get the ground truth label for the randomly selected image\n\n# Set a threshold for classification\nthreshold = 0.75\n\n# Classify based on the threshold\nif y_pred >= threshold:\n    prediction = \"stegano\"\nelse:\n    prediction = \"normal\"\n\n# Display the image\nplt.imshow(X_test[idx2])\nplt.title(f\"Predicted: {prediction}, Ground Truth: {ground_truth_label}\")\nplt.show()\n\n# Compare the prediction with the ground truth\nif prediction == \"stegano\" and ground_truth_label == 1:\n    print(\"Model correctly predicted stegano.\")\nelif prediction == \"normal\" and ground_truth_label == 0:\n    print(\"Model correctly predicted normal.\")\nelse:\n    print(\"Model prediction does not match the ground truth.\")\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-06T16:58:20.555759Z","iopub.execute_input":"2023-10-06T16:58:20.556746Z","iopub.status.idle":"2023-10-06T16:58:20.914462Z","shell.execute_reply.started":"2023-10-06T16:58:20.556712Z","shell.execute_reply":"2023-10-06T16:58:20.913689Z"},"trusted":true},"execution_count":null,"outputs":[]}]}