{"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":"markdown","source":"# SegFormer","metadata":{}},{"cell_type":"code","source":"# import tensorflow as tf; print(tf.__version__)\n# # 2.4.1\n# # 2.6.4 ","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:37:29.632693Z","iopub.execute_input":"2022-08-31T13:37:29.633359Z","iopub.status.idle":"2022-08-31T13:37:29.653776Z","shell.execute_reply.started":"2022-08-31T13:37:29.633266Z","shell.execute_reply":"2022-08-31T13:37:29.653142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install tensorflow==2.4.1","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:37:30.252711Z","iopub.execute_input":"2022-08-31T13:37:30.253208Z","iopub.status.idle":"2022-08-31T13:37:30.256878Z","shell.execute_reply.started":"2022-08-31T13:37:30.253178Z","shell.execute_reply":"2022-08-31T13:37:30.256228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ../input/hubmapsegformerb5/tokenizers-0.12.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:37:30.589375Z","iopub.execute_input":"2022-08-31T13:37:30.589803Z","iopub.status.idle":"2022-08-31T13:37:59.450176Z","shell.execute_reply.started":"2022-08-31T13:37:30.589772Z","shell.execute_reply":"2022-08-31T13:37:59.449332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ../input/hubmapsegformerb5/huggingface_hub-0.9.1-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:37:59.452053Z","iopub.execute_input":"2022-08-31T13:37:59.452363Z","iopub.status.idle":"2022-08-31T13:38:26.636885Z","shell.execute_reply.started":"2022-08-31T13:37:59.452325Z","shell.execute_reply":"2022-08-31T13:38:26.636062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ../input/hubmapsegformerb5/transformers-4.21.2-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:38:26.638591Z","iopub.execute_input":"2022-08-31T13:38:26.638880Z","iopub.status.idle":"2022-08-31T13:38:58.757976Z","shell.execute_reply.started":"2022-08-31T13:38:26.638842Z","shell.execute_reply":"2022-08-31T13:38:58.757154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install transformers==4.21.2\n# !pip install --force-reinstall ../input/hubmapsegformerb5/transformers-4.21.2-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:38:58.760432Z","iopub.execute_input":"2022-08-31T13:38:58.760742Z","iopub.status.idle":"2022-08-31T13:38:58.767000Z","shell.execute_reply.started":"2022-08-31T13:38:58.760704Z","shell.execute_reply":"2022-08-31T13:38:58.766205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from transformers import SegformerForSemanticSegmentation\nimport transformers\ntransformers.__version__\n# 4.5.1\n# 4.20.1","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:38:58.768965Z","iopub.execute_input":"2022-08-31T13:38:58.769256Z","iopub.status.idle":"2022-08-31T13:38:59.101866Z","shell.execute_reply.started":"2022-08-31T13:38:58.769219Z","shell.execute_reply":"2022-08-31T13:38:59.101068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile segformer.py\n\n\"\"\"\nLovasz-Softmax and Jaccard hinge loss in PyTorch\nMaxim Berman 2018 ESAT-PSI KU Leuven (MIT License)\n\"\"\"\nimport torch\nfrom torch.autograd import Variable\nimport torch.nn.functional as F\nimport torch.nn as nn\nimport numpy as np\ntry:\n    from itertools import  ifilterfalse\nexcept ImportError: # py3k\n    from itertools import  filterfalse","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:38:59.102827Z","iopub.execute_input":"2022-08-31T13:38:59.103061Z","iopub.status.idle":"2022-08-31T13:38:59.112569Z","shell.execute_reply.started":"2022-08-31T13:38:59.103025Z","shell.execute_reply":"2022-08-31T13:38:59.108529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\nimport os\nimport gc\nimport cv2\nimport rasterio\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nfrom tqdm import tqdm\nimport tifffile as tiff\nimport matplotlib.pyplot as plt\nfrom rasterio.windows import Window\nfrom torch.utils.data import Dataset, DataLoader\nimport warnings; warnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:38:59.113837Z","iopub.execute_input":"2022-08-31T13:38:59.114304Z","iopub.status.idle":"2022-08-31T13:38:59.120742Z","shell.execute_reply.started":"2022-08-31T13:38:59.114269Z","shell.execute_reply":"2022-08-31T13:38:59.119732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\nTH = 0.1\n\nDATA = '../input/hubmap-organ-segmentation/test_images/'\nCONFIG = '../input/hubmapsegformerb5/mit-b5.pickle'\nMODELS = [\n    \"../input/hubmapsegformerb5/last-new-fold0.pth\",\n#     \"../input/hubmapsegformerb5/last-new-fold1.pth\",\n    \"../input/hubmapsegformerb5/last-fold0.pth\",\n    \"../input/hubmapsegformerb5/last-fold1.pth\",\n    \"../input/hubmapsegformerb5/last-fold2.pth\",\n    \"../input/hubmapsegformerb5/last-fold3.pth\",\n    \"../input/hubmapsegformerb5/last-fold4.pth\",\n    \"../input/hubmapsegformerb5/model-b5-fold0.pth\",\n    \"../input/hubmapsegformerb5/model-b5-fold1.pth\",\n    \"../input/hubmapsegformerb5/model-b5-fold2.pth\",\n    \"../input/hubmapsegformerb5/model-b5-fold3.pth\",\n    \"../input/hubmapsegformerb5/model-b5-fold4.pth\",\n    \"../input/hubmapsegformerb5/last-fold0-4.pth\",\n]\n# MODELS = [f'../input/hubmapunext50base/model_{i}.pth' for i in range(4)]\ndf_sample = pd.read_csv('../input/hubmap-organ-segmentation/sample_submission.csv')\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:38:59.122352Z","iopub.execute_input":"2022-08-31T13:38:59.122880Z","iopub.status.idle":"2022-08-31T13:38:59.130553Z","shell.execute_reply.started":"2022-08-31T13:38:59.122848Z","shell.execute_reply":"2022-08-31T13:38:59.129754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\nclass2idx = {'prostate': 1,\n  'spleen': 2,\n  'lung': 3,\n  'kidney': 4,\n  'largeintestine': 5,\n  'none': 0}\nidx2class = {1: 'prostate',\n  2: 'spleen',\n  3: 'lung',\n  4: 'kidney',\n  5: 'largeintestine',\n  0: 'none'}","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:38:59.132242Z","iopub.execute_input":"2022-08-31T13:38:59.132493Z","iopub.status.idle":"2022-08-31T13:38:59.144239Z","shell.execute_reply.started":"2022-08-31T13:38:59.132462Z","shell.execute_reply":"2022-08-31T13:38:59.143274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\nfrom transformers import SegformerForSemanticSegmentation\n\nimport pickle\n\nconfig = {}\nwith open(CONFIG, mode=\"rb\") as f:\n    config = pickle.load(f)\n    \n    \n# from transformers import SegformerForSemanticSegmentation\n# from transformers import SegformerModel, SegformerConfig\n# MODEL_NAME=\"nvidia/segformer-b2-finetuned-ade-512-512\"\n# config = SegformerConfig.from_pretrained(MODEL_NAME,\n#                         num_labels=len(class2idx), \n#                         id2label=idx2class, \n#                         label2id=class2idx,\n# )\n\n# with open(\"mit-b2.pickle\", mode=\"wb\") as f :\n#     pickle.dump(config, f)\nmodels = []\nfor MODEL in MODELS:\n    model = SegformerForSemanticSegmentation(config)\n    model_path = MODEL\n    model.load_state_dict(torch.load(model_path))\n    model = model.cuda()\n    model.eval()\n    models.append(model)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:38:59.146869Z","iopub.execute_input":"2022-08-31T13:38:59.147186Z","iopub.status.idle":"2022-08-31T13:38:59.154617Z","shell.execute_reply.started":"2022-08-31T13:38:59.147155Z","shell.execute_reply":"2022-08-31T13:38:59.153848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\nDATA = '../input/hubmap-organ-segmentation/test_images/'\ndf_sample = pd.read_csv('../input/hubmap-organ-segmentation/test.csv').set_index('id')\n# DATA = '../input/hubmap-organ-segmentation/train_images/'\n# df_sample = pd.read_csv('../input/hubmap-organ-segmentation/train.csv').set_index('id')","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:38:59.155922Z","iopub.execute_input":"2022-08-31T13:38:59.156189Z","iopub.status.idle":"2022-08-31T13:38:59.169770Z","shell.execute_reply.started":"2022-08-31T13:38:59.156158Z","shell.execute_reply":"2022-08-31T13:38:59.169048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\ndef rle_encode_less_memory(img):\n    pixels = img.T.flatten()\n    pixels[0] = 0\n    pixels[-1] = 0\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:38:59.170934Z","iopub.execute_input":"2022-08-31T13:38:59.171536Z","iopub.status.idle":"2022-08-31T13:38:59.180901Z","shell.execute_reply.started":"2022-08-31T13:38:59.171409Z","shell.execute_reply":"2022-08-31T13:38:59.179987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\ndef calc_resize(w, p) :\n    a = 3000/512\n    target = int((w*p/0.4)/a + 0.5)\n    target = (target+31)//32 * 32\n    return target\n\ncalc_resize(2023, 0.4945), calc_resize(3000, 0.4)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:38:59.182501Z","iopub.execute_input":"2022-08-31T13:38:59.183208Z","iopub.status.idle":"2022-08-31T13:38:59.191002Z","shell.execute_reply.started":"2022-08-31T13:38:59.183176Z","shell.execute_reply":"2022-08-31T13:38:59.190214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\n# TH = 0.225\nfrom transformers import SegformerFeatureExtractor\n\nnames,preds = [],[]\nimgs, pd_mks = [],[]\ndebug = len(df_sample)<2\n# debug = True\n# tta = True\n\nmodel.eval()\n\ncnt = 0\nfile = \"\"\nfor idx,row in tqdm(df_sample.iterrows(),total=len(df_sample)):\n    image = Image.open(os.path.join(DATA,str(idx)+'.tiff'))\n    size = calc_resize(row.img_height, row.pixel_size)\n    feature_extractor = SegformerFeatureExtractor(reduce_labels=False, size=(size,size))\n    encoding = feature_extractor(image, return_tensors=\"pt\")\n#     print(idx)\n    pixel_values = encoding.pixel_values.cuda()\n#     print(pixel_values.shape)\n    organ = row.organ\n    height, width = row.img_height, row.img_width\n    index = class2idx[organ]\n    with torch.no_grad():\n        pred = []\n        for model in models:\n            model.eval()\n            with torch.no_grad() :\n                outputs = model(pixel_values=pixel_values)\n            upsampled_logits = nn.functional.interpolate(outputs['logits'],\n                        # size=image.size[::-1], # (height, width)\n                        (height, width),\n                        mode='bilinear',\n                        align_corners=False)\n            mask = upsampled_logits.argmax(dim=1)[0]\n            mask[mask != index] = 0\n            mask[mask == index] = 1\n            mask = mask * F.sigmoid(upsampled_logits[0][index])\n#             print(mask.shape, upsampled_logits.shape, mask.min(), mask.max())\n#             plt.imshow(mask.cpu().numpy())\n#             plt.show()\n            if len(pred) == 0 :\n                pred = mask.detach().cpu().numpy()\n            else :\n#                 pred += mask.detach().cpu().numpy()\n                pred = np.fmax(pred, mask.detach().cpu().numpy())\n#     pred /= len(models)\n#     plt.imshow(pred)\n#     plt.show()\n    pred[pred <= TH] = 0\n    pred[pred > TH] = 1\n#     plt.imshow(pred)\n#     plt.show()\n    rle = rle_encode_less_memory(pred)\n\n    names.append(str(idx))\n    preds.append(rle)\n    if debug:\n        imgs.append(image)\n        pd_mks.append(pred)\n    \n    if debug and cnt == 10:\n        file = os.path.join(DATA,str(idx)+'.tiff')\n        break\n    cnt+=1\n\n    del image, mask, rle, idx, row, pred, feature_extractor, encoding, pixel_values, upsampled_logits, outputs\n    gc.collect()   ","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:38:59.192581Z","iopub.execute_input":"2022-08-31T13:38:59.192846Z","iopub.status.idle":"2022-08-31T13:38:59.202608Z","shell.execute_reply.started":"2022-08-31T13:38:59.192814Z","shell.execute_reply":"2022-08-31T13:38:59.201924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\n#debug = True\nif debug:\n    import matplotlib.pyplot as plt\n    for img, mask in zip(imgs, pd_mks):\n        plt.figure(figsize=(12, 7))\n        plt.subplot(1, 3, 1); plt.imshow(img); plt.axis('OFF'); plt.title('image')\n        plt.subplot(1, 3, 2); plt.imshow(mask*255); plt.axis('OFF'); plt.title('mask')\n        plt.subplot(1, 3, 3); plt.imshow(img); plt.imshow(mask*255, alpha=0.4); plt.axis('OFF'); plt.title('overlay')\n        plt.tight_layout()\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:38:59.203834Z","iopub.execute_input":"2022-08-31T13:38:59.204290Z","iopub.status.idle":"2022-08-31T13:38:59.218017Z","shell.execute_reply.started":"2022-08-31T13:38:59.204257Z","shell.execute_reply":"2022-08-31T13:38:59.217298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a segformer.py\ndf = pd.DataFrame({'id':names,'rle':preds})\ndf.to_csv('submission1.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:38:59.218903Z","iopub.execute_input":"2022-08-31T13:38:59.219585Z","iopub.status.idle":"2022-08-31T13:38:59.228503Z","shell.execute_reply.started":"2022-08-31T13:38:59.219551Z","shell.execute_reply":"2022-08-31T13:38:59.227758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python3 segformer.py","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:39:51.410385Z","iopub.execute_input":"2022-08-31T13:39:51.410665Z","iopub.status.idle":"2022-08-31T13:41:15.596838Z","shell.execute_reply.started":"2022-08-31T13:39:51.410637Z","shell.execute_reply":"2022-08-31T13:41:15.595902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import sys\n\n# print(\"{}{: >25}{}{: >10}{}\".format('|','Variable Name','|','Memory','|'))\n# print(\" ------------------------------------ \")\n# for var_name in dir():\n#     if not var_name.startswith(\"_\") and sys.getsizeof(eval(var_name)) > 1000: #ここだけアレンジ\n#         print(\"{}{: >25}{}{: >10}{}\".format('|',var_name,'|',sys.getsizeof(eval(var_name)),'|'))","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:42:00.599622Z","iopub.execute_input":"2022-08-31T13:42:00.599934Z","iopub.status.idle":"2022-08-31T13:42:00.607575Z","shell.execute_reply.started":"2022-08-31T13:42:00.599900Z","shell.execute_reply":"2022-08-31T13:42:00.606547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:42:15.599831Z","iopub.execute_input":"2022-08-31T13:42:15.600160Z","iopub.status.idle":"2022-08-31T13:42:16.624510Z","shell.execute_reply.started":"2022-08-31T13:42:15.600101Z","shell.execute_reply":"2022-08-31T13:42:16.623666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# efficientnet","metadata":{}},{"cell_type":"markdown","source":"Copied from: https://www.kaggle.com/code/markwijkhuizen/hubmap-inference-tf-tpu-efficientnet-b7-640x640","metadata":{}},{"cell_type":"code","source":"%%writefile efficientnet.py\n\n# Import EfficientNet models with intermediate endpoints\nimport sys\nsys.path.append('../input/efficientnetv2-head-1x1-endpoint-v2/')\nsys.path.append('../input/efficientnetv2-head-1x1-endpoint-v2/efficientnetv2/')","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:26.870902Z","iopub.execute_input":"2022-08-31T13:44:26.871264Z","iopub.status.idle":"2022-08-31T13:44:26.877428Z","shell.execute_reply.started":"2022-08-31T13:44:26.871229Z","shell.execute_reply":"2022-08-31T13:44:26.876684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport tensorflow.keras.backend as K\nimport tensorflow_addons as tfa\nimport matplotlib.pyplot as plt\n\nfrom tensorflow.keras.mixed_precision import experimental as mixed_precision\nfrom kaggle_datasets import KaggleDatasets\nfrom tqdm.notebook import tqdm\nfrom multiprocessing import cpu_count\nfrom sklearn import metrics\nfrom sklearn.model_selection import KFold\n\nimport effnetv2_model\nimport tifffile\nimport re\nimport os\nimport io\nimport time\nimport pickle\nimport math\nimport random\nimport sys\nimport cv2\nimport gc\n\nprint(f'tensorflow version: {tf.__version__}')\nprint(f'tensorflow keras version: {tf.keras.__version__}')\nprint(f'python version: P{sys.version}')","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:27.995074Z","iopub.execute_input":"2022-08-31T13:44:27.995652Z","iopub.status.idle":"2022-08-31T13:44:28.001333Z","shell.execute_reply.started":"2022-08-31T13:44:27.995618Z","shell.execute_reply":"2022-08-31T13:44:28.000593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Seed all random number generators\ndef seed_everything(seed):\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    random.seed(seed)\n    np.random.seed(seed)\n    tf.random.set_seed(seed)\n    \nSEED = 42\nseed_everything(SEED)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:28.675632Z","iopub.execute_input":"2022-08-31T13:44:28.676434Z","iopub.status.idle":"2022-08-31T13:44:28.682033Z","shell.execute_reply.started":"2022-08-31T13:44:28.676376Z","shell.execute_reply":"2022-08-31T13:44:28.681230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Threshold to classify a pixel as mask\nTHRESHOLD = 0.400\nTHs = {'lung':0.32, 'kidney':0.52, 'largeintestine':0.51, 'prostate':0.30, 'spleen':0.45}\n#THs = {'lung':0.10, 'kidney':0.40, 'largeintestine':0.40, 'prostate':0.30, 'spleen':0.40}\n\n\nDEBUG = False\nIS_TPU = True\n\n# Image dimensions\nIMG_SIZE = 640\nPATCH_SIZE = 640\nN_CHANNELS = 3\nN_PATCHES_PER_IMAGE = (IMG_SIZE // PATCH_SIZE) ** 2\n\nINPUT_SHAPE = (PATCH_SIZE, PATCH_SIZE, N_CHANNELS)\n\n# EfficientNet version, b0/b1/b2/b3/s/m/l/xl/xxl\nEFN_SIZE = 'b8'\nLR_MAX = 0.02\nEPOCHS = 30\nMOMENTUM = 0.00\n\n# Batch size\nBATCH_SIZE = 64\n\n# Dataset Mean and Standard Deviation\nMEAN = np.load('/kaggle/input/hubmap-patched-tfrecords-300x300/MEAN.npy')\nSTD = np.load('/kaggle/input/hubmap-patched-tfrecords-300x300/STD.npy')\n\nprint(f'MEAN: {MEAN}, STD: {STD}')","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:29.914868Z","iopub.execute_input":"2022-08-31T13:44:29.915731Z","iopub.status.idle":"2022-08-31T13:44:29.926079Z","shell.execute_reply.started":"2022-08-31T13:44:29.915697Z","shell.execute_reply":"2022-08-31T13:44:29.924335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Hardware Configuration","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Detect hardware, return appropriate distribution strategy\ntry:\n    TPU = tf.distribute.cluster_resolver.TPUClusterResolver()  # TPU detection. No parameters necessary if TPU_NAME environment variable is set. On Kaggle this is always the case.\n    print('Running on TPU ', TPU.master())\nexcept ValueError:\n    print('Running on GPU')\n    TPU = None\n\nif TPU:\n    tf.config.experimental_connect_to_cluster(TPU)\n    tf.tpu.experimental.initialize_tpu_system(TPU)\n    strategy = tf.distribute.experimental.TPUStrategy(TPU)\nelse:\n    strategy = tf.distribute.get_strategy() # default distribution strategy in Tensorflow. Works on CPU and single GPU.\n\nREPLICAS = strategy.num_replicas_in_sync\nprint(f'REPLICAS: {REPLICAS}')","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:31.043478Z","iopub.execute_input":"2022-08-31T13:44:31.043764Z","iopub.status.idle":"2022-08-31T13:44:31.049039Z","shell.execute_reply.started":"2022-08-31T13:44:31.043728Z","shell.execute_reply":"2022-08-31T13:44:31.048327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# FPN","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\ndef FPN(xs, output_channels, last_layer, debug=False):\n    def _conv(x):\n        x = tf.keras.layers.ZeroPadding2D(padding=1)(x)\n        x = tf.keras.layers.Conv2D(output_channels * 2, 3, padding='SAME', kernel_initializer='he_normal', activation='relu')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = tf.keras.layers.ZeroPadding2D(padding=1)(x)\n        x = tf.keras.layers.Conv2D(output_channels, 3, padding='SAME', kernel_initializer='he_normal')(x)\n        x = tf.image.resize(x, size=target_size, method=tf.image.ResizeMethod.BILINEAR)\n        x = tf.nn.relu(x)\n        return x\n\n    target_size = last_layer.shape[1:3]\n    xs = tf.keras.layers.Concatenate()([_conv(x) for x in xs])\n    x = tf.keras.layers.Concatenate()([xs, last_layer])\n\n    if debug:\n        return x, xs\n    else:\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:32.121680Z","iopub.execute_input":"2022-08-31T13:44:32.122262Z","iopub.status.idle":"2022-08-31T13:44:32.128268Z","shell.execute_reply.started":"2022-08-31T13:44:32.122223Z","shell.execute_reply":"2022-08-31T13:44:32.127469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ASPP","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\ndef ASPP(x, mid_c=320, dilations=[1, 2, 3, 4], out_c=640, debug=False):\n    def _aspp_module(x, filters, kernel_size, padding, dilation, groups=1):\n        x = tf.keras.layers.ZeroPadding2D(padding=padding)(x)\n        x = tf.keras.layers.Conv2D(\n                filters=filters,\n                kernel_size=kernel_size,\n                dilation_rate=dilation,\n                groups=1 if IS_TPU else groups,\n                kernel_initializer='he_uniform',\n            )(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = tf.nn.relu(x)\n        \n        return x\n    \n    x0 = tf.math.reduce_max(x, axis=(1,2), keepdims=True)\n    x0 = tf.keras.layers.Conv2D(filters=mid_c, kernel_size=1, strides=1, kernel_initializer='he_uniform', use_bias=False)(x0)\n    x0 = tf.keras.layers.BatchNormalization(gamma_initializer=tf.constant_initializer(value=0.25))(x0)\n    x0 = tf.nn.relu(x0)\n                                  \n                                  \n    xs = (\n        [_aspp_module(x, mid_c, 1, padding=0, dilation=1)] +\n        [_aspp_module(x, mid_c, 3, padding=d, dilation=d, groups=4) for d in dilations]\n    )\n    \n    x0= tf.image.resize(x0, size=xs[0].shape[1:3])\n    x = tf.keras.layers.Concatenate()([x0] + xs)\n    x = tf.keras.layers.Conv2D(filters=out_c, kernel_size=1, kernel_initializer='he_uniform', use_bias=False)(x)\n    x = tf.keras.layers.BatchNormalization()(x)\n    x = tf.nn.relu(x)\n                       \n    if debug:\n        return x, x0, xs\n    else:\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:32.952052Z","iopub.execute_input":"2022-08-31T13:44:32.952775Z","iopub.status.idle":"2022-08-31T13:44:32.960113Z","shell.execute_reply.started":"2022-08-31T13:44:32.952734Z","shell.execute_reply":"2022-08-31T13:44:32.959155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Upsample","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\ndef PixelShuffle(x, upscale_factor=2):\n    _, w, h, c = x.shape\n    n = -1\n\n    c_out = c // upscale_factor ** 2\n    w_out = w * upscale_factor\n    h_out = h * upscale_factor\n\n    x = tf.reshape(x, [-1, upscale_factor, upscale_factor, w, h, c_out])\n    x = tf.transpose(x, [0, 3, 1, 4, 2, 5])\n    x = tf.reshape(x, [-1, w_out, h_out, c_out])\n\n    return x","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:33.646325Z","iopub.execute_input":"2022-08-31T13:44:33.646799Z","iopub.status.idle":"2022-08-31T13:44:33.652598Z","shell.execute_reply.started":"2022-08-31T13:44:33.646767Z","shell.execute_reply":"2022-08-31T13:44:33.651801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n\n# Inspiration: https://www.tensorflow.org/tutorials/generative/pix2pix#build_an_input_pipeline_with_tfdata\ndef upsample(x, concat, target_filters, name, conv2dt_kernel_init_max, relu=True, dropout=0, debug=False):\n#     x = PixelShuffle(x)\n\n    filters = concat.shape[-1]\n    x_up = tf.keras.layers.Conv2DTranspose(\n            filters, # Number of Convolutional Filters\n            kernel_size=4, # Kernel Size\n            strides=2, # Kernel Steps\n            padding='SAME', # linear scaling\n            name=f'Conv2DTranspose_{name}', # Name of Layer\n            kernel_initializer='he_uniform',\n            use_bias=False,\n        )(x)\n    \n    concat = tf.keras.layers.BatchNormalization(\n        gamma_initializer=tf.constant_initializer(value=0.25),\n        name=f'BatchNormalization_{name}'\n    )(concat)\n    x = tf.keras.layers.Concatenate(name=f'Concatenate_{name}')([x_up, concat])\n    x = tf.nn.relu(x)\n    \n        \n    x = tf.keras.layers.Conv2D(target_filters, 3, padding='SAME', kernel_initializer='he_uniform', activation='relu', name=f'Conv2D_1_{name}')(x)\n    x = tf.keras.layers.Conv2D(target_filters, 3, padding='SAME', kernel_initializer='he_uniform', name=f'Conv2D_2_{name}')(x)\n    \n    if relu:\n        x = tf.nn.relu(x)\n    \n    x = tf.keras.layers.Dropout(dropout, name=f'Dropout_{name}')(x)\n\n    if debug:\n        return x, x_up, concat\n    else:\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:33.806512Z","iopub.execute_input":"2022-08-31T13:44:33.806843Z","iopub.status.idle":"2022-08-31T13:44:33.814806Z","shell.execute_reply.started":"2022-08-31T13:44:33.806815Z","shell.execute_reply":"2022-08-31T13:44:33.814008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n\ndef get_model(dropout_decoder=0, dropout_cnn=0, file_path=None, lr=1e-3, eps=1e-7, clipnorm=5.0, wd_coef=1e-2, cnn_trainable=True):\n    with strategy.scope():\n        # EfficientNetV2 Backbone # \n        cnn = effnetv2_model.get_model(f'efficientnet-{EFN_SIZE}', include_top=False, weights=None, model_config={ 'conv_dropout': dropout_cnn })\n        cnn.trainable = cnn_trainable\n\n        # Inputs, note the names are equal to the dictionary keys in the dataset\n        image = tf.keras.layers.Input(INPUT_SHAPE, name='image', dtype=tf.float32)\n        image_norm = tf.cast(image, tf.float32) / 255\n        image_norm = tf.keras.layers.experimental.preprocessing.Normalization(mean=MEAN, variance=STD, dtype=tf.float32)(image_norm)\n\n        embedding, up6, up5, up4, up3, up2, up1 = cnn(image_norm, with_endpoints=True)\n        print(f'embedding shape: {embedding.shape} up1 shape: {up1.shape}, up2 shape: {up2.shape}')\n        print(f'up3 shape: {up3.shape}, up4 shape: {up4.shape}, up5 shape: {up5.shape}, up6 shape: {up6.shape}')\n        \n        dec0 = ASPP(up2)\n        dec0 = tf.keras.layers.Dropout(0.50)(dec0)\n\n        dec1 = upsample(dec0, up3, up4.shape[-1] * 4, 'upsample1', 0.02, dropout=dropout_decoder)\n        dec2 = upsample(dec1, up4, up5.shape[-1] * 2, 'upsample2', 0.02, dropout=dropout_decoder)\n        dec3 = upsample(dec2, up5, up6.shape[-1] * 2, 'upsample3', 0.02)\n        dec4 = upsample(dec3, up6, 64, 'upsample4', 0.02)\n        \n        print(f'dec0 shape: {dec0.shape}, dec1 shape: {dec1.shape}, dec2 shape: {dec2.shape}, dec3 shape: {dec3.shape}, dec4 shape: {dec4.shape}')\n        \n        dec_fpn = FPN([dec0, dec1, dec2, dec3], 32, dec4)\n        \n        print(f'dec_fpn shape: {dec_fpn.shape}')\n        \n        # Head\n        x = tf.keras.layers.Dropout(0.10)(dec_fpn)\n        x = tf.keras.layers.Conv2D(\n            filters=1,\n            kernel_size=1,\n            padding='SAME',\n            kernel_initializer=tf.random_normal_initializer(0.00, 0.05),\n            activation='sigmoid',\n            name='Conv2D_3_head'\n        )(x)\n        output = tf.image.resize(x, size=[IMG_SIZE, IMG_SIZE], method=tf.image.ResizeMethod.BILINEAR)\n        \n        model = tf.keras.models.Model(inputs=image, outputs=output)\n\n        if file_path:\n            print('Loading pretrained weights...')\n            model.load_weights(file_path)\n            \n        model.trainable = False\n\n        return model","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:34.368148Z","iopub.execute_input":"2022-08-31T13:44:34.368674Z","iopub.status.idle":"2022-08-31T13:44:34.376667Z","shell.execute_reply.started":"2022-08-31T13:44:34.368644Z","shell.execute_reply":"2022-08-31T13:44:34.375833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Pretrained File Path: '/kaggle/input/sartorius-training-dataset/model.h5'\nmodel = get_model(file_path='../input/hubmap-training-tf-tpu-efficientnet-b8-640640-p/model_0.h5')","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:34.704210Z","iopub.execute_input":"2022-08-31T13:44:34.704719Z","iopub.status.idle":"2022-08-31T13:44:34.710599Z","shell.execute_reply.started":"2022-08-31T13:44:34.704690Z","shell.execute_reply":"2022-08-31T13:44:34.709886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\nmodels = [\n#     model,\n    get_model(file_path='../input/hubmapefficientnetb8/model_0.h5'),\n    get_model(file_path='../input/hubmapefficientnetb8/model_1.h5'),\n    get_model(file_path='../input/hubmapefficientnetb8/model_2.h5'),\n    get_model(file_path='../input/hubmapefficientnetb8/model_3.h5'),\n]","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:35.079861Z","iopub.execute_input":"2022-08-31T13:44:35.080505Z","iopub.status.idle":"2022-08-31T13:44:35.086325Z","shell.execute_reply.started":"2022-08-31T13:44:35.080471Z","shell.execute_reply":"2022-08-31T13:44:35.085508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Plot model summary\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:35.777859Z","iopub.execute_input":"2022-08-31T13:44:35.778388Z","iopub.status.idle":"2022-08-31T13:44:35.783114Z","shell.execute_reply.started":"2022-08-31T13:44:35.778355Z","shell.execute_reply":"2022-08-31T13:44:35.782337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\ntf.keras.utils.plot_model(model, show_shapes=True, show_dtype=True, show_layer_names=True, expand_nested=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:36.394809Z","iopub.execute_input":"2022-08-31T13:44:36.395070Z","iopub.status.idle":"2022-08-31T13:44:36.402823Z","shell.execute_reply.started":"2022-08-31T13:44:36.395041Z","shell.execute_reply":"2022-08-31T13:44:36.401952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Utility Funtions","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Resized a tensor to the specified size\ndef resize_tensor(tensor, size=IMG_SIZE, dtype=np.uint8):\n    return cv2.resize(tensor, [size, size], interpolation=cv2.INTER_CUBIC).astype(dtype)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:37.326855Z","iopub.execute_input":"2022-08-31T13:44:37.327407Z","iopub.status.idle":"2022-08-31T13:44:37.332695Z","shell.execute_reply.started":"2022-08-31T13:44:37.327372Z","shell.execute_reply":"2022-08-31T13:44:37.331977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode\ndef get_mask(image_id):\n    row = train.loc[train['id'] == image_id].squeeze()\n    h, w = row[['img_height', 'img_width']]\n    mask = np.zeros(shape=[h * w], dtype=np.uint8)\n    s = row['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    for lo, hi in zip(starts, ends):\n        mask[lo : hi] = 1\n        \n    mask = mask.reshape([h, w]).T\n        \n    mask = resize_tensor(mask)\n    \n    mask = np.expand_dims(mask, axis=2)\n        \n    return mask","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:37.888857Z","iopub.execute_input":"2022-08-31T13:44:37.889572Z","iopub.status.idle":"2022-08-31T13:44:37.894976Z","shell.execute_reply.started":"2022-08-31T13:44:37.889539Z","shell.execute_reply":"2022-08-31T13:44:37.894114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Reads an image and returns the image and original image size\ndef get_image(image_id, folder, negative=True):\n    image = tifffile.imread(f'/kaggle/input/hubmap-organ-segmentation/{folder}_images/{image_id}.tiff')\n    if len(image.shape) == 5:\n        image = image.squeeze().transpose(1, 2, 0)\n    \n    # Image Size\n    image_size, _, _ = image.shape\n    \n    # Reverse pixels to make tissue colored and background black\n    if negative:\n        image = image - image.min()\n        image = image / (image.max() - image.min())\n        image = image * 255\n        image = 255 - image.astype(np.uint8)\n        \n    # Resize\n    image = resize_tensor(image)\n    \n    return image, image_size","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:38.506166Z","iopub.execute_input":"2022-08-31T13:44:38.506946Z","iopub.status.idle":"2022-08-31T13:44:38.512506Z","shell.execute_reply.started":"2022-08-31T13:44:38.506904Z","shell.execute_reply":"2022-08-31T13:44:38.511758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# extract patches from an image\ndef extract_patches(image):\n    _, _, c = image.shape\n    image = tf.expand_dims(image, 0)\n    image_patches = tf.image.extract_patches(image, [1,PATCH_SIZE,PATCH_SIZE,1], [1, PATCH_SIZE, PATCH_SIZE, 1], [1, 1, 1, 1], padding='SAME')\n    image_patches = tf.reshape(image_patches, [N_PATCHES_PER_IMAGE, PATCH_SIZE, PATCH_SIZE, c])\n    image_patches = image_patches.numpy()\n\n    return image_patches","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:39.235589Z","iopub.execute_input":"2022-08-31T13:44:39.235989Z","iopub.status.idle":"2022-08-31T13:44:39.241867Z","shell.execute_reply.started":"2022-08-31T13:44:39.235961Z","shell.execute_reply":"2022-08-31T13:44:39.240995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n#https://www.kaggle.com/bguberfain/memory-aware-rle-encoding\n#with transposed mask\ndef rle_encode_less_memory(img):\n    #the image should be transposed\n    pixels = img.T.flatten()\n    \n    # This simplified method requires first and last pixel to be zero\n    pixels[0] = 0\n    pixels[-1] = 0\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    runs[1::2] -= runs[::2]\n    \n    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:39.951955Z","iopub.execute_input":"2022-08-31T13:44:39.952522Z","iopub.status.idle":"2022-08-31T13:44:39.959055Z","shell.execute_reply.started":"2022-08-31T13:44:39.952488Z","shell.execute_reply":"2022-08-31T13:44:39.957970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Training DataFrame\ntrain = pd.read_csv('/kaggle/input/hubmap-organ-segmentation/train.csv')\n\n#display(train.head())\n#display(train.info())","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:41.072904Z","iopub.execute_input":"2022-08-31T13:44:41.073256Z","iopub.status.idle":"2022-08-31T13:44:41.079461Z","shell.execute_reply.started":"2022-08-31T13:44:41.073221Z","shell.execute_reply":"2022-08-31T13:44:41.078628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Test DataFrame\ntest = pd.read_csv('/kaggle/input/hubmap-organ-segmentation/test.csv')\n\n#display(test.head())\n#display(test.info())","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:41.966573Z","iopub.execute_input":"2022-08-31T13:44:41.966850Z","iopub.status.idle":"2022-08-31T13:44:41.972475Z","shell.execute_reply.started":"2022-08-31T13:44:41.966820Z","shell.execute_reply":"2022-08-31T13:44:41.971620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Reconstruct the original image from patches\ndef merge_patches(patches):\n    image = np.zeros(shape=[IMG_SIZE, IMG_SIZE, patches.shape[-1]], dtype=patches.dtype)\n    s = int(N_PATCHES_PER_IMAGE ** 0.50)\n    for r in range(s):\n        for c in range(s):\n            start_x = r * PATCH_SIZE\n            end_x = (r + 1) * PATCH_SIZE\n            start_y = c * PATCH_SIZE\n            end_y = (c + 1) * PATCH_SIZE\n            image[start_x:end_x, start_y:end_y] = patches[r * s + c]\n            \n    return image","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:42.523532Z","iopub.execute_input":"2022-08-31T13:44:42.524313Z","iopub.status.idle":"2022-08-31T13:44:42.529864Z","shell.execute_reply.started":"2022-08-31T13:44:42.524278Z","shell.execute_reply":"2022-08-31T13:44:42.528978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sanity Check","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Plot 10 predictions to verify the trained weights are correctly loaded\ntest_rows = []\nN = 10\n\nfor row_idx, row in tqdm(train[:N].iterrows(), total=N):\n    image, image_size = get_image(row['id'], 'train')\n    pred_all = []\n    \n    for model in models:\n#         model = models[0]\n        # Make Prediction\n        # Preprocess Image\n        image_patches = extract_patches(image)    \n        mask_patches_pred = model.predict(image_patches)\n        # Merge patches\n        mask_pred = merge_patches(mask_patches_pred)\n\n#         #------flip 0------\n#         image = np.flip(image, (0))\n#         image_patches = extract_patches(image)\n#         # Make Prediction\n#         mask_patches_pred = model.predict(image_patches)\n#         # Merge patches\n#         mask_pred += np.flip(merge_patches(mask_patches_pred), (0))\n#         image = np.flip(image, (0))\n\n#         #------flip 1------\n#         image = np.flip(image, (1))\n#         image_patches = extract_patches(image)\n#         # Make Prediction\n#         mask_patches_pred = model.predict(image_patches)\n#         # Merge patches\n#         mask_pred += np.flip(merge_patches(mask_patches_pred), (1))\n#         image = np.flip(image, (1))\n\n#         #------flip 0,1------\n#         image = np.flip(image, (0,1))\n#         image_patches = extract_patches(image)\n#         # Make Prediction\n#         mask_patches_pred = model.predict(image_patches)\n#         # Merge patches\n#         mask_pred += np.flip(merge_patches(mask_patches_pred), (0,1))\n#         image = np.flip(image, (0,1))\n#         mask_pred /= 4\n    \n        if len(pred_all) == 0 :\n            pred_all = mask_pred\n        else :\n            pred_all = np.fmax(pred_all, mask_pred)\n        \n#     pred_all /= len(models)\n\n    # Resize mask to original size\n    mask_pred_resized = resize_tensor(pred_all, size=image_size, dtype=np.float32)    \n    \n    fig, axes = plt.subplots(1,2, figsize=(8,4))\n    axes[0].imshow(mask_pred_resized > THs[row['organ']])\n    plt.title(row['id'])\n    axes[1].imshow(image)\n    plt.show()\n        \n    # Resize and Binarize Mask\n    mask_binary = (mask_pred_resized > THs[row['organ']]).astype(np.int8)\n    # Append to Result\n    test_rows.append({\n        'id': row['id'],\n        'rle': rle_encode_less_memory(mask_binary)\n    })","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:43.574369Z","iopub.execute_input":"2022-08-31T13:44:43.574690Z","iopub.status.idle":"2022-08-31T13:44:43.580545Z","shell.execute_reply.started":"2022-08-31T13:44:43.574660Z","shell.execute_reply":"2022-08-31T13:44:43.579840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Plot 10 predictions to verify the trained weights are correctly loaded\n# test_rows = []\n# N = 10\n\n# for row_idx, row in tqdm(train[:N].iterrows(), total=N):\n#     # Preprocess Image\n#     image, image_size = get_image(row['id'], 'train')\n#     image_patches = extract_patches(image)\n    \n#     # Make Prediction\n#     mask_patches_pred = model.predict(image_patches)\n#     mask_pred = merge_patches(mask_patches_pred)\n    \n#     #------flip 0------\n#     image = np.flip(image, (0))\n#     image_patches = extract_patches(image)\n#     # Make Prediction\n#     mask_patches_pred = model.predict(image_patches)\n#     # Merge patches\n#     mask_pred += np.flip(merge_patches(mask_patches_pred), (0))\n#     image = np.flip(image, (0))\n\n#     #------flip 1------\n#     image = np.flip(image, (1))\n#     image_patches = extract_patches(image)\n#     # Make Prediction\n#     mask_patches_pred = model.predict(image_patches)\n#     # Merge patches\n#     mask_pred += np.flip(merge_patches(mask_patches_pred), (1))\n#     image = np.flip(image, (1))\n\n#     #------flip 0,1------\n#     image = np.flip(image, (0,1))\n#     image_patches = extract_patches(image)\n#     # Make Prediction\n#     mask_patches_pred = model.predict(image_patches)\n#     # Merge patches\n#     mask_pred += np.flip(merge_patches(mask_patches_pred), (0,1))\n#     image = np.flip(image, (0,1))\n#     mask_pred /= 4\n    \n#     mask_pred_resized = resize_tensor(mask_pred, size=image_size, dtype=np.float32)\n    \n#     fig, axes = plt.subplots(1,2, figsize=(8,4))\n#     axes[0].imshow(mask_pred_resized)\n#     axes[1].imshow(image)\n#     plt.show()\n        \n#     # Resize and Binarize Mask\n#     mask_binary = (mask_pred_resized > THRESHOLD).astype(np.int8)\n#     # Append to Result\n#     test_rows.append({\n#         'id': row['id'],\n#         'rle': rle_encode_less_memory(mask_binary)\n#     })","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:44.159871Z","iopub.execute_input":"2022-08-31T13:44:44.160733Z","iopub.status.idle":"2022-08-31T13:44:44.166696Z","shell.execute_reply.started":"2022-08-31T13:44:44.160681Z","shell.execute_reply":"2022-08-31T13:44:44.165827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference Loop","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n#  Predictions are stored as a list of dictionaries\ntest_rows = []\n\n# Iterate over all test images\nfor row_idx, row in tqdm(test.iterrows(), total=len(test)):\n    # Preprocess Image\n    image, image_size = get_image(row['id'], 'test')\n    pred_all = []\n    \n    for model in models:\n        # Make Prediction\n        image_patches = extract_patches(image)\n        mask_patches_pred = model.predict(image_patches)\n        # Merge patches\n        mask_pred = merge_patches(mask_patches_pred)\n\n#         #------flip 0------\n#         image = np.flip(image, (0))\n#         image_patches = extract_patches(image)\n#         # Make Prediction\n#         mask_patches_pred = model.predict(image_patches)\n#         # Merge patches\n#         mask_pred += np.flip(merge_patches(mask_patches_pred), (0))\n#         image = np.flip(image, (0))\n\n#         #------flip 1------\n#         image = np.flip(image, (1))\n#         image_patches = extract_patches(image)\n#         # Make Prediction\n#         mask_patches_pred = model.predict(image_patches)\n#         # Merge patches\n#         mask_pred += np.flip(merge_patches(mask_patches_pred), (1))\n#         image = np.flip(image, (1))\n\n#         #------flip 0,1------\n#         image = np.flip(image, (0,1))\n#         image_patches = extract_patches(image)\n#         # Make Prediction\n#         mask_patches_pred = model.predict(image_patches)\n#         # Merge patches\n#         mask_pred += np.flip(merge_patches(mask_patches_pred), (0,1))\n#         image = np.flip(image, (0,1))\n#         mask_pred /= 4\n    \n        if len(pred_all) == 0 :\n            pred_all = mask_pred\n        else :\n            pred_all = np.fmax(pred_all, mask_pred)\n        \n#     pred_all /= len(models)\n\n    # Resize mask to original size\n    mask_pred_resized = resize_tensor(pred_all, size=image_size, dtype=np.float32)\n    \n    if row_idx == 0:\n        fig, axes = plt.subplots(1,2, figsize=(8,4))\n        axes[0].imshow(mask_pred_resized)\n        axes[1].imshow(image)\n        plt.show()\n        \n    # Resize and Binarize Mask\n    mask_binary = (mask_pred_resized > THs[row['organ']]).astype(np.int8)\n    # Append to Result\n    test_rows.append({\n        'id': row['id'],\n        'rle': rle_encode_less_memory(mask_binary)\n    })","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:45.082957Z","iopub.execute_input":"2022-08-31T13:44:45.083248Z","iopub.status.idle":"2022-08-31T13:44:45.090477Z","shell.execute_reply.started":"2022-08-31T13:44:45.083216Z","shell.execute_reply":"2022-08-31T13:44:45.089669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Predictions are stored as a list of dictionaries\n# test_rows = []\n\n# # Iterate over all test images\n# for row_idx, row in tqdm(test.iterrows(), total=len(test)):\n#     # Preprocess Image\n#     image, image_size = get_image(row['id'], 'test')\n#     image_patches = extract_patches(image)\n    \n#     # Make Prediction\n#     mask_patches_pred = model.predict(image_patches)\n#     # Merge patches\n#     mask_pred = merge_patches(mask_patches_pred)\n    \n#     #------flip 0------\n#     image = np.flip(image, (0))\n#     image_patches = extract_patches(image)\n#     # Make Prediction\n#     mask_patches_pred = model.predict(image_patches)\n#     # Merge patches\n#     mask_pred += np.flip(merge_patches(mask_patches_pred), (0))\n#     image = np.flip(image, (0))\n\n#     #------flip 1------\n#     image = np.flip(image, (1))\n#     image_patches = extract_patches(image)\n#     # Make Prediction\n#     mask_patches_pred = model.predict(image_patches)\n#     # Merge patches\n#     mask_pred += np.flip(merge_patches(mask_patches_pred), (1))\n#     image = np.flip(image, (1))\n\n#     #------flip 0,1------\n#     image = np.flip(image, (0,1))\n#     image_patches = extract_patches(image)\n#     # Make Prediction\n#     mask_patches_pred = model.predict(image_patches)\n#     # Merge patches\n#     mask_pred += np.flip(merge_patches(mask_patches_pred), (0,1))\n#     image = np.flip(image, (0,1))\n#     mask_pred /= 4\n    \n#     # Resize mask to original size\n#     mask_pred_resized = resize_tensor(mask_pred, size=image_size, dtype=np.float32)\n    \n#     if row_idx == 0:\n#         fig, axes = plt.subplots(1,2, figsize=(8,4))\n#         axes[0].imshow(mask_pred_resized)\n#         axes[1].imshow(image)\n#         plt.show()\n        \n#     # Resize and Binarize Mask\n#     mask_binary = (mask_pred_resized > THRESHOLD).astype(np.int8)\n#     # Append to Result\n#     test_rows.append({\n#         'id': row['id'],\n#         'rle': rle_encode_less_memory(mask_binary)\n#     })","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:45.703398Z","iopub.execute_input":"2022-08-31T13:44:45.704109Z","iopub.status.idle":"2022-08-31T13:44:45.709334Z","shell.execute_reply.started":"2022-08-31T13:44:45.704061Z","shell.execute_reply":"2022-08-31T13:44:45.708537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Make Submission CSV","metadata":{}},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Make submission DataFrame\ntest_df = pd.DataFrame(test_rows)\n\n#display(test_df.head())\n#display(test_df.info())","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:46.891387Z","iopub.execute_input":"2022-08-31T13:44:46.892159Z","iopub.status.idle":"2022-08-31T13:44:46.897413Z","shell.execute_reply.started":"2022-08-31T13:44:46.892104Z","shell.execute_reply":"2022-08-31T13:44:46.896595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile -a efficientnet.py\n# Write Submission CSV\ntest_df.to_csv('submission2.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:44:47.642597Z","iopub.execute_input":"2022-08-31T13:44:47.643011Z","iopub.status.idle":"2022-08-31T13:44:47.651696Z","shell.execute_reply.started":"2022-08-31T13:44:47.642980Z","shell.execute_reply":"2022-08-31T13:44:47.650878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python3 efficientnet.py","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:45:12.094212Z","iopub.execute_input":"2022-08-31T13:45:12.095051Z","iopub.status.idle":"2022-08-31T13:47:57.256173Z","shell.execute_reply.started":"2022-08-31T13:45:12.094995Z","shell.execute_reply":"2022-08-31T13:47:57.255065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import sys\n\n# print(\"{}{: >25}{}{: >10}{}\".format('|','Variable Name','|','Memory','|'))\n# print(\" ------------------------------------ \")\n# for var_name in dir():\n#     if not var_name.startswith(\"_\") and sys.getsizeof(eval(var_name)) > 1000: #ここだけアレンジ\n#         print(\"{}{: >25}{}{: >10}{}\".format('|',var_name,'|',sys.getsizeof(eval(var_name)),'|'))","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:48:07.310747Z","iopub.execute_input":"2022-08-31T13:48:07.311044Z","iopub.status.idle":"2022-08-31T13:48:07.316303Z","shell.execute_reply.started":"2022-08-31T13:48:07.311011Z","shell.execute_reply":"2022-08-31T13:48:07.315434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# del image, image_patches, mask_binary, mask_patches_pred, mask_pred, mask_pred_resized, train\n# # gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:48:08.093161Z","iopub.execute_input":"2022-08-31T13:48:08.093765Z","iopub.status.idle":"2022-08-31T13:48:08.096980Z","shell.execute_reply.started":"2022-08-31T13:48:08.093725Z","shell.execute_reply":"2022-08-31T13:48:08.096219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ensamble","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:50:48.107519Z","iopub.execute_input":"2022-08-31T13:50:48.108276Z","iopub.status.idle":"2022-08-31T13:50:48.113873Z","shell.execute_reply.started":"2022-08-31T13:50:48.108241Z","shell.execute_reply":"2022-08-31T13:50:48.112840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_1st = pd.read_csv(\"submission1.csv\")\ndf_1st","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:50:49.806098Z","iopub.execute_input":"2022-08-31T13:50:49.806669Z","iopub.status.idle":"2022-08-31T13:50:49.819935Z","shell.execute_reply.started":"2022-08-31T13:50:49.806637Z","shell.execute_reply":"2022-08-31T13:50:49.819137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_2nd = pd.read_csv(\"submission2.csv\")\ndf_2nd","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:50:49.999381Z","iopub.execute_input":"2022-08-31T13:50:50.000074Z","iopub.status.idle":"2022-08-31T13:50:50.012434Z","shell.execute_reply.started":"2022-08-31T13:50:50.000036Z","shell.execute_reply":"2022-08-31T13:50:50.011547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/hubmap-organ-segmentation/test.csv')\ntest","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:50:50.132615Z","iopub.execute_input":"2022-08-31T13:50:50.132889Z","iopub.status.idle":"2022-08-31T13:50:50.148612Z","shell.execute_reply.started":"2022-08-31T13:50:50.132858Z","shell.execute_reply":"2022-08-31T13:50:50.147819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_1st.id = df_1st.id.astype(str)\ndf_2nd.id = df_2nd.id.astype(str)\ntest.id = test.id.astype(str)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:51:26.029486Z","iopub.execute_input":"2022-08-31T13:51:26.030072Z","iopub.status.idle":"2022-08-31T13:51:26.035544Z","shell.execute_reply.started":"2022-08-31T13:51:26.030036Z","shell.execute_reply":"2022-08-31T13:51:26.034671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.merge(df_1st, df_2nd, on=\"id\")","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:51:26.210947Z","iopub.execute_input":"2022-08-31T13:51:26.211765Z","iopub.status.idle":"2022-08-31T13:51:26.219506Z","shell.execute_reply.started":"2022-08-31T13:51:26.211714Z","shell.execute_reply":"2022-08-31T13:51:26.218567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.merge(df, test, on='id')","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:51:26.352524Z","iopub.execute_input":"2022-08-31T13:51:26.353433Z","iopub.status.idle":"2022-08-31T13:51:26.362203Z","shell.execute_reply.started":"2022-08-31T13:51:26.353390Z","shell.execute_reply":"2022-08-31T13:51:26.361407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:51:26.503972Z","iopub.execute_input":"2022-08-31T13:51:26.504536Z","iopub.status.idle":"2022-08-31T13:51:26.518004Z","shell.execute_reply.started":"2022-08-31T13:51:26.504500Z","shell.execute_reply":"2022-08-31T13:51:26.517302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle2mask(mask_rle, shape=(3000,3000)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (width,height) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\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","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:51:26.952581Z","iopub.execute_input":"2022-08-31T13:51:26.953561Z","iopub.status.idle":"2022-08-31T13:51:26.960438Z","shell.execute_reply.started":"2022-08-31T13:51:26.953516Z","shell.execute_reply":"2022-08-31T13:51:26.959575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode_less_memory(img):\n    pixels = img.T.flatten()\n    pixels[0] = 0\n    pixels[-1] = 0\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:51:27.307618Z","iopub.execute_input":"2022-08-31T13:51:27.307901Z","iopub.status.idle":"2022-08-31T13:51:27.312968Z","shell.execute_reply.started":"2022-08-31T13:51:27.307870Z","shell.execute_reply":"2022-08-31T13:51:27.312234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rle = []\nfor i in range(len(df)):\n    id, rle1, rle2,_, _, h, w, _, _ = df.iloc[i]\n    img0 = rle2mask(rle1, shape=(w, h))\n    img1 = rle2mask(rle2, shape=(w, h))\n    img = np.fmax(img0, img1)\n    rle.append(rle_encode_less_memory(img))\n    if i == 0 :\n        plt.imshow(img0)\n        plt.show()\n        plt.imshow(img1)\n        plt.show()\n        plt.imshow(img)\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:51:27.678890Z","iopub.execute_input":"2022-08-31T13:51:27.679549Z","iopub.status.idle":"2022-08-31T13:51:29.525962Z","shell.execute_reply.started":"2022-08-31T13:51:27.679510Z","shell.execute_reply":"2022-08-31T13:51:29.525111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['rle'] = rle\ndf","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:51:30.522329Z","iopub.execute_input":"2022-08-31T13:51:30.523146Z","iopub.status.idle":"2022-08-31T13:51:30.538107Z","shell.execute_reply.started":"2022-08-31T13:51:30.523075Z","shell.execute_reply":"2022-08-31T13:51:30.537419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[['id','rle']].to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T13:51:31.362734Z","iopub.execute_input":"2022-08-31T13:51:31.364317Z","iopub.status.idle":"2022-08-31T13:51:31.371569Z","shell.execute_reply.started":"2022-08-31T13:51:31.364272Z","shell.execute_reply":"2022-08-31T13:51:31.370695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}