{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-12T14:25:25.408061Z","iopub.execute_input":"2023-01-12T14:25:25.4085Z","iopub.status.idle":"2023-01-12T14:25:25.440147Z","shell.execute_reply.started":"2023-01-12T14:25:25.408413Z","shell.execute_reply":"2023-01-12T14:25:25.439086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#unzip files\nimport zipfile\ndirs = ['train.zip','test.zip', 'train_masks.zip', 'train_masks.csv.zip']\nfor x in dirs:\n    with zipfile.ZipFile('../input/carvana-image-masking-challenge/' + x, 'r') as z:\n        z.extractall()","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:25:26.648159Z","iopub.execute_input":"2023-01-12T14:25:26.648862Z","iopub.status.idle":"2023-01-12T14:28:29.013655Z","shell.execute_reply.started":"2023-01-12T14:25:26.648827Z","shell.execute_reply":"2023-01-12T14:28:29.012662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dataframe analysis\ndf = pd.read_csv('./train_masks.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:28:46.734713Z","iopub.execute_input":"2023-01-12T14:28:46.735267Z","iopub.status.idle":"2023-01-12T14:28:47.125226Z","shell.execute_reply.started":"2023-01-12T14:28:46.735232Z","shell.execute_reply":"2023-01-12T14:28:47.124243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/milesial/Pytorch-UNet.git","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:37:14.112164Z","iopub.execute_input":"2023-01-12T14:37:14.112845Z","iopub.status.idle":"2023-01-12T14:37:17.613084Z","shell.execute_reply.started":"2023-01-12T14:37:14.11281Z","shell.execute_reply":"2023-01-12T14:37:17.611721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:37:24.085469Z","iopub.execute_input":"2023-01-12T14:37:24.08586Z","iopub.status.idle":"2023-01-12T14:37:25.102336Z","shell.execute_reply.started":"2023-01-12T14:37:24.085827Z","shell.execute_reply":"2023-01-12T14:37:25.101085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mv train/* Pytorch-UNet/data/imgs/","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:39:46.485949Z","iopub.execute_input":"2023-01-12T14:39:46.486388Z","iopub.status.idle":"2023-01-12T14:39:47.620476Z","shell.execute_reply.started":"2023-01-12T14:39:46.486352Z","shell.execute_reply":"2023-01-12T14:39:47.619169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mv train_masks/* Pytorch-UNet/data/masks/","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:40:40.18999Z","iopub.execute_input":"2023-01-12T14:40:40.190466Z","iopub.status.idle":"2023-01-12T14:40:41.322785Z","shell.execute_reply.started":"2023-01-12T14:40:40.190426Z","shell.execute_reply":"2023-01-12T14:40:41.321515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp train_masks.csv Pytorch-UNet/data/","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:41:27.878076Z","iopub.execute_input":"2023-01-12T14:41:27.878485Z","iopub.status.idle":"2023-01-12T14:41:28.914092Z","shell.execute_reply.started":"2023-01-12T14:41:27.878447Z","shell.execute_reply":"2023-01-12T14:41:28.912734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd Pytorch-UNet/","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:41:46.977271Z","iopub.execute_input":"2023-01-12T14:41:46.978032Z","iopub.status.idle":"2023-01-12T14:41:46.98652Z","shell.execute_reply.started":"2023-01-12T14:41:46.977992Z","shell.execute_reply":"2023-01-12T14:41:46.985299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:41:51.961844Z","iopub.execute_input":"2023-01-12T14:41:51.962224Z","iopub.status.idle":"2023-01-12T14:41:52.946241Z","shell.execute_reply.started":"2023-01-12T14:41:51.962193Z","shell.execute_reply":"2023-01-12T14:41:52.945091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import argparse\nimport logging\nimport os\nimport random\nimport sys\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torchvision.transforms as transforms\nimport torchvision.transforms.functional as TF\nfrom pathlib import Path\nfrom torch import optim\nfrom torch.utils.data import DataLoader, random_split\nfrom tqdm import tqdm\n\nimport wandb\nfrom evaluate import evaluate\nfrom unet import UNet\nfrom utils.data_loading import BasicDataset, CarvanaDataset\nfrom utils.dice_score import dice_loss","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:43:32.323978Z","iopub.execute_input":"2023-01-12T14:43:32.324412Z","iopub.status.idle":"2023-01-12T14:43:35.502076Z","shell.execute_reply.started":"2023-01-12T14:43:32.324374Z","shell.execute_reply":"2023-01-12T14:43:35.500794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_args():\n    parser = argparse.ArgumentParser(description='Train the UNet on images and target masks')\n    parser.add_argument('--epochs', '-e', metavar='E', type=int, default=5, help='Number of epochs')\n    parser.add_argument('--batch-size', '-b', dest='batch_size', metavar='B', type=int, default=1, help='Batch size')\n    parser.add_argument('--learning-rate', '-l', metavar='LR', type=float, default=1e-5,\n                        help='Learning rate', dest='lr')\n    parser.add_argument('--load', '-f', type=str, default=False, help='Load model from a .pth file')\n    parser.add_argument('--scale', '-s', type=float, default=0.5, help='Downscaling factor of the images')\n    parser.add_argument('--validation', '-v', dest='val', type=float, default=10.0,\n                        help='Percent of the data that is used as validation (0-100)')\n    parser.add_argument('--amp', action='store_true', default=False, help='Use mixed precision')\n    parser.add_argument('--bilinear', action='store_true', default=False, help='Use bilinear upsampling')\n    parser.add_argument('--classes', '-c', type=int, default=2, help='Number of classes')\n\n    return parser.parse_args()","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:44:32.021982Z","iopub.execute_input":"2023-01-12T14:44:32.022406Z","iopub.status.idle":"2023-01-12T14:44:32.034263Z","shell.execute_reply.started":"2023-01-12T14:44:32.022373Z","shell.execute_reply":"2023-01-12T14:44:32.033143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"args = get_args()","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:44:36.666086Z","iopub.execute_input":"2023-01-12T14:44:36.666458Z","iopub.status.idle":"2023-01-12T14:44:36.67217Z","shell.execute_reply.started":"2023-01-12T14:44:36.666426Z","shell.execute_reply":"2023-01-12T14:44:36.671247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"args","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:44:41.681072Z","iopub.execute_input":"2023-01-12T14:44:41.682034Z","iopub.status.idle":"2023-01-12T14:44:41.689827Z","shell.execute_reply.started":"2023-01-12T14:44:41.681977Z","shell.execute_reply":"2023-01-12T14:44:41.688538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Change here to adapt to your data\n# n_channels=3 for RGB images\n# n_classes is the number of probabilities you want to get per pixel\n\n\nmodel = UNet(n_channels=3, n_classes=args.classes, bilinear=args.bilinear)\nmodel = model.to(memory_format=torch.channels_last)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:44:46.483695Z","iopub.execute_input":"2023-01-12T14:44:46.484055Z","iopub.status.idle":"2023-01-12T14:44:46.951469Z","shell.execute_reply.started":"2023-01-12T14:44:46.484025Z","shell.execute_reply":"2023-01-12T14:44:46.950336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:44:52.359222Z","iopub.execute_input":"2023-01-12T14:44:52.359638Z","iopub.status.idle":"2023-01-12T14:44:52.369155Z","shell.execute_reply.started":"2023-01-12T14:44:52.359606Z","shell.execute_reply":"2023-01-12T14:44:52.36794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = torch.randn(1,3,224,224)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:45:23.307865Z","iopub.execute_input":"2023-01-12T14:45:23.309566Z","iopub.status.idle":"2023-01-12T14:45:23.322555Z","shell.execute_reply.started":"2023-01-12T14:45:23.309524Z","shell.execute_reply":"2023-01-12T14:45:23.321551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    y = model(x)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:45:58.922159Z","iopub.execute_input":"2023-01-12T14:45:58.92293Z","iopub.status.idle":"2023-01-12T14:46:00.142241Z","shell.execute_reply.started":"2023-01-12T14:45:58.922896Z","shell.execute_reply":"2023-01-12T14:46:00.141096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x.shape, y.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T14:46:13.257301Z","iopub.execute_input":"2023-01-12T14:46:13.257761Z","iopub.status.idle":"2023-01-12T14:46:13.267376Z","shell.execute_reply.started":"2023-01-12T14:46:13.257723Z","shell.execute_reply":"2023-01-12T14:46:13.266178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}