{"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":"!pip uninstall scipy -y","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:40:36.194484Z","iopub.execute_input":"2023-05-22T14:40:36.194781Z","iopub.status.idle":"2023-05-22T14:40:45.341909Z","shell.execute_reply.started":"2023-05-22T14:40:36.194755Z","shell.execute_reply":"2023-05-22T14:40:45.340786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install scipy==1.10.1","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:42:55.527547Z","iopub.execute_input":"2023-05-22T14:42:55.528445Z","iopub.status.idle":"2023-05-22T14:43:18.933283Z","shell.execute_reply.started":"2023-05-22T14:42:55.528400Z","shell.execute_reply":"2023-05-22T14:43:18.932159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport gc\nimport matplotlib\nimport matplotlib.pyplot as plt\nfrom sklearn import metrics\n\nimport matplotlib.image as mpimg\nfrom tqdm.notebook import tqdm\nimport random\nimport joblib\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import roc_curve, auc\nfrom time import time\nfrom collections import Counter\n\nfrom PIL import Image\nfrom random import shuffle\n\nimport tensorflow as tf\nfrom tensorflow.keras.metrics import AUC\n\nimport keras\nimport keras.backend as K\nimport keras.layers as L\nfrom keras import Model\nfrom keras.utils import plot_model\nfrom keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:54:10.887085Z","iopub.execute_input":"2023-05-24T13:54:10.887451Z","iopub.status.idle":"2023-05-24T13:54:18.922811Z","shell.execute_reply.started":"2023-05-24T13:54:10.887420Z","shell.execute_reply":"2023-05-24T13:54:18.921806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport PIL\nimport requests\nimport os\nimport random\nimport pickle\nimport matplotlib.pyplot as plt\nimport tensorflow as jf\nfrom tensorflow.keras import layers\nfrom keras.models import Sequential\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.python.keras import backend as K\nimport pathlib\n\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom skimage.filters import gabor\nfrom skimage.filters import gabor_kernel\n# importing Dependencies\n\nimport os\nimport glob\nimport pandas as pd \n\n\nimport tensorflow as tf\nfrom keras.models import Sequential\nfrom tensorflow.keras import layers\nimport tensorflow.keras as keras\n\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.layers import Dense, Input, Dropout, concatenate,Conv2D,MaxPooling2D, Flatten,Dense,BatchNormalization\n\nfrom tensorflow.keras.models import Model\n# from tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.utils import plot_model\n# from tensorflow.keras.callbacks import ReduceLROnPlateau\n\n#from skimage.feature import graycomatrix, graycoprops\nfrom sklearn.metrics import confusion_matrix, classification_report\nfrom sklearn.model_selection import train_test_split\n# from tensorflow.keras.applications import ResNet50\n# from tensorflow.keras.applications import NASNetLarge \n# import numpy as np\nfrom PIL import Image\nimport cv2\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:54:18.924931Z","iopub.execute_input":"2023-05-24T13:54:18.925530Z","iopub.status.idle":"2023-05-24T13:54:19.175511Z","shell.execute_reply.started":"2023-05-24T13:54:18.925501Z","shell.execute_reply":"2023-05-24T13:54:19.174519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##### import os\nimport cv2\nimport numpy as np\nfrom multiprocessing import Pool\n\nSIZE = 256\nBATCH_SIZE = 32\nTARGET_SIZE = (SIZE, SIZE)\nLIMIT_PER_FOLDER = 1000  # Maximum number of files per folder\ndir_path = '/kaggle/input/alaskas-v2-binary/ALASKA/ALASKA/Train'\n\ndef load_image(img):\n    try:\n        img_path = img[0]\n        folder = img[1]\n        \n        image = cv2.imread(img_path, 3)\n        image = cv2.resize(image, TARGET_SIZE)\n        image = np.array(image)\n        \n        return image, folder\n    except:\n        return None\n\nimages = []\nlabels = []  \n\nimage_paths = []\nfor folder in os.listdir(dir_path):\n    if folder == \"Test\" or folder == \"sample_submission.csv\":\n        continue\n    folder_path = os.path.join(dir_path, folder)\n    file_counter = 0  # Counter to keep track of the number of files per folder\n    for img in os.listdir(folder_path):\n        if file_counter >= LIMIT_PER_FOLDER:\n            break\n        \n        img_path = os.path.join(folder_path, img)\n        image_paths.append((img_path, folder))\n        file_counter += 1\n\npool = Pool(processes=4)\nresults = pool.map(load_image, image_paths)\npool.close()\npool.join()\n\nresults = [result for result in results if result is not None]\nimages, labels = zip(*results)\nimages = np.array(images)\nlabels = np.array(labels)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:54:19.180258Z","iopub.execute_input":"2023-05-24T13:54:19.182741Z","iopub.status.idle":"2023-05-24T13:54:31.805360Z","shell.execute_reply.started":"2023-05-24T13:54:19.182705Z","shell.execute_reply":"2023-05-24T13:54:31.804130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import StratifiedShuffleSplit\n\n\n# Split the data into train, test, and validation sets using StratifiedShuffleSplit\nsss = StratifiedShuffleSplit(n_splits=5, test_size=0.2, random_state=42)\n\nfor train_index, test_index in sss.split(images, labels):\n    X_train, X_test = images[train_index], images[test_index]\n    y_train, y_test = labels[train_index], labels[test_index]\n\n# Split the train set into train and validation sets using StratifiedShuffleSplit\nsss = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42)\n\nfor train_index, val_index in sss.split(X_train, y_train):\n    X_train, X_val = X_train[train_index], X_train[val_index]\n    y_train, y_val = y_train[train_index], y_train[val_index]\n","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:11.634328Z","iopub.execute_input":"2023-05-24T13:55:11.634844Z","iopub.status.idle":"2023-05-24T13:55:12.382331Z","shell.execute_reply.started":"2023-05-24T13:55:11.634801Z","shell.execute_reply":"2023-05-24T13:55:12.381289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(y_val))\nprint(len(y_train))\nprint(len(y_test))","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:13.387849Z","iopub.execute_input":"2023-05-24T13:55:13.388498Z","iopub.status.idle":"2023-05-24T13:55:13.393139Z","shell.execute_reply.started":"2023-05-24T13:55:13.388465Z","shell.execute_reply":"2023-05-24T13:55:13.392239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"# save on ram \ndel images \ndel labels","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:26.915622Z","iopub.execute_input":"2023-05-24T13:55:26.915970Z","iopub.status.idle":"2023-05-24T13:55:26.922682Z","shell.execute_reply.started":"2023-05-24T13:55:26.915942Z","shell.execute_reply":"2023-05-24T13:55:26.919271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# DCT Features for AEs \nThis part extracts the DCT features using Multi threading ","metadata":{}},{"cell_type":"code","source":"import cv2\nimport numpy as np\nfrom concurrent.futures import ThreadPoolExecutor\n\nDCT_Train = []\nDCT_Test = []\nDCT_Val = []\n\ndef process_image(img):\n    image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # Convert image to RGB color space\n\n    # Convert image to YCbCr color space\n    ycbcr_image = cv2.cvtColor(image, cv2.COLOR_RGB2YCrCb)\n\n    # Split the image into individual channels\n    channels = cv2.split(ycbcr_image)\n\n    # Apply DCT to each channel\n    dct_coeffs = []\n    for channel in channels:\n        dct_coeffs.append(cv2.dct(np.float32(channel)))\n\n    # Reshape DCT coefficients as input channels\n    dct_channels = np.concatenate(dct_coeffs, axis=-1)\n    dct_channels = dct_channels.reshape(-1, 32, 32)\n    return dct_channels\n\ndef featureAEDCT(img, folder):\n    dct_channels = process_image(img)\n    if folder == \"Train\":\n        DCT_Train.append(dct_channels)\n    elif folder == \"Test\":\n        DCT_Test.append(dct_channels)\n    else:\n        DCT_Val.append(dct_channels)\n\n# Load your X_train, X_test, and X_val data here\n\n# Process images using multiple threads\nwith ThreadPoolExecutor(max_workers=4) as executor:\n    # Process train images\n    train_futures = [executor.submit(featureAEDCT, img, \"Train\") for img in X_train]\n\n    # Process test images\n    test_futures = [executor.submit(featureAEDCT, img, \"Test\") for img in X_test]\n\n    # Process validation images\n    val_futures = [executor.submit(featureAEDCT, img, \"Val\") for img in X_val]\n\n# Wait for all threads to complete\nfor future in train_futures + test_futures + val_futures:\n    future.result()\n","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:28.498636Z","iopub.execute_input":"2023-05-24T13:55:28.498975Z","iopub.status.idle":"2023-05-24T13:55:31.100958Z","shell.execute_reply.started":"2023-05-24T13:55:28.498949Z","shell.execute_reply":"2023-05-24T13:55:31.099972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DCT_Train = np.array(DCT_Train)\nDCT_Val = np.array(DCT_Val)\nDCT_Test = np.array(DCT_Test)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:31.102829Z","iopub.execute_input":"2023-05-24T13:55:31.103185Z","iopub.status.idle":"2023-05-24T13:55:31.614211Z","shell.execute_reply.started":"2023-05-24T13:55:31.103151Z","shell.execute_reply":"2023-05-24T13:55:31.613245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Extraction model  : NOT USED \nI dont really think it helps here, though entropy might help I do not think that having more Auto Encoders will be a good idea, unless of course we push modeling to the extreme. Entropy might not help in RGB or the 6 channels but maybe in the DCT frequecny domain, alas model building is 15% so that might be for later exploration. **","metadata":{}},{"cell_type":"code","source":"import skimage\nimport pywt\nfrom scipy import ndimage\nimport numpy as np\nfrom skimage.measure import shannon_entropy\nfrom scipy.stats import entropy\nfrom skimage import io, color, feature, measure\n\n# Set the Gabor filter parameters\n\n\ndef feature_extractor(images):\n    image_dataset = pd.DataFrame()\n    for img in images:\n\n#         # hasten the proccess at the lack of data\n#         count += 1\n#         print(count, end=\"\\r\", flush=True)\n#         if folder == \"Train\" and count == 1000:\n#             print(\"Break_Train\")\n#             break\n#         if folder == \"Test\" and count == 100:\n#             print(\"Break_Test\")\n#             break\n#         if folder == \"Val\" and count == 100:\n#             print(\"Break_Val\")\n#             break\n\n        # GLCM Features Extraction\n        dists = [1,2]\n        angles = [0]\n\n        df = pd.DataFrame()\n\n        # r,g,b, rg, gb, br\n        # Extract GLCM features for R channel\n        glcm_r = skimage.feature.graycomatrix(\n            img[:, :, 0], dists, angles, 256, symmetric=True, normed=True\n        )\n        # Extract GLCM features for G channel\n        glcm_g = skimage.feature.graycomatrix(\n            img[:, :, 1], dists, angles, 256, symmetric=True, normed=True\n        )\n        # Extract GLCM features for B channel\n        glcm_b = skimage.feature.graycomatrix(\n            img[:, :, 2], dists, angles, 256, symmetric=True, normed=True\n        )\n        # Extract GLCM features for RG channel\n        img_rg = np.zeros((img.shape[0], img.shape[1], 2))\n        img_rg[:, :, 0] = img[:, :, 0]\n        img_rg[:, :, 1] = img[:, :, 1]\n        img_rg_8bit = img_rg[:, :, 1].astype(np.uint8)\n        glcm_rg = skimage.feature.graycomatrix(\n            img_rg_8bit, dists, angles, 256, symmetric=True, normed=True\n        )\n        # Extract GLCM features for RB channel\n        img_rb = np.zeros((img.shape[0], img.shape[1], 2))\n        img_rb[:, :, 0] = img[:, :, 0]\n        img_rb[:, :, 1] = img[:, :, 2]\n        img_rb_8bit = img_rb[:, :, 1].astype(np.uint8)\n        glcm_rb = skimage.feature.graycomatrix(\n            img_rb_8bit, dists, angles, 256, symmetric=True, normed=True\n        )\n        # Extract GLCM features for GB channel\n        img_gb = np.zeros((img.shape[0], img.shape[1], 2))\n        img_gb[:, :, 0] = img[:, :, 1]\n        img_gb[:, :, 1] = img[:, :, 2]\n        img_gb_8bit = img_gb[:, :, 1].astype(np.uint8)\n        glcm_gb = skimage.feature.graycomatrix(\n            img_gb_8bit, dists, angles, 256, symmetric=True, normed=True\n        )\n        \n\n        # Extract the contrast, correlation, energy, and homogeneity properties\n        for i, glcm in enumerate([glcm_r, glcm_g, glcm_b, glcm_rg, glcm_rb, glcm_gb]):\n            df[f\"contrast_{i}\"] = skimage.feature.graycoprops(glcm, \"contrast\")[0]\n            df[f\"correlation_{i}\"] = skimage.feature.graycoprops(glcm, \"correlation\")[0]\n            df[f\"energy_{i}\"] = skimage.feature.graycoprops(glcm, \"energy\")[0]\n            df[f\"homogeneity_{i}\"] = skimage.feature.graycoprops(glcm, \"homogeneity\")[0]\n            df[f\"dissimilarity_{i}\"] = skimage.feature.graycoprops(glcm, \"dissimilarity\")[0]\n\n        # Entropy or the amount of disorder in an image may tell us if the image has been tampered \n        df[\"Entropy_glcm_r\"] = shannon_entropy(img[:, :, 0])\n        df[\"Entropy_glcm_g\"] = shannon_entropy(img[:, :, 1])\n        df[\"Entropy_glcm_b\"] = shannon_entropy(img[:, :, 2])\n        df[\"Entropy_glcm_rg\"] = shannon_entropy(img_rg_8bit)\n        df[\"Entropy_glcm_gb\"] = shannon_entropy(img_gb_8bit)\n        df[\"Entropy_glcm_rb\"] = shannon_entropy(img_rb_8bit)\n        df[\"Entropy_glcm_rgb\"] = shannon_entropy(img)\n        \n        # Convert RGB image to YCbCr\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)\n        # DCT & DWT\n        channels = cv2.split(img)\n        \n        # Extract DWT and DCT from channels\n        dwt_coeffs = []\n        dct_coeffs = []\n\n        for channel in channels:\n            # Perform DWT on the channel\n            dwt_coeffs.append(pywt.dwt2(channel, 'haar'))  # You can choose the desired wavelet here\n\n            # Perform DCT on the channel\n            dct = cv2.dct(np.float32(channel))\n            dct_coeffs.append(dct.flatten())\n\n        # Accessing DWT and DCT coefficients\n        # Example: Accessing DWT coefficients of the first channel\n        cA, (cH, cV, cD) = dwt_coeffs[0]\n       #print(cA, (cH, cV, cD))\n        # Example: Accessing DCT coefficients of the second channel\n        dct_coefficients = dct_coeffs[1]\n        print(dct_coefficients)\n        break\n\n\n        # Compute DCT and extract statistical features for each channel\n        features = []\n        for channel in channels:\n            # Compute the DCT\n            dct = cv2.dct(np.float32(channel))\n\n            # Extract statistical features from the DCT\n            mean_dct = np.mean(dct, axis=(0, 1))\n            var_dct = np.var(dct, axis=(0, 1))\n            # skew_dct = np.skew(dct, axis=(0,1))\n            # kurt_dct = np.kurtosis(dct, axis=(0,1))\n\n            # Append the features for the current channel to the overall feature list\n            features += [mean_dct, var_dct]\n\n        # Compute the DCT of the RG, GB, and BR channels\n        RG = np.concatenate((channels[0], channels[1]), axis=1)\n        GB = np.concatenate((channels[1], channels[2]), axis=1)\n        BR = np.concatenate((channels[2], channels[0]), axis=1)\n        dct_rg = cv2.dct(np.float32(RG))\n        dct_gb = cv2.dct(np.float32(GB))\n        dct_br = cv2.dct(np.float32(BR))\n\n        # Extract statistical features from the DCT of the RG, GB, and BR channels\n        mean_dct_rg = np.mean(dct_rg, axis=(0, 1))\n        var_dct_rg = np.var(dct_rg, axis=(0, 1))\n        # skew_dct_rg = np.skew(dct_rg, axis=(0,1))\n        # kurt_dct_rg = np.kurtosis(dct_rg, axis=(0,1))\n\n        mean_dct_gb = np.mean(dct_gb, axis=(0, 1))\n        var_dct_gb = np.var(dct_gb, axis=(0, 1))\n        # skew_dct_gb = np.skew(dct_gb, axis=(0,1))\n        # kurt_dct_gb = np.kurtosis(dct_gb, axis=(0,1))\n\n        mean_dct_br = np.mean(dct_br, axis=(0, 1))\n        var_dct_br = np.var(dct_br, axis=(0, 1))\n        # skew_dct_br = np.skew(dct_br, axis=(0,1))\n        # kurt_dct_br = np.kurtosis(dct_br, axis=(0,1))\n\n        # Append the features for the RG, GB, and BR channels to the overall feature list\n        features += [\n            mean_dct_rg,\n            var_dct_rg,\n            mean_dct_gb,\n            var_dct_gb,\n            mean_dct_br,\n            var_dct_br,\n        ]\n\n        for i in range(len(features)):\n            df[f\"DCT_{i}\"] = features[i].astype(\"float64\")\n\n        R, G, B = cv2.split(img)\n        channels = [R, G, B, R + G, G + B, B + R]\n        featuresDFT = []\n        for channel in channels:\n            dft = cv2.dft(np.float32(channel), flags=cv2.DFT_COMPLEX_OUTPUT)\n            mag = 20 * np.log(cv2.magnitude(dft[:, :, 0], dft[:, :, 1]))\n            mean_mag = np.mean(mag)\n            var_mag = np.var(mag)\n            featuresDFT.extend([mean_mag, var_mag])\n\n        for i in range(len(featuresDFT)):\n            df[f\"DFT_{i}\"] = featuresDFT[i].astype('float64')\n            \n            \n            \n#         GABOR FILTERS, Very CPU INTENSIVE \n#         Run if have very good single core performance , or run if you change the code to use all cores instead of single core\n#                 # Set up the Gabor filter parameters\n#                 sigmas = (1, 3)\n#                 thetas = np.deg2rad([0, 45, 90, 135])\n#                 frequencies = (0.05, 0.25)\n#                 psis = np.pi/2\n\n#                 # Create a list of Gabor filter kernels for each color channel\n#                 kernels = []\n#                 for sigma in sigmas:\n#                     for theta in thetas:\n#                         for frequency in frequencies:\n#                             kernel = []\n#                             for color in range(3):\n#                                 kernel.append(np.real(gabor_kernel(frequency=frequency, theta=theta, sigma_x=sigma, sigma_y=sigma/0.5, n_stds=3)))\n#                             kernels.append(kernel)\n\n#                 # Apply each Gabor filter kernel to the image channels and get the mean response\n#                 for i, kernel in enumerate(kernels):\n#                     filtered = []\n#                     for j in range(3):\n#                         filtered_channel = ndimage.convolve(img[:, :, j], kernel[j], mode='constant', cval=0)\n#                         filtered.append(filtered_channel)\n#                     filtered = np.mean(filtered, axis=0)\n#                     df[f'gabor_{i}'] = [filtered.mean()]\n#                     print(filtered.mean())\n\n        image_dataset = image_dataset.append(df, ignore_index=True)\n\n    return image_dataset\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\n\n# Ignore all warnings\nwarnings.filterwarnings(\"ignore\")\n\ntrain_extr_features = feature_extractor(X_train)\ntest_extr_features = feature_extractor(X_test)\nval_extr_features = feature_extractor(X_val)","metadata":{"_kg_hide-output":true,"scrolled":true,"execution":{"iopub.status.busy":"2023-05-23T15:09:48.574843Z","iopub.status.idle":"2023-05-23T15:09:48.575588Z","shell.execute_reply.started":"2023-05-23T15:09:48.575281Z","shell.execute_reply":"2023-05-23T15:09:48.575311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  Normalisation of extracted features: Not Used","metadata":{}},{"cell_type":"code","source":"#Train \n\nimport numpy as np\n\n# Check for NaN values in each column\ncolumns_with_inf_g = np.any(np.isinf(train_extr_features), axis=0)\ncolumns_with_nan= np.any(np.isnan(train_extr_features), axis=0)\n# Get the column names with NaN values\ncolumns_with_nan_names = train_extr_features.columns[columns_with_nan]\ncolumns_with_nan_names_g = train_extr_features.columns[columns_with_inf_g]\n# Print the columns with NaN values\n\n\nfor i in columns_with_nan_names:\n    train_extr_features[i].fillna(train_extr_features[i].mean(), inplace=True)\n    \n    \nfor i in columns_with_nan_names_g:\n#%@#$@@^@$$@$^^^^^^^^^^^^^^^^^\n    train_extr_features[i].replace(-np.inf,0, inplace=True)\n    \n    \nprint(columns_with_nan_names)\nprint(columns_with_nan_names_g)\n\n#TEST \ncolumns_with_inf_A = np.any(np.isinf(test_extr_features), axis=0)\ncolumns_with_nan_A = np.any(np.isnan(test_extr_features), axis=0)\ncolumns_with_nan_names_A = test_extr_features.columns[columns_with_nan_A]\ncolumns_with_inf_names_A = test_extr_features.columns[columns_with_inf_A]\n\n\n\nfor i in columns_with_nan_names_A:\n    test_extr_features[i].fillna(test_extr_features[i].mean(), inplace=True)\n    \n    \nfor i in columns_with_inf_names_A:\n    # might create some noise due to it being 0 ^\n    test_extr_features[i].replace(-np.inf,0, inplace=True)\n    \nprint(columns_with_nan_names_A)\nprint(columns_with_inf_names_A)\n\n# VAL\ncolumns_with_inf_B = np.any(np.isinf(val_extr_features), axis=0)\ncolumns_with_nan_B = np.any(np.isnan(val_extr_features), axis=0)\ncolumns_with_nan_names_B = val_extr_features.columns[columns_with_nan_B]\ncolumns_with_inf_names_B = val_extr_features.columns[columns_with_inf_B]\n\n\nfor i in columns_with_nan_names_B:\n    val_extr_features[i].fillna(val_extr_features[i].mean(), inplace=True)\n    \n    \nfor i in columns_with_inf_names_B:\n    # might create some noise due to it being 0 \n    val_extr_features[i].replace(-np.inf,0, inplace=True)\n    \nprint(columns_with_nan_names_B)\nprint(columns_with_inf_names_B)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import preprocessing\ntrain_extr_features = preprocessing.StandardScaler().fit_transform(train_extr_features)\ntest_extr_features = preprocessing.StandardScaler().fit_transform(test_extr_features)\nval_extr_features = preprocessing.StandardScaler().fit_transform(val_extr_features)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Building ","metadata":{}},{"cell_type":"code","source":"def build_cnn():\n    model = Sequential()\n    pretrained_model=tf.keras.applications.efficientnet.EfficientNetB2(include_top=False,\n                      input_shape=(SIZE,SIZE,3),\n                      pooling='avg',classes=2,\n                      weights=\"imagenet\")\n    # Unfreezing all layers due to the complete difference in dataset, initialised on imagenet weights , from discussion from the winner of the competition\n    for layer in pretrained_model.layers:\n             layer.trainable=True \n\n    model.add(pretrained_model)\n    model.add(Flatten())\n    \n    model.add(Dense(512, activation='relu'))\n    model.add(BatchNormalization())\n    \n    model.add(Dense(256, activation='relu'))\n    model.add(BatchNormalization())\n    \n    model.add(Dense(128, activation='relu'))\n    model.add(BatchNormalization())\n    \n    model.add(Dense(64, activation='relu'))\n    model.add(BatchNormalization())\n    \n    model.add(Dense(64, activation='relu'))\n    model.add(BatchNormalization())\n    \n\n    return model\n\n\ndef build_advanced_encoder():\n    input_layer = keras.Input(shape=(192, 32, 32), name='Input')\n\n    # Encoder\n    encoded = keras.layers.Conv2D(512, (3, 3), activation='relu', padding='same')(input_layer)\n    encoded = keras.layers.BatchNormalization()(encoded)\n    encoded = keras.layers.MaxPooling2D((2, 2))(encoded)\n    encoded = keras.layers.Conv2D(128, (3, 3), activation='relu', padding='same')(encoded)\n    encoded = keras.layers.BatchNormalization()(encoded)\n    encoded = keras.layers.MaxPooling2D((2, 2))(encoded)\n    encoded = keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same')(encoded)\n    encoded = keras.layers.BatchNormalization()(encoded)\n    encoded = keras.layers.MaxPooling2D((2, 2))(encoded)\n    encoded = keras.layers.Flatten()(encoded)\n    encoded = keras.layers.Dense(32, activation='relu')(encoded)\n\n    # Define the model\n    model = keras.Model(inputs=input_layer, outputs=encoded)\n    return model\n\n\n# SRNET: https://www.kaggle.com/code/pednt9/alaska2-srnet-in-keras/notebook\ndef layer_type1(x_inp, filters, kernel_size=(3, 3), dropout_rate=0):\n    x = L.Conv2D(filters, kernel_size, padding=\"same\")(x_inp)\n    x = L.BatchNormalization()(x)\n    x = L.ReLU()(x)\n    if dropout_rate > 0:\n        x = L.Dropout(dropout_rate)(x)\n\n    return x\n\ndef layer_type2(x_inp, filters, kernel_size=(3, 3), dropout_rate=0):\n    x = layer_type1(x_inp, filters)\n    x = L.Conv2D(filters, kernel_size, padding=\"same\")(x)\n    x = L.BatchNormalization()(x)\n    x = L.ReLU()(x)\n    if dropout_rate > 0:\n        x = L.Dropout(dropout_rate)(x)\n\n    x = L.Add()([x, x_inp])\n    \n    return x\n\ndef layer_type3(x_inp, filters, kernel_size=(3, 3), dropout_rate=0):\n    x = layer_type1(x_inp, filters)\n    x = L.Conv2D(filters, kernel_size, padding=\"same\")(x)\n    x = L.BatchNormalization()(x)\n    x = L.ReLU()(x)\n    x = L.AveragePooling2D(pool_size=(3, 3), strides=(2, 2))(x)\n    if dropout_rate > 0:\n        x = L.Dropout(dropout_rate)(x)\n        \n    x_res = L.Conv2D(filters, kernel_size, strides=(2, 2))(x_inp)\n    x_res = L.BatchNormalization()(x_res)\n    if dropout_rate > 0:\n        x_res = L.Dropout(dropout_rate)(x_res)\n\n    x = L.Add()([x, x_res])\n    \n    return x\n\ndef layer_type4(x_inp, filters, kernel_size=(3, 3), dropout_rate=0):\n    x = layer_type1(x_inp, filters)\n    x = L.Conv2D(filters, kernel_size=(3, 3), padding=\"same\")(x)\n    x = L.BatchNormalization()(x)\n    if dropout_rate > 0:\n        x = L.Dropout(dropout_rate)(x)    \n    x = L.GlobalAveragePooling2D()(x)\n    \n    return x\n\ndef make_model(input_shape=(SIZE, SIZE, 3), num_type2=5, dropout_rate=0):\n\n    # I reduced the size (image size, filters and depth) of the original network because it was way to big\n    inputs = L.Input(shape=input_shape)\n    \n    x = layer_type1(inputs, filters=64, dropout_rate=dropout_rate)\n    x = layer_type1(x, filters=16, dropout_rate=dropout_rate)    \n    \n    for _ in range(num_type2):\n        x = layer_type2(x, filters=16, dropout_rate=dropout_rate)         \n    \n    x = layer_type3(x, filters=16, dropout_rate=dropout_rate) \n    x = layer_type3(x, filters=32, dropout_rate=dropout_rate)            \n    x = layer_type3(x, filters=64, dropout_rate=dropout_rate)            \n    x = layer_type3(x, filters=128, dropout_rate=dropout_rate) \n    \n    x = layer_type4(x, filters=128, dropout_rate=dropout_rate)        \n    \n    x = L.Dense(64)(x)\n    x = L.BatchNormalization()(x)\n    x = L.ReLU()(x)\n    if dropout_rate > 0:\n        x = L.Dropout(dropout_rate)(x)\n\n    model = Model(inputs=inputs, outputs=x)\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:35.657608Z","iopub.execute_input":"2023-05-24T13:55:35.657966Z","iopub.status.idle":"2023-05-24T13:55:35.686166Z","shell.execute_reply.started":"2023-05-24T13:55:35.657939Z","shell.execute_reply":"2023-05-24T13:55:35.685264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn = build_cnn()\nDALP_DCT_YCCBR = build_advanced_encoder()\nDALP_DCT_RGB = build_advanced_encoder()\nSRNET_P = make_model() # Because why not ? Run if you care for every bit of accuracy and ditched optimising inference or training speed. Run if you have > 32 GB Ram, or a V100 - 3090. Convergence is slower but what you get is something like ensemble \n\ncombinedInput = concatenate([DALP_DCT_RGB.input,DALP_DCT_YCCBR.output, cnn.output,SRNET_P.output])\n\nx = Dense(512, activation=\"relu\")(combinedInput)\nkeras.layers.BatchNormalization()(x)\nx = Dropout(0.25)(x)\nx = Dense(128, activation=\"relu\")(x)\nx = Dropout(0.2)(x)\nx = Dense(64, activation=\"relu\")(x)\nkeras.layers.BatchNormalization()(x)\nx = Dense(32, activation=\"relu\")(x)\nx = Dense(16, activation=\"relu\")(x)\nx = Dropout(0.15)(x)\nkeras.layers.BatchNormalization()(x)\nx = Dense(4, activation=\"relu\")(x)\nx = Dropout(0.1)(x)\nx = Dense(1, activation=\"sigmoid\")(x)\n\nmodel = Model(inputs=[DALP_DCT_RGB.output,DALP_DCT_YCCBR.input, cnn.input,SRNET_P.input], outputs=x)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:36.664012Z","iopub.execute_input":"2023-05-24T13:55:36.664377Z","iopub.status.idle":"2023-05-24T13:55:44.263673Z","shell.execute_reply.started":"2023-05-24T13:55:36.664347Z","shell.execute_reply":"2023-05-24T13:55:44.262717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot_model(model, show_shapes=True, to_file=\"model.png\")","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:44:19.592070Z","iopub.execute_input":"2023-05-24T13:44:19.592435Z","iopub.status.idle":"2023-05-24T13:44:19.599567Z","shell.execute_reply.started":"2023-05-24T13:44:19.592400Z","shell.execute_reply":"2023-05-24T13:44:19.598664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Optimizer ","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_addons as tfa\n# Better version of Adam and SGD, recommended to be used in the literature\noptimizer = tfa.optimizers.AdamW(weight_decay=0.000001)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:44.265398Z","iopub.execute_input":"2023-05-24T13:55:44.265739Z","iopub.status.idle":"2023-05-24T13:55:44.403874Z","shell.execute_reply.started":"2023-05-24T13:55:44.265707Z","shell.execute_reply":"2023-05-24T13:55:44.402978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preprocessing for modeling ","metadata":{}},{"cell_type":"code","source":"# conversion to YCrCB color space \ntrain_images_SR = []\ntest_images_SR = []\nval_images_SR = []\n\nfor i in X_train : \n    train_images_SR.append(cv2.cvtColor(i, cv2.COLOR_BGR2YCrCb))\nfor i in X_test: \n    test_images_SR.append(cv2.cvtColor(i, cv2.COLOR_BGR2YCrCb))\nfor i in X_val: \n    val_images_SR.append(cv2.cvtColor(i, cv2.COLOR_BGR2YCrCb))","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:46.414593Z","iopub.execute_input":"2023-05-24T13:55:46.414933Z","iopub.status.idle":"2023-05-24T13:55:46.785320Z","shell.execute_reply.started":"2023-05-24T13:55:46.414907Z","shell.execute_reply":"2023-05-24T13:55:46.784349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_SR =np.array(train_images_SR)\ntest_images_SR =np.array(test_images_SR)\nval_images_SR =np.array(val_images_SR)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:46.787064Z","iopub.execute_input":"2023-05-24T13:55:46.787424Z","iopub.status.idle":"2023-05-24T13:55:46.909780Z","shell.execute_reply.started":"2023-05-24T13:55:46.787393Z","shell.execute_reply":"2023-05-24T13:55:46.908806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_norm_SR = train_images_SR.astype('float32')\ntest_images_norm_SR = test_images_SR.astype('float32')\nval_images_norm_SR = val_images_SR.astype('float32')\n# normalize to the range 0-1\ntrain_images_norm_SR /= 255.0\ntest_images_norm_SR /= 255.0\nval_images_norm_SR /= 255.0","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:46.972444Z","iopub.execute_input":"2023-05-24T13:55:46.972737Z","iopub.status.idle":"2023-05-24T13:55:47.492622Z","shell.execute_reply.started":"2023-05-24T13:55:46.972713Z","shell.execute_reply":"2023-05-24T13:55:47.491681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n# Convert the labels to numerical values , from \"Fake\" == 0 'True' == 1\nencoder = LabelEncoder()\n\ny_train = encoder.fit_transform(y_train)\ny_test = encoder.fit_transform(y_test)\ny_val= encoder.fit_transform(y_val)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:47.498291Z","iopub.execute_input":"2023-05-24T13:55:47.498598Z","iopub.status.idle":"2023-05-24T13:55:47.504682Z","shell.execute_reply.started":"2023-05-24T13:55:47.498575Z","shell.execute_reply":"2023-05-24T13:55:47.503649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\n\n#distinct & model input requirement\ny_train = to_categorical(y_train)\ny_test = to_categorical(y_test)\ny_val = to_categorical(y_val)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:47.980656Z","iopub.execute_input":"2023-05-24T13:55:47.981258Z","iopub.status.idle":"2023-05-24T13:55:47.987731Z","shell.execute_reply.started":"2023-05-24T13:55:47.981222Z","shell.execute_reply":"2023-05-24T13:55:47.986827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" #Convert the input data to numpy arrays, images need to be converted to NP array\nX_train = np.array(X_train)\nX_val = np.array(X_val)\nX_test = np.array(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:48.250940Z","iopub.execute_input":"2023-05-24T13:55:48.251515Z","iopub.status.idle":"2023-05-24T13:55:48.422562Z","shell.execute_reply.started":"2023-05-24T13:55:48.251478Z","shell.execute_reply":"2023-05-24T13:55:48.421466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_norm = X_train.astype('float32')\ntest_images_norm = X_test.astype('float32')\nval_images_norm = X_val.astype('float32')\n# normalize to the range 0-1\ntrain_images_norm /= 255.0\ntest_images_norm /= 255.0\nval_images_norm /= 255.0","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:48.460795Z","iopub.execute_input":"2023-05-24T13:55:48.461799Z","iopub.status.idle":"2023-05-24T13:55:49.049933Z","shell.execute_reply.started":"2023-05-24T13:55:48.461754Z","shell.execute_reply":"2023-05-24T13:55:49.048937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Competition Metric","metadata":{}},{"cell_type":"code","source":"def alaska_weighted_auc(y_true, y_valid):\n    tpr_thresholds = [0.0, 0.4, 1.0]\n    weights =        [       2,   1]\n    \n    fpr, tpr, thresholds = roc_curve(y_true, y_valid)\n    \n    # size of subsets\n    areas = np.array(tpr_thresholds[1:]) - np.array(tpr_thresholds[:-1])\n    \n    # The total area is normalized by the sum of weights such that the final weighted AUC is between 0 and 1.\n    normalization = np.dot(areas, weights)\n    \n    try:\n    \n        competition_metric = 0\n        for idx, weight in enumerate(weights):\n            y_min = tpr_thresholds[idx]\n            y_max = tpr_thresholds[idx + 1]\n            mask = (y_min < tpr) & (tpr < y_max)\n\n            x_padding = np.linspace(fpr[mask][-1], 1, 100)\n\n            x = np.concatenate([fpr[mask], x_padding])\n            y = np.concatenate([tpr[mask], [y_max] * len(x_padding)])\n            y = y - y_min # normalize such that curve starts at y=0\n            score = auc(x, y)\n            submetric = score * weight\n            best_subscore = (y_max - y_min) * weight\n            competition_metric += submetric\n    except:\n        # sometimes there's a weird bug so return naive score\n        return .5\n        \n    return competition_metric / normalization\n\ndef alaska_tf(y_true, y_val):\n    \"\"\"Wrapper for the above function\"\"\"\n    return tf.py_function(func=alaska_weighted_auc, inp=[y_true, y_val], Tout=tf.float32)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:53.464823Z","iopub.execute_input":"2023-05-24T13:55:53.465175Z","iopub.status.idle":"2023-05-24T13:55:53.475139Z","shell.execute_reply.started":"2023-05-24T13:55:53.465146Z","shell.execute_reply":"2023-05-24T13:55:53.474066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Application of Label Smoothing ","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import Callback\nfrom keras import backend as K\n\nclass CosineAnnealingScheduler(Callback):\n    def __init__(self, initial_lr, epochs):\n        super(CosineAnnealingScheduler, self).__init__()\n        self.initial_lr = initial_lr\n        self.epochs = epochs\n\n    def on_epoch_begin(self, epoch, logs=None):\n        current_lr = self.initial_lr * 0.5 * (1 + np.cos(epoch / self.epochs * np.pi))\n        K.set_value(self.model.optimizer.lr, current_lr)\n\n\n\n# Set the initial learning rate and the total number of epochs\ninitial_lr = 0.0001\nepochs = 100\n\n# Create an instance of the CosineAnnealingScheduler callback\ncosine_annealing = CosineAnnealingScheduler(initial_lr, epochs)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:54.435162Z","iopub.execute_input":"2023-05-24T13:55:54.436236Z","iopub.status.idle":"2023-05-24T13:55:54.443340Z","shell.execute_reply.started":"2023-05-24T13:55:54.436188Z","shell.execute_reply":"2023-05-24T13:55:54.442370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def label_smoothing(y_true,y_pred):\n     return tf.keras.losses.binary_crossentropy(y_true,y_pred,label_smoothing=0.1)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:56.281341Z","iopub.execute_input":"2023-05-24T13:55:56.282216Z","iopub.status.idle":"2023-05-24T13:55:56.287342Z","shell.execute_reply.started":"2023-05-24T13:55:56.282183Z","shell.execute_reply":"2023-05-24T13:55:56.286268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MODEL FITTING","metadata":{}},{"cell_type":"code","source":"# model.load_weights('/kaggle/working/best_model_weights.h5')\n\nmodel.compile(optimizer=optimizer, loss=label_smoothing,\n              metrics=[alaska_tf])","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:57.741783Z","iopub.execute_input":"2023-05-24T13:55:57.742138Z","iopub.status.idle":"2023-05-24T13:55:57.771124Z","shell.execute_reply.started":"2023-05-24T13:55:57.742109Z","shell.execute_reply":"2023-05-24T13:55:57.770252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X_train\ndel X_test\ndel X_val","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:59.407115Z","iopub.execute_input":"2023-05-24T13:55:59.407477Z","iopub.status.idle":"2023-05-24T13:55:59.412893Z","shell.execute_reply.started":"2023-05-24T13:55:59.407449Z","shell.execute_reply":"2023-05-24T13:55:59.411965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect() # clears up memory","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:55:59.809432Z","iopub.execute_input":"2023-05-24T13:55:59.810085Z","iopub.status.idle":"2023-05-24T13:56:00.116793Z","shell.execute_reply.started":"2023-05-24T13:55:59.810055Z","shell.execute_reply":"2023-05-24T13:56:00.115916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:56:00.509333Z","iopub.execute_input":"2023-05-24T13:56:00.510798Z","iopub.status.idle":"2023-05-24T13:56:00.517050Z","shell.execute_reply.started":"2023-05-24T13:56:00.510763Z","shell.execute_reply":"2023-05-24T13:56:00.515937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_norm.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:56:02.139494Z","iopub.execute_input":"2023-05-24T13:56:02.139843Z","iopub.status.idle":"2023-05-24T13:56:02.146240Z","shell.execute_reply.started":"2023-05-24T13:56:02.139816Z","shell.execute_reply":"2023-05-24T13:56:02.145356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_norm_SR.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:56:02.412290Z","iopub.execute_input":"2023-05-24T13:56:02.413039Z","iopub.status.idle":"2023-05-24T13:56:02.419804Z","shell.execute_reply.started":"2023-05-24T13:56:02.413011Z","shell.execute_reply":"2023-05-24T13:56:02.418705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\nearly_stopping = EarlyStopping(monitor='alaska_tf', patience=5)\n\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\n\n# Model Callbacks \n# cb = [\n#     ReduceLROnPlateau(\n#         monitor='alaska_tf',\n#         factor=0.1,\n#         patience=3,\n#         mode='auto',\n#         min_delta=0.0002,\n#         cooldown=5,\n#         min_lr=10e-10,\n#         verbose=1,\n#     )\n# ]\n    \n# dataset_inputs = tf.data.Dataset.from_tensor_slices((train_extr_features, train_images_norm))\n# y_train = y_train[:, 0]\n# y_val = y_val[:, 0]\n\n# dataset_label = tf.data.Dataset.from_tensor_slices(y_train)\nfrom keras.callbacks import ModelCheckpoint\n\n# Create a ModelCheckpoint callback to save the weights of the best model\ncheckpoint = ModelCheckpoint('/kaggle/working/best_model_weights.h5', monitor='val_alaska_tf', save_best_only=True, save_weights_only=True, mode='max', verbose=1)\n\n\n# fit model\nearly_stopping = EarlyStopping(monitor='val_alaska_tf', patience=15,restore_best_weights=True)\n# The bigger the batch the better \nhistory = model.fit(x=[DCT_Train, train_images_norm,train_images_norm_SR],y=y_train[:,0],epochs = 20, batch_size=20,validation_data=([DCT_Test,test_images_norm,test_images_norm_SR],y_test[:,0]),callbacks=([cosine_annealing,checkpoint]))","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:56:07.468059Z","iopub.execute_input":"2023-05-24T13:56:07.471363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.load_weights('best_model_weights.h5')\n\nscore = model.evaluate([DCT_Val,val_images_norm,val_images_norm],y=y_val[:,0], batch_size=32 )\nprint(f'Test loss: {score[0]} / Test accuracy: {score[1]}')","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:53:28.728979Z","iopub.status.idle":"2023-05-24T13:53:28.729825Z","shell.execute_reply.started":"2023-05-24T13:53:28.729551Z","shell.execute_reply":"2023-05-24T13:53:28.729573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict([DCT_Val, val_images_norm,val_images_norm])\n# Define a threshold probability value\nthreshold = 0.5\n\n# Convert probabilities to class labels\ny_pred_classes = np.zeros_like(y_pred, dtype=int)\ny_pred_classes[y_pred > threshold] = 1","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:13:51.740135Z","iopub.execute_input":"2023-05-24T13:13:51.740583Z","iopub.status.idle":"2023-05-24T13:13:53.398834Z","shell.execute_reply.started":"2023-05-24T13:13:51.740554Z","shell.execute_reply":"2023-05-24T13:13:53.397882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nfrom sklearn.metrics import confusion_matrix, classification_report\ndef show_confusion_matrix(confusion_matrix):\n  hmap = sns.heatmap(confusion_matrix, annot=True, fmt=\"d\", cmap=\"Blues\")\n  hmap.yaxis.set_ticklabels(hmap.yaxis.get_ticklabels(), rotation=0, ha='right')\n  hmap.xaxis.set_ticklabels(hmap.xaxis.get_ticklabels(), rotation=30, ha='right')\n  plt.ylabel('True class')\n  plt.xlabel('Predicted class');\n\ncm = confusion_matrix(y_val[:,0], y_pred_classes)\ndf_cm = pd.DataFrame(cm)\nshow_confusion_matrix(df_cm)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T13:13:53.402256Z","iopub.execute_input":"2023-05-24T13:13:53.402991Z","iopub.status.idle":"2023-05-24T13:13:53.692989Z","shell.execute_reply.started":"2023-05-24T13:13:53.402963Z","shell.execute_reply":"2023-05-24T13:13:53.692094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Standard CNN Transfer Leanring model","metadata":{}},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, Flatten, MaxPooling2D\nfrom keras.optimizers import Adam, SGD\n\ntrain_datagen = ImageDataGenerator(rescale=1./255,vertical_flip = 0.5,\nhorizontal_flip = 0.5)\n\nval_datagen = ImageDataGenerator(rescale=1./255)\n# can we scale image down in return for a more robust model using features extraction ?\ntrain_generator = train_datagen.flow_from_directory(\n        '/kaggle/input/alaskas-v2-binary/ALASKA/ALASKA/Train',\n        target_size=(512, 512),\n        batch_size=20,\n        class_mode='binary',shuffle=True)\n\nval_generator = val_datagen.flow_from_directory(\n        '/kaggle/input/alaskas-v2-binary/ALASKA/ALASKA/Test',\n        target_size=(512, 512),\n        batch_size=512,\n        class_mode='binary',shuffle=False)\n\n\n# Define the model\nmodel = Sequential()\npretrained_model=tf.keras.applications.efficientnet.EfficientNetB2(include_top=False,\n                      input_shape=(512,512,3),\n                      pooling='avg',classes=2,\n                      weights='imagenet')\nfor layer in pretrained_model.layers:\n         layer.trainable=True \nx = Flatten()(pretrained_model.output)\nx = Dense(512, activation='relu')(x)\nx = Dropout(0.5)(x)\nx = Dense(128, activation='relu')(x)\nx = Dropout(0.5)(x)\noutput = Dense(1, activation='sigmoid')(x)\nmodel = Model(inputs=pretrained_model.input, outputs=output)\nmodel.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=[alaska_tf])\n\nmodel.fit(train_generator, steps_per_epoch=len(train_generator), epochs=75)\nscore = model.evaluate(val_generator, steps=len(val_generator))\nprint('Validation Loss:', score[0])\nprint('Validation Accuracy:', score[1])\n","metadata":{"execution":{"iopub.status.busy":"2023-05-24T09:25:02.155368Z","iopub.execute_input":"2023-05-24T09:25:02.155802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# RL Model for Image Classification ","metadata":{}},{"cell_type":"code","source":"!pip install stable-baselines3\n!pip install gym\n!pip install opencv-python\n!pip install torch\n!pip install tensorflow\n!pip install keras","metadata":{"execution":{"iopub.status.idle":"2023-05-24T06:45:56.735023Z","shell.execute_reply.started":"2023-05-24T06:44:31.427621Z","shell.execute_reply":"2023-05-24T06:45:56.733868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the checkpoint callback\nfrom stable_baselines3.common.callbacks import CheckpointCallback\ncheckpoint_callback = CheckpointCallback(save_freq=10000, save_path='/kaggle/working/', name_prefix='ppo_mnist')","metadata":{"execution":{"iopub.status.busy":"2023-05-24T06:45:56.737681Z","iopub.execute_input":"2023-05-24T06:45:56.738428Z","iopub.status.idle":"2023-05-24T06:46:07.016999Z","shell.execute_reply.started":"2023-05-24T06:45:56.738387Z","shell.execute_reply":"2023-05-24T06:46:07.015906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, Flatten, MaxPooling2D\nfrom keras.optimizers import Adam, SGD\n\ntrain_datagen = ImageDataGenerator(rescale=1./255)\n\nval_datagen = ImageDataGenerator(rescale=1./255)\ntrain_generator = train_datagen.flow_from_directory(\n        '/kaggle/input/alaskas-v2-binary/ALASKA/ALASKA/Train',\n        target_size=(512, 512),\n        batch_size=16,\n        class_mode='binary',shuffle=True)\n\nval_generator = val_datagen.flow_from_directory(\n        '/kaggle/input/alaskas-v2-binary/ALASKA/ALASKA/Test',\n        target_size=(512, 512),\n        batch_size=16,\n        class_mode='binary',shuffle=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-24T06:46:07.018567Z","iopub.execute_input":"2023-05-24T06:46:07.019336Z","iopub.status.idle":"2023-05-24T06:46:35.196823Z","shell.execute_reply.started":"2023-05-24T06:46:07.019278Z","shell.execute_reply":"2023-05-24T06:46:35.195925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nimport gym\nimport random\nimport numpy as np\nfrom tensorflow import keras\nimport time\nfrom stable_baselines3 import PPO\nfrom stable_baselines3.common.env_util import make_vec_env\nfrom stable_baselines3.common.monitor import Monitor\n\nfrom stable_baselines3.common.logger import configure\n\nfrom stable_baselines3.common.vec_env import DummyVecEnv\nfrom stable_baselines3.common.monitor import Monitor\n\n# Model / data parameters\nnum_classes = 2\ninput_shape = (26, 26,3)\n\n\n\n\nclass MnistEnv(gym.Env):\n    def __init__(self, images_per_episode=1, dataset=(train_images_norm, y_train[:,0]), random=True):\n        super().__init__()\n\n        self.action_space = gym.spaces.Discrete(1)\n        self.observation_space = gym.spaces.Box(low=0, high=1,\n                                                shape=(256,256,3),\n                                                dtype=np.float32)\n\n        self.images_per_episode = images_per_episode\n        self.step_count = 0\n\n        self.x, self.y = dataset\n        self.random = random\n        self.dataset_idx = 0\n\n    def step(self, action):\n        done = False\n        reward = int(action == self.expected_action)\n\n        obs = self._next_obs()\n\n        self.step_count += 1\n        if self.step_count >= self.images_per_episode:\n            done = True\n\n        return obs, reward, done, {}\n\n    def reset(self):\n        self.step_count = 0\n\n        obs = self._next_obs()\n        return obs\n\n    def _next_obs(self):\n        if self.random:\n            next_obs_idx = random.randint(0, len(self.x) - 1)\n            self.expected_action = int(self.y[next_obs_idx])\n            obs = self.x[next_obs_idx]\n\n        else:\n            obs = self.x[self.dataset_idx]\n            self.expected_action = int(self.y[self.dataset_idx])\n\n            self.dataset_idx += 1\n            if self.dataset_idx >= len(self.x):\n                raise StopIteration()\n\n        return obs\n\n\ndef mnist_ppo():\n    env = DummyVecEnv([lambda: Monitor(MnistEnv(images_per_episode=1), './logs/mnist_ppo')])\n\n    model = PPO(\n        'MlpPolicy',\n        env,\n        verbose=1,\n        n_steps=32,\n        ent_coef=0.01,\n        learning_rate=2.5e-5,\n        batch_size=256,\n        n_epochs=15,\n        gamma=0.99,\n        gae_lambda=0.95,\n        clip_range=0.2\n    )\n\n    model.learn(total_timesteps=int(1.2e5), callback=checkpoint_callback)\n    return model\n\n\nppo_model =mnist_ppo()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T06:46:35.200236Z","iopub.execute_input":"2023-05-24T06:46:35.200558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mnist_ppo_eval(ppo_model):\n    attempts, correct = 0,0\n\n    env = DummyVecEnv([lambda: MnistEnv(images_per_episode=1, dataset=(x_test, y_test[:,0]), random=False)])\n    \n    try:\n        while True:\n            obs, done = env.reset(), [False]\n            while not done[0]:\n                action, _ = ppo_model.predict(obs)\n                obs, rew, done, _ = env.step(action)\n\n                attempts += 1\n                if rew[0] > 0:\n                    correct += 1\n\n    except StopIteration:\n        print()\n        print('validation done...')\n        print('Accuracy: {0}%'.format((float(correct) / attempts) * 100))\n\nmnist_ppo_eval(ppo_model)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}