{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":10338,"databundleVersionId":862042,"sourceType":"competition"},{"sourceId":6325612,"sourceType":"datasetVersion","datasetId":3640638}],"dockerImageVersionId":30498,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import warnings\nfrom sklearn.exceptions import ConvergenceWarning\nwarnings.filterwarnings(\"ignore\", category=ConvergenceWarning)\nwarnings.simplefilter(action='ignore', category=FutureWarning)\nwarnings.simplefilter(action='ignore', category=UserWarning)","metadata":{"_uuid":"ad3009c0-65be-4101-9e30-a65bc9ca13fd","_cell_guid":"0bf16d4c-aaae-4498-8993-23b0ed2975e6","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-07-22T16:30:10.318231Z","iopub.execute_input":"2025-07-22T16:30:10.318574Z","iopub.status.idle":"2025-07-22T16:30:10.770238Z","shell.execute_reply.started":"2025-07-22T16:30:10.318549Z","shell.execute_reply":"2025-07-22T16:30:10.769524Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import random\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport pydicom as dcm\nfrom pathlib import Path\nimport os\nfrom tqdm.notebook import tqdm\nimport cv2\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report\nfrom tensorflow.keras.layers import Layer, Convolution2D, Dense, RandomRotation, RandomFlip, Resizing, Rescaling\nfrom tensorflow.keras.layers import Concatenate, UpSampling2D, Conv2D, Reshape, GlobalAveragePooling2D, GlobalMaxPooling2D\nfrom tensorflow.keras.layers import Dense, Activation, Flatten, Dropout, MaxPooling2D, BatchNormalization\nfrom tensorflow.keras.layers import Input, ReLU, AveragePooling2D, Activation, Flatten\nfrom tensorflow.keras.models import Model, Sequential\nfrom tensorflow.keras.optimizers import Adam, AdamW\nfrom tensorflow.keras import losses, optimizers\nfrom keras import backend as K\nfrom tensorflow.keras.callbacks import Callback, EarlyStopping, ModelCheckpoint, ReduceLROnPlateau","metadata":{"_uuid":"425b5b15-a75c-4791-aee2-6f61db3d8bd7","_cell_guid":"e1dbf62c-a275-4154-bbad-6f47a5afaa52","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-07-22T16:30:10.771946Z","iopub.execute_input":"2025-07-22T16:30:10.772637Z","iopub.status.idle":"2025-07-22T16:30:19.814543Z","shell.execute_reply.started":"2025-07-22T16:30:10.772613Z","shell.execute_reply":"2025-07-22T16:30:19.813694Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_class = pd.read_csv('../input/rsna-pneumonia-detection-challenge/stage_2_detailed_class_info.csv')\ntrain_labels = pd.read_csv('../input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv')\n\ntrain_path = Path('../input/rsna-pneumonia-detection-challenge/stage_2_train_images')\ntest_path = Path('../input/rsna-pneumonia-detection-challenge/stage_2_test_images')\n\ntrain_meta = pd.concat([train_labels, train_class.drop(columns=['patientId'])], axis=1)","metadata":{"_uuid":"edf1feb9-1787-4793-a06b-1461c943fa98","_cell_guid":"18e9fc71-c29f-4dc1-b77d-291b34e5e7ea","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-07-22T16:30:19.815517Z","iopub.execute_input":"2025-07-22T16:30:19.81606Z","iopub.status.idle":"2025-07-22T16:30:19.926306Z","shell.execute_reply.started":"2025-07-22T16:30:19.816037Z","shell.execute_reply":"2025-07-22T16:30:19.92551Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def save_img_from_dcm(dcm_dir, img_dir, patient_id):\n    img_fp = os.path.join(img_dir, \"{}.jpg\".format(patient_id))\n    if os.path.exists(img_fp):\n        return\n    dcm_fp = os.path.join(dcm_dir, \"{}.dcm\".format(patient_id))\n    img_1ch = pydicom.read_file(dcm_fp).pixel_array\n    img_3ch = np.stack([img_1ch]*3, -1)\n\n    img_fp = os.path.join(img_dir, \"{}.jpg\".format(patient_id))\n    cv2.imwrite(img_fp, img_3ch)","metadata":{"_uuid":"38a5cd10-85b1-4846-b8bd-48d291acf5eb","_cell_guid":"bd489acd-2066-44ff-ad19-f26f1b5bd0ec","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-07-22T16:30:19.928307Z","iopub.execute_input":"2025-07-22T16:30:19.928579Z","iopub.status.idle":"2025-07-22T16:30:19.93432Z","shell.execute_reply.started":"2025-07-22T16:30:19.928557Z","shell.execute_reply":"2025-07-22T16:30:19.933397Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\ndef get_image(dcm_file):\n    ADJUSTED_IMAGE_SIZE = 128\n    dcm_data = dcm.read_file(dcm_file)\n    img = dcm_data.pixel_array\n    img = np.stack((img,) * 3, -1)\n\n    img = np.array(img).astype(np.uint8)\n    res = cv2.resize(img,(ADJUSTED_IMAGE_SIZE,ADJUSTED_IMAGE_SIZE), interpolation = cv2.INTER_LINEAR)\n    return res\n\ndef read_train(rowData):\n    imageList = []\n    for index, row in tqdm(rowData.iterrows()):\n        patientId = row.patientId\n        dcm_file = '/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images/'+'{}.dcm'.format(patientId)\n        imageList.append(get_image(dcm_file))\n\n    return np.array(imageList)\n\ndef read_test(path):\n    imageList = []\n    for file_name in tqdm(os.listdir(path)):\n        dcm_file = dcm_file = os.sep.join([path, file_name])\n        imageList.append(get_image(dcm_file))\n    return np.array(imageList)\n        \ntest_images_path = '/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_test_images'\ntest_images = read_test(test_images_path)\n\ntrain_images = read_train(train_labels)\nprint(train_images.shape)","metadata":{"_uuid":"97c227fa-558d-4297-8f80-bae9c2d7f29a","_cell_guid":"2968b5c8-37f5-4631-b901-7e6f35dd1600","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-07-22T16:30:19.935348Z","iopub.execute_input":"2025-07-22T16:30:19.935585Z","iopub.status.idle":"2025-07-22T16:43:06.978192Z","shell.execute_reply.started":"2025-07-22T16:30:19.935565Z","shell.execute_reply":"2025-07-22T16:43:06.977276Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(25,25)) # \nfor i, image in enumerate(train_images[:9]):\n\n    plt.subplot(3,3,i+1)\n    plt.imshow(image)\n    if train_labels.loc[i][\"Target\"]:\n        plt.title(\"Pneumonia\", color=\"red\", fontsize=25)\n    else:\n        plt.title(\"No Pneumonia\", color=\"blue\", fontsize=25)\n    plt.axis('off')\nplt.show()","metadata":{"_uuid":"909e608c-7343-41a6-a563-1ad1863eff94","_cell_guid":"402b521d-3246-40e1-b60c-c3f42b4a1405","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-07-22T16:43:06.979341Z","iopub.execute_input":"2025-07-22T16:43:06.979608Z","iopub.status.idle":"2025-07-22T16:43:07.8496Z","shell.execute_reply.started":"2025-07-22T16:43:06.979586Z","shell.execute_reply":"2025-07-22T16:43:07.848799Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y = pd.get_dummies(train_labels[\"Target\"]).values\nrandom_state = 42\n\nX_train, X_test, y_train, y_test = train_test_split(train_images, y, test_size=0.2, random_state=random_state)","metadata":{"_uuid":"11796185-e352-4155-887a-1a37fd41c0e4","_cell_guid":"d1300941-6d4c-4920-beab-745dbe839b0b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-07-22T16:43:07.850803Z","iopub.execute_input":"2025-07-22T16:43:07.851526Z","iopub.status.idle":"2025-07-22T16:43:08.288029Z","shell.execute_reply.started":"2025-07-22T16:43:07.851495Z","shell.execute_reply":"2025-07-22T16:43:08.287316Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Building CNN Model","metadata":{"_uuid":"90b6e4c8-7fca-4e96-8308-a33713a94326","_cell_guid":"ecb070b6-b6ae-4e8f-a54d-538a04ece6ca","trusted":true}},{"cell_type":"code","source":"\ndef recall_m(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    recall = true_positives / (possible_positives + K.epsilon())\n    return recall\n\ndef precision_m(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    precision = true_positives / (predicted_positives + K.epsilon())\n    return precision\n\ndef f1_m(y_true, y_pred):\n    precision = precision_m(y_true, y_pred)\n    recall = recall_m(y_true, y_pred)\n    return 2*((precision*recall)/(precision+recall+K.epsilon()))","metadata":{"_uuid":"80f31fee-1fed-4282-a812-b879b499cece","_cell_guid":"09c968cd-65e9-4b17-bd33-3ab00566b628","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-07-22T16:43:08.288975Z","iopub.execute_input":"2025-07-22T16:43:08.28923Z","iopub.status.idle":"2025-07-22T16:43:08.295373Z","shell.execute_reply.started":"2025-07-22T16:43:08.28921Z","shell.execute_reply":"2025-07-22T16:43:08.294503Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nADJUSTED_IMAGE_SIZE = 128\ninput_shape = (ADJUSTED_IMAGE_SIZE, ADJUSTED_IMAGE_SIZE, 3)\nnum_classes = y_train.shape[1]\n\nmodel = Sequential()\nmodel.add(RandomRotation(factor=0.15))\nmodel.add(Rescaling(1./255))\nmodel.add(Conv2D(32, (3, 3), input_shape=input_shape)) # (3, 3) - conv kernel\n\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.2))\n\nmodel.add(Conv2D(32, (3, 3)))\n\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.3))\n\nmodel.add(Conv2D(64, (3, 3)))\n\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.4))\n\nmodel.add(Flatten())\nmodel.add(Dense(64))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(num_classes))\nmodel.add(Activation('softmax'))\n\nmodel.compile(loss='categorical_crossentropy',\n              optimizer=AdamW(learning_rate=0.0005),\n              metrics=['accuracy'] # ,f1_m\n             )\n# model.summary()\n\nhistory = model.fit(X_train, \n                  y_train, \n                  epochs = 50, \n                  validation_data = (X_test,y_test),\n                  batch_size = 16,\n                    callbacks=[\n                        EarlyStopping(monitor = \"val_loss\", patience = 10, restore_best_weights = True),\n                        ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=2, mode='min')\n                    ]\n                   )\n\nfcl_loss, fcl_accuracy = model.evaluate(X_test, y_test, verbose=1) # , fcl_f1\nprint('Test loss:', fcl_loss)\nprint('Test accuracy:', fcl_accuracy)\n# print('Test F1:', fcl_f1)\n\ndf = pd.DataFrame({\"pred\": np.argmax(model.predict(X_test), axis=1), \"true\": np.argmax(y_test, axis=1)})\nplt.matshow(confusion_matrix(df[\"true\"], df[\"pred\"]))\nprint(classification_report(df[\"true\"], df[\"pred\"]))","metadata":{"_uuid":"572ff589-dcdf-4e30-9a05-172ea6cde934","_cell_guid":"e2ef77b6-f9c1-48d2-8f6b-3c6f4633f9a4","collapsed":false,"jupyter":{"outputs_hidden":false},"_kg_hide-input":true,"execution":{"iopub.status.busy":"2025-07-22T16:43:08.296584Z","iopub.execute_input":"2025-07-22T16:43:08.29682Z","iopub.status.idle":"2025-07-22T16:49:23.783518Z","shell.execute_reply.started":"2025-07-22T16:43:08.2968Z","shell.execute_reply":"2025-07-22T16:49:23.782179Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nplt.figure(figsize=(16, 8))\n\nplt.plot(history.history['accuracy'], label='Train')\nplt.plot(history.history['val_accuracy'], label='Validation')\nplt.ylabel('Cross Entropy accuracy')\nplt.xlabel('Epoch')\nplt.title('Train accuracy', pad=13, fontsize=25)\nplt.legend(loc='upper right')\nplt.grid(000.1)\n\nplt.show()\n\n\nplt.figure(figsize=(16, 8))\n\nplt.plot(history.history['loss'], label='Train')\nplt.plot(history.history['val_loss'], label='Validation')\nplt.ylabel('Cross Entropy Loss')\nplt.xlabel('Epoch')\nplt.title('Train Loss', pad=13, fontsize=25)\nplt.legend(loc='upper right')\nplt.grid(000.1)\n\nplt.show()","metadata":{"_uuid":"1f43c8e4-92ea-413a-87cf-f0fe72373fc8","_cell_guid":"28f4e44f-70ae-435c-90c8-2f8d6432a3af","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-07-22T16:49:23.78973Z","iopub.execute_input":"2025-07-22T16:49:23.790219Z","iopub.status.idle":"2025-07-22T16:49:24.431964Z","shell.execute_reply.started":"2025-07-22T16:49:23.790175Z","shell.execute_reply":"2025-07-22T16:49:24.431151Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Build transfer-learning model","metadata":{"_uuid":"84961b0b-b351-41b2-9192-075b637e8be7","_cell_guid":"b84b603f-e4fe-418f-9adb-0ce857961c71","trusted":true}},{"cell_type":"code","source":"# potential models: Xception ResNet50V2 InceptionV3 EfficientNetB3 ResNet152 VGG16\nbase_model = tf.keras.applications.VGG16(weights = 'imagenet', include_top = False)\nfor layer in base_model.layers:\n      layer.trainable = False\n\n# Data Augmentation Step\naugment = Sequential([\n#   RandomFlip(\"horizontal\"),\n#     Rescaling(1./255), # pretrained model already include rescaling\n    RandomRotation(0.15)\n], name='AugmentationLayer')\n\ninputs = Input(shape = input_shape, name='inputLayer')\nx = augment(inputs)\npretrain_out = base_model(x, training = False)\nx = Flatten()(pretrain_out)\nx = Dense(64, activation='relu')(x)\nx = Dense(y_train.shape[1], name='outputLayer')(x)\noutputs = Activation(activation=\"softmax\", dtype=tf.float32, name='activationLayer')(x)\nmodel = Model(inputs=inputs, outputs=outputs)\n\nmodel.compile(optimizer=AdamW(learning_rate=0.0005), # \n                   loss=losses.categorical_crossentropy, \n                   metrics=['accuracy']) # ,f1_m\n\nhistory = model.fit(X_train, \n                         y_train, \n                         batch_size=16, \n                         epochs=60, \n                         validation_data=(X_test, y_test),\n                         callbacks=[\n                             EarlyStopping(monitor = \"val_loss\", patience = 15, \n                                           restore_best_weights = True, mode='min'),\n                             ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=2, mode='min')\n                    ]\n                        )\nfcl_loss, fcl_accuracy  = model.evaluate(X_test, y_test, verbose=1) # fcl_f1\nprint('Test loss:', fcl_loss)\nprint('Test accuracy:', fcl_accuracy)\n# print('Test F1:', fcl_f1)\n\n\ndf = pd.DataFrame({\"pred\": np.argmax(model.predict(X_test), axis=1), \"true\": np.argmax(y_test, axis=1)})\nplt.matshow(confusion_matrix(df[\"true\"], df[\"pred\"]))\nprint(classification_report(df[\"true\"], df[\"pred\"]))\nprint(confusion_matrix(df[\"true\"], df[\"pred\"]))","metadata":{"_uuid":"53ad2ee3-0282-4425-81ea-b22ecbfef3dc","_cell_guid":"6aa7419b-fe81-4d2f-b95c-d656d6ea5922","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-07-22T16:49:24.433334Z","iopub.execute_input":"2025-07-22T16:49:24.433659Z","iopub.status.idle":"2025-07-22T17:00:36.371711Z","shell.execute_reply.started":"2025-07-22T16:49:24.433629Z","shell.execute_reply":"2025-07-22T17:00:36.369193Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save(\"trained_normal_model.h5\")","metadata":{"execution":{"iopub.status.busy":"2025-07-22T17:00:36.373231Z","iopub.execute_input":"2025-07-22T17:00:36.373859Z","iopub.status.idle":"2025-07-22T17:00:36.531243Z","shell.execute_reply.started":"2025-07-22T17:00:36.37378Z","shell.execute_reply":"2025-07-22T17:00:36.530283Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"'''\nfrom tensorflow.keras.models import load_model\nfrom PIL import Image\nimport pandas as pd\nimport numpy as np\nfrom sklearn.metrics import classification_report, confusion_matrix\nfrom sklearn.preprocessing import LabelEncoder\nimport matplotlib.pyplot as plt\nimport os\n\n# Load the trained model\nmodel = load_model(\"trained_normal_model.h5\")  # Load your model's updated filename\n\n# Load CSV file containing image labels\ncsv_path = \"/kaggle/input/local-data-stage3/Data Collection.csv\"  # Replace with your CSV file path\ndata = pd.read_csv(csv_path)\n\n# Create lists to store images and labels\nimages = []\nlabels = []\n\n# Load images and labels from the local dataset\nfor index, row in data.iterrows():\n    image_id = str(row[\"id\"])  # Convert the integer ID to a string\n    image_path = os.path.join(\"/kaggle/input/local-data-stage3/images_labaled\", image_id + \".jpg\")  # Replace with your image folder path\n    image = Image.open(image_path)\n    image = image.resize((128, 128))  # Resize to match your model's input size\n    image = np.array(image)\n    images.append(image)\n    labels.append(row[\"Diagnosis class\"])  # Adjust this to match the column name in your CSV\n\n# Convert lists to NumPy arrays\nX_local = np.array(images)\ny_local = np.array(labels)\n\n# Normalize image data (if needed)\nX_local = X_local / 255.0  # Assuming your model uses inputs in the range [0, 1]\n\n# Make predictions on the local dataset\npredictions = model.predict(X_local)\npredicted_classes = np.argmax(predictions, axis=1)\n\n# Use LabelEncoder to encode string labels to numeric values\nlabel_encoder = LabelEncoder()\ny_local_encoded = label_encoder.fit_transform(y_local)\n\n# Generate classification report\nclassification_rep = classification_report(y_local_encoded, predicted_classes)\nprint(\"Classification Report:\\n\", classification_rep)\n\n# Calculate confusion matrix\nconf_matrix = confusion_matrix(y_local_encoded, predicted_classes)\n\n# Plot confusion matrix\nplt.figure(figsize=(8, 6))\nplt.imshow(conf_matrix, interpolation=\"nearest\", cmap=plt.cm.Blues)\nplt.title(\"Confusion Matrix\")\nplt.colorbar()\ntick_marks = np.arange(len(label_encoder.classes_))\nplt.xticks(tick_marks, label_encoder.classes_, rotation=45)\nplt.yticks(tick_marks, label_encoder.classes_)\nplt.ylabel(\"True Labels\")\nplt.xlabel(\"Predicted Labels\")\nplt.show()\n'''","metadata":{"execution":{"iopub.status.busy":"2025-07-22T17:00:36.532447Z","iopub.execute_input":"2025-07-22T17:00:36.532714Z","iopub.status.idle":"2025-07-22T17:00:36.539761Z","shell.execute_reply.started":"2025-07-22T17:00:36.532693Z","shell.execute_reply":"2025-07-22T17:00:36.538988Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"'''\nfrom tensorflow.keras.models import load_model\n\n# Load the trained model\nmodel = load_model(\"trained_normal_model.h5\")  # Change to your model's filename\n'''","metadata":{"execution":{"iopub.status.busy":"2025-07-22T17:00:36.540945Z","iopub.execute_input":"2025-07-22T17:00:36.541663Z","iopub.status.idle":"2025-07-22T17:00:36.555885Z","shell.execute_reply.started":"2025-07-22T17:00:36.541631Z","shell.execute_reply":"2025-07-22T17:00:36.555186Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"'''\nimport pandas as pd\nimport numpy as np\nimport os\nfrom PIL import Image  # Use PIL for image processing\n\n# Load CSV file containing image labels\ncsv_path = \"/kaggle/input/local-data-stage3/Data Collection.csv\"  # Replace with your CSV file path\ndata = pd.read_csv(csv_path)\n\n# Create lists to store images and labels\nimages = []\nlabels = []\n\n# Load images and labels from the local dataset\nfor index, row in data.iterrows():\n    image_id = str(row[\"id\"])\n    image_path = os.path.join(\"/kaggle/input/local-data-stage3/images_labaled\", image_id + \".jpg\")  # Replace with your image folder path\n    image = Image.open(image_path)\n    image = image.resize((128, 128))  # Resize to match your model's input size\n    image = np.array(image)\n    images.append(image)\n    labels.append(row[\"Diagnosis class\"])  # Adjust this to match the column name in your CSV\n\n# Convert lists to NumPy arrays\nX_local = np.array(images)\ny_local = np.array(labels)\n'''","metadata":{"execution":{"iopub.status.busy":"2025-07-22T17:00:36.556776Z","iopub.execute_input":"2025-07-22T17:00:36.556985Z","iopub.status.idle":"2025-07-22T17:00:36.567633Z","shell.execute_reply.started":"2025-07-22T17:00:36.556966Z","shell.execute_reply":"2025-07-22T17:00:36.566897Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"'''\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.metrics import classification_report\nX_local = X_local / 255.0  # Assuming your model uses inputs in the range [0, 1]\n\n# Make predictions on the local dataset\npredictions = model.predict(X_local)\npredicted_classes = np.argmax(predictions, axis=1)\n\n# Use LabelEncoder to encode string labels to numeric values\nlabel_encoder = LabelEncoder()\ny_local_encoded = label_encoder.fit_transform(y_local)\n\n# Generate classification report\nclassification_rep = classification_report(y_local_encoded, predicted_classes)\nprint(classification_rep)\n'''","metadata":{"execution":{"iopub.status.busy":"2025-07-22T17:00:36.568798Z","iopub.execute_input":"2025-07-22T17:00:36.569351Z","iopub.status.idle":"2025-07-22T17:00:36.58123Z","shell.execute_reply.started":"2025-07-22T17:00:36.569321Z","shell.execute_reply":"2025-07-22T17:00:36.580338Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"'''\nfrom sklearn.metrics import classification_report, confusion_matrix\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# ... Load the model, preprocess the data, make predictions ...\n\n# Use LabelEncoder to encode string labels to numeric values\nlabel_encoder = LabelEncoder()\ny_local_encoded = label_encoder.fit_transform(y_local)\n\n# Generate classification report\nclassification_rep = classification_report(y_local_encoded, predicted_classes)\nprint(classification_rep)\n\n# Generate confusion matrix\nconf_matrix = confusion_matrix(y_local_encoded, predicted_classes)\n\n# Calculate TP, TN, FP, FN\nTP = conf_matrix[1, 1]\nTN = conf_matrix[0, 0]\nFP = conf_matrix[0, 1]\nFN = conf_matrix[1, 0]\n\n# Plot confusion matrix\nplt.figure(figsize=(8, 6))\nsns.heatmap(conf_matrix, annot=True, fmt=\"d\", cmap=\"Blues\", cbar=False)\nplt.xlabel(\"Predicted Labels\")\nplt.ylabel(\"True Labels\")\nplt.title(\"Confusion Matrix\")\nplt.show()\n\n# Print TP, TN, FP, FN values\nprint(\"True Positives:\", TP)\nprint(\"True Negatives:\", TN)\nprint(\"False Positives:\", FP)\nprint(\"False Negatives:\", FN)\n'''","metadata":{"execution":{"iopub.status.busy":"2025-07-22T17:00:36.582272Z","iopub.execute_input":"2025-07-22T17:00:36.582513Z","iopub.status.idle":"2025-07-22T17:00:36.594064Z","shell.execute_reply.started":"2025-07-22T17:00:36.582493Z","shell.execute_reply":"2025-07-22T17:00:36.593364Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nplt.figure(figsize=(16, 8))\n\nplt.plot(history.history['accuracy'], label='Train')\nplt.plot(history.history['val_accuracy'], label='Validation')\nplt.ylabel('Cross Entropy accuracy')\nplt.xlabel('Epoch')\nplt.title('Train accuracy', pad=13, fontsize=25)\nplt.legend(loc='upper right')\nplt.grid(000.1)\n\nplt.show()\n\n\nplt.figure(figsize=(16, 8))\n\nplt.plot(history.history['loss'], label='Train')\nplt.plot(history.history['val_loss'], label='Validation')\nplt.ylabel('Cross Entropy Loss')\nplt.xlabel('Epoch')\nplt.title('Train Loss', pad=13, fontsize=25)\nplt.legend(loc='upper right')\nplt.grid(000.1)\n\nplt.show()","metadata":{"_uuid":"133ecd23-7750-4c8c-826b-db84488fadf4","_cell_guid":"3348e524-3f63-450a-aff0-ebd8f89eab37","execution":{"iopub.status.busy":"2025-07-22T17:00:36.59501Z","iopub.execute_input":"2025-07-22T17:00:36.595309Z","iopub.status.idle":"2025-07-22T17:00:37.812221Z","shell.execute_reply.started":"2025-07-22T17:00:36.595278Z","shell.execute_reply":"2025-07-22T17:00:37.811369Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Fine tuning model","metadata":{"_uuid":"b8783bf3-42f4-4eed-bc31-c04077ede3c9","_cell_guid":"697ea634-67bb-4f75-8735-17559483230c","trusted":true}},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nbase_model.trainable = True\nfor layer in base_model.layers:\n    if isinstance(layer, BatchNormalization): # set BatchNorm layers as not trainable\n        layer.trainable = False\n\nmodel.compile(optimizer=AdamW(learning_rate=0.00001), # \n                   loss=losses.categorical_crossentropy, \n                   metrics=['accuracy']) # ,f1_m\n\nhistory = model.fit(X_train, \n                         y_train, \n                         batch_size=16, \n                         epochs=50, \n                         validation_data=(X_test, y_test),\n                         callbacks=[\n                             EarlyStopping(monitor = \"val_loss\", patience = 15, \n                                           restore_best_weights = True, mode='min'),\n                             ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=2, mode='min')\n                    ]\n                        )\nfcl_loss, fcl_accuracy  = model.evaluate(X_test, y_test, verbose=1) # fcl_f1\nprint('Test loss:', fcl_loss)\nprint('Test accuracy:', fcl_accuracy)\n# print('Test F1:', fcl_f1)\n\ndf = pd.DataFrame({\"pred\": np.argmax(model.predict(X_test), axis=1), \"true\": np.argmax(y_test, axis=1)})\nplt.matshow(confusion_matrix(df[\"true\"], df[\"pred\"]))\nprint(classification_report(df[\"true\"], df[\"pred\"]))\nprint(confusion_matrix(df[\"true\"], df[\"pred\"]))","metadata":{"_uuid":"698aa63e-a499-4be2-a7aa-dd164da376c6","_cell_guid":"59e6950d-ec6f-4d5d-8bdd-0733053bdb84","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-07-22T17:00:37.81348Z","iopub.execute_input":"2025-07-22T17:00:37.81407Z","iopub.status.idle":"2025-07-22T17:25:08.688731Z","shell.execute_reply.started":"2025-07-22T17:00:37.814031Z","shell.execute_reply":"2025-07-22T17:25:08.687337Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Create subplots\nfig, axs = plt.subplots(1, 2, figsize=(12, 5))\n\n# Subplot (a) - Training Accuracy\naxs[0].plot(history.history['accuracy'], color='blue')\naxs[0].set_title('Figure 3(a): Training Accuracy', fontsize=12)\naxs[0].set_xlabel('Epoch', fontsize=10)\naxs[0].set_ylabel('Accuracy', fontsize=10)\naxs[0].tick_params(axis='both', labelsize=9)\n\n# Subplot (b) - Validation Accuracy\naxs[1].plot(history.history['val_accuracy'], color='green')\naxs[1].set_title('Figure 3(b): Validation Accuracy', fontsize=12)\naxs[1].set_xlabel('Epoch', fontsize=10)\naxs[1].set_ylabel('Accuracy', fontsize=10)\naxs[1].tick_params(axis='both', labelsize=9)\n\nplt.suptitle('Figure 3: Training and Validation Accuracy of ResNet101', fontsize=14)\nplt.tight_layout(rect=[0, 0.03, 1, 0.95])\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T17:25:08.691032Z","iopub.execute_input":"2025-07-22T17:25:08.692019Z","iopub.status.idle":"2025-07-22T17:25:09.273092Z","shell.execute_reply.started":"2025-07-22T17:25:08.691977Z","shell.execute_reply":"2025-07-22T17:25:09.272241Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Create 1 row × 2 columns subplot\nfig, axs = plt.subplots(1, 2, figsize=(14, 5))\n\n# Subplot (a) – Accuracy\naxs[0].plot(history.history['accuracy'], label='Train Accuracy', color='blue')\naxs[0].plot(history.history['val_accuracy'], label='Validation Accuracy', color='green')\naxs[0].set_title('Figure 3(a): Training and Validation Accuracy', fontsize=12)\naxs[0].set_xlabel('Epoch', fontsize=10)\naxs[0].set_ylabel('Accuracy', fontsize=10)\naxs[0].legend(fontsize=9)\naxs[0].tick_params(axis='both', labelsize=9)\n\n# Subplot (b) – Loss\naxs[1].plot(history.history['loss'], label='Train Loss', color='red')\naxs[1].plot(history.history['val_loss'], label='Validation Loss', color='orange')\naxs[1].set_title('Figure 3(b): Training and Validation Loss', fontsize=12)\naxs[1].set_xlabel('Epoch', fontsize=10)\naxs[1].set_ylabel('Loss', fontsize=10)\naxs[1].legend(fontsize=9)\naxs[1].tick_params(axis='both', labelsize=9)\n\n# Super title for both plots\nplt.suptitle('Figure 3: Performance Curves of ResNet101 Model', fontsize=14)\nplt.tight_layout(rect=[0, 0.03, 1, 0.95])\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T17:32:48.766214Z","iopub.execute_input":"2025-07-22T17:32:48.767093Z","iopub.status.idle":"2025-07-22T17:32:49.46995Z","shell.execute_reply.started":"2025-07-22T17:32:48.767063Z","shell.execute_reply":"2025-07-22T17:32:49.469086Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Create 1 row × 2 columns subplot\nfig, axs = plt.subplots(1, 2, figsize=(14, 5))\n\n# Subplot (a) – Accuracy\naxs[0].plot(history.history['accuracy'], label='Train Accuracy', color='blue')\naxs[0].plot(history.history['val_accuracy'], label='Validation Accuracy', color='green')\naxs[0].set_title('Figure 3(a): Training and Validation Accuracy', fontsize=12)\naxs[0].set_xlabel('Epoch', fontsize=10)\naxs[0].set_ylabel('Accuracy', fontsize=10)\naxs[0].legend(fontsize=9)\naxs[0].tick_params(axis='both', labelsize=9)\n\n# Subplot (b) – Loss\naxs[1].plot(history.history['loss'], label='Train Loss', color='red')\naxs[1].plot(history.history['val_loss'], label='Validation Loss', color='orange')\naxs[1].set_title('Figure 3(b): Training and Validation Loss', fontsize=12)\naxs[1].set_xlabel('Epoch', fontsize=10)\naxs[1].set_ylabel('Loss', fontsize=10)\naxs[1].legend(fontsize=9)\naxs[1].tick_params(axis='both', labelsize=9)\n\n# Super title\nplt.suptitle('Figure 3: Performance Curves of ResNet101 Model', fontsize=14)\n\n# Save the figure\nplt.savefig('figure3_resnet101.png', dpi=300, bbox_inches='tight')  # Save in high resolution\n\n# Show the figure\nplt.tight_layout(rect=[0, 0.03, 1, 0.95])\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T17:34:01.630244Z","iopub.execute_input":"2025-07-22T17:34:01.630921Z","iopub.status.idle":"2025-07-22T17:34:02.975704Z","shell.execute_reply.started":"2025-07-22T17:34:01.630894Z","shell.execute_reply":"2025-07-22T17:34:02.974716Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save(\"trained_model.h5\")","metadata":{"execution":{"iopub.status.busy":"2025-07-22T17:40:21.924796Z","iopub.execute_input":"2025-07-22T17:40:21.925784Z","iopub.status.idle":"2025-07-22T17:40:22.248616Z","shell.execute_reply.started":"2025-07-22T17:40:21.925749Z","shell.execute_reply":"2025-07-22T17:40:22.247618Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\n\n# Load the trained model\nmodel = load_model(\"trained_model.h5\")  # Change to your model's filename","metadata":{"execution":{"iopub.status.busy":"2025-07-22T17:40:46.251738Z","iopub.execute_input":"2025-07-22T17:40:46.252714Z","iopub.status.idle":"2025-07-22T17:40:47.018563Z","shell.execute_reply.started":"2025-07-22T17:40:46.252679Z","shell.execute_reply":"2025-07-22T17:40:47.017812Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Normalize pixel values to [0, 1]\nX_local = X_local.astype('float32') / 255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T17:45:45.352234Z","iopub.execute_input":"2025-07-22T17:45:45.35262Z","iopub.status.idle":"2025-07-22T17:45:45.363925Z","shell.execute_reply.started":"2025-07-22T17:45:45.352593Z","shell.execute_reply":"2025-07-22T17:45:45.36321Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nfrom tensorflow.keras.utils import to_categorical\n\nle = LabelEncoder()\ny_local_encoded = le.fit_transform(y_local)  # Binary: 0 = Normal, 1 = Pneumonia\ny_local_categorical = to_categorical(y_local_encoded)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T17:46:14.494497Z","iopub.execute_input":"2025-07-22T17:46:14.494789Z","iopub.status.idle":"2025-07-22T17:46:14.500827Z","shell.execute_reply.started":"2025-07-22T17:46:14.494769Z","shell.execute_reply":"2025-07-22T17:46:14.500046Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Evaluate on local dataset\nloss, accuracy = model.evaluate(X_local, y_local_categorical, verbose=1)\nprint(f\"🧪 Local Dataset Accuracy: {accuracy*100:.2f}% | Loss: {loss:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T17:46:36.533429Z","iopub.execute_input":"2025-07-22T17:46:36.533764Z","iopub.status.idle":"2025-07-22T17:46:36.897307Z","shell.execute_reply.started":"2025-07-22T17:46:36.533738Z","shell.execute_reply":"2025-07-22T17:46:36.896401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nfrom sklearn.metrics import roc_auc_score, roc_curve, confusion_matrix, ConfusionMatrixDisplay\nimport matplotlib.pyplot as plt\n\n# Get predicted probabilities and labels\ny_probs = model.predict(X_local)\ny_preds = (y_probs[:, 1] > 0.5).astype(int)  # threshold = 0.5 for binary\n\n# ROC and AUC\nfpr, tpr, _ = roc_curve(y_local_encoded, y_probs[:, 1])\nauc_score = roc_auc_score(y_local_encoded, y_probs[:, 1])\n\nplt.figure(figsize=(6, 5))\nplt.plot(fpr, tpr, label=f\"AUC = {auc_score:.2f}\", color='darkorange')\nplt.plot([0, 1], [0, 1], linestyle='--', color='gray')\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"ROC Curve (Local Dataset)\")\nplt.legend(loc=\"lower right\")\nplt.grid()\nplt.tight_layout()\nplt.savefig(\"roc_curve_local.png\")  # Save if needed\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T17:46:58.751751Z","iopub.execute_input":"2025-07-22T17:46:58.75248Z","iopub.status.idle":"2025-07-22T17:46:59.343525Z","shell.execute_reply.started":"2025-07-22T17:46:58.752451Z","shell.execute_reply":"2025-07-22T17:46:59.342653Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cm = confusion_matrix(y_local_encoded, y_preds)\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=le.classes_)\n\nplt.figure(figsize=(5, 4))\ndisp.plot(cmap=\"Blues\", values_format='d')\nplt.title(\"Confusion Matrix (Local Dataset)\")\nplt.grid(False)\nplt.tight_layout()\nplt.savefig(\"confusion_matrix_local.png\")  # Save if needed\nplt.show()\n\nprint(\"🟩 Confusion Matrix:\\n\", cm)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T17:47:25.605065Z","iopub.execute_input":"2025-07-22T17:47:25.606016Z","iopub.status.idle":"2025-07-22T17:47:25.901043Z","shell.execute_reply.started":"2025-07-22T17:47:25.605984Z","shell.execute_reply":"2025-07-22T17:47:25.900084Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nfrom PIL import Image  # Use PIL for image processing\n\n# Load CSV file containing image labels\ncsv_path = \"/kaggle/input/local-data-stage3/Data Collection.csv\"  # Replace with your CSV file path\ndata = pd.read_csv(csv_path)\n\n# Create lists to store images and labels\nimages = []\nlabels = []\n\n# Load images and labels from the local dataset\nfor index, row in data.iterrows():\n    image_id = str(row[\"id\"])\n    image_path = os.path.join(\"/kaggle/input/local-data-stage3/images_labaled\", image_id + \".jpg\")  # Replace with your image folder path\n    image = Image.open(image_path)\n    image = image.resize((128, 128))  # Resize to match your model's input size\n    image = np.array(image)\n    images.append(image)\n    labels.append(row[\"Diagnosis class\"])  # Adjust this to match the column name in your CSV\n\n# Convert lists to NumPy arrays\nX_local = np.array(images)\ny_local = np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2025-07-22T17:41:02.777973Z","iopub.execute_input":"2025-07-22T17:41:02.778621Z","iopub.status.idle":"2025-07-22T17:41:23.496962Z","shell.execute_reply.started":"2025-07-22T17:41:02.778583Z","shell.execute_reply":"2025-07-22T17:41:23.496167Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nfrom sklearn.metrics import classification_report\nX_local = X_local / 255.0  # Assuming your model uses inputs in the range [0, 1]\n\n# Make predictions on the local dataset\npredictions = model.predict(X_local)\npredicted_classes = np.argmax(predictions, axis=1)\n\n# Use LabelEncoder to encode string labels to numeric values\nlabel_encoder = LabelEncoder()\ny_local_encoded = label_encoder.fit_transform(y_local)\n\n# Generate classification report\nclassification_rep = classification_report(y_local_encoded, predicted_classes)\nprint(classification_rep)","metadata":{"execution":{"iopub.status.busy":"2025-07-22T17:41:40.497447Z","iopub.execute_input":"2025-07-22T17:41:40.497793Z","iopub.status.idle":"2025-07-22T17:41:41.245788Z","shell.execute_reply.started":"2025-07-22T17:41:40.497769Z","shell.execute_reply":"2025-07-22T17:41:41.24467Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# ... Load the model, preprocess the data, make predictions ...\n\n# Use LabelEncoder to encode string labels to numeric values\nlabel_encoder = LabelEncoder()\ny_local_encoded = label_encoder.fit_transform(y_local)\n\n# Generate classification report\nclassification_rep = classification_report(y_local_encoded, predicted_classes)\nprint(classification_rep)\n\n# Generate confusion matrix\nconf_matrix = confusion_matrix(y_local_encoded, predicted_classes)\n\n# Calculate TP, TN, FP, FN\nTP = conf_matrix[1, 1]\nTN = conf_matrix[0, 0]\nFP = conf_matrix[0, 1]\nFN = conf_matrix[1, 0]\n\n# Plot confusion matrix\nplt.figure(figsize=(8, 6))\nsns.heatmap(conf_matrix, annot=True, fmt=\"d\", cmap=\"Blues\", cbar=False)\nplt.xlabel(\"Predicted Labels\")\nplt.ylabel(\"True Labels\")\nplt.title(\"Confusion Matrix\")\nplt.show()\n\n# Print TP, TN, FP, FN values\nprint(\"True Positives:\", TP)\nprint(\"True Negatives:\", TN)\nprint(\"False Positives:\", FP)\nprint(\"False Negatives:\", FN)","metadata":{"execution":{"iopub.status.busy":"2025-07-22T17:42:02.782765Z","iopub.execute_input":"2025-07-22T17:42:02.78369Z","iopub.status.idle":"2025-07-22T17:42:03.083117Z","shell.execute_reply.started":"2025-07-22T17:42:02.783656Z","shell.execute_reply":"2025-07-22T17:42:03.081973Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score, roc_curve, auc\nimport matplotlib.pyplot as plt\n\n# Example: assuming these are already available\n# y_local_encoded = true labels (0 or 1)\n# y_probs = model.predict(X_local)  # probability predictions, shape: (n_samples, 2)\n\n# If using sigmoid:\n# y_probs = model.predict(X_local)[:, 0] if binary classifier\n\n# Compute ROC\nfpr, tpr, thresholds = roc_curve(y_local_encoded, y_probs)\nauc_score = auc(fpr, tpr)\n\n# Plot ROC\nplt.figure(figsize=(6, 6))\nplt.plot(fpr, tpr, label=f\"ROC curve (AUC = {auc_score:.2f})\", color=\"blue\")\nplt.plot([0, 1], [0, 1], \"k--\")  # Diagonal line\nplt.xlim([0.0, 1.0])\nplt.ylim([0.0, 1.05])\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"Figure 5: ROC Curve on Local Dataset\")\nplt.legend(loc=\"lower right\")\nplt.grid()\nplt.savefig(\"figure5_roc_auc.png\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-22T17:55:14.365605Z","iopub.execute_input":"2025-07-22T17:55:14.366702Z","iopub.status.idle":"2025-07-22T17:55:14.528996Z","shell.execute_reply.started":"2025-07-22T17:55:14.366671Z","shell.execute_reply":"2025-07-22T17:55:14.52781Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import plot_confusion_matrix\nX_local = X_local / 255.0  # Assuming your model uses inputs in the range [0, 1]\n\n# Make predictions on the local dataset\npredictions = model.predict(X_local)\npredicted_classes = np.argmax(predictions, axis=1)\n\n# Use LabelEncoder to encode string labels to numeric values\nlabel_encoder = LabelEncoder()\ny_local_encoded = label_encoder.fit_transform(y_local)\n\n# Generate classification report\nclassification_rep = classification_report(y_local_encoded, predicted_classes)\nprint(\"Classification Report:\\n\", classification_rep)\n\n# Calculate confusion matrix\ncm = confusion_matrix(y_local_encoded, predicted_classes)\n\n# Plot confusion matrix\nplt.figure(figsize=(8, 8))\nplot_confusion_matrix(model, X_local, y_local_encoded, display_labels=label_encoder.classes_, cmap=plt.cm.Blues)\nplt.title(\"Confusion Matrix\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-07-22T17:42:33.523004Z","iopub.execute_input":"2025-07-22T17:42:33.523425Z","iopub.status.idle":"2025-07-22T17:42:34.060691Z","shell.execute_reply.started":"2025-07-22T17:42:33.523392Z","shell.execute_reply":"2025-07-22T17:42:34.059306Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\nplt.figure(figsize=(16, 8))\n\nplt.plot(history.history['accuracy'], label='Train')\nplt.plot(history.history['val_accuracy'], label='Validation')\nplt.ylabel('Cross Entropy accuracy')\nplt.xlabel('Epoch')\nplt.title('Train accuracy', pad=13, fontsize=25)\nplt.legend(loc='upper right')\nplt.grid(000.1)\n\nplt.show()\n\n\nplt.figure(figsize=(16, 8))\n\nplt.plot(history.history['loss'], label='Train')\nplt.plot(history.history['val_loss'], label='Validation')\nplt.ylabel('Cross Entropy Loss')\nplt.xlabel('Epoch')\nplt.title('Train Loss', pad=13, fontsize=25)\nplt.legend(loc='upper right')\nplt.grid(000.1)\n\nplt.show()","metadata":{"_uuid":"a616f381-773c-404f-a411-94074f580540","_cell_guid":"26e5ffb8-d9c3-4b65-b724-acf2266e0608","execution":{"iopub.status.busy":"2023-08-23T20:49:21.54485Z","iopub.execute_input":"2023-08-23T20:49:21.545832Z","iopub.status.idle":"2023-08-23T20:49:22.256531Z","shell.execute_reply.started":"2023-08-23T20:49:21.545793Z","shell.execute_reply":"2023-08-23T20:49:22.255685Z"},"trusted":true}},{"cell_type":"code","source":"'''def preprocess_your_data(dataset_path):\n    imageList = []\n    for file_name in tqdm(os.listdir('/kaggle/input/local-data-stage3')):\n        dcm_file = os.path.join(dataset_path, file_name)\n        imageList.append(get_image(dcm_file))  # Assuming 'get_image' is your preprocessing function\n    \n    return np.array(imageList)\n","metadata":{"_uuid":"96982f02-9722-4824-9a4d-8a6b785d5aa3","_cell_guid":"2df22887-9826-44ab-8eed-fb9af0bec991","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-07-22T17:25:09.290647Z","iopub.status.idle":"2025-07-22T17:25:09.291075Z","shell.execute_reply.started":"2025-07-22T17:25:09.290855Z","shell.execute_reply":"2025-07-22T17:25:09.290876Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"'''your_dataset_path = '/kaggle/input/local-data-stage3'\nyour_preprocessed_data = preprocess_your_data(your_dataset_path)\npredictions = model.predict(your_preprocessed_data)\npredicted_classes = np.argmax(predictions, axis=1)\n\ntrue_labels = your_ground_truth_labels  # Replace with your actual ground truth labels\nclassification_rep = classification_report(true_labels, predicted_classes)\nconf_matrix = confusion_matrix(true_labels, predicted_classes)\n\nprint(\"Classification Report:\\n\", classification_rep)\nprint(\"Confusion Matrix:\\n\", conf_matrix)\n","metadata":{"execution":{"iopub.status.busy":"2025-07-22T17:25:09.292525Z","iopub.status.idle":"2025-07-22T17:25:09.292821Z","shell.execute_reply.started":"2025-07-22T17:25:09.292678Z","shell.execute_reply":"2025-07-22T17:25:09.292691Z"},"trusted":true},"outputs":[],"execution_count":null}]}