{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.15","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[{"sourceId":11848,"databundleVersionId":862157,"sourceType":"competition"},{"sourceId":180709,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":153994,"modelId":176475}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport tensorflow as tf\nimport random\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Layer\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, GlobalMaxPooling2D, Dense, Conv2D\nfrom tensorflow.keras.initializers import lecun_normal\nfrom tensorflow.keras.utils import register_keras_serializable\n\n\n# Set global seeds for reproducibility\nSEED = 42\ntf.random.set_seed(SEED)\nnp.random.seed(SEED)\nrandom.seed(SEED)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T18:16:11.372023Z","iopub.execute_input":"2024-11-27T18:16:11.372675Z","iopub.status.idle":"2024-11-27T18:16:27.585466Z","shell.execute_reply.started":"2024-11-27T18:16:11.372641Z","shell.execute_reply":"2024-11-27T18:16:27.584686Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(tf.__version__)  # Should output 2.15.1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T18:16:27.586950Z","iopub.execute_input":"2024-11-27T18:16:27.587436Z","iopub.status.idle":"2024-11-27T18:16:27.591542Z","shell.execute_reply.started":"2024-11-27T18:16:27.587405Z","shell.execute_reply":"2024-11-27T18:16:27.590889Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load data","metadata":{}},{"cell_type":"code","source":"test_images = '/kaggle/input/histopathologic-cancer-detection/test/'\n\ntest_df = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/sample_submission.csv')\n\ntest_df['id'] = test_df['id'] + '.tif'\ntest_df['label'] = test_df['label'].astype(str)\n\nprint('Test Set Size:', test_df.shape)\ntest_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T18:16:27.592279Z","iopub.execute_input":"2024-11-27T18:16:27.592496Z","iopub.status.idle":"2024-11-27T18:16:27.800860Z","shell.execute_reply.started":"2024-11-27T18:16:27.592473Z","shell.execute_reply":"2024-11-27T18:16:27.800239Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# View Image Sample","metadata":{}},{"cell_type":"code","source":"# Sample 16 images and labels from the training set\nsample_images = test_df.sample(16)\n\n# Set up the figure and axes\nfig, axes = plt.subplots(4, 4, figsize=(6, 6))\nfig.tight_layout(pad=1.0)\n\n# Loop through the images and display each one with its label\nfor i, ax in enumerate(axes.flat):\n    # Get the filename and label for each sample\n    id = sample_images.iloc[i]['id']  \n    label = sample_images.iloc[i]['label']  \n\n    # Load the image from file\n    img = mpimg.imread(os.path.join(test_images, id))\n\n    # Display the image\n    ax.imshow(img, cmap='gray')\n    ax.set_title(f\"Label: {label}\")\n    ax.axis('off')\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T18:16:27.802350Z","iopub.execute_input":"2024-11-27T18:16:27.802695Z","iopub.status.idle":"2024-11-27T18:16:28.871265Z","shell.execute_reply.started":"2024-11-27T18:16:27.802670Z","shell.execute_reply":"2024-11-27T18:16:28.870396Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Create data generator and loader","metadata":{}},{"cell_type":"code","source":"def standardize(image):\n    # Standardize pixel values to mean=0, std=1\n    mean = np.mean(image, axis=(0, 1, 2), keepdims=True)  # Compute mean across all channels\n    std = np.std(image, axis=(0, 1, 2), keepdims=True)    # Compute std across all channels\n    return (image - mean) / (std + 1e-7)                  # Subtract mean and divide by std\n\n# Data generator\ntest_datagen = ImageDataGenerator(\n    rescale=1/255,\n    preprocessing_function=standardize  # Apply standardization\n) # Normalize pixel values\n\n# Data loader\ntest_loader = test_datagen.flow_from_dataframe(\n    dataframe = test_df,\n    directory = test_images,\n    x_col = 'id',\n    y_col = 'label',\n    batch_size = 64,\n    seed = 1,\n    shuffle = False,\n    class_mode = 'categorical',\n    target_size = (96,96)\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T18:16:28.872167Z","iopub.execute_input":"2024-11-27T18:16:28.872405Z","iopub.status.idle":"2024-11-27T18:18:39.760270Z","shell.execute_reply.started":"2024-11-27T18:16:28.872380Z","shell.execute_reply":"2024-11-27T18:18:39.759034Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Create CBAM object","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.utils import register_keras_serializable\n\n@register_keras_serializable(package=\"Custom\")\nclass CBAM(Layer):\n    def __init__(self, channels, reduction_ratio=16, **kwargs):\n        super(CBAM, self).__init__(**kwargs)\n        self.channels = channels\n        self.reduction_ratio = reduction_ratio\n\n        # Channel attention layers\n        self.global_avg_pool = GlobalAveragePooling2D()\n        self.global_max_pool = GlobalMaxPooling2D()\n        self.fc1 = Dense(\n            units=channels // reduction_ratio,\n            activation='selu',  # Use SELU for scaled exponential units\n            kernel_initializer=lecun_normal()\n        )\n        self.fc2 = Dense(\n            units=channels,\n            activation='sigmoid',  # Scale attention values\n            kernel_initializer=lecun_normal()\n        )\n\n        # Spatial attention layers\n        self.conv = Conv2D(\n            filters=1,\n            kernel_size=7,\n            padding='same',\n            activation='sigmoid',  # Scale attention values\n            kernel_initializer=lecun_normal()\n        )\n\n    def call(self, inputs, **kwargs):\n        # Channel attention mechanism\n        avg_pooled = self.global_avg_pool(inputs)\n        max_pooled = self.global_max_pool(inputs)\n        avg_fc = self.fc2(self.fc1(avg_pooled))\n        max_fc = self.fc2(self.fc1(max_pooled))\n        channel_attention = avg_fc + max_fc\n        channel_attention = tf.expand_dims(channel_attention, axis=1)\n        channel_attention = tf.expand_dims(channel_attention, axis=1)\n        channel_refined = inputs * channel_attention\n\n        # Spatial attention mechanism\n        avg_spatial = tf.reduce_mean(channel_refined, axis=-1, keepdims=True)\n        max_spatial = tf.reduce_max(channel_refined, axis=-1, keepdims=True)\n        spatial_attention = self.conv(tf.concat([avg_spatial, max_spatial], axis=-1))\n        spatial_refined = channel_refined * spatial_attention\n\n        return spatial_refined\n\n    def get_config(self):\n        config = super(CBAM, self).get_config()\n        config.update({\n            \"channels\": self.channels,\n            \"reduction_ratio\": self.reduction_ratio\n        })\n        return config\n\n\n@register_keras_serializable(package=\"Custom\")\nclass CustomAlphaDropout(Layer):\n    def __init__(self, rate, **kwargs):\n        super(CustomAlphaDropout, self).__init__(**kwargs)\n        self.rate = rate\n\n    def call(self, inputs, training=None):\n        # Ensure `training` is explicitly passed or inferred\n        if training is None:\n            training = tf.constant(False)  # Default to inference mode if not specified\n\n        if not training:\n            return inputs\n\n        # Alpha Dropout logic\n        noise_shape = tf.shape(inputs)\n        keep_prob = 1 - self.rate\n        random_tensor = keep_prob + tf.random.uniform(noise_shape, 0, 1)\n        binary_tensor = tf.floor(random_tensor)\n        outputs = inputs * binary_tensor / keep_prob\n        return outputs\n\n    def compute_output_shape(self, input_shape):\n        # Output shape is the same as input shape\n        return input_shape\n\n    def get_config(self):\n        # Save rate parameter for serialization\n        config = super(CustomAlphaDropout, self).get_config()\n        config.update({\"rate\": self.rate})\n        return config","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T18:18:39.762029Z","iopub.execute_input":"2024-11-27T18:18:39.762344Z","iopub.status.idle":"2024-11-27T18:18:39.777321Z","shell.execute_reply.started":"2024-11-27T18:18:39.762317Z","shell.execute_reply":"2024-11-27T18:18:39.776467Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load model","metadata":{}},{"cell_type":"code","source":"cnn = keras.models.load_model('/kaggle/input/4conv2d-w-selu-v2/keras/default/1/cbam_selu (2).keras', custom_objects={'CBAM': CBAM, \"CustomAlphaDropout\": CustomAlphaDropout})\ncnn.summary()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T18:18:39.778268Z","iopub.execute_input":"2024-11-27T18:18:39.778525Z","iopub.status.idle":"2024-11-27T18:18:46.767864Z","shell.execute_reply.started":"2024-11-27T18:18:39.778488Z","shell.execute_reply":"2024-11-27T18:18:46.766789Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Predict test images","metadata":{}},{"cell_type":"code","source":"test_preds = cnn.predict(test_loader)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T18:18:46.768992Z","iopub.execute_input":"2024-11-27T18:18:46.769277Z","iopub.status.idle":"2024-11-27T18:24:40.216467Z","shell.execute_reply.started":"2024-11-27T18:18:46.769250Z","shell.execute_reply":"2024-11-27T18:24:40.215271Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Save predictions to csv","metadata":{}},{"cell_type":"code","source":"# Convert probabilities to predicted class labels\npredicted_labels = np.argmax(test_preds, axis=1)\n\nsubmission_df = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/sample_submission.csv')\n\n# Create the submission DataFrame using filenames from test_df\nsubmission = pd.DataFrame({'id': submission_df['id'], 'label': predicted_labels})\n\n# Save the DataFrame to a CSV file for submission\nsubmission.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T18:24:40.218042Z","iopub.execute_input":"2024-11-27T18:24:40.218380Z","iopub.status.idle":"2024-11-27T18:24:40.363015Z","shell.execute_reply.started":"2024-11-27T18:24:40.218349Z","shell.execute_reply":"2024-11-27T18:24:40.361721Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T18:24:40.365475Z","iopub.execute_input":"2024-11-27T18:24:40.365813Z","iopub.status.idle":"2024-11-27T18:24:40.374299Z","shell.execute_reply.started":"2024-11-27T18:24:40.365782Z","shell.execute_reply":"2024-11-27T18:24:40.373441Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Calculate frequency distribution\nfrequency_distribution = (submission.label.value_counts() / len(submission)).to_frame()\n\n# Plotting the frequency distribution as a bar chart\nplt.figure(figsize=(6, 4))\ncolors = ['lightgreen', 'lightcoral']  # light green for benign, light red for malignant\n\n# Plotting bar chart with specified colors\nfrequency_distribution.iloc[:, 0].plot(kind='bar', color=colors)\n\n# Customizing chart\nplt.title('Frequency Distribution of Labels')\nplt.xlabel('Label')\nplt.ylabel('Frequency')\nplt.xticks([0, 1], ['Benign', 'Malignant'], rotation=0)\n\n# Show the plot\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-27T18:24:40.375297Z","iopub.execute_input":"2024-11-27T18:24:40.375576Z","iopub.status.idle":"2024-11-27T18:24:40.506217Z","shell.execute_reply.started":"2024-11-27T18:24:40.375550Z","shell.execute_reply":"2024-11-27T18:24:40.505288Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}