{"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":30201,"databundleVersionId":2750748,"sourceType":"competition"},{"sourceId":2931428,"sourceType":"datasetVersion","datasetId":1787847},{"sourceId":8428218,"sourceType":"datasetVersion","datasetId":4932282},{"sourceId":8450845,"sourceType":"datasetVersion","datasetId":5035958},{"sourceId":8450857,"sourceType":"datasetVersion","datasetId":5036232},{"sourceId":8457388,"sourceType":"datasetVersion","datasetId":5035964}],"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\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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\npd.options.plotting.backend = \"plotly\"\nimport random\nfrom glob import glob\nimport os, shutil\nfrom tqdm import tqdm\ntqdm.pandas()\nimport time\nimport copy\nimport joblib\nfrom collections import defaultdict\nimport gc\nfrom IPython import display as ipd\n\n# visualization\nimport cv2\nimport matplotlib.pyplot as plt\nimport csv\n\n# Sklearn\nfrom sklearn.model_selection import StratifiedKFold, KFold\nfrom sklearn.metrics import confusion_matrix, precision_score, recall_score\n\n# PyTorch \nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torch.nn.functional as F\nfrom torch.optim import lr_scheduler\nfrom torch.utils.data import Dataset, DataLoader\nfrom torch.cuda import amp\n\n#import timm\nimport seaborn as sns\nimport itertools\nimport tifffile\n\n# Albumentations for augmentations\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\n\n# For colored terminal text\nfrom colorama import Fore, Back, Style\nc_  = Fore.GREEN\nsr_ = Style.RESET_ALL\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n# For descriptive error messages\nos.environ['CUDA_LAUNCH_BLOCKING'] = \"1\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-19T08:43:03.759452Z","iopub.execute_input":"2024-05-19T08:43:03.759708Z","iopub.status.idle":"2024-05-19T08:43:10.592467Z","shell.execute_reply.started":"2024-05-19T08:43:03.759684Z","shell.execute_reply":"2024-05-19T08:43:10.591662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed = 42):\n    '''Sets the seed of the entire notebook so results are the same every time we run.\n    This is for REPRODUCIBILITY.'''\n    np.random.seed(seed)\n    random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    # When running on the CuDNN backend, two further options must be set\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n    # Set a fixed value for the hash seed\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    print('> SEEDING DONE')\n    \nset_seed(101)","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:43:10.594118Z","iopub.execute_input":"2024-05-19T08:43:10.594647Z","iopub.status.idle":"2024-05-19T08:43:10.604289Z","shell.execute_reply.started":"2024-05-19T08:43:10.594614Z","shell.execute_reply":"2024-05-19T08:43:10.603394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dependencies","metadata":{}},{"cell_type":"code","source":"!rm -rf cellpose","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:43:10.609113Z","iopub.execute_input":"2024-05-19T08:43:10.609417Z","iopub.status.idle":"2024-05-19T08:43:11.553703Z","shell.execute_reply.started":"2024-05-19T08:43:10.609394Z","shell.execute_reply":"2024-05-19T08:43:11.552664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install fastremap\n!pip install roifile\n!pip install natsort\n!pip install segmentation_models_pytorch","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:43:11.555114Z","iopub.execute_input":"2024-05-19T08:43:11.555409Z","iopub.status.idle":"2024-05-19T08:44:07.954291Z","shell.execute_reply.started":"2024-05-19T08:43:11.555380Z","shell.execute_reply":"2024-05-19T08:44:07.953121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/paesbe17/cellpose.git","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:07.955704Z","iopub.execute_input":"2024-05-19T08:44:07.956010Z","iopub.status.idle":"2024-05-19T08:44:17.003773Z","shell.execute_reply.started":"2024-05-19T08:44:07.955981Z","shell.execute_reply":"2024-05-19T08:44:17.002806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from cellpose.cellpose import core, utils, io, models, metrics, dynamics\nfrom glob import glob","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:17.005172Z","iopub.execute_input":"2024-05-19T08:44:17.005484Z","iopub.status.idle":"2024-05-19T08:44:31.617762Z","shell.execute_reply.started":"2024-05-19T08:44:17.005456Z","shell.execute_reply":"2024-05-19T08:44:31.616843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualising Flows\n#### 5-fold data Generated in cellpose format here: https://www.kaggle.com/ks2019/sartorius-train-tif","metadata":{}},{"cell_type":"code","source":"def plot_example(img_path):\n    mask_path = img_path.replace('img','masks')\n    flow_path = img_path.replace('img','flows')\n    img = tifffile.imread(img_path)\n    masks = tifffile.imread(mask_path)\n    flows = tifffile.imread(flow_path)\n    plt.figure(figsize=(25,10))\n    plt.subplot(2,3,1)\n    plt.axis('off')\n    plt.imshow(img)\n    plt.title('Imagen Original')\n    plt.subplot(2,3,2)\n    plt.axis('off')\n    plt.imshow(masks)\n    plt.title('Máscara de instancias')\n    for k in range(4):\n        plt.subplot(2,3,3+k)\n        plt.axis('off')\n        plt.imshow(flows[k])\n        plt.title(f'Representaciones de los flujos {k}')\n    plt.show()\n    \nroot = '/kaggle/input/45grados-cellpose/cellpose-90grados/'\nsample_paths = os.listdir(root)\nsample_paths = [x for x in sample_paths if 'img' in x]\nrandom.shuffle(sample_paths)\nfor k in range(3):\n    img_path = sample_paths[k]\n    plot_example(root+img_path)","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:31.619032Z","iopub.execute_input":"2024-05-19T08:44:31.619668Z","iopub.status.idle":"2024-05-19T08:44:34.385677Z","shell.execute_reply.started":"2024-05-19T08:44:31.619632Z","shell.execute_reply":"2024-05-19T08:44:34.384709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preparación de los datos","metadata":{}},{"cell_type":"code","source":"BASE_PATH  = '/kaggle/input/sartorius-cell-instance-segmentation'\nBASE_PATH2 = '/kaggle/input/cellpose-flows-512x512/sartorius-cellpose-dataset'\nBASE_PATH3 = '/kaggle/input/df-orientation-cellpose'\nBASE_PATH4 = '/kaggle/input/45grados-cellpose/cellpose-90grados'","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:34.386942Z","iopub.execute_input":"2024-05-19T08:44:34.387249Z","iopub.status.idle":"2024-05-19T08:44:34.391721Z","shell.execute_reply.started":"2024-05-19T08:44:34.387223Z","shell.execute_reply":"2024-05-19T08:44:34.390764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train Data\ndf               = pd.read_csv(f'{BASE_PATH}/train.csv')\ndf['image_path'] = BASE_PATH2 + '/' + df['id'] + '_img.tif'\ntmp_df           = df.drop_duplicates(subset=[\"id\", \"image_path\"]).reset_index(drop=True)\ntmp_df[\"annotation\"] = df.groupby(\"id\")[\"annotation\"].agg(list).reset_index(drop=True)\ndf               = tmp_df.copy()\ndf['mask_path']  = BASE_PATH2 + '/' + df['id'] + '_masks.tif'\ndf['flow_path']  = BASE_PATH2 + '/' + df['id'] + '_flows.tif'\ndisplay(df.head(2))","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:34.395735Z","iopub.execute_input":"2024-05-19T08:44:34.395999Z","iopub.status.idle":"2024-05-19T08:44:35.056012Z","shell.execute_reply.started":"2024-05-19T08:44:34.395976Z","shell.execute_reply":"2024-05-19T08:44:35.055061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train Data\ndf_90           = pd.read_csv(f'{BASE_PATH3}/df_90.csv')\ndf_90['image_path'] = BASE_PATH4 + '/' + df_90['id'] + '_img.tif'\ntmp_df           = df_90.drop_duplicates(subset=[\"id\", \"image_path\"]).reset_index(drop=True)\ntmp_df[\"annotation\"] = df_90.groupby(\"id\")[\"annotation\"].agg(list).reset_index(drop=True)\ndf_90               = tmp_df.copy()\ndf_90['mask_path']  = BASE_PATH4 + '/' + df_90['id'] + '_masks.tif'\ndf_90['flow_path']  = BASE_PATH4 + '/' + df_90['id'] + '_flows.tif'\ndisplay(df_90.head(2))","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:35.057108Z","iopub.execute_input":"2024-05-19T08:44:35.057429Z","iopub.status.idle":"2024-05-19T08:44:35.153562Z","shell.execute_reply.started":"2024-05-19T08:44:35.057402Z","shell.execute_reply":"2024-05-19T08:44:35.152648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=101)\nfor fold, (train_idx, val_idx) in enumerate(skf.split(df, df[\"cell_type\"])):\n    df.loc[val_idx, 'fold'] = fold\ndisplay(df.groupby(['fold'])['id'].count())","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:35.154525Z","iopub.execute_input":"2024-05-19T08:44:35.154775Z","iopub.status.idle":"2024-05-19T08:44:35.173782Z","shell.execute_reply.started":"2024-05-19T08:44:35.154754Z","shell.execute_reply":"2024-05-19T08:44:35.172945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=101)\nfor fold, (train_idx, val_idx) in enumerate(skf.split(df_90, df_90[\"cell_type\"])):\n    df_90.loc[val_idx, 'fold'] = fold\ndisplay(df_90.groupby(['fold'])['id'].count())","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:35.174919Z","iopub.execute_input":"2024-05-19T08:44:35.175312Z","iopub.status.idle":"2024-05-19T08:44:35.191271Z","shell.execute_reply.started":"2024-05-19T08:44:35.175277Z","shell.execute_reply":"2024-05-19T08:44:35.190391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class BuildDataset(torch.utils.data.Dataset):\n    def __init__(self, df, transforms=None):\n        self.df = df\n        self.img_paths = df['image_path'].values\n        self.msk_paths = df['mask_path'].values if 'mask_path' in df.columns else None\n        self.flow_paths = df['flow_path'].values if 'flow_path' in df.columns else None\n        self.transforms = transforms\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, index):\n        img_path = self.img_paths[index]\n        img = tifffile.imread(img_path)\n\n        msk = None\n        if self.msk_paths is not None:\n            msk_path = self.msk_paths[index]\n            msk = tifffile.imread(msk_path)\n\n        if self.flow_paths is not None:\n            flow_path = self.flow_paths[index]\n            flow = tifffile.imread(flow_path)\n\n        if self.transforms:\n            # Aplicar transformaciones a la imagen y la máscara\n            data = self.transforms(image=img, mask=msk)\n            img  = data['image']\n            msk  = data['mask']\n        return img, msk, flow\n    \n    def get_img_path(self, index):\n        return self.img_paths[index]\n\n    def get_mask_path(self, index):\n        return self.msk_paths[index] if self.msk_paths is not None else None\n\n    def get_flow_path(self, index):\n        return self.flow_paths[index] if self.flow_paths is not None else None\n    \n    def get_img_name(self, index):\n        return os.path.basename(self.get_img_path(index))\n\n    def get_mask_name(self, index):\n        return os.path.basename(self.get_mask_path(index)) if self.get_mask_path(index) else None\n\n    def get_flow_name(self, index):\n        return os.path.basename(self.get_flow_path(index)) if self.get_flow_path(index) else None","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:35.192331Z","iopub.execute_input":"2024-05-19T08:44:35.192648Z","iopub.status.idle":"2024-05-19T08:44:35.204802Z","shell.execute_reply.started":"2024-05-19T08:44:35.192623Z","shell.execute_reply":"2024-05-19T08:44:35.203884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_transforms = {\n    \"train\": A.Compose([\n        A.Resize(512, 512),\n        A.CLAHE(p=0.35),\n        A.ColorJitter(p=0.5),\n        A.HorizontalFlip(p=0.5),\n        A.VerticalFlip(p=0.5),\n        A.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.1, rotate_limit=90, p=0.5),\n        A.OneOf([\n            A.GridDistortion(num_steps=5, distort_limit=0.05, p=1.0),\n            A.ElasticTransform(alpha=1, sigma=50, alpha_affine=50, p=1.0)\n        ], p=0.25),\n        #A.CoarseDropout(max_holes=8, max_height=512//20, max_width=512//20,\n                         #min_holes=5, fill_value=0, mask_fill_value=0, p=0.5),\n    ]),\n    \"valid\": A.Compose([\n        A.Resize(512, 512),\n    ]),\n}","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:35.205915Z","iopub.execute_input":"2024-05-19T08:44:35.206178Z","iopub.status.idle":"2024-05-19T08:44:35.217895Z","shell.execute_reply.started":"2024-05-19T08:44:35.206125Z","shell.execute_reply":"2024-05-19T08:44:35.217011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepare_loaders(fold):\n    train_df = df[df.fold != fold].reset_index(drop=True)\n    valid_df = df_90[df_90.fold == fold].reset_index(drop=True)\n    \n    train_dataset = BuildDataset(train_df, transforms=data_transforms['train'])\n    valid_dataset = BuildDataset(valid_df, transforms=data_transforms['valid'])\n    \n    return train_dataset, valid_dataset","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:35.218973Z","iopub.execute_input":"2024-05-19T08:44:35.219346Z","iopub.status.idle":"2024-05-19T08:44:35.229457Z","shell.execute_reply.started":"2024-05-19T08:44:35.219315Z","shell.execute_reply":"2024-05-19T08:44:35.228761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset, valid_dataset = prepare_loaders(fold=0)","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:35.230575Z","iopub.execute_input":"2024-05-19T08:44:35.230823Z","iopub.status.idle":"2024-05-19T08:44:35.241405Z","shell.execute_reply.started":"2024-05-19T08:44:35.230802Z","shell.execute_reply":"2024-05-19T08:44:35.240591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_batch(imgs, msks):\n    if len(imgs) == 1:\n        fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12, 4))\n        \n        img = imgs[0]\n        msk = msks[0]\n\n        axes[0].imshow(img)\n        axes[0].set_title('Imagen Original', fontsize=15)\n        axes[0].axis('off')\n\n        axes[1].imshow(msk)\n        axes[1].set_title('Máscara', fontsize=15)\n        axes[1].axis('off')\n\n        axes[2].imshow(img)\n        axes[2].imshow(msk, alpha=0.3)\n        axes[2].set_title('Superposición de ambas', fontsize=15)\n        axes[2].axis('off')\n\n    plt.tight_layout()\n    plt.show()\n\n# Aplicar la función plot_batch a las primeras tres imágenes y máscaras\nfor i in range(3):\n    img, msk, flows = train_dataset[i]\n    plot_batch([img], [msk])  # Aquí se pasan listas con un solo elemento","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:35.242431Z","iopub.execute_input":"2024-05-19T08:44:35.244248Z","iopub.status.idle":"2024-05-19T08:44:37.590087Z","shell.execute_reply.started":"2024-05-19T08:44:35.244222Z","shell.execute_reply":"2024-05-19T08:44:37.589164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Aplicar la función plot_batch a las primeras tres imágenes y máscaras\nfor i in range(3):\n    img, msk, flows = valid_dataset[i]\n    plot_batch([img], [msk])  # Aquí se pasan listas con un solo elemento","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:37.591458Z","iopub.execute_input":"2024-05-19T08:44:37.591792Z","iopub.status.idle":"2024-05-19T08:44:39.313512Z","shell.execute_reply.started":"2024-05-19T08:44:37.591763Z","shell.execute_reply":"2024-05-19T08:44:39.312559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Imprimir el formato de la imagen\nprint(\"Formato de la imagen:\")\nprint(\"Tipo de datos:\", type(img))\nprint(\"Dimensiones de la imagen:\", img.shape)\nprint(\"Tipo de datos de píxeles:\", img.dtype)\nprint(\"Valor mínimo de la imagen:\", np.min(img))\nprint(\"Valor máximo de la imagen:\", np.max(img))\n\n# Imprimir el formato de la máscara (si está disponible)\nif msk is not None:\n    print(\"\\nFormato de la máscara:\")\n    print(\"Tipo de datos:\", type(msk))\n    print(\"Dimensiones de la máscara:\", msk.shape)\n    print(\"Tipo de datos de píxeles:\", msk.dtype)\n    print(\"\\nValor mínimo de la máscara:\", np.min(msk))\n    print(\"Valor máximo de la máscara:\", np.max(msk))\nelse:\n    print(\"\\nNo hay máscara disponible para esta imagen.\")","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:39.314675Z","iopub.execute_input":"2024-05-19T08:44:39.314968Z","iopub.status.idle":"2024-05-19T08:44:39.322956Z","shell.execute_reply.started":"2024-05-19T08:44:39.314942Z","shell.execute_reply":"2024-05-19T08:44:39.322139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train parameters","metadata":{}},{"cell_type":"code","source":"!mkdir './fold_0'\n!mkdir './fold_1'\n!mkdir './fold_2'\n!mkdir './fold_3'\n!mkdir './fold_4'","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:39.324277Z","iopub.execute_input":"2024-05-19T08:44:39.324576Z","iopub.status.idle":"2024-05-19T08:44:44.062626Z","shell.execute_reply.started":"2024-05-19T08:44:39.324551Z","shell.execute_reply":"2024-05-19T08:44:44.061511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from cellpose.cellpose import train\nmodel_to_load = 'cyto' ## cyto, cyto2, nuclei\nnumber_of_epochs = 50  ## Train more epochs for better results\nbatch_size = 8\nweight_decay = 0.0001 #@param {type:\"number\"}\ninitial_learning_rate = 0.005 # copiado del de unet\nTraining_channel = 0 # For grayscale\nSecond_training_channel= 0\nscheduler_type= 'CosineAnnealingLR'","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:44.064053Z","iopub.execute_input":"2024-05-19T08:44:44.064368Z","iopub.status.idle":"2024-05-19T08:44:49.348766Z","shell.execute_reply.started":"2024-05-19T08:44:44.064339Z","shell.execute_reply":"2024-05-19T08:44:49.347861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Conjunto de entrenamiento","metadata":{}},{"cell_type":"code","source":"def prepare_fold(fold):\n    # Obtener los datasets para la fold específica\n    train_dataset, valid_dataset = prepare_loaders(fold=fold)\n    \n    # Crear listas vacías para almacenar imágenes y máscaras\n    train_data = []\n    train_labels = []\n\n    # Iterar sobre train_dataset para obtener imágenes y máscaras\n    for img, msk, flows in train_dataset:\n        train_data.append(img)\n        train_labels.append(msk)\n    \n    # Crear listas vacías para almacenar imágenes y máscaras\n    test_data = []\n    test_labels = []\n\n    # Iterar sobre train_dataset para obtener imágenes y máscaras\n    for img, msk, flows in valid_dataset:\n        test_data.append(img)\n        test_labels.append(msk)\n    return train_data, train_labels, test_data, test_labels","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:44:49.350392Z","iopub.execute_input":"2024-05-19T08:44:49.350750Z","iopub.status.idle":"2024-05-19T08:44:49.357991Z","shell.execute_reply.started":"2024-05-19T08:44:49.350714Z","shell.execute_reply":"2024-05-19T08:44:49.356711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_loss_train = [] \nall_loss_val = [] \nall_dice_train = [] \nall_dice_val = []\n\n# Definir la ruta base del archivo CSV\nbase_csv_file_path = \"resultados_entrenamiento_fold\"\n\n# Definir la carpeta de salida para los archivos CSV\noutput_folder = \"resultados_folds\"\nos.makedirs(output_folder, exist_ok=True)\n\n# Definir la ruta del archivo CSV general\ngeneral_csv_file_path = \"resultados_entrenamiento_best_epoch.csv\"\n\n# Crear o abrir el archivo CSV general en modo de escritura\nwith open(general_csv_file_path, mode='w', newline='') as general_file:\n    general_writer = csv.writer(general_file)\n\n    # Escribir encabezados en el archivo CSV general\n    general_writer.writerow(['Fold', 'Best_Epoch', 'Dice_Train', 'Dice_Val', 'Loss_Train', 'Loss_Val', 'Sensitivity_Train', 'Precision_Train', 'Sensitivity_Val', 'Precision_Val'])\n\n    # Iterar sobre los folds\n    for i in range(5):\n        # Imprimir el número de fold actual\n        print(f\"Entrenando Fold {i}...\")\n\n        # Activar GPU si está disponible\n        use_GPU = core.use_gpu()\n        print('>>> GPU activated? %d'%use_GPU)\n\n        # Crear el modelo Cellpose\n        model = models.CellposeModel(gpu=use_GPU, model_type=model_to_load)\n\n        # Cargar datos de entrenamiento y prueba\n        train_data, train_labels, test_data, test_labels = prepare_fold(i)\n\n        # Entrenar el modelo\n        model_path, epochs, loss_train, loss_val, dice_train, dice_val, sensitivity_train, precision_train, sensitivity_valid, precision_valid = train.train_seg(\n            model.net,\n            train_data=train_data,\n            train_labels=train_labels,\n            test_data=test_data,\n            test_labels=test_labels,\n            channels=[Training_channel, Second_training_channel],\n            save_path=f'./fold_{i}',  # Guardar los modelos en carpetas separadas para cada fold\n            n_epochs=number_of_epochs,\n            learning_rate=initial_learning_rate,\n            weight_decay=weight_decay,\n            batch_size=batch_size,\n            scheduler_type = scheduler_type\n        )\n\n        # Encontrar la mejor época\n        best_epoch = np.argmax(dice_val)\n        best_epoch_dice_train = dice_train[best_epoch]\n        best_epoch_dice_val = dice_val[best_epoch]\n        best_epoch_loss_train = loss_train[best_epoch]\n        best_epoch_loss_val = loss_val[best_epoch]\n\n        # Escribir los resultados de la mejor época en el archivo CSV general\n        general_writer.writerow([i, best_epoch, best_epoch_dice_train, best_epoch_dice_val, \n                         best_epoch_loss_train, best_epoch_loss_val, sensitivity_train, precision_train, sensitivity_valid, precision_valid])\n\n        print(f\"Los resultados del Fold {i} de la mejor época se han almacenado en el archivo CSV:\", general_csv_file_path)\n    \n        # Almacenar las pérdidas de entrenamiento y validación de este fold\n        all_loss_train.append(loss_train)\n        all_loss_val.append(loss_val)\n        all_dice_train.append(dice_train)\n        all_dice_val.append(dice_val)\n\n        # Definir la ruta del archivo CSV específico para este fold\n        fold_csv_file_path = os.path.join(output_folder, f\"{base_csv_file_path}_{i}.csv\")\n\n        # Crear o abrir el archivo CSV específico para este fold en modo de escritura\n        with open(fold_csv_file_path, mode='w', newline='') as fold_file:\n            fold_writer = csv.writer(fold_file)\n            fold_writer.writerow(['Epoch', 'Loss_train', 'Loss_val', 'Dice_train', 'Dice_val'])\n            for epoch, lt, lv, dt, dv in zip(epochs, loss_train, loss_val, dice_train, dice_val):\n                fold_writer.writerow([epoch, lt, lv, dt, dv])\n\n            print(f\"Los resultados del Fold {i} se han almacenado en el archivo CSV:\", fold_csv_file_path)","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:47:02.058577Z","iopub.execute_input":"2024-05-19T08:47:02.058941Z","iopub.status.idle":"2024-05-19T08:47:04.402915Z","shell.execute_reply.started":"2024-05-19T08:47:02.058914Z","shell.execute_reply":"2024-05-19T08:47:04.401303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calcular la media de las pérdidas de entrenamiento y validación de todos los folds\nmean_loss_train = np.mean(all_loss_train, axis=0)\nmean_loss_val = np.mean(all_loss_val, axis=0)\n\n# Trazar la media de las pérdidas de entrenamiento y validación\nfig = plt.figure(figsize=(6, 4))\nplt.plot(epochs, mean_loss_train, label='Train Loss')\nplt.plot(epochs, mean_loss_val, label='Validation Loss')\nplt.legend()\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.title('Media de la Loss según la época para train y val')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:47:01.631612Z","iopub.status.idle":"2024-05-19T08:47:01.631968Z","shell.execute_reply.started":"2024-05-19T08:47:01.631799Z","shell.execute_reply":"2024-05-19T08:47:01.631815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calcular la media de las pérdidas de entrenamiento y validación de todos los folds\nmean_dice_train = np.mean(all_dice_train, axis=0)\nmean_dice_val = np.mean(all_dice_val, axis=0)\n\n# Trazar la media de las pérdidas de entrenamiento y validación\nfig = plt.figure(figsize=(6, 4))\nplt.plot(epochs, mean_dice_train, label='Train Dice')\nplt.plot(epochs, mean_dice_val, label='Validation Dice')\nplt.legend()\nplt.xlabel('Epoch')\nplt.ylabel('Dice')\nplt.title('Media del Índice de Dice según la época para train y val')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:47:01.633506Z","iopub.status.idle":"2024-05-19T08:47:01.633840Z","shell.execute_reply.started":"2024-05-19T08:47:01.633675Z","shell.execute_reply":"2024-05-19T08:47:01.633689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Definir la ruta del archivo CSV\ncsv_file_path = \"resultados_entrenamiento_media.csv\"\n\n# Calcular la media de las pérdidas de entrenamiento y validación\nmean_loss_train = np.mean(all_loss_train, axis=0)\nmean_loss_val = np.mean(all_loss_val, axis=0)\nmean_dice_train = np.mean(all_dice_train, axis=0)\nmean_dice_val = np.mean(all_dice_val, axis=0)\n\n# Crear o abrir el archivo CSV en modo de escritura\nwith open(csv_file_path, mode='w', newline='') as file:\n    writer = csv.writer(file)\n\n    # Escribir encabezados\n    writer.writerow(['Epoch', 'Loss_train', 'Loss_val', 'Jaccard_train', 'Jaccard_val'])\n\n    # Escribir los datos de cada época\n    for i in range(len(epochs)):\n        epoch_data = [epochs[i], mean_loss_train[i], mean_loss_val[i], mean_dice_train[i], mean_dice_val[i]]\n        writer.writerow(epoch_data)\n\nprint(\"Los resultados se han almacenado en el archivo CSV:\", csv_file_path)","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:47:01.635286Z","iopub.status.idle":"2024-05-19T08:47:01.635624Z","shell.execute_reply.started":"2024-05-19T08:47:01.635463Z","shell.execute_reply":"2024-05-19T08:47:01.635477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# diameter of labels in training images\ndiam_labels = model.net.diam_labels.item()\nprint(diam_labels)","metadata":{"execution":{"iopub.status.busy":"2024-05-19T08:47:01.637131Z","iopub.status.idle":"2024-05-19T08:47:01.637600Z","shell.execute_reply.started":"2024-05-19T08:47:01.637365Z","shell.execute_reply":"2024-05-19T08:47:01.637384Z"},"trusted":true},"execution_count":null,"outputs":[]}]}