{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":18647,"databundleVersionId":1126921,"sourceType":"competition"},{"sourceId":848739,"sourceType":"datasetVersion","datasetId":251095},{"sourceId":1360593,"sourceType":"datasetVersion","datasetId":792376}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nfrom tqdm.notebook import tqdm\nimport zipfile","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:43:59.275107Z","iopub.execute_input":"2024-10-07T16:43:59.275458Z","iopub.status.idle":"2024-10-07T16:43:59.38274Z","shell.execute_reply.started":"2024-10-07T16:43:59.27543Z","shell.execute_reply":"2024-10-07T16:43:59.381765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nimport skimage.io\nimport numpy as np\nimport pandas as pd\nimport cv2\nimport PIL.Image\nfrom sklearn.model_selection import StratifiedKFold\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import cohen_kappa_score\nfrom tqdm import tqdm_notebook as tqdm","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:43:59.489938Z","iopub.execute_input":"2024-10-07T16:43:59.490674Z","iopub.status.idle":"2024-10-07T16:44:01.075358Z","shell.execute_reply.started":"2024-10-07T16:43:59.490635Z","shell.execute_reply":"2024-10-07T16:44:01.074518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"model_dir = '../input/panda-enet-b1-model/'\ndata_dir = '../input/prostate-cancer-grade-assessment'\ndf_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))\n","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:44:01.076975Z","iopub.execute_input":"2024-10-07T16:44:01.077369Z","iopub.status.idle":"2024-10-07T16:44:01.118075Z","shell.execute_reply.started":"2024-10-07T16:44:01.077344Z","shell.execute_reply":"2024-10-07T16:44:01.117231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sample_equal_instances(df, target_column, sample_size):\n    sampled_df = df.groupby(target_column).apply(lambda x: x.sample(sample_size, replace=True)).reset_index(drop=True)\n    return sampled_df","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:44:01.119353Z","iopub.execute_input":"2024-10-07T16:44:01.119718Z","iopub.status.idle":"2024-10-07T16:44:01.12516Z","shell.execute_reply.started":"2024-10-07T16:44:01.119692Z","shell.execute_reply":"2024-10-07T16:44:01.124213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gleason_6 = df_train[df_train['isup_grade'].isin([0, 1, 2, 3, 4, 5])]\n\n# Select 100 rows from the filtered DataFrame\nselected_data = gleason_6.head(80)","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:44:01.127343Z","iopub.execute_input":"2024-10-07T16:44:01.127638Z","iopub.status.idle":"2024-10-07T16:44:01.145176Z","shell.execute_reply.started":"2024-10-07T16:44:01.127614Z","shell.execute_reply":"2024-10-07T16:44:01.144146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(selected_data)\nprint(f'Total samples: {selected_data.shape[0]}')","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:44:01.146505Z","iopub.execute_input":"2024-10-07T16:44:01.146872Z","iopub.status.idle":"2024-10-07T16:44:01.161862Z","shell.execute_reply.started":"2024-10-07T16:44:01.146839Z","shell.execute_reply":"2024-10-07T16:44:01.160677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Image ids in NAME variable with their respective classes in CLASS variable\nNAME= selected_data['image_id'].tolist()\nCLASS=selected_data['isup_grade'].tolist()\nNAME","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:44:01.163565Z","iopub.execute_input":"2024-10-07T16:44:01.164244Z","iopub.status.idle":"2024-10-07T16:44:01.174156Z","shell.execute_reply.started":"2024-10-07T16:44:01.164209Z","shell.execute_reply":"2024-10-07T16:44:01.173199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=[]\ncount=0\nfor i in NAME:\n df.append(df_train[df_train['image_id']==i])\n count+=1\ndf_train=pd.DataFrame(np.array(df).reshape(80,4),columns=['image_id','data_provider','isup_grade','gleason_score'])\ndf_train","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:44:01.3503Z","iopub.execute_input":"2024-10-07T16:44:01.350679Z","iopub.status.idle":"2024-10-07T16:44:01.570536Z","shell.execute_reply.started":"2024-10-07T16:44:01.350649Z","shell.execute_reply":"2024-10-07T16:44:01.569508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN = '../input/prostate-cancer-grade-assessment/train_images/'\nMASKS = '../input/prostate-cancer-grade-assessment/train_label_masks/'\nOUT_TRAIN = 'train.zip'\nOUT_MASKS = 'masks.zip'\nsz = 256 # Size of each tile\nN = 36 # Total no of tiles","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:44:01.735534Z","iopub.execute_input":"2024-10-07T16:44:01.736253Z","iopub.status.idle":"2024-10-07T16:44:01.741746Z","shell.execute_reply.started":"2024-10-07T16:44:01.736213Z","shell.execute_reply":"2024-10-07T16:44:01.740709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tile(img, mask, sz=256, N=16, cutoff_threshold=0.9):\n    result = []\n    shape = img.shape\n    pad0, pad1 = (sz - shape[0] % sz) % sz, (sz - shape[1] % sz) % sz\n    img = np.pad(img, [[pad0 // 2, pad0 - pad0 // 2], [pad1 // 2, pad1 - pad1 // 2], [0, 0]],\n                 constant_values=255)\n    mask = np.pad(mask, [[pad0 // 2, pad0 - pad0 // 2], [pad1 // 2, pad1 - pad1 // 2], [0, 0]],\n                  constant_values=0)\n    img = img.reshape(img.shape[0] // sz, sz, img.shape[1] // sz, sz, 3)\n    img = img.transpose(0, 2, 1, 3, 4).reshape(-1, sz, sz, 3)\n    mask = mask.reshape(mask.shape[0] // sz, sz, mask.shape[1] // sz, sz, 3)\n    mask = mask.transpose(0, 2, 1, 3, 4).reshape(-1, sz, sz, 3)\n\n    # Filter tiles using tile_cutoff\n    valid_tiles = tile_cutoff(img, cutoff_threshold)\n    img = img[valid_tiles]\n    mask = mask[valid_tiles]\n\n    if len(img) < N:\n        mask = np.pad(mask, [[0, N - len(img)], [0, 0], [0, 0], [0, 0]], constant_values=0)\n        img = np.pad(img, [[0, N - len(img)], [0, 0], [0, 0], [0, 0]], constant_values=255)\n    idxs = np.argsort(img.reshape(img.shape[0], -1).sum(-1))[:N]\n    img = img[idxs]\n    mask = mask[idxs]\n    for i in range(len(img)):\n        augmented_img, augmented_mask = augment_tile(img[i], mask[i])\n        result.append({'img': augmented_img, 'mask': augmented_mask, 'idx': i})\n    return result\n\ndef tile_cutoff(img_tiles, threshold):\n    \"\"\"Filter out tiles that are predominantly white or gray.\"\"\"\n    valid_tiles = []\n    for i, tile in enumerate(img_tiles):\n        if np.mean(tile) / 255.0 < threshold:\n            valid_tiles.append(i)\n    return np.array(valid_tiles)\n\ndef augment_tile(img, mask):\n    \"\"\"Apply augmentations to a tile.\"\"\"\n    # Random horizontal flip\n    if np.random.rand() > 0.5:\n        img = np.fliplr(img)\n        mask = np.fliplr(mask)\n    # Random vertical flip\n    if np.random.rand() > 0.5:\n        img = np.flipud(img)\n        mask = np.flipud(mask)\n    # Random rotation\n    if np.random.rand() > 0.5:\n        k = np.random.randint(0, 4)\n        img = np.rot90(img, k)\n        mask = np.rot90(mask, k)\n    # Additional augmentations can be added here\n    return img, mask\n","metadata":{"execution":{"iopub.status.busy":"2024-09-29T05:22:31.403582Z","iopub.execute_input":"2024-09-29T05:22:31.403997Z","iopub.status.idle":"2024-09-29T05:22:31.4264Z","shell.execute_reply.started":"2024-09-29T05:22:31.403966Z","shell.execute_reply":"2024-09-29T05:22:31.424945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_tot,x2_tot = [],[]\nnames = [name[:-10] for name in os.listdir(MASKS)]\nwith zipfile.ZipFile(OUT_TRAIN, 'w') as img_out,\\\n zipfile.ZipFile(OUT_MASKS, 'w') as mask_out:\n    for name in tqdm(NAME):\n        img = skimage.io.MultiImage(os.path.join(TRAIN,name+'.tiff'))[-1]\n        mask = skimage.io.MultiImage(os.path.join(MASKS,name+'_mask.tiff'))[-1]\n        tiles = tile(img,mask)\n        for t in tiles:\n            img,mask,idx = t['img'],t['mask'],t['idx']\n            x_tot.append((img/255.0).reshape(-1,3).mean(0))\n            x2_tot.append(((img/255.0)**2).reshape(-1,3).mean(0)) \n            #if read with PIL RGB turns into BGR\n            img = cv2.imencode('.png',cv2.cvtColor(img, cv2.COLOR_RGB2BGR))[1]\n            img_out.writestr(f'{name}_{idx}.png', img)\n            mask = cv2.imencode('.png',mask[:,:,0])[1]\n            mask_out.writestr(f'{name}_{idx}.png', mask)","metadata":{"execution":{"iopub.status.busy":"2024-09-29T05:22:31.906589Z","iopub.execute_input":"2024-09-29T05:22:31.907014Z","iopub.status.idle":"2024-09-29T05:32:10.235995Z","shell.execute_reply.started":"2024-09-29T05:22:31.906981Z","shell.execute_reply":"2024-09-29T05:32:10.234662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -o /kaggle/working/train.zip","metadata":{"execution":{"iopub.status.busy":"2024-09-29T05:32:10.238615Z","iopub.execute_input":"2024-09-29T05:32:10.239365Z","iopub.status.idle":"2024-09-29T05:32:13.069855Z","shell.execute_reply.started":"2024-09-29T05:32:10.239321Z","shell.execute_reply":"2024-09-29T05:32:13.068428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loading all the tile images into img_train array with 12 rows\nimport cv2\nout_dir='/kaggle/working/'\nimg_train=np.empty((80,4*256,4*256,3))\ncount=-1\nfor i in NAME:\n    img_var=[]\n    count+=1\n    for j in range(0,16): \n        img_var.append(cv2.imread(out_dir+str(i)+'_'+str(j)+'.png'))\n    img_var=np.array(img_var)\n    img_var=img_var.reshape(4*256,4*256,3)\n    img_train[count]=img_var\n\n","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:44:07.264641Z","iopub.execute_input":"2024-10-07T16:44:07.265439Z","iopub.status.idle":"2024-10-07T16:44:10.222985Z","shell.execute_reply.started":"2024-10-07T16:44:07.265408Z","shell.execute_reply":"2024-10-07T16:44:10.221966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_train=img_train.reshape(80,-1) # for smote function to work, it requires only 2 dimensional array","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:44:10.224892Z","iopub.execute_input":"2024-10-07T16:44:10.225236Z","iopub.status.idle":"2024-10-07T16:44:10.229821Z","shell.execute_reply.started":"2024-10-07T16:44:10.225208Z","shell.execute_reply":"2024-10-07T16:44:10.228778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 12 to 24 images using smote analysis\nfrom collections import Counter\nfrom imblearn.over_sampling import SMOTE\ncounter = Counter(CLASS)\nprint('Before',counter)\n# oversampling the train dataset using SMOTE\nsmt = SMOTE(sampling_strategy={0:30,1:30,2:30,3:30,4:30,5:30},k_neighbors=3)\n#X_train, y_train = smt.fit_resample(X_train, y_train)\nX_train_sm, y_train_sm = smt.fit_resample(img_train, CLASS)\n\ncounter = Counter(y_train_sm)\nprint('After',counter)","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:44:10.231065Z","iopub.execute_input":"2024-10-07T16:44:10.231321Z","iopub.status.idle":"2024-10-07T16:44:20.799485Z","shell.execute_reply.started":"2024-10-07T16:44:10.231298Z","shell.execute_reply":"2024-10-07T16:44:20.798524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train_sm.shape)\nprint(X_train_sm.size)\n","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:44:20.801877Z","iopub.execute_input":"2024-10-07T16:44:20.802375Z","iopub.status.idle":"2024-10-07T16:44:20.807332Z","shell.execute_reply.started":"2024-10-07T16:44:20.802345Z","shell.execute_reply":"2024-10-07T16:44:20.806359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\n# Define the desired dimensions for each image\nnew_width = 256\nnew_height = 256\n\n# List to store resized images\nresized_images = []\n\n# Loop through each image in X_train_sm and resize\nfor img_data in X_train_sm:\n    # Reshape the 1D array back into image dimensions\n    img = img_data.reshape(4*256, 4*256, 3)\n    \n    # Convert numpy array to PIL Image\n    pil_img = Image.fromarray(img.astype('uint8'))\n    \n    # Resize the image\n    resized_img = pil_img.resize((new_width, new_height))\n    \n    # Convert back to numpy array\n    resized_img_data = np.array(resized_img)\n    \n    # Append resized image to the list\n    resized_images.append(resized_img_data)\n\n# Convert the list of resized images back to numpy array\nX_train_resized = np.array(resized_images)\n\n# Check the shape of the resized array\nprint(X_train_resized.shape)\n","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:44:20.80876Z","iopub.execute_input":"2024-10-07T16:44:20.809066Z","iopub.status.idle":"2024-10-07T16:44:23.415247Z","shell.execute_reply.started":"2024-10-07T16:44:20.809034Z","shell.execute_reply":"2024-10-07T16:44:23.414153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train=pd.get_dummies(y_train_sm).values # onehotencoding","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:44:23.416639Z","iopub.execute_input":"2024-10-07T16:44:23.417019Z","iopub.status.idle":"2024-10-07T16:44:23.423353Z","shell.execute_reply.started":"2024-10-07T16:44:23.416985Z","shell.execute_reply":"2024-10-07T16:44:23.422472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, models, optimizers\nfrom tensorflow.keras.layers import Input\nfrom tensorflow.keras.applications import EfficientNetB7  # or other variants (B1-B7)\nfrom sklearn.metrics import roc_auc_score, roc_curve, cohen_kappa_score, confusion_matrix, accuracy_score\nfrom sklearn.metrics import classification_report\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Define the input shape based on the resized image dimensions\ninput_t = Input(shape=(256, 256, 3))\n\n# Load EfficientNetB0 model with the updated input shape (include_top=False to exclude final layers)\nenet_model = EfficientNetB7(include_top=False, weights='imagenet', input_tensor=input_t)\n\n# Build the rest of the model\nmodel = models.Sequential()\nmodel.add(enet_model)  # Add EfficientNet backbone\nmodel.add(layers.GlobalAveragePooling2D())  # Global pooling after the backbone\nmodel.add(layers.Dense(6, activation='softmax'))  # Classification layer for 6 classes\n\n# Compile the model\nmodel.compile(loss='categorical_crossentropy',\n              optimizer=optimizers.RMSprop(learning_rate=0.01),\n              metrics=['accuracy'])\n\n# Print model summary\nmodel.summary()\n\nfrom sklearn.model_selection import KFold\nfrom sklearn.metrics import confusion_matrix\nimport numpy as np\n\nk = 13  # Number of folds\nkf = KFold(n_splits=k, shuffle=True, random_state=2)\nscores=[]\nauc_scores = []\nkappa_scores = []\nsensitivity_scores = []\nspecificity_scores = []\nconfusion_matrices = []\nfold_no= 1\n# Modified loop\nfor train_index, test_index in kf.split(X_train_resized):\n    print(f'Fold {fold_no}')\n\n    # Split the data\n    X_train, X_test = X_train_resized[train_index], X_train_resized[test_index]\n    y_train, y_test = Y_train[train_index], Y_train[test_index]\n\n    # Fit the model\n    model.fit(X_train, y_train, epochs=10)\n\n    # Evaluate the model\n    score = model.evaluate(X_test, y_test)\n    scores.append(score)\n\n    # Generate predictions for confusion matrix\n    y_pred_prob = model.predict(X_test)\n    y_pred = np.argmax(y_pred_prob, axis=1)\n    y_true = np.argmax(y_test, axis=1)  # Assuming one-hot encoding for y_test\n\n    # Calculate confusion matrix\n    cm = confusion_matrix(y_true, y_pred)\n    confusion_matrices.append(cm)\n\n    try:\n        roc_auc = roc_auc_score(y_test, y_pred_prob, multi_class='ovr')\n        auc_scores.append(roc_auc)\n        print(f'ROC AUC for fold {fold_no}: {roc_auc}')\n        fpr = {}\n        tpr = {}\n        for i in range(6):  # Assuming 6 classes\n            fpr[i], tpr[i], _ = roc_curve(y_test[:, i], y_pred_prob[:, i])\n            plt.plot(fpr[i], tpr[i], label=f'Class {i} ROC curve (area = {roc_auc:.2f})')\n            \n    except ValueError as e:\n        print(f'ROC AUC calculation skipped for fold {fold_no}: {e}')\n\n\n    # Cohen's Kappa score\n    kappa = cohen_kappa_score(y_true, y_pred)\n    kappa_scores.append(kappa)\n\n    # Sensitivity and Specificity calculation\n    report = classification_report(y_true, y_pred, output_dict=True)\n    sensitivity = report['1']['recall']  # Sensitivity for class 1 (positive class)\n    specificity = report['0']['recall']  # Specificity for class 0 (negative class)\n    sensitivity_scores.append(sensitivity)\n    specificity_scores.append(specificity)\n\n    # Print metrics\n    print(f'Score for fold {fold_no}: {model.metrics_names[1]} of {score[1]*100}%')\n    print(f'Confusion matrix for fold {fold_no}:\\n{cm}')\n    print(f\"Cohen's Kappa for fold {fold_no}: {kappa}\")\n    print(f'Sensitivity for fold {fold_no}: {sensitivity}')\n    print(f'Specificity for fold {fold_no}: {specificity}')\n\n    # Plot ROC Curve for each fold\n\n    plt.xlabel('False Positive Rate')\n    plt.ylabel('True Positive Rate')\n    plt.title(f'ROC Curve for Fold {fold_no}')\n    plt.legend(loc='best')\n    plt.show()\n\n    fold_no += 1\n\n# Calculate and print average metrics across all folds\nprint(f'Average Cohen\\'s Kappa: {np.mean(kappa_scores)}')\nprint(f'Average Sensitivity: {np.mean(sensitivity_scores)}')\nprint(f'Average Specificity: {np.mean(specificity_scores)}')\ntry:\n    print(f'Average ROC AUC: {np.mean(auc_scores)}')\nexcept ValueError as e:\n        print(f'ROC AUC calculation skipped for fold {fold_no}: {e}')","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:58:12.601552Z","iopub.execute_input":"2024-10-07T16:58:12.601919Z","iopub.status.idle":"2024-10-07T17:13:15.707922Z","shell.execute_reply.started":"2024-10-07T16:58:12.601889Z","shell.execute_reply":"2024-10-07T17:13:15.706566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\n\n# Sample ROC AUC values for 6 classes across 7 folds\nroc_values = {\n    'Class 1': [0.89, 0.85, 0.88, 0.90, 0.91, 0.92, 0.93],\n    'Class 2': [0.85, 0.80, 0.82, 0.84, 0.86, 0.87, 0.89],\n    'Class 3': [0.80, 0.78, 0.79, 0.80, 0.82, 0.83, 0.84],\n    'Class 4': [0.83, 0.81, 0.82, 0.84, 0.86, 0.87, 0.88],\n    'Class 5': [0.79, 0.75, 0.77, 0.78, 0.80, 0.81, 0.82],\n    'Class 6': [0.77, 0.75, 0.76, 0.78, 0.79, 0.80, 0.81],\n}\n\n# Fold numbers\nfolds = np.arange(1, 8)\n\n# Create the plot\nplt.figure(figsize=(10, 6))\n\nfor class_label, values in roc_values.items():\n    plt.plot(folds, values, marker='o', label=f'{class_label} (AUC)')\n\nplt.title('ROC AUC Scores for Each Class Across Folds')\nplt.xlabel('Fold Number')\nplt.ylabel('ROC AUC Score')\nplt.ylim(0.7, 1.0)  # Set y-limits for better visibility\nplt.xticks(folds)\nplt.legend(loc='lower right')\nplt.grid(True)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:23:01.801634Z","iopub.execute_input":"2024-10-07T17:23:01.80243Z","iopub.status.idle":"2024-10-07T17:23:02.158555Z","shell.execute_reply.started":"2024-10-07T17:23:01.802392Z","shell.execute_reply":"2024-10-07T17:23:02.15739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Defining the model\nimport tensorflow.keras as K\nfrom tensorflow.keras.layers import Input\n\n# Define the input shape based on the resized image dimensions\ninput_t = Input(shape=(256, 256, 3))\n\n# Load ResNet50 model with the updated input shape\n# res_model = K.applications.ResNeXt50(include_top=False, weights=\"imagenet\", input_tensor=input_t)\n\n# Build the rest of the model\nmodel = K.models.Sequential()\nmodel.add(model13)\nmodel.add(K.layers.Flatten())\nmodel.add(K.layers.Dense(6, activation='softmax'))\nmodel.compile(loss='categorical_crossentropy',optimizer=K.optimizers.RMSprop(lr=0.01),metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:44:23.448153Z","iopub.status.idle":"2024-10-07T16:44:23.448566Z","shell.execute_reply.started":"2024-10-07T16:44:23.44839Z","shell.execute_reply":"2024-10-07T16:44:23.448405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import KFold\nfrom sklearn.metrics import confusion_matrix\nimport numpy as np\n\nk = 13  # Number of folds\nkf = KFold(n_splits=k, shuffle=True, random_state=2)\nfold_no = 1\nscores = []\nconfusion_matrices = []  # To store confusion matrices for each fold\n\nfor train_index, test_index in kf.split(X_train_resized):\n    print(f'Fold {fold_no}')\n\n    # Split the data\n    X_train, X_test = X_train_resized[train_index], X_train_resized[test_index]\n    y_train, y_test = Y_train[train_index], Y_train[test_index]\n\n    # Fit the model\n    model.fit(X_train, y_train, epochs=10)\n\n    # Evaluate the model\n    score = model.evaluate(X_test, y_test)\n    scores.append(score)\n\n    # Generate predictions for confusion matrix\n    y_pred = np.argmax(model.predict(X_test), axis=1)\n    y_true = np.argmax(y_test, axis=1)  # Assuming one-hot encoding for y_test\n\n    # Calculate confusion matrix\n    cm = confusion_matrix(y_true, y_pred)\n    confusion_matrices.append(cm)\n\n    print(f'Score for fold {fold_no}: {model.metrics_names[1]} of {score[1]*100}%')\n    print(f'Confusion matrix for fold {fold_no}:\\n{cm}')\n\n    fold_no += 1\n\n# Calculate and print average accuracy across all folds\naverage_accuracy = np.mean([s[1] for s in scores]) * 100\nprint(f'Average accuracy: {average_accuracy}%')\n","metadata":{"execution":{"iopub.status.busy":"2024-10-07T16:44:23.449751Z","iopub.status.idle":"2024-10-07T16:44:23.450087Z","shell.execute_reply.started":"2024-10-07T16:44:23.449904Z","shell.execute_reply":"2024-10-07T16:44:23.449917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}