{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport pydicom\nfrom matplotlib import pyplot as plt\nimport cv2\nfrom tensorflow.keras.applications.mobilenet import preprocess_input\nimport tensorflow as tensorflow\nimport gc\nimport csv\nfrom datetime import datetime","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"files = []\npath = '../input/rsna-pneumonia-detection-challenge/stage_2_train_images'\nfor dirpath,dirname,filenames in os.walk(r'../input/rsna-pneumonia-detection-challenge/stage_2_train_images'):\n    for filename in filenames:\n        files.append(filename)\n\nprint(\"Number of train images :\",len(files))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.applications.mobilenet import MobileNet\nfrom tensorflow.keras.layers import Concatenate, UpSampling2D, Conv2D, Reshape\nfrom tensorflow.keras.models import Model\n\nimage_size = 224\ndef create_model(trainable=True):\n    model = MobileNet(input_shape=(image_size, image_size, 3), include_top=False, alpha=1.0, weights = \"imagenet\")\n\n    for layer in model.layers:\n        layer.trainable = trainable\n\n    block1 = model.get_layer(\"conv_pw_1_relu\").output\n    block2 = model.get_layer(\"conv_pw_3_relu\").output\n    block3 = model.get_layer(\"conv_pw_5_relu\").output\n    block6 = model.get_layer(\"conv_pw_11_relu\").output\n    block7 = model.get_layer(\"conv_pw_13_relu\").output\n\n    x = Concatenate()([UpSampling2D()(block7), block6])\n    x = Concatenate()([UpSampling2D()(x), block3])\n    x = Concatenate()([UpSampling2D()(x), block2])\n    x = Concatenate()([UpSampling2D()(x), block1])\n    x =  UpSampling2D()(x)\n    x = Conv2D(1, kernel_size=1, activation=\"sigmoid\")(x)\n    x = Reshape((image_size, image_size))(x)\n\n    return Model(inputs=model.input, outputs=x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = create_model()\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def dice_coefficient(y_true, y_pred):\n    #### Add your code here ####\n    numerator = 2 * tensorflow.reduce_sum(y_true * y_pred)\n    denominator = tensorflow.reduce_sum(y_true + y_pred)\n\n    return numerator / (denominator + tensorflow.keras.backend.epsilon()) #### Add your code here ####","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.losses import binary_crossentropy\nfrom tensorflow.keras.backend import log, epsilon\ndef loss(y_true, y_pred):\n    return binary_crossentropy(y_true, y_pred) - log(dice_coefficient(y_true, y_pred) + epsilon())\n\ndef iou_loss(y_true, y_pred):\n    y_true = tf.reshape(y_true, [-1])\n    y_pred = tf.reshape(y_pred, [-1])\n    intersection = tf.reduce_sum(y_true * y_pred)\n    score = (intersection + 1.) / (tf.reduce_sum(y_true) + tf.reduce_sum(y_pred) - intersection + 1.)\n    return 1 - score\n\n# mean iou as a metric\ndef mean_iou(y_true, y_pred):\n    y_pred = tf.round(y_pred)    \n    #intersect = tf.reduce_sum(y_true * y_pred, axis=[1, 2, 3])\n    intersect = tf.reduce_sum(y_true * y_pred, axis=[1])\n    #union = tf.reduce_sum(y_true, axis=[1, 2, 3]) + tf.reduce_sum(y_pred, axis=[1, 2, 3])\n    union = tf.reduce_sum(y_true, axis=[1]) + tf.reduce_sum(y_pred, axis=[1])\n    smooth = tf.ones(tf.shape(intersect))\n    return tf.reduce_mean((intersect + smooth) / (union - intersect + smooth))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.losses import binary_crossentropy\n\noptimizer = Adam(lr=1e-6, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)\n#model.compile(loss=loss, optimizer=optimizer, metrics=[dice_coefficient])\nmodel.compile(loss=iou_loss, optimizer=optimizer, metrics=[mean_iou])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\ncheckpoint = ModelCheckpoint(\"model-{loss:.2f}.h5\", monitor=\"loss\", verbose=1, save_best_only=True,\n                             save_weights_only=True, mode=\"min\", period=1)\nstop = EarlyStopping(monitor=\"loss\", patience=5, mode=\"min\")\nreduce_lr = ReduceLROnPlateau(monitor=\"loss\", factor=0.1, patience=3, min_lr=1e-6, verbose=1, mode=\"min\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mask_coord = {}\nwith open(os.path.join('../input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv'), mode='r') as infile:\n    reader = csv.reader(infile)\n    next(reader, None)\n    for rows in reader:\n        filename = rows[0]\n        coord = rows[1:5]\n        tgt = rows[5]\n        if tgt == '1':\n            coord = [int(float(i)) for i in coord]\n            if filename in mask_coord:\n                mask_coord[filename].append(coord)\n            else:\n                mask_coord[filename] = [coord]\nprint(\"Number of positive targets :\",len(mask_coord))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"size = len(files)\nbatch_size = 3000\nbatch_num = 1\n#batch_num = int(size/batch_size + 1)\nimage_size = 224\n\nfor i in range (batch_num):\n    x_train = np.zeros((batch_size, image_size, image_size,3))\n    masks = np.zeros((batch_size,image_size,image_size))\n    start = i * batch_size\n    end = ((i+1) * batch_size)-1\n    if (end > size):\n        end = size\n    print(\"Batch start and end points   : \",start,end,datetime.now())\n    \n    x=0\n    for j in range(start,end):\n        train_image = (pydicom.dcmread(path + '/' + files[j]))\n        img = train_image.pixel_array\n        img = cv2.resize(img, dsize=(image_size, image_size), interpolation=cv2.INTER_CUBIC)\n        img = np.stack((img,)*3, axis=-1)\n        x_train[x] = preprocess_input(np.array(img, dtype=np.float32))\n        \n        filename = files[x].split('.')[0]\n        if filename in mask_coord:\n            mask = np.zeros((1024,1024))\n            loc = mask_coord[filename]\n            for i in range(len(loc)):\n                X,Y,W,H = loc[i]\n                mask[Y:Y+H, X:X+W] = 1\n            mask = cv2.resize(mask, dsize=(image_size, image_size), interpolation=cv2.INTER_CUBIC)\n            masks[x]= mask\n        x = x + 1\n    \n    print(\"Model training starts        : \",datetime.now())\n    trail = model.fit(x_train, masks, epochs=20, batch_size=9, verbose=1, callbacks=[checkpoint, reduce_lr, stop])\n    print(\"Model training ends          : \",datetime.now())\n    \n    del x_train\n    del masks\n    gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.utils.vis_utils import model_to_dot\nfrom IPython.display import Image\n\ndef show_model(in_model):\n    f = model_to_dot(in_model, show_shapes=True, rankdir='UD')\n    return Image(f.create_png())\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_model(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(12,4))\nplt.subplot(121)\nplt.plot(trail.epoch, trail.history[\"loss\"], label=\"Train IoU\")\nplt.legend()\nplt.subplot(122)\nplt.plot(trail.epoch, trail.history[\"mean_iou\"], label=\"Train Mean IoU\")\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}