{"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":"!apt-get -qq update && apt-get -qq install -y  libgdcm-tools python-gdcm\n!conda install -y -q -c conda-forge opencv gdcm pydicom apache-beam -y","metadata":{"execution":{"iopub.status.busy":"2021-08-08T06:11:41.071508Z","iopub.execute_input":"2021-08-08T06:11:41.072026Z","iopub.status.idle":"2021-08-08T06:14:56.818595Z","shell.execute_reply.started":"2021-08-08T06:11:41.071922Z","shell.execute_reply":"2021-08-08T06:14:56.817453Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from absl import flags\nfrom absl.flags import FLAGS\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.layers import (\n    Add,\n    Concatenate,\n    Conv2D,\n    Input,\n    Lambda,\n    LeakyReLU,\n    MaxPool2D,\n    UpSampling2D,\n    ZeroPadding2D,\n    BatchNormalization,\n)\nfrom tensorflow.keras.regularizers import l2\nfrom tensorflow.keras.losses import (\n    binary_crossentropy,\n    sparse_categorical_crossentropy\n)\n\nyolo_max_boxes= 100\nyolo_iou_threshold=0.5\nyolo_score_threshold=0.5\n\nyolo_anchors = np.array([(10, 13), (16, 30), (33, 23), (30, 61), (62, 45),\n                         (59, 119), (116, 90), (156, 198), (373, 326)],\n                        np.float32) / 416\nyolo_anchor_masks = np.array([[6, 7, 8], [3, 4, 5], [0, 1, 2]])\n\nyolo_tiny_anchors = np.array([(10, 14), (23, 27), (37, 58),\n                              (81, 82), (135, 169),  (344, 319)],\n                             np.float32) / 416\nyolo_tiny_anchor_masks = np.array([[3, 4, 5], [0, 1, 2]])\n\n","metadata":{"execution":{"iopub.status.busy":"2021-08-08T06:15:25.186005Z","iopub.execute_input":"2021-08-08T06:15:25.186518Z","iopub.status.idle":"2021-08-08T06:15:30.563982Z","shell.execute_reply.started":"2021-08-08T06:15:25.186469Z","shell.execute_reply":"2021-08-08T06:15:30.562882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gdcm\nimport numpy as np\nimport tensorflow as tf\nimport apache_beam\nimport pydicom\n\nimport cv2 as cv\nimport matplotlib.pyplot as plt\nimport os\nimport sys\nimport csv\nimport pickle\nimport random\nfrom datetime import datetime\nprint('Libraries Imported!')\nimport matplotlib.pyplot as plt\nimport PIL\nimport numpy as np\nimport os\nimport six.moves.urllib as urllib\nimport sys\nimport tarfile\nimport tensorflow as tf\nimport zipfile\nimport pandas as pd\nfrom collections import defaultdict\nfrom io import StringIO\nfrom matplotlib import pyplot as plt\nfrom PIL import Image\nfrom skimage import exposure\nfrom skimage.transform import resize\n\nnp.set_printoptions(threshold=sys.maxsize)\nprint('Settings Set!')\nprint(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T06:15:41.414877Z","iopub.execute_input":"2021-08-08T06:15:41.415281Z","iopub.status.idle":"2021-08-08T06:15:43.188763Z","shell.execute_reply.started":"2021-08-08T06:15:41.415247Z","shell.execute_reply":"2021-08-08T06:15:43.187514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('yolov3')","metadata":{"execution":{"iopub.status.busy":"2021-08-08T06:15:47.852562Z","iopub.execute_input":"2021-08-08T06:15:47.853207Z","iopub.status.idle":"2021-08-08T06:15:47.860138Z","shell.execute_reply.started":"2021-08-08T06:15:47.853158Z","shell.execute_reply":"2021-08-08T06:15:47.858722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights= \"/kaggle/input/yolov3weights/yolov3.weights\"\noutputFile= \"./yolov3/yolov3.tf\"\nsize = 416\nnum_classes = 4\nweights_num_classes = None","metadata":{"execution":{"iopub.status.busy":"2021-08-08T06:15:50.729144Z","iopub.execute_input":"2021-08-08T06:15:50.729595Z","iopub.status.idle":"2021-08-08T06:15:50.735875Z","shell.execute_reply.started":"2021-08-08T06:15:50.729557Z","shell.execute_reply":"2021-08-08T06:15:50.734429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"\ndef DarknetConv(x, filters, size, strides=1, batch_norm=True):\n    if strides == 1:\n        padding = 'same'\n    else:\n        x = ZeroPadding2D(((1, 0), (1, 0)))(x)  # top left half-padding\n        padding = 'valid'\n    x = Conv2D(filters=filters, kernel_size=size,\n               strides=strides, padding=padding,\n               use_bias=not batch_norm, kernel_regularizer=l2(0.0005))(x)\n    if batch_norm:\n        x = BatchNormalization()(x)\n        x = LeakyReLU(alpha=0.1)(x)\n    return x\n\n\ndef DarknetResidual(x, filters):\n    prev = x\n    x = DarknetConv(x, filters // 2, 1)\n    x = DarknetConv(x, filters, 3)\n    x = Add()([prev, x])\n    return x\n\n\ndef DarknetBlock(x, filters, blocks):\n    x = DarknetConv(x, filters, 3, strides=2)\n    for _ in range(blocks):\n        x = DarknetResidual(x, filters)\n    return x\n\n\ndef Darknet(name=None):\n    x = inputs = Input([None, None, 3])\n    x = DarknetConv(x, 32, 3)\n    x = DarknetBlock(x, 64, 1)\n    x = DarknetBlock(x, 128, 2)  # skip connection\n    x = x_36 = DarknetBlock(x, 256, 8)  # skip connection\n    x = x_61 = DarknetBlock(x, 512, 8)\n    x = DarknetBlock(x, 1024, 4)\n    return tf.keras.Model(inputs, (x_36, x_61, x), name=name)\n\n\ndef DarknetTiny(name=None):\n    x = inputs = Input([None, None, 3])\n    x = DarknetConv(x, 16, 3)\n    x = MaxPool2D(2, 2, 'same')(x)\n    x = DarknetConv(x, 32, 3)\n    x = MaxPool2D(2, 2, 'same')(x)\n    x = DarknetConv(x, 64, 3)\n    x = MaxPool2D(2, 2, 'same')(x)\n    x = DarknetConv(x, 128, 3)\n    x = MaxPool2D(2, 2, 'same')(x)\n    x = x_8 = DarknetConv(x, 256, 3)  # skip connection\n    x = MaxPool2D(2, 2, 'same')(x)\n    x = DarknetConv(x, 512, 3)\n    x = MaxPool2D(2, 1, 'same')(x)\n    x = DarknetConv(x, 1024, 3)\n    return tf.keras.Model(inputs, (x_8, x), name=name)\n\n\ndef YoloConv(filters, name=None):\n    def yolo_conv(x_in):\n        if isinstance(x_in, tuple):\n            inputs = Input(x_in[0].shape[1:]), Input(x_in[1].shape[1:])\n            x, x_skip = inputs\n\n            # concat with skip connection\n            x = DarknetConv(x, filters, 1)\n            x = UpSampling2D(2)(x)\n            x = Concatenate()([x, x_skip])\n        else:\n            x = inputs = Input(x_in.shape[1:])\n\n        x = DarknetConv(x, filters, 1)\n        x = DarknetConv(x, filters * 2, 3)\n        x = DarknetConv(x, filters, 1)\n        x = DarknetConv(x, filters * 2, 3)\n        x = DarknetConv(x, filters, 1)\n        return Model(inputs, x, name=name)(x_in)\n    return yolo_conv\n\n\ndef YoloConvTiny(filters, name=None):\n    def yolo_conv(x_in):\n        if isinstance(x_in, tuple):\n            inputs = Input(x_in[0].shape[1:]), Input(x_in[1].shape[1:])\n            x, x_skip = inputs\n\n            # concat with skip connection\n            x = DarknetConv(x, filters, 1)\n            x = UpSampling2D(2)(x)\n            x = Concatenate()([x, x_skip])\n        else:\n            x = inputs = Input(x_in.shape[1:])\n            x = DarknetConv(x, filters, 1)\n\n        return Model(inputs, x, name=name)(x_in)\n    return yolo_conv\n\n\ndef YoloOutput(filters, anchors, classes, name=None):\n    def yolo_output(x_in):\n        x = inputs = Input(x_in.shape[1:])\n        x = DarknetConv(x, filters * 2, 3)\n        x = DarknetConv(x, anchors * (classes + 5), 1, batch_norm=False)\n        x = Lambda(lambda x: tf.reshape(x, (-1, tf.shape(x)[1], tf.shape(x)[2],\n                                            anchors, classes + 5)))(x)\n        return tf.keras.Model(inputs, x, name=name)(x_in)\n    return yolo_output\n\n\n# As tensorflow lite doesn't support tf.size used in tf.meshgrid, \n# we reimplemented a simple meshgrid function that use basic tf function.\ndef _meshgrid(n_a, n_b):\n\n    return [\n        tf.reshape(tf.tile(tf.range(n_a), [n_b]), (n_b, n_a)),\n        tf.reshape(tf.repeat(tf.range(n_b), n_a), (n_b, n_a))\n    ]\n\n\ndef yolo_boxes(pred, anchors, classes):\n    # pred: (batch_size, grid, grid, anchors, (x, y, w, h, obj, ...classes))\n    grid_size = tf.shape(pred)[1:3]\n    box_xy, box_wh, objectness, class_probs = tf.split(\n        pred, (2, 2, 1, classes), axis=-1)\n\n    box_xy = tf.sigmoid(box_xy)\n    objectness = tf.sigmoid(objectness)\n    class_probs = tf.sigmoid(class_probs)\n    pred_box = tf.concat((box_xy, box_wh), axis=-1)  # original xywh for loss\n\n    # !!! grid[x][y] == (y, x)\n    grid = _meshgrid(grid_size[1],grid_size[0])\n    grid = tf.expand_dims(tf.stack(grid, axis=-1), axis=2)  # [gx, gy, 1, 2]\n\n    box_xy = (box_xy + tf.cast(grid, tf.float32)) / \\\n        tf.cast(grid_size, tf.float32)\n    box_wh = tf.exp(box_wh) * anchors\n\n    box_x1y1 = box_xy - box_wh / 2\n    box_x2y2 = box_xy + box_wh / 2\n    bbox = tf.concat([box_x1y1, box_x2y2], axis=-1)\n\n    return bbox, objectness, class_probs, pred_box\n\n\ndef yolo_nms(outputs, anchors, masks, classes):\n    # boxes, conf, type\n    b, c, t = [], [], []\n    print(t)\n\n    for o in outputs:\n        b.append(tf.reshape(o[0], (tf.shape(o[0])[0], -1, tf.shape(o[0])[-1])))\n        c.append(tf.reshape(o[1], (tf.shape(o[1])[0], -1, tf.shape(o[1])[-1])))\n        t.append(tf.reshape(o[2], (tf.shape(o[2])[0], -1, tf.shape(o[2])[-1])))\n\n    bbox = tf.concat(b, axis=1)\n    confidence = tf.concat(c, axis=1)\n    class_probs = tf.concat(t, axis=1)\n\n    scores = confidence * class_probs\n\n    dscores = tf.squeeze(scores, axis=0)\n    scores = tf.reduce_max(dscores,[1])\n    bbox = tf.reshape(bbox,(-1,4))\n    classes = tf.argmax(dscores,1)\n    selected_indices, selected_scores = tf.image.non_max_suppression_with_scores(\n        boxes=bbox,\n        scores=scores,\n        max_output_size=yolo_max_boxes,\n        iou_threshold=yolo_iou_threshold,\n        score_threshold=yolo_score_threshold,\n        soft_nms_sigma=0.5\n    )\n    \n    num_valid_nms_boxes = tf.shape(selected_indices)[0]\n\n    selected_indices = tf.concat([selected_indices,tf.zeros(yolo_max_boxes-num_valid_nms_boxes, tf.int32)], 0)\n    selected_scores = tf.concat([selected_scores,tf.zeros(yolo_max_boxes-num_valid_nms_boxes,tf.float32)], -1)\n\n    boxes=tf.gather(bbox, selected_indices)\n    boxes = tf.expand_dims(boxes, axis=0)\n    scores=selected_scores\n    scores = tf.expand_dims(scores, axis=0)\n    classes = tf.gather(classes,selected_indices)\n    classes = tf.expand_dims(classes, axis=0)\n    valid_detections=num_valid_nms_boxes\n    valid_detections = tf.expand_dims(valid_detections, axis=0)\n\n    return boxes, scores, classes, valid_detections\n\n\ndef YoloV3(size=None, channels=3, anchors=yolo_anchors,\n           masks=yolo_anchor_masks, classes=80, training=False):\n    x = inputs = Input([size, size, channels], name='input')\n\n    x_36, x_61, x = Darknet(name='yolo_darknet')(x)\n\n    x = YoloConv(512, name='yolo_conv_0')(x)\n    output_0 = YoloOutput(512, len(masks[0]), classes, name='yolo_output_0')(x)\n\n    x = YoloConv(256, name='yolo_conv_1')((x, x_61))\n    output_1 = YoloOutput(256, len(masks[1]), classes, name='yolo_output_1')(x)\n\n    x = YoloConv(128, name='yolo_conv_2')((x, x_36))\n    output_2 = YoloOutput(128, len(masks[2]), classes, name='yolo_output_2')(x)\n\n    if training:\n        return Model(inputs, (output_0, output_1, output_2), name='yolov3')\n\n    boxes_0 = Lambda(lambda x: yolo_boxes(x, anchors[masks[0]], classes),\n                     name='yolo_boxes_0')(output_0)\n    boxes_1 = Lambda(lambda x: yolo_boxes(x, anchors[masks[1]], classes),\n                     name='yolo_boxes_1')(output_1)\n    boxes_2 = Lambda(lambda x: yolo_boxes(x, anchors[masks[2]], classes),\n                     name='yolo_boxes_2')(output_2)\n\n    outputs = Lambda(lambda x: yolo_nms(x, anchors, masks, classes),\n                     name='yolo_nms')((boxes_0[:3], boxes_1[:3], boxes_2[:3]))\n\n    return Model(inputs, outputs, name='yolov3')\n\n\ndef YoloV3Tiny(size=None, channels=3, anchors=yolo_tiny_anchors,\n               masks=yolo_tiny_anchor_masks, classes=80, training=False):\n    x = inputs = Input([size, size, channels], name='input')\n\n    x_8, x = DarknetTiny(name='yolo_darknet')(x)\n\n    x = YoloConvTiny(256, name='yolo_conv_0')(x)\n    output_0 = YoloOutput(256, len(masks[0]), classes, name='yolo_output_0')(x)\n\n    x = YoloConvTiny(128, name='yolo_conv_1')((x, x_8))\n    output_1 = YoloOutput(128, len(masks[1]), classes, name='yolo_output_1')(x)\n\n    if training:\n        return Model(inputs, (output_0, output_1), name='yolov3')\n\n    boxes_0 = Lambda(lambda x: yolo_boxes(x, anchors[masks[0]], classes),\n                     name='yolo_boxes_0')(output_0)\n    boxes_1 = Lambda(lambda x: yolo_boxes(x, anchors[masks[1]], classes),\n                     name='yolo_boxes_1')(output_1)\n    outputs = Lambda(lambda x: yolo_nms(x, anchors, masks, classes),\n                     name='yolo_nms')((boxes_0[:3], boxes_1[:3]))\n    return Model(inputs, outputs, name='yolov3_tiny')\n\n\ndef YoloLoss(anchors, classes=80, ignore_thresh=0.5):\n    def yolo_loss(y_true, y_pred):\n        # 1. transform all pred outputs\n        # y_pred: (batch_size, grid, grid, anchors, (x, y, w, h, obj, ...cls))\n        pred_box, pred_obj, pred_class, pred_xywh = yolo_boxes(\n            y_pred, anchors, classes)\n        pred_xy = pred_xywh[..., 0:2]\n        pred_wh = pred_xywh[..., 2:4]\n\n        # 2. transform all true outputs\n        # y_true: (batch_size, grid, grid, anchors, (x1, y1, x2, y2, obj, cls))\n        true_box, true_obj, true_class_idx = tf.split(\n            y_true, (4, 1, 1), axis=-1)\n        true_xy = (true_box[..., 0:2] + true_box[..., 2:4]) / 2\n        true_wh = true_box[..., 2:4] - true_box[..., 0:2]\n\n        # give higher weights to small boxes\n        box_loss_scale = 2 - true_wh[..., 0] * true_wh[..., 1]\n\n        # 3. inverting the pred box equations\n        grid_size = tf.shape(y_true)[1]\n        grid = tf.meshgrid(tf.range(grid_size), tf.range(grid_size))\n        grid = tf.expand_dims(tf.stack(grid, axis=-1), axis=2)\n        true_xy = true_xy * tf.cast(grid_size, tf.float32) - \\\n            tf.cast(grid, tf.float32)\n        true_wh = tf.math.log(true_wh / anchors)\n        true_wh = tf.where(tf.math.is_inf(true_wh),\n                           tf.zeros_like(true_wh), true_wh)\n\n        # 4. calculate all masks\n        obj_mask = tf.squeeze(true_obj, -1)\n        # ignore false positive when iou is over threshold\n        best_iou = tf.map_fn(\n            lambda x: tf.reduce_max(broadcast_iou(x[0], tf.boolean_mask(\n                x[1], tf.cast(x[2], tf.bool))), axis=-1),\n            (pred_box, true_box, obj_mask),\n            tf.float32)\n        ignore_mask = tf.cast(best_iou < ignore_thresh, tf.float32)\n\n        # 5. calculate all losses\n        xy_loss = obj_mask * box_loss_scale * \\\n            tf.reduce_sum(tf.square(true_xy - pred_xy), axis=-1)\n        wh_loss = obj_mask * box_loss_scale * \\\n            tf.reduce_sum(tf.square(true_wh - pred_wh), axis=-1)\n        obj_loss = binary_crossentropy(true_obj, pred_obj)\n        obj_loss = obj_mask * obj_loss + \\\n            (1 - obj_mask) * ignore_mask * obj_loss\n        # TODO: use binary_crossentropy instead\n        class_loss = obj_mask * sparse_categorical_crossentropy(\n            true_class_idx, pred_class)\n\n        # 6. sum over (batch, gridx, gridy, anchors) => (batch, 1)\n        xy_loss = tf.reduce_sum(xy_loss, axis=(1, 2, 3))\n        wh_loss = tf.reduce_sum(wh_loss, axis=(1, 2, 3))\n        obj_loss = tf.reduce_sum(obj_loss, axis=(1, 2, 3))\n        class_loss = tf.reduce_sum(class_loss, axis=(1, 2, 3))\n\n        return xy_loss + wh_loss + obj_loss + class_loss\n    return yolo_loss","metadata":{"execution":{"iopub.status.busy":"2021-08-08T06:15:54.718133Z","iopub.execute_input":"2021-08-08T06:15:54.718488Z","iopub.status.idle":"2021-08-08T06:15:54.793272Z","shell.execute_reply.started":"2021-08-08T06:15:54.718455Z","shell.execute_reply":"2021-08-08T06:15:54.791937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"YOLOV3_LAYER_LIST = [\n    'yolo_darknet',\n    'yolo_conv_0',\n    'yolo_output_0',\n    'yolo_conv_1',\n    'yolo_output_1',\n    'yolo_conv_2',\n    'yolo_output_2',\n]","metadata":{"execution":{"iopub.status.busy":"2021-08-08T06:16:08.594967Z","iopub.execute_input":"2021-08-08T06:16:08.595337Z","iopub.status.idle":"2021-08-08T06:16:08.599947Z","shell.execute_reply.started":"2021-08-08T06:16:08.595306Z","shell.execute_reply":"2021-08-08T06:16:08.598716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_darknet_weights(model, weights_file, tiny=False):\n    wf = open(weights_file, 'rb')\n    major, minor, revision, seen, _ = np.fromfile(wf, dtype=np.int32, count=5)\n\n    layers = YOLOV3_LAYER_LIST\n\n    for layer_name in layers:\n        sub_model = model.get_layer(layer_name)\n        for i, layer in enumerate(sub_model.layers):\n            if not layer.name.startswith('conv2d'):\n                continue\n            batch_norm = None\n            if i + 1 < len(sub_model.layers) and \\\n                    sub_model.layers[i + 1].name.startswith('batch_norm'):\n                batch_norm = sub_model.layers[i + 1]\n\n           # logging.info(\"{}/{} {}\".format(\n            #    sub_model.name, layer.name, 'bn' if batch_norm else 'bias'))\n\n            filters = layer.filters\n            size = layer.kernel_size[0]\n            in_dim = layer.get_input_shape_at(0)[-1]\n\n            if batch_norm is None:\n                conv_bias = np.fromfile(wf, dtype=np.float32, count=filters)\n            else:\n                # darknet [beta, gamma, mean, variance]\n                bn_weights = np.fromfile(\n                    wf, dtype=np.float32, count=4 * filters)\n                # tf [gamma, beta, mean, variance]\n                bn_weights = bn_weights.reshape((4, filters))[[1, 0, 2, 3]]\n\n            # darknet shape (out_dim, in_dim, height, width)\n            conv_shape = (filters, in_dim, size, size)\n            conv_weights = np.fromfile(\n                wf, dtype=np.float32, count=np.product(conv_shape))\n            # tf shape (height, width, in_dim, out_dim)\n            conv_weights = conv_weights.reshape(\n                conv_shape).transpose([2, 3, 1, 0])\n\n            if batch_norm is None:\n                layer.set_weights([conv_weights, conv_bias])\n            else:\n                layer.set_weights([conv_weights])\n                batch_norm.set_weights(bn_weights)\n    #print(wf.read())\n    assert len(wf.read()) == 0, 'failed to read all data'\n    wf.close()\n","metadata":{"execution":{"iopub.status.busy":"2021-08-08T06:24:00.898366Z","iopub.execute_input":"2021-08-08T06:24:00.899Z","iopub.status.idle":"2021-08-08T06:24:00.91109Z","shell.execute_reply.started":"2021-08-08T06:24:00.898964Z","shell.execute_reply":"2021-08-08T06:24:00.910218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"physical_devices = tf.config.experimental.list_physical_devices('GPU')\n#print(physical_devices)\nif len(physical_devices) > 0:\n    tf.config.experimental.set_memory_growth(physical_devices[0], True)\n\nyolo = YoloV3(classes=num_classes)\nyolo.summary()\n#logging.info('model created')\n\nload_darknet_weights(yolo, weights, False)\n#logging.info('weights loaded')\n\nimg = np.random.random((1, 320, 320, 3)).astype(np.float32)\noutput = yolo(img)\n#logging.info('sanity check passed')\n\nyolo.save_weights(outputFile)\n#logging.info('weights saved')","metadata":{"execution":{"iopub.status.busy":"2021-08-08T06:16:17.612575Z","iopub.execute_input":"2021-08-08T06:16:17.613178Z","iopub.status.idle":"2021-08-08T06:16:26.65428Z","shell.execute_reply.started":"2021-08-08T06:16:17.61314Z","shell.execute_reply":"2021-08-08T06:16:26.653216Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading images ","metadata":{}},{"cell_type":"code","source":"# Change this to True if you want to do something with all the data\n# For testing purposes, you can just use the small list of files defined in the next cell\nTRAIN_FOLDER = '/kaggle/input/siim-covid19-detection/train'\nTEST_FOLDER = '/kaggle/input/siim-covid19-detection/test'\nVERBOSE = False\ndef list_dicoms_in_folder(folder):\n    '''Lists the full path to all the DICOM files in a given folder.'''\n    my_list = []\n    i = 0 \n    for dirname, _, filenames in os.walk(folder):\n        for filename in filenames:\n            if filename[-4:]=='.dcm':\n                my_list.append([os.path.join(dirname, filename),filename.split('.')[0]])\n                i = i +1\n        if i ==50:\n            break\n    return my_list\n\nif True:\n    Train_Data = list_dicoms_in_folder(TRAIN_FOLDER)\n    Test_Data = list_dicoms_in_folder(TEST_FOLDER)\n    \ntrain_images=[]\ntest_images=[]\n\nfor element,name in Train_Data:\n    ds = pydicom.dcmread(element)\n    train_images.append([ds.pixel_array,name])\n\n#showDataset(train_images, 48)    \n\n","metadata":{"execution":{"iopub.status.busy":"2021-08-08T06:16:40.580734Z","iopub.execute_input":"2021-08-08T06:16:40.581117Z","iopub.status.idle":"2021-08-08T06:16:55.269712Z","shell.execute_reply.started":"2021-08-08T06:16:40.581082Z","shell.execute_reply":"2021-08-08T06:16:55.26847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('train')","metadata":{"execution":{"iopub.status.busy":"2021-08-08T06:16:59.362018Z","iopub.execute_input":"2021-08-08T06:16:59.362401Z","iopub.status.idle":"2021-08-08T06:16:59.368204Z","shell.execute_reply.started":"2021-08-08T06:16:59.362369Z","shell.execute_reply":"2021-08-08T06:16:59.366828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.config.run_functions_eagerly(False)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T10:43:56.916512Z","iopub.execute_input":"2021-08-07T10:43:56.916925Z","iopub.status.idle":"2021-08-07T10:43:56.922186Z","shell.execute_reply.started":"2021-08-07T10:43:56.916893Z","shell.execute_reply":"2021-08-07T10:43:56.921113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import ast\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport csv\ni = 0\ndf = pd.read_csv('/kaggle/input/siim-covid19-detection/train_image_level.csv')  \ndf2 = pd.read_csv('/kaggle/input/siim-covid19-detection/train_study_level.csv')  \nxmin = []\nymin = []\nxmax = []\nymax = []\nclasses = []\nclasses_text = []\n# create annotation.txt and append values\n\nf = open(\"annotation.txt\",\"a+\")\nf.truncate(0)\nwriter = tf.io.TFRecordWriter(\"./train.tfrecords\")\nres = [(row,img) for index,row in df.iterrows() \n        for img,name in train_images if name in row['id']]\n \n# Iterate over image csv rows\nfor r,img in (res):\n    result= (np.maximum(img,0) / img.max()) * 255.0\n        \n    cv.imwrite(os.path.join('/kaggle/working/train/', r['id'][:-6]+'.jpg'), result)\n    # Read jpeg image\n    jpg_img = cv.imread(os.path.join('/kaggle/working/train/', r['id'][:-6]+'.jpg'))\n    filepath= '/kaggle/working/train/'\n    filename = str(r['id'][:-6])+\".jpg\"\n    full_path = os.path.join(filepath,filename)\n\n    if str(r['boxes'])!='nan':\n        grp = [c for index,c in df2.iterrows() if r['StudyInstanceUID'] in c['id']]\n       \n        if grp[0]['Negative for Pneumonia'] ==1:\n             classification = 0\n        elif grp[0]['Typical Appearance'] ==1:\n             classification = 1\n        elif grp[0]['Indeterminate Appearance'] ==1:\n             classification = 2\n        elif grp[0]['Atypical Appearance'] ==1:\n             classification = 3\n        classes_text.append(classification)\n        \n        for i,b in enumerate(ast.literal_eval(r['boxes'])):\n            x1= b['x']\n            y1 = b['y']\n            x2 = b['width'] + b['x']\n            y2= b['height'] + b['y']\n            width = b['width']\n            height = b['height']\n            xmin.append(float(x1) / width)\n            ymin.append(float(y1) / height)\n            xmax.append(float(x2) / width)\n            ymax.append(float(y2) / height)\n            \nexample = tf.train.Example(features=tf.train.Features(feature={\n        'image/encoded': tf.train.Feature(bytes_list=tf.train.BytesList(value=[full_path])),\n        'image/object/bbox/xmin': tf.train.Feature(float_list=tf.train.FloatList(value=xmin)),\n        'image/object/bbox/xmax': tf.train.Feature(float_list=tf.train.FloatList(value=xmax)),\n        'image/object/bbox/ymin': tf.train.Feature(float_list=tf.train.FloatList(value=ymin)),\n        'image/object/bbox/ymax': tf.train.Feature(float_list=tf.train.FloatList(value=ymax)),\n        'image/object/class/text': tf.train.Feature(float_list=tf.train.FloatList(value=classes_text)),\n          }))  \nwriter.write(example.SerializeToString())\nwriter.close()","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:45:25.669597Z","iopub.execute_input":"2021-08-08T08:45:25.670029Z","iopub.status.idle":"2021-08-08T08:45:57.459896Z","shell.execute_reply.started":"2021-08-08T08:45:25.669993Z","shell.execute_reply":"2021-08-08T08:45:57.458417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nf1 = open(\"class.names\",\"w+\")\nf1.write('Negative_for_Pneumonia 0')\nf1.write('\\n')\nf1.write('Typical_Appearance 1')\nf1.write('\\n')\nf1.write('Indeterminate_Appearance 2')\nf1.write('\\n')\nf1.write('Atypical_Appearance 3')\nf1.write('\\n')\nf1.close()\n","metadata":{"execution":{"iopub.status.busy":"2021-08-08T07:24:09.62784Z","iopub.execute_input":"2021-08-08T07:24:09.628252Z","iopub.status.idle":"2021-08-08T07:24:09.635262Z","shell.execute_reply.started":"2021-08-08T07:24:09.628214Z","shell.execute_reply":"2021-08-08T07:24:09.63412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"IMAGE_FEATURE_MAP = {\n    # 'image/width': tf.io.FixedLenFeature([], tf.int64),\n    # 'image/height': tf.io.FixedLenFeature([], tf.int64),\n    # 'image/filename': tf.io.FixedLenFeature([], tf.string),\n    # 'image/source_id': tf.io.FixedLenFeature([], tf.string),\n    # 'image/key/sha256': tf.io.FixedLenFeature([], tf.string),\n    'image/encoded': tf.io.FixedLenFeature([], tf.string),\n    # 'image/format': tf.io.FixedLenFeature([], tf.string),\n    'image/object/bbox/xmin': tf.io.VarLenFeature(tf.float32),\n    'image/object/bbox/ymin': tf.io.VarLenFeature(tf.float32),\n    'image/object/bbox/xmax': tf.io.VarLenFeature(tf.float32),\n    'image/object/bbox/ymax': tf.io.VarLenFeature(tf.float32),\n    'image/object/class/text':tf.io.VarLenFeature(tf.float32),\n    # 'image/object/class/label': tf.io.VarLenFeature(tf.int64),\n    # 'image/object/difficult': tf.io.VarLenFeature(tf.int64),\n    # 'image/object/truncated': tf.io.VarLenFeature(tf.int64),\n    # 'image/object/view': tf.io.VarLenFeature(tf.string),\n}","metadata":{"execution":{"iopub.status.busy":"2021-08-08T07:24:14.5912Z","iopub.execute_input":"2021-08-08T07:24:14.591583Z","iopub.status.idle":"2021-08-08T07:24:14.600181Z","shell.execute_reply.started":"2021-08-08T07:24:14.591552Z","shell.execute_reply":"2021-08-08T07:24:14.598484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom absl.flags import FLAGS\n\n@tf.function\ndef transform_targets_for_output(y_true, grid_size, anchor_idxs):\n    # y_true: (N, boxes, (x1, y1, x2, y2, class, best_anchor))\n    N = tf.shape(y_true)[0]\n\n    # y_true_out: (N, grid, grid, anchors, [x1, y1, x2, y2, obj, class])\n    y_true_out = tf.zeros(\n        (N, grid_size, grid_size, tf.shape(anchor_idxs)[0], 6))\n    #[0, 54, 72, 2]\n    anchor_idxs = tf.cast(anchor_idxs, tf.int32)\n\n    indexes = tf.TensorArray(tf.int32, 1, dynamic_size=True)\n    updates = tf.TensorArray(tf.float32, 1, dynamic_size=True)\n    idx = 0\n    for i in tf.range(N):\n        for j in tf.range(tf.shape(y_true)[1]):\n            if tf.equal(y_true[i][j][2], 0):\n                continue\n            anchor_eq = tf.equal(\n                anchor_idxs, tf.cast(y_true[i][j][5], tf.int32))\n\n            if tf.reduce_any(anchor_eq):\n                box = y_true[i][j][0:4]\n                box_xy = (y_true[i][j][0:2] + y_true[i][j][2:4]) / 2\n\n                anchor_idx = tf.cast(tf.where(anchor_eq), tf.int32)\n                grid_xy = tf.cast(box_xy // (1/grid_size), tf.int32)\n\n                # grid[y][x][anchor] = (tx, ty, bw, bh, obj, class)\n                indexes = indexes.write(\n                    idx, [i, grid_xy[1], grid_xy[0], anchor_idx[0][0]])\n                \n                updates = updates.write(\n                    idx, [box[0], box[1], box[2], box[3], 1, y_true[i][j][4]])\n                idx += 1\n                \n    tf.print(indexes.stack())\n    tf.print(updates.stack())\n    return tf.tensor_scatter_nd_update(\n        y_true_out, indexes.stack(), updates.stack())\n\n\ndef transform_targets(y_train, anchors, anchor_masks, size):\n    y_outs = []\n    grid_size = size // 32\n\n    # calculate anchor index for true boxes\n    anchors = tf.cast(anchors, tf.float32)\n    anchor_area = anchors[..., 0] * anchors[..., 1]\n    box_wh = y_train[..., 2:4] - y_train[..., 0:2]\n    box_wh = tf.tile(tf.expand_dims(box_wh, -2),\n                     (1, 1, tf.shape(anchors)[0], 1))\n    box_area = box_wh[..., 0] * box_wh[..., 1]\n    intersection = tf.minimum(box_wh[..., 0], anchors[..., 0]) * \\\n        tf.minimum(box_wh[..., 1], anchors[..., 1])\n    iou = intersection / (box_area + anchor_area - intersection)\n    \n    anchor_idx = tf.cast(tf.argmax(iou, axis=-1), tf.float32)\n    anchor_idx = tf.expand_dims(anchor_idx, axis=-1)\n\n    y_train = tf.concat([y_train, anchor_idx], axis=-1)\n\n    for anchor_idxs in anchor_masks:\n        y_outs.append(transform_targets_for_output(\n            y_train, grid_size, anchor_idxs))\n        grid_size *= 2\n    print(y_outs)\n    return tuple(y_outs)\n\n\ndef transform_images(x_train, size):\n    print(x_train.shape)\n    x_train = tf.image.resize(x_train, (size, size))\n    x_train = x_train / 255\n    return x_train\n\nfrom tensorflow.python.framework import sparse_tensor\ndef parse_tfrecord(tfrecord, class_table, size):\n    x = tf.io.parse_single_example(tfrecord, IMAGE_FEATURE_MAP)\n\n    x_train = tf.image.decode_jpeg(tf.io.read_file(x['image/encoded']), channels=1)\n    \n    x_train = tf.image.resize(x_train, (size, size))\n    \n    class_text = tf.sparse.to_dense(x['image/object/class/text'], default_value=0)\n\n    labels = tf.cast(class_text, tf.float32)\n    \n    y_train = tf.stack([tf.sparse.to_dense(x['image/object/bbox/xmin']),\n                        tf.sparse.to_dense(x['image/object/bbox/ymin']),\n                        tf.sparse.to_dense(x['image/object/bbox/xmax']),\n                        tf.sparse.to_dense(x['image/object/bbox/ymax']),\n                        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]], axis=1)\n\n    paddings = [[0, 416 - tf.shape(y_train)[0]], [0, 0]]\n    y_train = tf.pad(y_train, paddings)\n\n    return x_train, y_train\n\n\ndef load_tfrecord_dataset(file_pattern, class_file, size=416):\n   \n    class_table = tf.lookup.StaticHashTable(tf.lookup.TextFileInitializer(\n        class_file, tf.string, 0, tf.float32, 1, delimiter=\" \"), -1)\n    files = tf.data.Dataset.list_files(file_pattern)\n    dataset = files.flat_map(tf.data.TFRecordDataset)\n    return dataset.map(lambda x: parse_tfrecord(x, class_table, size))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-08T08:04:24.352031Z","iopub.execute_input":"2021-08-08T08:04:24.352447Z","iopub.status.idle":"2021-08-08T08:04:24.386803Z","shell.execute_reply.started":"2021-08-08T08:04:24.352412Z","shell.execute_reply":"2021-08-08T08:04:24.385766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#class_files = open(,'rb').read()\ntrain_dataset = load_tfrecord_dataset(\"./train.tfrecords\",\"./class.names\", size=416)\ntrain_dataset = train_dataset.shuffle(buffer_size=512)\ntrain_dataset = train_dataset.batch(4)\ntrain_dataset = train_dataset.map(lambda x, y: (transform_images(x, 416),\ntransform_targets(y, yolo_anchors, yolo_anchor_masks, 416)))\ntrain_dataset = train_dataset.prefetch(\nbuffer_size=tf.data.experimental.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:44:52.092993Z","iopub.execute_input":"2021-08-08T08:44:52.093535Z","iopub.status.idle":"2021-08-08T08:44:52.512563Z","shell.execute_reply.started":"2021-08-08T08:44:52.093486Z","shell.execute_reply":"2021-08-08T08:44:52.511812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nval_dataset = load_tfrecord_dataset(\"./train.tfrecords\",\"./class.names\",  size=416)\nval_dataset = val_dataset.shuffle(buffer_size=512)\nval_dataset = val_dataset.batch(4)\nval_dataset = val_dataset.map(lambda x, y: (\ntransform_images(x, 416),\ntransform_targets(y, yolo_tiny_anchors, yolo_tiny_anchor_masks, 416)))\nval_dataset = val_dataset.prefetch(\nbuffer_size=tf.data.experimental.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:44:59.650813Z","iopub.execute_input":"2021-08-08T08:44:59.651396Z","iopub.status.idle":"2021-08-08T08:44:59.793673Z","shell.execute_reply.started":"2021-08-08T08:44:59.651342Z","shell.execute_reply":"2021-08-08T08:44:59.792326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = YoloV3(size, training=True, classes=num_classes)\nanchors = yolo_anchors\nanchor_masks = yolo_anchor_masks\nmodel_pretrained = YoloV3(size, training=True, classes=weights_num_classes or num_classes)\nmodel_pretrained.load_weights(outputFile)\nmodel.get_layer('yolo_darknet').set_weights(model_pretrained.get_layer('yolo_darknet').get_weights())\noptimizer = tf.keras.optimizers.Adam(lr=1e-3)\nloss = [YoloLoss(anchors[mask], classes=num_classes)\n        for mask in anchor_masks]\n\nmodel.compile(optimizer=optimizer, loss=loss,\n              run_eagerly=(model.fit))","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:45:03.504559Z","iopub.execute_input":"2021-08-08T08:45:03.50492Z","iopub.status.idle":"2021-08-08T08:45:10.198011Z","shell.execute_reply.started":"2021-08-08T08:45:03.504891Z","shell.execute_reply":"2021-08-08T08:45:10.197239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import (\n    ReduceLROnPlateau,\n    EarlyStopping,\n    ModelCheckpoint,\n    TensorBoard\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T06:31:40.093371Z","iopub.execute_input":"2021-08-08T06:31:40.09387Z","iopub.status.idle":"2021-08-08T06:31:40.100091Z","shell.execute_reply.started":"2021-08-08T06:31:40.093829Z","shell.execute_reply":"2021-08-08T06:31:40.098352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T21:35:09.487996Z","iopub.execute_input":"2021-08-07T21:35:09.488586Z","iopub.status.idle":"2021-08-07T21:35:09.520595Z","shell.execute_reply.started":"2021-08-07T21:35:09.488535Z","shell.execute_reply":"2021-08-07T21:35:09.519693Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nhistory = model.fit(train_dataset,\n                    epochs=100,validation_data=val_dataset)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:45:14.27247Z","iopub.execute_input":"2021-08-08T08:45:14.273151Z","iopub.status.idle":"2021-08-08T08:45:16.036533Z","shell.execute_reply.started":"2021-08-08T08:45:14.273088Z","shell.execute_reply":"2021-08-08T08:45:16.035052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = load_tfrecord_dataset(\"./train.tfrecords\",\"./class.names\", 416)\ndataset = dataset.shuffle(512)\n\nfor image, labels in dataset.take(10):\n    boxes = []\n    scores = []\n    classes = []\n    for x1, y1, x2, y2, label in labels:\n        if x1 == 0 and x2 == 0:\n            continue\n        boxes.append((x1, y1, x2, y2))\n        scores.append(1)\n        classes.append(label)\n    nums = [len(boxes)]\n    boxes = [boxes]\n    scores = [scores]\n    classes = [classes]\n    plt.figure(figsize=(5,5))\n    print(image.shape)\n    plt.imshow(cv.imread(image,0), cmap=\"gray\")  \n    print(classes)\n    for i in range(nums[0]):\n        print(np.array(scores[0][i]))\n        print(np.array(boxes[0][i]))\n","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:42:45.358272Z","iopub.execute_input":"2021-08-08T08:42:45.358964Z","iopub.status.idle":"2021-08-08T08:42:45.465394Z","shell.execute_reply.started":"2021-08-08T08:42:45.3589Z","shell.execute_reply":"2021-08-08T08:42:45.464325Z"},"trusted":true},"execution_count":null,"outputs":[]}]}