{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":9988,"databundleVersionId":868324,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# cell entirely copypasted from https://nikhilroxtomar.medium.com/vgg19-unet-implementation-in-tensorflow-idiot-developer-2d10299e796d\n# on purpose\nfrom tensorflow.keras.layers import Conv2D, BatchNormalization, Activation, MaxPool2D, Conv2DTranspose, Concatenate, Input\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.applications import VGG19\n \ndef conv_block(input, num_filters):\n    x = Conv2D(num_filters, 3, padding=\"same\")(input)\n    x = BatchNormalization()(x)\n    x = Activation(\"relu\")(x)\n \n    x = Conv2D(num_filters, 3, padding=\"same\")(x)\n    x = BatchNormalization()(x)\n    x = Activation(\"relu\")(x)\n \n    return x\n \ndef decoder_block(input, skip_features, num_filters):\n    x = Conv2DTranspose(num_filters, (2, 2), strides=2, padding=\"same\")(input)\n    x = Concatenate()([x, skip_features])\n    x = conv_block(x, num_filters)\n    return x\n \ndef build_vgg19_unet(input_shape):\n    \"\"\" Input \"\"\"\n    inputs = Input(input_shape)\n \n    \"\"\" Pre-trained VGG19 Model \"\"\"\n    vgg19 = VGG19(include_top=False, weights=\"imagenet\", input_tensor=inputs)\n \n    \"\"\" Encoder \"\"\"\n    s1 = vgg19.get_layer(\"block1_conv2\").output         ## (512 x 512)\n    s2 = vgg19.get_layer(\"block2_conv2\").output         ## (256 x 256)\n    s3 = vgg19.get_layer(\"block3_conv4\").output         ## (128 x 128)\n    s4 = vgg19.get_layer(\"block4_conv4\").output         ## (64 x 64)\n \n    \"\"\" Bridge \"\"\"\n    b1 = vgg19.get_layer(\"block5_conv4\").output         ## (32 x 32)\n \n    \"\"\" Decoder \"\"\"\n    d1 = decoder_block(b1, s4, 512)                     ## (64 x 64)\n    d2 = decoder_block(d1, s3, 256)                     ## (128 x 128)\n    d3 = decoder_block(d2, s2, 128)                     ## (256 x 256)\n    d4 = decoder_block(d3, s1, 64)                      ## (512 x 512)\n \n    \"\"\" Output \"\"\"\n    outputs = Conv2D(1, 1, padding=\"same\", activation=\"sigmoid\")(d4)\n \n    model = Model(inputs, outputs, name=\"VGG19_U-Net\")\n    return model","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-22T11:34:25.749972Z","iopub.execute_input":"2023-11-22T11:34:25.750679Z","iopub.status.idle":"2023-11-22T11:34:39.825653Z","shell.execute_reply.started":"2023-11-22T11:34:25.750644Z","shell.execute_reply":"2023-11-22T11:34:39.824503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = build_vgg19_unet((128, 128, 3))","metadata":{"execution":{"iopub.status.busy":"2023-11-22T11:34:39.827887Z","iopub.execute_input":"2023-11-22T11:34:39.828626Z","iopub.status.idle":"2023-11-22T11:34:44.726456Z","shell.execute_reply.started":"2023-11-22T11:34:39.828587Z","shell.execute_reply":"2023-11-22T11:34:44.725374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport pandas as pd\nimport numpy as np\nfrom matplotlib import pyplot as plt\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2023-11-22T11:34:44.727691Z","iopub.execute_input":"2023-11-22T11:34:44.728003Z","iopub.status.idle":"2023-11-22T11:34:44.963756Z","shell.execute_reply.started":"2023-11-22T11:34:44.727973Z","shell.execute_reply":"2023-11-22T11:34:44.962585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CORRUPTED_IMAGE_IDS = ['6384c3e78.jpg']","metadata":{"execution":{"iopub.status.busy":"2023-11-22T11:34:44.965791Z","iopub.execute_input":"2023-11-22T11:34:44.96611Z","iopub.status.idle":"2023-11-22T11:34:44.972817Z","shell.execute_reply.started":"2023-11-22T11:34:44.966082Z","shell.execute_reply":"2023-11-22T11:34:44.97179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ship_segmentations_v2 = pd.read_csv(\"/kaggle/input/airbus-ship-detection/train_ship_segmentations_v2.csv\", index_col = 'ImageId')","metadata":{"execution":{"iopub.status.busy":"2023-11-22T11:34:44.973867Z","iopub.execute_input":"2023-11-22T11:34:44.974804Z","iopub.status.idle":"2023-11-22T11:34:46.116183Z","shell.execute_reply.started":"2023-11-22T11:34:44.974763Z","shell.execute_reply":"2023-11-22T11:34:46.115131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoded_pixels_s = (\n    train_ship_segmentations_v2['EncodedPixels'].fillna('') + ' ').groupby('ImageId').sum(min_count = 1).str.strip().drop(CORRUPTED_IMAGE_IDS)","metadata":{"execution":{"iopub.status.busy":"2023-11-22T11:34:46.11762Z","iopub.execute_input":"2023-11-22T11:34:46.118348Z","iopub.status.idle":"2023-11-22T11:34:46.546005Z","shell.execute_reply.started":"2023-11-22T11:34:46.118308Z","shell.execute_reply":"2023-11-22T11:34:46.54498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_image(image_id, train = True):\n    return cv2.imread(('/kaggle/input/airbus-ship-detection/train_v2/' if train else '/kaggle/input/airbus-ship-detection/test_v2/') + image_id) / 255.","metadata":{"execution":{"iopub.status.busy":"2023-11-22T11:34:46.547385Z","iopub.execute_input":"2023-11-22T11:34:46.547708Z","iopub.status.idle":"2023-11-22T11:34:46.554178Z","shell.execute_reply.started":"2023-11-22T11:34:46.547679Z","shell.execute_reply":"2023-11-22T11:34:46.553017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_image(image_id, train = True):\n    return tf.image.resize(get_image(image_id)[None], [384, 384])","metadata":{"execution":{"iopub.status.busy":"2023-11-22T11:34:46.555673Z","iopub.execute_input":"2023-11-22T11:34:46.555964Z","iopub.status.idle":"2023-11-22T11:34:46.571044Z","shell.execute_reply.started":"2023-11-22T11:34:46.555938Z","shell.execute_reply":"2023-11-22T11:34:46.569964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_mask(image_id):\n    encoded_pixels = encoded_pixels_s[image_id]\n    mask = np.zeros(768 ** 2, dtype = bool)\n    pairs = np.array(encoded_pixels.split(), dtype = int).reshape(-1, 2)\n    for start, length in pairs:\n        mask[start:start+length] = 1\n    return mask","metadata":{"execution":{"iopub.status.busy":"2023-11-22T11:34:46.572469Z","iopub.execute_input":"2023-11-22T11:34:46.572912Z","iopub.status.idle":"2023-11-22T11:34:46.581529Z","shell.execute_reply.started":"2023-11-22T11:34:46.572874Z","shell.execute_reply":"2023-11-22T11:34:46.580532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_mask(image_id):\n    return tf.image.resize(tf.cast(get_mask(image_id).reshape(768, 768, 1), float), [384, 384])[..., 0][None, ..., None]","metadata":{"execution":{"iopub.status.busy":"2023-11-22T11:34:46.583975Z","iopub.execute_input":"2023-11-22T11:34:46.584313Z","iopub.status.idle":"2023-11-22T11:34:46.59238Z","shell.execute_reply.started":"2023-11-22T11:34:46.584284Z","shell.execute_reply":"2023-11-22T11:34:46.591342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.BinaryCrossentropy(),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-11-22T11:34:46.593252Z","iopub.execute_input":"2023-11-22T11:34:46.59355Z","iopub.status.idle":"2023-11-22T11:34:46.616993Z","shell.execute_reply.started":"2023-11-22T11:34:46.593524Z","shell.execute_reply":"2023-11-22T11:34:46.616136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_generator():\n    while True:\n        for image_id in encoded_pixels_s[encoded_pixels_s != ''].index:\n            yield tf.constant(get_model_image(image_id)), tf.constant(get_model_mask(image_id))","metadata":{"execution":{"iopub.status.busy":"2023-11-22T11:34:46.618158Z","iopub.execute_input":"2023-11-22T11:34:46.619222Z","iopub.status.idle":"2023-11-22T11:34:46.624079Z","shell.execute_reply.started":"2023-11-22T11:34:46.619186Z","shell.execute_reply":"2023-11-22T11:34:46.6231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(\n    x = train_generator(),\n    #x = train_generated[0][None],\n    #y = train_generated[1][None],\n    batch_size = 32,\n    epochs = 5,\n    steps_per_epoch = 4_000,\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-22T11:34:46.625226Z","iopub.execute_input":"2023-11-22T11:34:46.625989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}