{"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":45867,"databundleVersionId":6924515,"sourceType":"competition"}],"dockerImageVersionId":30674,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\nimport warnings\nwarnings.filterwarnings('ignore')\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-06T05:53:56.000609Z","iopub.execute_input":"2024-04-06T05:53:56.001519Z","iopub.status.idle":"2024-04-06T05:53:57.432617Z","shell.execute_reply.started":"2024-04-06T05:53:56.001486Z","shell.execute_reply":"2024-04-06T05:53:57.431688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/UBC-OCEAN/train.csv\")\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:53:57.434577Z","iopub.execute_input":"2024-04-06T05:53:57.435029Z","iopub.status.idle":"2024-04-06T05:53:57.468767Z","shell.execute_reply.started":"2024-04-06T05:53:57.435003Z","shell.execute_reply":"2024-04-06T05:53:57.467882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_train = \"/kaggle/input/UBC-OCEAN/train_images\"\npath_test = \"/kaggle/input/UBC-OCEAN/test_images\"\ntrain_folder = os.listdir(path_train)\ntest_folder = os.listdir(path_test)\n\nprint(len(train_folder))\nprint(len(test_folder))","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:53:57.469964Z","iopub.execute_input":"2024-04-06T05:53:57.470291Z","iopub.status.idle":"2024-04-06T05:53:57.476732Z","shell.execute_reply.started":"2024-04-06T05:53:57.470263Z","shell.execute_reply":"2024-04-06T05:53:57.475792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Images in small size\npath_train_copy = \"/kaggle/input/UBC-OCEAN/train_thumbnails\"\npath_test_copy = \"/kaggle/input/UBC-OCEAN/test_thumbnails\"\ntrain_folder_copy = os.listdir(path_train_copy)\ntest_folder_copy = os.listdir(path_test_copy)\n\nprint(len(train_folder_copy))\nprint(len(test_folder_copy))","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:53:57.477933Z","iopub.execute_input":"2024-04-06T05:53:57.478188Z","iopub.status.idle":"2024-04-06T05:53:57.485213Z","shell.execute_reply.started":"2024-04-06T05:53:57.478162Z","shell.execute_reply":"2024-04-06T05:53:57.484436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Image id >> Tissue Microarray\ntrain_df_tma = train_df[train_df['is_tma']==True]","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:53:57.488271Z","iopub.execute_input":"2024-04-06T05:53:57.488809Z","iopub.status.idle":"2024-04-06T05:53:57.49506Z","shell.execute_reply.started":"2024-04-06T05:53:57.488785Z","shell.execute_reply":"2024-04-06T05:53:57.494191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_tma","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:53:57.496281Z","iopub.execute_input":"2024-04-06T05:53:57.496523Z","iopub.status.idle":"2024-04-06T05:53:57.512917Z","shell.execute_reply.started":"2024-04-06T05:53:57.496502Z","shell.execute_reply":"2024-04-06T05:53:57.51206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_no_tma = train_df[train_df['is_tma']==False]\ntrain_df_no_tma","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:53:57.51394Z","iopub.execute_input":"2024-04-06T05:53:57.514192Z","iopub.status.idle":"2024-04-06T05:53:57.529732Z","shell.execute_reply.started":"2024-04-06T05:53:57.514171Z","shell.execute_reply":"2024-04-06T05:53:57.528749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_no_tma['image_id_path'] = [f\"{i}_thumbnail.png\" for i in train_df_no_tma['image_id']]","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:53:57.53091Z","iopub.execute_input":"2024-04-06T05:53:57.531162Z","iopub.status.idle":"2024-04-06T05:53:57.538181Z","shell.execute_reply.started":"2024-04-06T05:53:57.53114Z","shell.execute_reply":"2024-04-06T05:53:57.537351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_no_tma","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:53:57.539231Z","iopub.execute_input":"2024-04-06T05:53:57.539568Z","iopub.status.idle":"2024-04-06T05:53:57.552613Z","shell.execute_reply.started":"2024-04-06T05:53:57.539536Z","shell.execute_reply":"2024-04-06T05:53:57.551805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_tma['image_id_path'] = [f\"{i}.png\" for i in train_df_tma['image_id']]","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:53:57.553562Z","iopub.execute_input":"2024-04-06T05:53:57.553897Z","iopub.status.idle":"2024-04-06T05:53:57.562925Z","shell.execute_reply.started":"2024-04-06T05:53:57.553874Z","shell.execute_reply":"2024-04-06T05:53:57.562062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_tma","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:53:57.563871Z","iopub.execute_input":"2024-04-06T05:53:57.564175Z","iopub.status.idle":"2024-04-06T05:53:57.581015Z","shell.execute_reply.started":"2024-04-06T05:53:57.564145Z","shell.execute_reply":"2024-04-06T05:53:57.580111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image \nimport cv2\nimage_data = []\nimage_label = []\npath = \"/kaggle/input/UBC-OCEAN/train_thumbnails\"\npath1 = \"/kaggle/input/UBC-OCEAN/train_images/\"\n\nfor img, label in zip(train_df_no_tma['image_id_path'], train_df_no_tma['label']):\n  image = Image.open(path + \"/\" + img)\n  image = image.resize((224, 224))  # Consistent resize\n  image = np.array(image)\n  image= cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)\n  \n  image_data.append(image)\n  image_label.append(label)\n\nfor img, label in zip(train_df_tma['image_id_path'], train_df_tma['label']):\n  image = Image.open(path1 + \"/\" + img)\n  image = image.resize((224, 224))  # Consistent resize\n  image = np.array(image)\n  image= cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)\n  \n  image_data.append(image)\n  image_label.append(label)\n\n# Now you can convert the lists to NumPy arrays\nx = np.array(image_data)\ny = np.array(image_label)","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:53:57.582083Z","iopub.execute_input":"2024-04-06T05:53:57.582384Z","iopub.status.idle":"2024-04-06T05:56:53.953557Z","shell.execute_reply.started":"2024-04-06T05:53:57.582361Z","shell.execute_reply":"2024-04-06T05:56:53.95258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report , confusion_matrix , accuracy_score , auc\nfrom sklearn.model_selection import train_test_split\n","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:56:53.954919Z","iopub.execute_input":"2024-04-06T05:56:53.955341Z","iopub.status.idle":"2024-04-06T05:56:55.104271Z","shell.execute_reply.started":"2024-04-06T05:56:53.955305Z","shell.execute_reply":"2024-04-06T05:56:55.103267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_label_1 = []\nfor i in image_label:\n    if i==\"CC\":\n        image_label_1.append(0)\n    elif i==\"EC\":\n        image_label_1.append(1)\n    elif i==\"HGSC\":\n        image_label_1.append(2)\n    elif i==\"LGSC\":\n        image_label_1.append(3)\n    elif i==\"MC\":\n        image_label_1.append(4)","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:56:55.107886Z","iopub.execute_input":"2024-04-06T05:56:55.108287Z","iopub.status.idle":"2024-04-06T05:56:55.114085Z","shell.execute_reply.started":"2024-04-06T05:56:55.108261Z","shell.execute_reply":"2024-04-06T05:56:55.113138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_label_1[:5]","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:56:55.115098Z","iopub.execute_input":"2024-04-06T05:56:55.115344Z","iopub.status.idle":"2024-04-06T05:56:55.12858Z","shell.execute_reply.started":"2024-04-06T05:56:55.115323Z","shell.execute_reply":"2024-04-06T05:56:55.127708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_data = []\npath = \"/kaggle/input/UBC-OCEAN/test_thumbnails\"\n#path1 = \"/kaggle/input/UBC-OCEAN/test_images\"\n\ntest_thumbnail_folder = os.listdir(path)\n\nfor img in test_thumbnail_folder:\n    image = Image.open(\"/kaggle/input/UBC-OCEAN/test_thumbnails/\"+img)\n    image = image.resize((224,224))\n    image = np.array(image)\n    image= cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)\n    image_data.append(image)\n    test_image_data.append(image)","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:56:55.129815Z","iopub.execute_input":"2024-04-06T05:56:55.130084Z","iopub.status.idle":"2024-04-06T05:56:55.404138Z","shell.execute_reply.started":"2024-04-06T05:56:55.130061Z","shell.execute_reply":"2024-04-06T05:56:55.403319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_test_array = np.array(test_image_data)\n\n\n## Scale The Test Images\npredict_test_array_scaled = predict_test_array/128","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:56:55.405569Z","iopub.execute_input":"2024-04-06T05:56:55.406047Z","iopub.status.idle":"2024-04-06T05:56:55.410792Z","shell.execute_reply.started":"2024-04-06T05:56:55.406011Z","shell.execute_reply":"2024-04-06T05:56:55.409893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Print the shapes of x and y before splitting\nprint(\"Shape of x:\", x.shape)\nprint(\"Shape of y:\", y.shape)\n\nx_train , x_test, y_train, y_test = train_test_split(x,y,test_size=0.10,shuffle=True)\nprint(x_train.shape)\nprint(x_test.shape)\nprint(y_train.shape)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:56:55.412109Z","iopub.execute_input":"2024-04-06T05:56:55.412449Z","iopub.status.idle":"2024-04-06T05:56:55.432791Z","shell.execute_reply.started":"2024-04-06T05:56:55.412417Z","shell.execute_reply":"2024-04-06T05:56:55.431917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nfrom sklearn.preprocessing import OneHotEncoder\n\nplt.figure(figsize=(12, 16))\nclass_labels = ['CC', 'EC', 'HGSC', 'LGSC', 'MC']\n\n# Assuming y_train is not already one-hot encoded\nencoder = OneHotEncoder(sparse=False)  # Set sparse=False for dense output\ny_train_encoded = encoder.fit_transform(y_train.reshape(-1, 1))  # Reshape for 2D\n\nfor i in range(12):\n  plt.subplot(4, 3, i + 1)\n  plt.imshow(x_train[i])\n\n  # Access label using one-hot encoded index (assuming first element)\n  label_index = np.argmax(y_train_encoded[i])  # Get index of maximum value\n  label = class_labels[label_index]  # Retrieve label from class_labels\n\n  plt.title(f\"Label:{label}\")","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:56:55.433942Z","iopub.execute_input":"2024-04-06T05:56:55.434278Z","iopub.status.idle":"2024-04-06T05:56:58.642479Z","shell.execute_reply.started":"2024-04-06T05:56:55.434248Z","shell.execute_reply":"2024-04-06T05:56:58.641332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_scaled = x_train/128\nx_test_scaled = x_test/128","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:56:58.643685Z","iopub.execute_input":"2024-04-06T05:56:58.644138Z","iopub.status.idle":"2024-04-06T05:56:58.715416Z","shell.execute_reply.started":"2024-04-06T05:56:58.644112Z","shell.execute_reply":"2024-04-06T05:56:58.714573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train_scaled.shape)\nprint(x_test_scaled.shape)\nprint(len(x_train_scaled))","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:56:58.716488Z","iopub.execute_input":"2024-04-06T05:56:58.716796Z","iopub.status.idle":"2024-04-06T05:56:58.722146Z","shell.execute_reply.started":"2024-04-06T05:56:58.716771Z","shell.execute_reply":"2024-04-06T05:56:58.721193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\ncheckpointer = tf.keras.callbacks.ModelCheckpoint(filepath='model_for_nuclei.keras',\n                                                 verbose=1,\n                                                 save_best_only=True)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:57:43.799502Z","iopub.execute_input":"2024-04-06T05:57:43.799911Z","iopub.status.idle":"2024-04-06T05:57:43.80473Z","shell.execute_reply.started":"2024-04-06T05:57:43.799879Z","shell.execute_reply":"2024-04-06T05:57:43.803795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''def train_and_evaluate(params):\n    model = create_dlunet_model(params)\n\n  # Compile the model (assuming you have defined optimizer, loss and metrics in create_dlunet_model)\n    model.compile()\n\n  # Train the model with early stopping (replace epochs with your desired value)\n    history = model.fit(x_train_scaled, y_train_encoded, validation_split=0.1, epochs=10, callbacks=[EarlyStopping(patience=2)])\n\n  # Access validation loss from history (assuming 'val_loss' is the validation metric)\n    val_loss = history.history['val_loss'][-1]\n\n    return val_loss  # Return validation loss for comparison'''","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:57:48.479504Z","iopub.execute_input":"2024-04-06T05:57:48.479888Z","iopub.status.idle":"2024-04-06T05:57:48.486691Z","shell.execute_reply.started":"2024-04-06T05:57:48.479856Z","shell.execute_reply":"2024-04-06T05:57:48.485499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''search_space = {\n  'filters': hp.choice('filters', [16, 32, 64, 128]),  # Number of filters in convolutional layers\n  'learning_rate': hp.loguniform('learning_rate', -6, -3),  # Learning rate for optimizer\n  'num_layers': hp.choice('num_layers', [3, 4, 5]),  # Number of convolutional layers per block (encoder/decoder)\n}\n\nbest_params = fmin(train_and_evaluate, space=search_space, algo=hp.randsearch)'''","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:59:07.065546Z","iopub.execute_input":"2024-04-06T05:59:07.06629Z","iopub.status.idle":"2024-04-06T05:59:07.072144Z","shell.execute_reply.started":"2024-04-06T05:59:07.066259Z","shell.execute_reply":"2024-04-06T05:59:07.071266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''model = dlb_Unet(best_params)\nmodel.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])\nmodel.summary()'''","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:59:11.879178Z","iopub.execute_input":"2024-04-06T05:59:11.880018Z","iopub.status.idle":"2024-04-06T05:59:11.885818Z","shell.execute_reply.started":"2024-04-06T05:59:11.879985Z","shell.execute_reply":"2024-04-06T05:59:11.884851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder\n\n# Assuming y_train is not already one-hot encoded\nencoder = OneHotEncoder(sparse=False)  # Set sparse=False for dense output\ny_train_encoded = encoder.fit_transform(y_train.reshape(-1, 1))  # Reshape for 2D","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:59:15.484823Z","iopub.execute_input":"2024-04-06T05:59:15.485628Z","iopub.status.idle":"2024-04-06T05:59:15.492918Z","shell.execute_reply.started":"2024-04-06T05:59:15.485597Z","shell.execute_reply":"2024-04-06T05:59:15.491961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Model\nfrom keras.layers import concatenate\nfrom keras.layers import GlobalAveragePooling2D\nfrom keras.layers import Activation, Conv2D, MaxPooling2D, UpSampling2D, Input, Dropout\nfrom kerastuner import HyperModel\nfrom kerastuner.tuners import RandomSearch\nfrom keras.optimizers import Adam\n\nclass DLB_UNetHyperModel(HyperModel):\n    def __init__(self, input_shape, num_classes):\n        self.input_shape = input_shape\n        self.num_classes = num_classes\n\n    def build(self, hp):\n        merge_axis = -1\n        data = Input(shape=self.input_shape)\n\n        # convolution layer 1\n        conv1 = Conv2D(hp.Int('conv1_filters', 32, 256, step=32), kernel_size=3, activation='relu', padding='same')(data)\n        conv1 = Dropout(hp.Float('conv1_dropout', 0.0, 0.5, step=0.1))(conv1)\n        conv1 = Conv2D(hp.Int('conv1_filters', 32, 256, step=32), kernel_size=3, activation='relu', padding='same')(conv1)\n        pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)\n\n        # convolution layer 2\n        conv2 = Conv2D(hp.Int('conv2_filters', 64, 512, step=64), kernel_size=3, activation='relu', padding='same')(pool1)\n        conv2 = Dropout(hp.Float('conv2_dropout', 0.0, 0.5, step=0.1))(conv2)\n        conv2 = Conv2D(hp.Int('conv2_filters', 64, 512, step=64), kernel_size=3, activation='relu', padding='same')(conv2)\n        pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)\n\n        # convolution layer 3\n        conv3 = Conv2D(hp.Int('conv3_filters', 128, 1024, step=128), kernel_size=3, activation='relu', padding='same')(pool2)\n        conv3 = Dropout(hp.Float('conv3_dropout', 0.0, 0.5, step=0.1))(conv3)\n        conv3 = Conv2D(hp.Int('conv3_filters', 128, 1024, step=128), kernel_size=3, activation='relu', padding='same')(conv3)\n        pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)\n\n        # convolution layer 4\n        conv4 = Conv2D(hp.Int('conv4_filters', 256, 2048, step=256), kernel_size=3, activation='relu', padding='same')(pool3)\n        conv4 = Dropout(hp.Float('conv4_dropout', 0.0, 0.5, step=0.1))(conv4)\n        conv4 = Conv2D(hp.Int('conv4_filters', 256, 2048, step=256), kernel_size=3, activation='relu', padding='same')(conv4)\n        pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)\n\n        ################################################################\n        conv5 = Conv2D(1024, kernel_size=3, padding='same', activation='relu')(pool4)\n\n        ################################################################\n\n        up1 = UpSampling2D(size=(2, 2))(conv5)\n\n        # deconvolution layer 1\n        conv6 = Conv2D(512, kernel_size=3, padding='same', activation='relu')(up1)\n        conv6 = Conv2D(512, kernel_size=3, padding='same', activation='relu')(conv6)\n        merged1 = concatenate([conv4, conv6], axis=merge_axis)\n        conv6 = Conv2D(512, kernel_size=3, padding='same', activation='relu')(merged1)\n\n        up2 = UpSampling2D(size=(2, 2))(conv6)\n\n        # deconvolution layer 2\n        conv7 = Conv2D(256, kernel_size=3, padding='same', activation='relu')(up2)\n        conv7 = Conv2D(256, kernel_size=3, padding='same', activation='relu')(conv7)\n        merged2 = concatenate([conv3, conv7], axis=merge_axis)\n        conv7 = Conv2D(256, kernel_size=3, padding='same', activation='relu')(merged2)\n\n        up3 = UpSampling2D(size=(2, 2))(conv7)\n\n        # deconvolution layer 3\n        conv8 = Conv2D(128, kernel_size=3, padding='same', activation='relu')(up3)\n        conv8 = Conv2D(128, kernel_size=3, padding='same', activation='relu')(conv8)\n        merged3 = concatenate([conv2, conv8], axis=merge_axis)\n        conv8 = Conv2D(128, kernel_size=3, padding='same', activation='relu')(merged3)\n\n        up4 = UpSampling2D(size=(2, 2))(conv8)\n\n        # deconvolution layer 4\n        conv9 = Conv2D(64, kernel_size=3, padding='same', activation='relu')(up4)\n        conv9 = Conv2D(64, kernel_size=3, padding='same', activation='relu')(conv9)\n        merged4 = concatenate([conv1, conv9], axis=merge_axis)\n        conv9 = Conv2D(64, kernel_size=3, padding='same', activation='relu')(merged4)\n\n        conv10 = Conv2D(self.num_classes, 1, activation='softmax')(conv9)\n        output = GlobalAveragePooling2D()(conv10)\n\n        model = Model(inputs=data, outputs=output)\n        \n        # Tuning the learning rate\n        lr = hp.Float('learning_rate', min_value=1e-4, max_value=1e-2, sampling='LOG')\n        model.compile(optimizer=Adam(learning_rate=lr), loss='categorical_crossentropy', metrics=['accuracy'])\n\n        return model\n\ninput_shape = (224, 224, 1)\nnum_classes = 5\n\nhypermodel = DLB_UNetHyperModel(input_shape=input_shape, num_classes=num_classes)\n\ntuner = RandomSearch(\n    hypermodel,\n    objective='val_accuracy',\n    max_trials=10,\n    directory='dlb_unet_tuning',\n    project_name='dlb_unet_hyperparams'\n)\n\n# Print the shape of x_train_scaled before reshaping\nprint(\"Original shape of x_train_scaled:\", x_train_scaled.shape)\n\n# Reshape x_train_scaled to match the input shape of the model\nx_train_scaled_reshaped = x_train_scaled.reshape((-1, 224, 224, 1))\n\ntuner.search(x_train_scaled_reshaped, y_train_encoded, epochs=25, validation_split=0.1)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-06T05:59:18.831256Z","iopub.execute_input":"2024-04-06T05:59:18.831613Z","iopub.status.idle":"2024-04-06T07:55:06.27507Z","shell.execute_reply.started":"2024-04-06T05:59:18.831583Z","shell.execute_reply":"2024-04-06T07:55:06.274184Z"},"trusted":true},"execution_count":null,"outputs":[]}]}