{"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":"!pip install segmentation_models_pytorch\n!pip -q install cairosvg==2.5.2\n!pip -q install reportlab==3.5.65\n!pip -q install cssutils==2.2.0\n\nimport numpy as np\nfrom PIL import Image\nimport pandas as pd\nimport cv2\nimport glob\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms\nimport torch\nfrom matplotlib import pyplot as plt\nimport os\nfrom tqdm import tqdm_notebook as tqdm\nfrom skimage.io import imread\nimport segmentation_models_pytorch as smp\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nfrom segmentation_models_pytorch.encoders import get_preprocessing_fn\nfrom segmentation_models_pytorch.utils.metrics import IoU\nfrom segmentation_models_pytorch.losses import DiceLoss\n\n# Set random seed for reproducibility\nseed = 42\ntorch.manual_seed(seed)\nnp.random.seed(seed)\n\n# Constants\nENCODER = 'resnet18'\nENCODER_WEIGHTS = 'imagenet'\nCLASSES = ['ships']\nACTIVATION = 'sigmoid' # could be None for logits or 'softmax2d' for multiclass segmentation\nDEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'\nLEARNING_RATE = 0.0001\nNUM_EPOCHS = 40\nDECAY_EPOCHS = 25\nBATCH_SIZE = 64","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-28T09:20:07.387582Z","iopub.execute_input":"2023-05-28T09:20:07.388052Z","iopub.status.idle":"2023-05-28T09:21:56.716898Z","shell.execute_reply.started":"2023-05-28T09:20:07.388019Z","shell.execute_reply":"2023-05-28T09:21:56.715556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create segmentation model with pretrained encoder\nmodel = smp.FPN(\n    encoder_depth=5,\n    encoder_name=ENCODER, \n    encoder_weights=ENCODER_WEIGHTS, \n    classes=len(CLASSES), \n    activation=ACTIVATION,\n)\n\npreprocessing_fn = smp.encoders.get_preprocessing_fn(ENCODER, ENCODER_WEIGHTS)\nloss = DiceLoss(mode='binary')\nmetrics = [\n    IoU(threshold=0.5),\n]\n\noptimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)\n\ndef rle_decode(mask_rle, shape=(768, 768)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height, width) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0] * shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T  # Needed to align to RLE direction\n\nclass MyDataset(Dataset):\n    def __init__(self, data_dir, paths, masks_df, transform=None, train=False,size=224):\n        self.data_dir = data_dir\n        self.paths = paths\n        self.masks_df = masks_df\n        self.transform = transform\n        self.train = train\n        self.length = len(self.paths)\n        self.size = size\n\n    \n    def __len__(self):\n        return self.length\n    \n    def __getitem__(self, idx):\n        p = os.path.join(self.data_dir, self.paths[idx])\n        ImageId = p.split(\"/\")[1]\n        img_masks = self.masks_df.loc[self.masks_df['ImageId'] == ImageId, 'EncodedPixels'].tolist()\n        all_masks = np.zeros((768, 768))\n        for mask in img_masks:\n            all_masks += rle_decode(mask, (768, 768))\n    \n        image = cv2.imread(p)\n        all_masks = cv2.resize(all_masks, (224, 224))\n        image = cv2.resize(image, (self.size, self.size))\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    \n        if self.train:\n            image = Image.fromarray(image)\n            image = self.transform(image)\n    \n        image = preprocessing_fn(np.array(image))\n    \n        return image.transpose([2, 0, 1]).astype(np.float32), all_masks\n\n\n\n\n\n\nmasks = pd.read_csv(\"../input/airbus-ship-detection/train_ship_segmentations_v2.csv\") \npaths = masks.ImageId.array","metadata":{"execution":{"iopub.status.busy":"2023-05-28T09:23:12.181947Z","iopub.execute_input":"2023-05-28T09:23:12.182369Z","iopub.status.idle":"2023-05-28T09:23:14.09755Z","shell.execute_reply.started":"2023-05-28T09:23:12.182336Z","shell.execute_reply":"2023-05-28T09:23:14.096345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision import transforms\n# Data augmentation and normalization\ntrain_transform = transforms.Compose([\n    transforms.RandomHorizontalFlip(),\n    transforms.RandomRotation((-120, 120)),\n    transforms.ToTensor(),\n    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n])\ndata_dir = '/kaggle/input/airbus-ship-detection/sample_submission_v2.csv'\ntrainset = MyDataset(data_dir, paths, masks, transform=train_transform, train=True,size=224)\ntrain_loader = DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True, num_workers=0)\n\n# Training loop\nmax_score = 0\n\nfor epoch in range(NUM_EPOCHS):\n    print('\\nEpoch: {}'.format(epoch))\n    model.train()\n    train_loss = 0.0\n    iou_score = 0.0\n    \n    for batch_idx, (inputs, masks) in enumerate(tqdm(train_loader)):\n        inputs = inputs.to(DEVICE)\n        masks = masks.to(DEVICE)\n        \n        optimizer.zero_grad()\n        outputs = model(inputs)\n        loss_value = loss(outputs, masks)\n        loss_value.backward()\n        optimizer.step()\n        \n        train_loss += loss_value.item()\n        iou_score += metrics[0](outputs, masks).item()\n    \n    train_loss /= len(train_loader)\n    iou_score /= len(train_loader)\n    \n    print('Train Loss: {:.4f}, IOU Score: {:.4f}'.format(train_loss, iou_score))\n    \n    if iou_score > max_score:\n        max_score = iou_score\n        torch.save(model.state_dict(), './best_model.pth')\n        print('Model saved!')\n    \n    if epoch == DECAY_EPOCHS:\n        optimizer.param_groups[0]['lr'] = 1e-5\n        print('Decrease decoder learning rate to 1e-5!')\n","metadata":{"execution":{"iopub.status.busy":"2023-05-28T09:31:23.474537Z","iopub.execute_input":"2023-05-28T09:31:23.475499Z","iopub.status.idle":"2023-05-28T09:31:23.742319Z","shell.execute_reply.started":"2023-05-28T09:31:23.475446Z","shell.execute_reply":"2023-05-28T09:31:23.740739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install torch torchvision\n\nimport os\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision.transforms import transforms\nimport segmentation_models_pytorch as smp\n\n# Set the device\nDEVICE = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n# Define U-Net architecture\nclass UNet(nn.Module):\n    def _init_(self):\n        super(UNet, self)._init_()\n        # Define your U-Net architecture here\n\n    def forward(self, x):\n        pass\n        # Forward pass of U-Net\n\n# Define FPN architecture\nclass FPN(nn.Module):\n    def _init_(self):\n        super(FPN, self)._init_()\n        # Define your FPN architecture here\n\n    def forward(self, x):\n        pass\n        # Forward pass of FPN\n\n# Define your dataset class\nclass MyDataset(Dataset):\n    def _init_(self, data_dir, masks_df, transform=None):\n        self.data_dir = data_dir\n        self.masks_df = masks_df\n        self.transform = transform\n\n    def _len_(self):\n        return\n  \n\n\n\n\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}