{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":6496514,"sourceType":"datasetVersion","datasetId":3754858}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Image Preprocessing ","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport cv2\nimport os\nimport matplotlib.pyplot as plt\nimport glob\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-13T22:43:58.366866Z","iopub.execute_input":"2024-05-13T22:43:58.367825Z","iopub.status.idle":"2024-05-13T22:44:11.944403Z","shell.execute_reply.started":"2024-05-13T22:43:58.367786Z","shell.execute_reply":"2024-05-13T22:44:11.943566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\nif tf.test.gpu_device_name():\n    print('Default GPU Device:', tf.test.gpu_device_name())\nelse:\n    print(\"Please install GPU version of TF\")","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:44:11.945968Z","iopub.execute_input":"2024-05-13T22:44:11.946486Z","iopub.status.idle":"2024-05-13T22:44:12.233793Z","shell.execute_reply.started":"2024-05-13T22:44:11.946459Z","shell.execute_reply":"2024-05-13T22:44:12.232613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Histogram Image Enhancement ","metadata":{}},{"cell_type":"code","source":"def apply_histogram_equalization(image):\n    if len(image.shape) == 2:  # Grayscale image\n        return cv2.equalizeHist(image)\n    elif len(image.shape) == 3:  # Color image\n        # Convert to YUV color space\n        yuv_img = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)\n        # Equalize the histogram of the Y channel\n        yuv_img[:, :, 0] = cv2.equalizeHist(yuv_img[:, :, 0])\n        # Convert back to RGB color space\n        result_img = cv2.cvtColor(yuv_img, cv2.COLOR_YUV2RGB)\n        return result_img\n\n# Path to the directory containing image files\ndirectory_path = '/kaggle/input/brain-tumors-256x256/Data/glioma_tumor'\n\n# List all files in the directory\nimage_files = [f for f in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, f))]\n\n# Choose one image randomly for detailed comparison\nchosen_image = image_files[0]\n\n# Load the chosen image\nchosen_image_path = os.path.join(directory_path, chosen_image)\nchosen_image_data = cv2.imread(chosen_image_path, cv2.IMREAD_COLOR)\n\nif chosen_image_data is None:\n    print(\"Error: Image could not be read. Make sure the path is correct.\")\nelse:\n    # Convert color from BGR to RGB\n    chosen_image_data_rgb = cv2.cvtColor(chosen_image_data, cv2.COLOR_BGR2RGB)\n\n    # Apply histogram equalization\n    chosen_enhanced_image = apply_histogram_equalization(chosen_image_data_rgb)\n\n    # Plotting original and enhanced images\n    plt.figure(figsize=(12, 6))\n\n    plt.subplot(1, 2, 1)\n    plt.imshow(chosen_image_data_rgb)\n    plt.title('Original Image')\n    plt.axis('off')\n\n    plt.subplot(1, 2, 2)\n    plt.imshow(chosen_enhanced_image)\n    plt.title('Histogram Enhanced Image')\n    plt.axis('off')\n\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:44:12.240303Z","iopub.execute_input":"2024-05-13T22:44:12.240673Z","iopub.status.idle":"2024-05-13T22:44:13.122968Z","shell.execute_reply.started":"2024-05-13T22:44:12.240639Z","shell.execute_reply":"2024-05-13T22:44:13.121993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### applying the enhancement on the dataset","metadata":{}},{"cell_type":"code","source":"input_directory = '/kaggle/input/brain-tumors-256x256/Data/glioma_tumor'\n\n# Path to the directory where enhanced images will be saved\noutput_directory = 'Data/glioma_tumor_histo'\nos.makedirs(output_directory, exist_ok=True)\n\n# List all files in the input directory\nimage_files = [f for f in os.listdir(input_directory) if os.path.isfile(os.path.join(input_directory, f))]\n\n# Process each image\nfor filename in image_files:\n    # Load the image\n    image_path = os.path.join(input_directory, filename)\n    image = cv2.imread(image_path, cv2.IMREAD_COLOR)\n\n    if image is not None:\n        # Apply histogram equalization\n        enhanced_image = apply_histogram_equalization(image)\n\n        # Save the enhanced image to the output directory\n        output_path = os.path.join(output_directory, filename)\n        cv2.imwrite(output_path, enhanced_image)\n\nprint(\"Enhanced images saved to:\", output_directory)","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:44:13.124022Z","iopub.execute_input":"2024-05-13T22:44:13.124311Z","iopub.status.idle":"2024-05-13T22:44:22.63012Z","shell.execute_reply.started":"2024-05-13T22:44:13.124288Z","shell.execute_reply":"2024-05-13T22:44:22.629167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Contrast Stretching \nUsed for normalizing the brightness levels ","metadata":{}},{"cell_type":"code","source":"def contrast_stretching(img):\n    # Convert to float to avoid overflow issues during calculation\n    img_float = img.astype(float)\n    \n    # Compute the min and max pixel values\n    a = np.min(img_float)\n    b = np.max(img_float)\n    \n    # Perform contrast stretching\n    stretched_img = 255 * ((img_float - a) / (b - a))\n    stretched_img = stretched_img.astype(np.uint8)\n    \n    return stretched_img\n\n# Load an image in grayscale\nimage = cv2.imread('/kaggle/input/brain-tumors-256x256/Data/meningioma_tumor/M_1.jpg', 0)\n\n# Apply contrast stretching\nstretched_image = contrast_stretching(image)\n\n# Display the original and stretched images\nplt.figure(figsize=(12, 8))\n\nplt.subplot(2, 2, 1)\nplt.title('Original Image')\nplt.imshow(image, cmap='gray')\n\nplt.subplot(2, 2, 2)\nplt.title('Strechted Image')\nplt.imshow(stretched_image, cmap='gray')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:44:22.631194Z","iopub.execute_input":"2024-05-13T22:44:22.631458Z","iopub.status.idle":"2024-05-13T22:44:23.018672Z","shell.execute_reply.started":"2024-05-13T22:44:22.631436Z","shell.execute_reply":"2024-05-13T22:44:23.017808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_directory = '/kaggle/input/brain-tumors-256x256/Data/meningioma_tumor'\n\n# Path to the directory where enhanced images will be saved\noutput_directory = 'Data/meningioma_tumor_contrast'\nos.makedirs(output_directory, exist_ok=True)\n\n# List all files in the input directory\nimage_files = [f for f in os.listdir(input_directory) if os.path.isfile(os.path.join(input_directory, f))]\n\n# Process each image\nfor filename in image_files:\n    # Load the image\n    image_path = os.path.join(input_directory, filename)\n    image = cv2.imread(image_path, cv2.IMREAD_COLOR)\n\n    if image is not None:\n        # Apply histogram equalization\n        enhanced_image = contrast_stretching(image)\n\n        # Save the enhanced image to the output directory\n        output_path = os.path.join(output_directory, filename)\n        cv2.imwrite(output_path, enhanced_image)\n\nprint(\"Enhanced images saved to:\", output_directory)","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:44:23.020061Z","iopub.execute_input":"2024-05-13T22:44:23.020446Z","iopub.status.idle":"2024-05-13T22:44:33.176449Z","shell.execute_reply.started":"2024-05-13T22:44:23.020409Z","shell.execute_reply":"2024-05-13T22:44:33.175531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Applying Gamma Noise","metadata":{}},{"cell_type":"code","source":"# Function to apply gamma noise to an image\ndef apply_gamma_noise(image, shape, scale):\n    # Generate gamma-distributed noise\n    gamma_noise = np.random.gamma(shape, scale, image.shape[:2])\n    \n    # Add the noise to the image\n    noisy_image = np.clip(image + gamma_noise[:, :, np.newaxis], 0, 255).astype(np.uint8)\n    \n    return noisy_image\n\n# Load an image\nimage_path = '/kaggle/input/brain-tumors-256x256/Data/normal/N_1.jpg'\nimage = cv2.imread(image_path, cv2.IMREAD_COLOR)\n\nif image is None:\n    print(\"Error: Image could not be read. Make sure the path is correct.\")\nelse:\n    # Convert color from BGR to RGB\n    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n    # Parameters for the gamma distribution\n    shape = 2.0  # Shape parameter (k)\n    scale = 1.0  # Scale parameter (theta)\n\n    # Apply gamma noise\n    noisy_image = apply_gamma_noise(image_rgb, shape, scale)\n\n    # Plotting original and noisy images\n    plt.figure(figsize=(12, 6))\n\n    plt.subplot(1, 2, 1)\n    plt.imshow(image_rgb)\n    plt.title('Original Image')\n    plt.axis('off')\n\n    plt.subplot(1, 2, 2)\n    plt.imshow(noisy_image)\n    plt.title('Image with Gamma Noise')\n    plt.axis('off')\n\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:44:33.177627Z","iopub.execute_input":"2024-05-13T22:44:33.177913Z","iopub.status.idle":"2024-05-13T22:44:33.585326Z","shell.execute_reply.started":"2024-05-13T22:44:33.177889Z","shell.execute_reply":"2024-05-13T22:44:33.584496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_directory = '/kaggle/input/brain-tumors-256x256/Data/normal'\n\n# Path to the directory where enhanced images will be saved\noutput_directory = 'normal_gamma'\nos.makedirs(output_directory, exist_ok=True)\n\n# List all files in the input directory\nimage_files = [f for f in os.listdir(input_directory) if os.path.isfile(os.path.join(input_directory, f))]\n\n# Process each image\nfor filename in image_files:\n    # Load the image\n    image_path = os.path.join(input_directory, filename)\n    image = cv2.imread(image_path, cv2.IMREAD_COLOR)\n\n    if image is not None:\n        # Apply histogram equalization\n        enhanced_image = apply_gamma_noise(image,  shape, scale)\n\n        # Save the enhanced image to the output directory\n        output_path = os.path.join(output_directory, filename)\n        cv2.imwrite(output_path, enhanced_image)\n\nprint(\"Enhanced images saved to:\", output_directory)","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:44:33.586402Z","iopub.execute_input":"2024-05-13T22:44:33.586658Z","iopub.status.idle":"2024-05-13T22:44:40.180392Z","shell.execute_reply.started":"2024-05-13T22:44:33.586635Z","shell.execute_reply":"2024-05-13T22:44:40.179481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Removing Gamma Noise","metadata":{}},{"cell_type":"code","source":"def adjust_gamma(image, gamma=1.0):\n    # Build a lookup table mapping the pixel values [0, 255] to\n    # their adjusted gamma values\n    inv_gamma = 1.0 / gamma\n    table = np.array([((i / 255.0) ** inv_gamma) * 255\n                      for i in np.arange(256)]).astype(\"uint8\")\n    \n    return cv2.LUT(image, table)\n\n# Load an image\nimage = noisy_image\nif image is None:\n    print(\"Error: Image could not be read. Make sure the path is correct.\")\n    exit()\n\n# Convert image from BGR to RGB\nimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n# Specify gamma value; try different values to see the effect\ngamma = 0.5  # Less than 1 - Brighten; greater than 1 - Darken\n\n# Apply gamma correction\nadjusted = adjust_gamma(image, gamma=gamma)\n\n# Set up the plot with subplots\nfig, ax = plt.subplots(1, 2, figsize=(10, 4))\n\n# Plot the original image\nax[0].imshow(image)\nax[0].set_title('Original Image')\nax[0].axis('off')  # Turn off axis numbers and ticks\n\n# Plot the gamma adjusted image\nax[1].imshow(adjusted)\nax[1].set_title('Gamma Adjusted Image')\nax[1].axis('off')  # Turn off axis numbers and ticks\n\n# Display the plots\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:44:40.184142Z","iopub.execute_input":"2024-05-13T22:44:40.184435Z","iopub.status.idle":"2024-05-13T22:44:40.495387Z","shell.execute_reply.started":"2024-05-13T22:44:40.184409Z","shell.execute_reply":"2024-05-13T22:44:40.494443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_directory = '/kaggle/working/normal_gamma'\n\n# Path to the directory where enhanced images will be saved\noutput_directory = 'Data/normal_corrected_gamma'\nos.makedirs(output_directory, exist_ok=True)\n\n# List all files in the input directory\nimage_files = [f for f in os.listdir(input_directory) if os.path.isfile(os.path.join(input_directory, f))]\n\n# Process each image\nfor filename in image_files:\n    # Load the image\n    image_path = os.path.join(input_directory, filename)\n    image = cv2.imread(image_path, cv2.IMREAD_COLOR)\n\n    if image is not None:\n        # Apply histogram equalization\n        enhanced_image = adjust_gamma(image)\n\n        # Save the enhanced image to the output directory\n        output_path = os.path.join(output_directory, filename)\n        cv2.imwrite(output_path, enhanced_image)\n\nprint(\"Enhanced images saved to:\", output_directory)","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:44:40.496481Z","iopub.execute_input":"2024-05-13T22:44:40.49679Z","iopub.status.idle":"2024-05-13T22:44:41.975602Z","shell.execute_reply.started":"2024-05-13T22:44:40.496741Z","shell.execute_reply":"2024-05-13T22:44:41.974559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Applying Impulse Noise (Salt and Pepper)","metadata":{}},{"cell_type":"code","source":"image = cv2.imread('/kaggle/input/hpa-single-cell-image-classification/train/002679c2-bbb6-11e8-b2ba-ac1f6b6435d0_red.png', 0)\n\ndef add_salt_and_pepper_noise(image, salt_prob=0.05, pepper_prob=0.05):\n    noisy_image = np.copy(image)\n    height, width = image.shape[:2]\n\n    # Add salt noise\n    num_salt = np.ceil(salt_prob * image.size)\n    salt_coords = [np.random.randint(0, i-1, int(num_salt)) for i in image.shape]\n    noisy_image[salt_coords[0], salt_coords[1]] = 255\n\n    # Add pepper noise\n    num_pepper = np.ceil(pepper_prob * image.size)\n    pepper_coords = [np.random.randint(0, i-1, int(num_pepper)) for i in image.shape]\n    noisy_image[pepper_coords[0], pepper_coords[1]] = 0\n\n    return noisy_image\n\n# Load an image in grayscale\nimage = cv2.imread('/kaggle/input/brain-tumors-256x256/Data/pituitary_tumor/P_1.jpg', 0)\n# Apply contrast stretching\nnoisy_image = add_salt_and_pepper_noise(image)\n\n# Display the original and stretched images\nplt.figure(figsize=(12, 8))\n\nplt.subplot(2, 2, 1)\nplt.title('Original Image')\nplt.imshow(image, cmap='gray')\n\nplt.subplot(2, 2, 2)\nplt.title('noisy Image')\nplt.imshow(noisy_image, cmap='gray')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:44:41.97699Z","iopub.execute_input":"2024-05-13T22:44:41.977285Z","iopub.status.idle":"2024-05-13T22:44:42.449622Z","shell.execute_reply.started":"2024-05-13T22:44:41.977259Z","shell.execute_reply":"2024-05-13T22:44:42.448705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_directory = '/kaggle/input/brain-tumors-256x256/Data/pituitary_tumor'\n\n# Path to the directory where enhanced images will be saved\noutput_directory = 'pituitary_tumor_impulsive'\nos.makedirs(output_directory, exist_ok=True)\n\n# List all files in the input directory\nimage_files = [f for f in os.listdir(input_directory) if os.path.isfile(os.path.join(input_directory, f))]\n\n# Process each image\nfor filename in image_files:\n    # Load the image\n    image_path = os.path.join(input_directory, filename)\n    image = cv2.imread(image_path, cv2.IMREAD_COLOR)\n\n    if image is not None:\n        # Apply histogram equalization\n        enhanced_image = add_salt_and_pepper_noise(image)\n\n        # Save the enhanced image to the output directory\n        output_path = os.path.join(output_directory, filename)\n        cv2.imwrite(output_path, enhanced_image)\n\nprint(\"Enhanced images saved to:\", output_directory)","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:44:42.4508Z","iopub.execute_input":"2024-05-13T22:44:42.451085Z","iopub.status.idle":"2024-05-13T22:44:52.571139Z","shell.execute_reply.started":"2024-05-13T22:44:42.451061Z","shell.execute_reply":"2024-05-13T22:44:52.570208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Removing noise using Median filter","metadata":{}},{"cell_type":"code","source":"def median1(image):\n    final = cv2.medianBlur(image, 3)\n    return final\n\nfinal = median1(noisy_image)\nplt.figure(figsize=(12, 8))\n\nplt.subplot(2, 2, 1)\nplt.title('noisy Image')\nplt.imshow(noisy_image, cmap='gray')\n\nplt.subplot(2, 2, 2)\nplt.title('cured Image')\nplt.imshow(final, cmap='gray')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:44:52.572281Z","iopub.execute_input":"2024-05-13T22:44:52.57255Z","iopub.status.idle":"2024-05-13T22:44:53.034581Z","shell.execute_reply.started":"2024-05-13T22:44:52.572528Z","shell.execute_reply":"2024-05-13T22:44:53.033641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Another implementation for median filter","metadata":{}},{"cell_type":"code","source":"def median(noisy_image):\n    m, n = noisy_image.shape \n    new = np.zeros([m, n]) \n\n    for i in range(1, m-1): \n        for j in range(1, n-1): \n            temp = [noisy_image[i-1, j-1], \n                noisy_image[i-1, j], \n                noisy_image[i-1, j + 1], \n                noisy_image[i, j-1], \n                noisy_image[i, j], \n                noisy_image[i, j + 1], \n                noisy_image[i + 1, j-1], \n                noisy_image[i + 1, j], \n                noisy_image[i + 1, j + 1]] \n\n            temp = sorted(temp) \n            new[i, j]= temp[4] \n\n    new = new.astype(np.uint8)\n    return new\n\nnew = median(noisy_image)\n\ncv2.imwrite('new_median_filtered.png', new)\nplt.figure(figsize=(12, 8))\nplt.subplot(2, 2, 1)\nplt.title('noisy Image')\nplt.imshow(noisy_image, cmap='gray')\n\nplt.subplot(2, 2, 2)\nplt.title('cured Image')\nplt.imshow(new, cmap='gray')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:44:53.035885Z","iopub.execute_input":"2024-05-13T22:44:53.036222Z","iopub.status.idle":"2024-05-13T22:44:53.781226Z","shell.execute_reply.started":"2024-05-13T22:44:53.036194Z","shell.execute_reply":"2024-05-13T22:44:53.780228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_directory = '/kaggle/working/pituitary_tumor_impulsive'\n\n# Path to the directory where enhanced images will be saved\noutput_directory = 'Data/pituitary_tumor_impulsive_median'\nos.makedirs(output_directory, exist_ok=True)\n\n# List all files in the input directory\nimage_files = [f for f in os.listdir(input_directory) if os.path.isfile(os.path.join(input_directory, f))]\n\n# Process each image\nfor filename in image_files:\n    # Load the image\n    image_path = os.path.join(input_directory, filename)\n    image = cv2.imread(image_path, cv2.IMREAD_COLOR)\n\n    if image is not None:\n        # Apply histogram equalization\n        enhanced_image = median1(image)\n\n        # Save the enhanced image to the output directory\n        output_path = os.path.join(output_directory, filename)\n        cv2.imwrite(output_path, enhanced_image)\n\nprint(\"Enhanced images saved to:\", output_directory)","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:44:53.782534Z","iopub.execute_input":"2024-05-13T22:44:53.782852Z","iopub.status.idle":"2024-05-13T22:44:56.373111Z","shell.execute_reply.started":"2024-05-13T22:44:53.782826Z","shell.execute_reply":"2024-05-13T22:44:56.372213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.efficientnet import preprocess_input\n\n# Parameters\nIMG_SIZE = (256, 256)\nBATCH_SIZE = 64\nDATA_DIR = '/kaggle/working/Data'\n\n# Use ImageDataGenerator for data augmentation\ndatagen = ImageDataGenerator(\n    preprocessing_function=preprocess_input, \n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    horizontal_flip=True,\n    validation_split=0.2  # Split 20% of data for validation\n)\n\ntrain_gen = datagen.flow_from_directory(\n    DATA_DIR,\n    target_size=IMG_SIZE,\n    batch_size=BATCH_SIZE,\n    class_mode='sparse',\n    subset='training'\n)\n\nval_gen = datagen.flow_from_directory(\n    DATA_DIR,\n    target_size=IMG_SIZE,\n    batch_size=BATCH_SIZE,\n    class_mode='sparse',\n    subset='validation'\n)","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:44:56.374382Z","iopub.execute_input":"2024-05-13T22:44:56.374666Z","iopub.status.idle":"2024-05-13T22:44:56.523164Z","shell.execute_reply.started":"2024-05-13T22:44:56.374642Z","shell.execute_reply":"2024-05-13T22:44:56.522253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import EfficientNetB2\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Flatten, Dense, Dropout\nfrom tensorflow.keras.callbacks import LearningRateScheduler\n\nbase_model = EfficientNetB2(weights='imagenet', include_top=False, input_shape=(256, 256, 3))\n\nx = base_model.output\nx = Flatten()(x)\nx = Dense(1024, activation='relu')(x)\nx = Dropout(0.5)(x)\npredictions = Dense(4, activation='softmax')(x)\n\nmodel = Model(inputs=base_model.input, outputs=predictions)\n\n# Optionally, freeze the layers of the base model for initial training\nfor layer in base_model.layers:\n    layer.trainable = False\n\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:44:56.524406Z","iopub.execute_input":"2024-05-13T22:44:56.526041Z","iopub.status.idle":"2024-05-13T22:45:00.763817Z","shell.execute_reply.started":"2024-05-13T22:44:56.526006Z","shell.execute_reply":"2024-05-13T22:45:00.763029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import LearningRateScheduler\n\nEPOCHS = 10\ninitial_lr = 0.001\n\n# Define the learning rate schedule function\ndef lr_schedule(epoch, lr):\n    if epoch % 5 == 0 and epoch != 0:\n        return lr * 0.5\n    else:\n        return lr\n\n# Create the LearningRateScheduler callback\nlr_callback = LearningRateScheduler(lr_schedule, verbose=1)\n\ncallbacks = [\n    EarlyStopping(monitor='val_loss', patience=3),\n    lr_callback,\n#     ModelCheckpoint('best_model.h5', monitor='val_loss', save_best_only=True)\n]\nhistory = model.fit(\n    train_gen,\n    validation_data=val_gen,\n    epochs=EPOCHS,\n    callbacks=callbacks\n)","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:48:12.239015Z","iopub.execute_input":"2024-05-13T22:48:12.239916Z","iopub.status.idle":"2024-05-13T22:55:38.320973Z","shell.execute_reply.started":"2024-05-13T22:48:12.239883Z","shell.execute_reply":"2024-05-13T22:55:38.320139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 4))\n\nplt.subplot(1, 2, 1)\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.legend()\nplt.title('Loss')\n\nplt.subplot(1, 2, 2)\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.legend()\nplt.title('Accuracy')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:55:43.765561Z","iopub.execute_input":"2024-05-13T22:55:43.766269Z","iopub.status.idle":"2024-05-13T22:55:44.382335Z","shell.execute_reply.started":"2024-05-13T22:55:43.766237Z","shell.execute_reply":"2024-05-13T22:55:44.381386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport seaborn as sns\n\n# Obtain the true labels from the validation generator\ntrue_labels = val_gen.classes[val_gen.index_array]\n\n# Predict the classes using the model\npredictions = model.predict(val_gen, steps=val_gen.n // val_gen.batch_size + 1)\npredicted_classes = np.argmax(predictions, axis=1)\nprint(predictions[:5])\n\n# Compute the confusion matrix\ncm = confusion_matrix(true_labels, predicted_classes)\n\n# Visualize the confusion matrix\nplt.figure(figsize=(10, 7))\nsns.heatmap(cm, annot=True, fmt='g', cmap='Blues',\n            xticklabels=val_gen.class_indices, \n            yticklabels=val_gen.class_indices)\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-13T22:55:49.530136Z","iopub.execute_input":"2024-05-13T22:55:49.530983Z","iopub.status.idle":"2024-05-13T22:56:15.143985Z","shell.execute_reply.started":"2024-05-13T22:55:49.53095Z","shell.execute_reply":"2024-05-13T22:56:15.143045Z"},"trusted":true},"execution_count":null,"outputs":[]}]}