{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os,re,gc\nimport numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt \n\nimport albumentations as albu\nimport tensorflow as tf \nfrom tensorflow.keras.applications import ResNet152","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Configuration"},{"metadata":{"trusted":true},"cell_type":"code","source":"LABELS = np.array(['ETT - Abnormal', 'ETT - Borderline',\n       'ETT - Normal', 'NGT - Abnormal', 'NGT - Borderline',\n       'NGT - Incompletely Imaged', 'NGT - Normal', 'CVC - Abnormal',\n       'CVC - Borderline', 'CVC - Normal', 'Swan Ganz Catheter Present'])\n\nN_LABELS = 11 \nAUTO = tf.data.experimental.AUTOTUNE\n\nclass CONFIG: \n    tta = 5\n    batchsize = 32\n    imsize = (512,512)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"## decoder \ndef decode_fn(path):\n    file_bytes = tf.io.read_file(path)\n    img = tf.io.decode_jpeg(file_bytes,channels=3)\n    img = tf.image.resize(img,CONFIG.imsize)\n    img = tf.cast(img,tf.uint8) \n    return img\n\n## Test Time Augmentation \ntransform = albu.Compose([\n    albu.HorizontalFlip(p=0.5),\n    albu.VerticalFlip(p=0.5),\n    albu.CLAHE(clip_limit=(1,10),p=1)\n])\n\ndef aug_fn(image):\n    aug_img = transform(image = image)[\"image\"]\n    aug_img = tf.cast(aug_img/255, tf.float32)\n    aug_img = tf.image.resize(aug_img,CONFIG.imsize) \n    return aug_img\n\ndef process_data(image):\n    aug_img = tf.numpy_function(func=aug_fn, inp=[image], Tout=tf.float32)\n    aug_img.set_shape((*CONFIG.imsize,3))\n    return aug_img\n\n## Make CLAHE Data \ndef make_clahe_dataset(paths,cache_dir=False):\n    if cache_dir:\n        os.makedirs(cache_dir,exist_ok=True)\n    dset = tf.data.Dataset.from_tensor_slices(paths)\n    dset = dset.map(decode_fn,num_parallel_calls=AUTO)\n    dset = dset.cache(cache_dir) if cache_dir else dset \n    dset = dset.map(process_data,num_parallel_calls=AUTO)\n    dset = dset.repeat()\n    dset = dset.batch(CONFIG.batchsize)\n    dset = dset.prefetch(AUTO)\n    return dset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_df = pd.read_csv(\"../input/ranzcr-clip-catheter-line-classification/sample_submission.csv\")\ntest_paths = \"../input/ranzcr-clip-catheter-line-classification/test/\" + sub_df[\"StudyInstanceUID\"] + \".jpg\"\nclahe_dset = make_clahe_dataset(test_paths)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Show Image"},{"metadata":{"trusted":true},"cell_type":"code","source":"def view_image(ds,num=4):\n    print(ds)\n    fig = plt.figure(figsize=(22, 22))\n    images = next(iter(ds))\n    for i,img in enumerate(images):\n        if i == num:\n            break \n        img = img.numpy()\n        ax = fig.add_subplot(3,4,i+1,xticks=[],yticks=[])\n        ax.imshow(img)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"view_image(clahe_dset) ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_model(config):\n    model = tf.keras.Sequential([\n        ResNet152(input_shape=(*config.imsize,3),\n                              weights=None,\n                              include_top=False),\n        tf.keras.layers.GlobalAveragePooling2D(),\n        tf.keras.layers.Dense(N_LABELS,activation=\"sigmoid\")\n    ])\n    model.summary()\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = create_model(CONFIG)\nmodel.load_weights(\"../input/model-clahe-512/model_nb13_4_0.h5\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Inference"},{"metadata":{"trusted":true},"cell_type":"code","source":"TEST_NUM = sub_df.shape[0]\n\nsteps = (CONFIG.tta*TEST_NUM + CONFIG.batchsize - 1)//CONFIG.batchsize\npred = model.predict(clahe_dset,steps=steps,verbose=1)[:CONFIG.tta*TEST_NUM]\npred = np.mean(pred.reshape((TEST_NUM,CONFIG.tta,N_LABELS),order = \"F\"),axis = 1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Submission"},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_df[LABELS] = pred\nsub_df.to_csv('submission.csv', index=False)\nsub_df.head()","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}