{"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":"## !ls -lh /kaggle/input/","metadata":{"execution":{"iopub.status.busy":"2022-08-08T11:47:56.155234Z","iopub.execute_input":"2022-08-08T11:47:56.155626Z","iopub.status.idle":"2022-08-08T11:47:57.459439Z","shell.execute_reply.started":"2022-08-08T11:47:56.155595Z","shell.execute_reply":"2022-08-08T11:47:57.458284Z"}}},{"cell_type":"code","source":"!cp -r /kaggle/input/hpapytorchzoozip/ /kaggle/temp/\n!ls /kaggle/temp/\n!pip install /kaggle/temp/pytorch_zoo-master\n!pip install /kaggle/input/pycocotools202/pycocotools-2.0.2-cp37-cp37m-linux_x86_64.whl","metadata":{"execution":{"iopub.status.busy":"2022-08-08T11:47:57.463869Z","iopub.execute_input":"2022-08-08T11:47:57.46444Z","iopub.status.idle":"2022-08-08T11:48:59.165175Z","shell.execute_reply.started":"2022-08-08T11:47:57.464387Z","shell.execute_reply":"2022-08-08T11:48:59.16366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nimport torch\nimport pandas as pd\nfrom tqdm import tqdm\nfrom PIL import Image\nimport numpy as np\nfrom statistics import mean\n\n\nPIPELINE_ROOT = '/kaggle/input/hpapipeline20/'\n\nsys.path.append(PIPELINE_ROOT)\nsys.path.append(PIPELINE_ROOT + 'classification')\nsys.path.append(PIPELINE_ROOT + 'segmentations')\nsys.path.append('../input/efficientnet0/efficientnet-pytorch-v1/EfficientNet-PyTorch/EfficientNet-PyTorch-master')\n\nfrom inference_utils.ClassifierHandler import ClassifierHandler\nfrom inference_utils.SegmentationHandler import SegmentationHandler\nfrom inference_utils.SubmissionHandler import SubmissionHandler\nfrom inference_utils.ImageDataHandler import ImageDataHandler\nfrom kymatio.torch import Scattering2D\nfrom utils import create_batches\nfrom Defines import *\n\nimport warnings\n\nimport mlcrate as mlc\nimport xgboost as xgb\nimport cv2\n\nwarnings.simplefilter(\"ignore\")\nprint(sys.path)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T11:48:59.168542Z","iopub.execute_input":"2022-08-08T11:48:59.169211Z","iopub.status.idle":"2022-08-08T11:49:09.65029Z","shell.execute_reply.started":"2022-08-08T11:48:59.169164Z","shell.execute_reply":"2022-08-08T11:49:09.64856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class XGBOOSTBadCellPredictor:\n    def __init__(self, model_file):\n        self.models, xgbver, self.columns = mlc.load(model_file)\n        if xgb.__version__ != xgbver:\n            print('[WARNING] XGBoost version mismatch, models trained with {}, inference with {}'.format(xgbver, xgb.__version__))\n        \n    def predict(self, X):      \n        X = X[self.columns]\n        assert (X.columns == self.columns).all(), 'BadCellPredictor columns mismatch, expected {}, got {}'.format(self.columns, X.columns)\n        \n        dmat = xgb.DMatrix(X.values)\n        preds = []\n        for m in self.models:\n            preds.append(m.predict(dmat))\n            \n        return np.mean(preds, axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T11:49:09.652406Z","iopub.execute_input":"2022-08-08T11:49:09.652835Z","iopub.status.idle":"2022-08-08T11:49:09.668308Z","shell.execute_reply.started":"2022-08-08T11:49:09.652771Z","shell.execute_reply":"2022-08-08T11:49:09.665907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class BadCellPredictor:\n    def __init__(self, inference_setup):\n        self.bcp_model = ClassifierHandler(device=device,\n                                           model_path=inference_setup.BCP_MODEL_PATH,\n                                           data_shape=inference_setup.DATA_SHAPE,\n                                           model_name=inference_setup.BCP_MODEL_NAME,\n                                           classes=inference_setup.BCP_NUM_CLASSES,\n                                           en_timing=inference_setup.ENABLE_TIMING,\n                                           orig_weights_path=None,\n                                           num_tta_passes=None,\n                                           use_sqr_pad=inference_setup.USE_CELL_SQR_PADDING,\n                                           is_dual_head=False)\n        \n    def predict(self, cell_image_4ch):      \n        cell_tensor = self.bcp_model.cell2tensor(Image.fromarray(cell_image_4ch))\n        \n        bin_probs = self.bcp_model.predict_cell_from_tensor(cell_tensor, use_softmax=True, out_probs=FINAL_PROBS)\n        \n        #print(f'BadCellPredictor::predict. bin_probs FINAL_PROBS: {bin_probs}')\n        \n        return bin_probs","metadata":{"execution":{"iopub.status.busy":"2022-08-08T11:49:09.674338Z","iopub.execute_input":"2022-08-08T11:49:09.675012Z","iopub.status.idle":"2022-08-08T11:49:09.68759Z","shell.execute_reply.started":"2022-08-08T11:49:09.674966Z","shell.execute_reply":"2022-08-08T11:49:09.68657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def is_boundary_cell(bbox, orig_h, orig_w):\n    if min(bbox[0], bbox[1]) <= 0:\n        return True\n\n    y_right = bbox[0] + bbox[2]\n    x_right = bbox[1] + bbox[3]\n    if y_right >= orig_h - 1 or x_right >= orig_w - 1:\n        return True\n    return False\n\ndef calculate_cell_multiplier(bcp, cell, orig_h, orig_w, boundary_cells_only=True):\n\n    final_multiplier = 1.0\n    \n    if boundary_cells_only:\n        if not is_boundary_cell(cell['bbox'], orig_h, orig_w):\n            # print(f'Not a boundary cell. Final Cell Multiplier: {final_multiplier}')\n            return final_multiplier\n    \n    bin_probs = bcp.predict(cell['4channels'])\n    \n    idx=np.argmax(bin_probs)\n    if idx == 0:\n        final_multiplier = 0.1\n    elif idx == 1:\n        final_multiplier = 0.5\n        \n    return final_multiplier\n\ndef calculate_xgboostcell_multiplier(cell, orig_h, orig_w):\n\n    if not is_boundary_cell(cell['bbox'], orig_h, orig_w):\n        return 1.0\n    \n    binary_mask = np.asarray(cell['mask']*255, dtype=np.uint8)\n    \n    # Calulate cell parameters\n    area = np.sum(binary_mask > 0)\n    binary_mask = cv2.Laplacian(binary_mask, cv2.CV_8U)\n    perimeter = np.sum(binary_mask > 0)\n    compactness = (4 * np.pi * area) / perimeter\n    aspect_ratio = binary_mask.shape[0]/binary_mask.shape[1]  # h/w\n    bbox_area = binary_mask.shape[0] * binary_mask.shape[1]\n    max_dim = max(binary_mask.shape[0], binary_mask.shape[1])\n    \n    sums = []  # sums of non zero points\n    for i in range(4):\n        sum_of_nonzero_pnts = np.sum( cell['4channels'][:, :, i] > 0)\n        sums.append(sum_of_nonzero_pnts)\n    gb2all_ratio = (sums[1] + sums[2]) / (sums[0] + sums[3] + sums[1] + sums[2])\n    green = sums[1]\n    blue = sums[2]\n    isValid_DM = True\n    if max_dim < 500:\n        if gb2all_ratio <= 0.5:\n            if min(blue, green) < 45000:\n                isValid_DM = False\n    \n    df = pd.DataFrame({\n                        'heights': [binary_mask.shape[0]],\n                        'widths': [binary_mask.shape[1]],\n                        'aspect_ratios': [aspect_ratio],\n                        'bbox_areas': [bbox_area],\n                        'mask_areas': [area],\n                        'mask_perimeters': [perimeter],\n                        'max_dim': [max_dim],\n                        'isValid_DM': [isValid_DM]\n                     })\n\n    bcp = XGBOOSTBadCellPredictor('../input/modelshpa/XGBoost/xboost_models/bcp_models_v4.pkl')\n    \n    prob_of_bad = bcp.predict(df)\n        \n    prob_of_good = 1.0-prob_of_bad\n    \n    #print(f'Final Cell prob_of_good: {prob_of_good}')\n\n    \n    return float(prob_of_good)\n\ndef calculate_darkcell_multiplier(cell):\n    sums = []  # sums of non zero points\n    for i in range(4):\n        sum_of_nonzero_pnts = np.sum( cell['4channels'][:, :, i] > 0)\n        sums.append(sum_of_nonzero_pnts)\n    \n    red=sums[0]\n    blue = sums[2]\n    yellow=sums[3]\n    blue_yellow=blue*yellow\n \n    return red, blue_yellow, blue","metadata":{"execution":{"iopub.status.busy":"2022-08-08T11:49:09.691023Z","iopub.execute_input":"2022-08-08T11:49:09.692112Z","iopub.status.idle":"2022-08-08T11:49:09.716476Z","shell.execute_reply.started":"2022-08-08T11:49:09.692029Z","shell.execute_reply":"2022-08-08T11:49:09.714687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def init_classifiers(device, inference_setup, is_dual_head, scat_setup=None):\n\n    if is_dual_head:\n        classifiers_paths = inference_setup.DH_CLASSIFIER_PATHS\n        model_names = inference_setup.DH_CLASSIFIER_MODEL_NAMES\n    else:\n        if scat_setup:\n            classifiers_paths = inference_setup.HYBRID_CLASSIFIER_PATHS\n            model_names = inference_setup.HYBRID_CLASSIFIER_MODEL_NAMES\n        else:\n            classifiers_paths = inference_setup.SH_CLASSIFIER_PATHS\n            model_names = inference_setup.SH_CLASSIFIER_MODEL_NAMES\n\n    if not isinstance(classifiers_paths, list):\n        return [ClassifierHandler(device=device,\n                                  model_path=classifiers_paths,\n                                  data_shape=inference_setup.DATA_SHAPE,\n                                  model_name=model_names,\n                                  classes=inference_setup.NUM_CLASSES,\n                                  en_timing=inference_setup.ENABLE_TIMING,\n                                  orig_weights_path=inference_setup.ORIG_WEIGHTS_PATH,\n                                  num_tta_passes=inference_setup.NUM_TTA_PASSES,\n                                  use_sqr_pad=inference_setup.USE_CELL_SQR_PADDING,\n                                  is_dual_head=is_dual_head,\n                                  setup_arguments=scat_setup)]\n\n    if len(classifiers_paths) != len(model_names):\n        raise AssertionError(f'Please check model names and models paths. There should be the same number of both')\n\n\n    # Check original weights path\n    if not isinstance(inference_setup.ORIG_WEIGHTS_PATH, list):\n        inference_setup.ORIG_WEIGHTS_PATH = [inference_setup.ORIG_WEIGHTS_PATH for i in\n                                             range(len(classifiers_paths))]\n\n    # Check padding setup\n    if not isinstance(inference_setup.USE_CELL_SQR_PADDING, list):\n        inference_setup.USE_CELL_SQR_PADDING = [inference_setup.USE_CELL_SQR_PADDING for i in\n                                                range(len(classifiers_paths))]\n\n    classifiers = []\n    for i in range(len(model_names)):\n        print(f'Creating an instance of a classifier:'\n              f'\\nName: {model_names[i]}; Path: {classifiers_paths[i]}')\n        classifiers.append(ClassifierHandler(device=device,\n                                             model_path=classifiers_paths[i],\n                                             data_shape=inference_setup.DATA_SHAPE,\n                                             model_name=model_names[i],\n                                             classes=inference_setup.NUM_CLASSES,\n                                             en_timing=inference_setup.ENABLE_TIMING,\n                                             orig_weights_path=None,\n                                             num_tta_passes=inference_setup.NUM_TTA_PASSES,\n                                             use_sqr_pad=False,\n                                             is_dual_head=is_dual_head,\n                                             setup_arguments=scat_setup))\n\n    return classifiers, model_names","metadata":{"execution":{"iopub.status.busy":"2022-08-08T11:49:09.719447Z","iopub.execute_input":"2022-08-08T11:49:09.720302Z","iopub.status.idle":"2022-08-08T11:49:09.748312Z","shell.execute_reply.started":"2022-08-08T11:49:09.720259Z","shell.execute_reply":"2022-08-08T11:49:09.746323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def combine_net_probabilities(dh_probs_image_and_cell, sh_probs):\n\n    dualnets_probs = []\n    for dh_prob in dh_probs_image_and_cell:\n        for dual_prob in dh_probs_image_and_cell:\n            image_level=dh_prob[0]\n            cell_level=dual_prob[1]\n            cell_dual_prob=image_level*cell_level        \n            cell_dual_prob[:,11]=cell_level[:,11]\n            cell_dual_prob[:,18]=cell_level[:,18]\n            dualnets_probs.append(cell_dual_prob)   \n    \n    dualnets_probs = np.asarray(dualnets_probs)\n    dualnets_mean=np.mean(dualnets_probs, axis=0)\n    return dualnets_mean","metadata":{"execution":{"iopub.status.busy":"2022-08-08T11:49:09.751993Z","iopub.execute_input":"2022-08-08T11:49:09.752472Z","iopub.status.idle":"2022-08-08T11:49:09.775301Z","shell.execute_reply.started":"2022-08-08T11:49:09.752431Z","shell.execute_reply":"2022-08-08T11:49:09.773523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class InferenceSetup(object):\n    ENABLE_TIMING = False\n\n    DATA_FILE = '/kaggle/input/hpa-single-cell-image-classification/sample_submission.csv'\n    IMAGE_FOLDER = '/kaggle/input/hpa-single-cell-image-classification/test'\n    BATCH_SIZE = 1\n    \n    SEG_TYPE = 'baseline'\n    NUC_MODEL = PIPELINE_ROOT + 'segmentations/libs/HPA_Cell_Segmentation/models/nuclei_model.pth'\n    CELL_MODEL = PIPELINE_ROOT + 'segmentations/libs/HPA_Cell_Segmentation/models/cell_3ch_model.pth'\n    SCALE_FACTOR = 0.25\n    USE_SOFT_MASK = False\n\n    USE_CELL_SQR_PADDING = False\n\n    DATA_SHAPE = (256, 256, 4)\n    SWINT_SHAPE = (224, 224, 4)\n    NUM_CLASSES = 19\n    NUM_TTA_PASSES = 2 #None to switch off\n    ORIG_WEIGHTS_PATH = None\n    \n    DH_CLASSIFIER_PATHS = [\n                           '../input/modelshpa/DSA/DSAWB-EF4-E2.pt']\n    DH_CLASSIFIER_MODEL_NAMES = ['DSA_eff']\n\n    SH_CLASSIFIER_PATHS = []\n    SH_CLASSIFIER_MODEL_NAMES = []\n    HYBRID_CLASSIFIER_PATHS = []\n    HYBRID_CLASSIFIER_MODEL_NAMES = []\n    \n    NET_WEIGHT2ADJUST = {}   \n    USE_BCP = True\n    BCP_BOUNDARY_CELLS_ONLY = True\n    XG_BCP_MODEL_PATH = '../input/modelshpa/XGBoost/xboost_models/bcp_models_v4.pkl'\n    BCP_MODEL_NAME = 'eff_b2'\n    BCP_MODEL_PATH = '../input/modelshpa/VID/VID_EFFB2_E7.pt'\n    BCP_NUM_CLASSES = 4\n    BCP_LIMIT = 0.3\n\n    FINAL_SAVING_DIR = '/kaggle/working/'","metadata":{"execution":{"iopub.status.busy":"2022-08-08T11:52:25.512254Z","iopub.execute_input":"2022-08-08T11:52:25.512677Z","iopub.status.idle":"2022-08-08T11:52:25.524394Z","shell.execute_reply.started":"2022-08-08T11:52:25.512623Z","shell.execute_reply":"2022-08-08T11:52:25.522907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"warnings.simplefilter(\"ignore\")\n\ninference_setup = InferenceSetup()\ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nsubmission_file = pd.read_csv(inference_setup.DATA_FILE)\nall_images = submission_file.ID.tolist()\n\nif len(all_images) == 559:\n    all_images = all_images[:1]\n\nsegmentation_handler = SegmentationHandler(nuc_model=inference_setup.NUC_MODEL,\n                                           cell_model=inference_setup.CELL_MODEL,\n                                           scale_factor=inference_setup.SCALE_FACTOR,\n                                           device=device,\n                                           image_dir=inference_setup.IMAGE_FOLDER,\n                                           en_visual=False,\n                                           en_timing=inference_setup.ENABLE_TIMING,\n                                           seg_type=inference_setup.SEG_TYPE)\n\ndh_classifiers, dh_names = init_classifiers(device=device, inference_setup=inference_setup, is_dual_head=True)\nsh_classifiers, sh_names = init_classifiers(device=device, inference_setup=inference_setup, is_dual_head=False)\nbcp_classifier = BadCellPredictor(inference_setup)\nxgBoost_cell_predictor = XGBOOSTBadCellPredictor(inference_setup.XG_BCP_MODEL_PATH)\n\nscat_setup = {\n    'scat_order': 1,\n    'J': 2,\n    'in_shape': inference_setup.DATA_SHAPE[0],\n    'in_channels': inference_setup.DATA_SHAPE[2],\n    'disable_net_path': False,\n    'disable_scat_path': False,\n    'hybrid_type': 3,  # Hv3\n    'hybrid_version': 3,  # Hv3_3\n    'hybrid_kernel_size': 3\n}\n\nsc1 = Scattering2D(max_order=scat_setup['scat_order'], J=scat_setup['J'],\n                   shape=(scat_setup['in_shape'], scat_setup['in_shape']))\nsc2 = Scattering2D(max_order=scat_setup['scat_order'], J=scat_setup['J'] + 1,\n                   shape=(scat_setup['in_shape'], scat_setup['in_shape']))\nhybrid_classifiers, h_names = init_classifiers(device=device, inference_setup=inference_setup, is_dual_head=False,\n                                               scat_setup=scat_setup)\nprint(f'ScatNets Ready')\n\nscatNet = {\n    'sc1': sc1,\n    'sc2': sc2\n}\n\nall_classifiers = {\n    \"classifierHandler\": [],\n    \"is_dual_head\": [],\n    \"model_names\": []\n}\n\nall_classifiers['classifierHandler'] += dh_classifiers\nall_classifiers['model_names'] += dh_names\nall_classifiers['is_dual_head'] += ([True] * (len(dh_classifiers)))\n\nall_classifiers['classifierHandler'] += hybrid_classifiers\nall_classifiers['model_names'] += h_names\nall_classifiers['is_dual_head'] += ([False] * (len(hybrid_classifiers)))\n\nall_classifiers['classifierHandler'] += sh_classifiers\nall_classifiers['model_names'] += sh_names\nall_classifiers['is_dual_head'] += ([False] * (len(sh_classifiers)))\n\nsubmission_handler = SubmissionHandler(sample_sub_file=inference_setup.DATA_FILE,\n                                       saving_dir=inference_setup.FINAL_SAVING_DIR)\n\nbatches = create_batches(all_images, inference_setup.BATCH_SIZE)\n\nbatch_num = 0\n\nfor i in tqdm(range(len(batches)), file=sys.stdout, desc='Batch Processing', unit='batch'):\n    image_data = ImageDataHandler(image_names=batches[i],\n                                  image_directory=inference_setup.IMAGE_FOLDER,\n                                  enable_timing=inference_setup.ENABLE_TIMING)\n\n    img_masks_formated = segmentation_handler.segment(image_data=image_data, image_ids=batches[i])\n\n    images_with_cells = segmentation_handler.extract_cells(image_data=image_data,\n                                                           img_masks=img_masks_formated,\n                                                           use_soft_mask=inference_setup.USE_SOFT_MASK)\n    '''\n    image_cells = {\n                'basename':str,\n                'cells': [] > {\n                                'bbox': (y_min, x_min, height, width), > bounding box coordinates. Anchor: bottom left\n                                'pred_label': [int],\n                                'confidence': [float],\n                                'mask': cell_mask,\n                                '4channels': [r,g,b,y]\n                            \n                            }\n            }\n    '''\n    image_count = 0\n    for image in images_with_cells:\n\n        image_w = image_data.batch['_red'][image_count].shape[1]\n        image_h = image_data.batch['_red'][image_count].shape[0]\n\n        # Construct collections of cells to be predicted.\n        cells_per_image_tensors = (inference_setup.NUM_TTA_PASSES + 1) * [\n            None] if inference_setup.NUM_TTA_PASSES else [None]\n        # cells_per_image_tensors holds an array of tta'd cells for the image. len = # tta passes + 1 (original)\n\n        for cell in image['cells']:\n            cell_tensor = all_classifiers['classifierHandler'][0].cell2tensor(Image.fromarray(cell['4channels']))\n            tta_passes = 0 if not inference_setup.NUM_TTA_PASSES else inference_setup.NUM_TTA_PASSES\n\n            for tta_ind in range(tta_passes + 1):\n                cell_tta = torch.unsqueeze(cell_tensor[tta_ind], 0)\n                if cells_per_image_tensors[tta_ind] is None:\n                    cells_per_image_tensors[tta_ind] = cell_tta\n                else:\n                    cells_per_image_tensors[tta_ind] = torch.cat([cells_per_image_tensors[tta_ind],\n                                                                  cell_tta], dim=0)\n\n        # Do predictions\n        image_confs = (inference_setup.NUM_TTA_PASSES + 1) * [None] if inference_setup.NUM_TTA_PASSES else [None]\n        # image_confs structure: [1. tta transform level [2. classifier probs level] ]\n\n        for tta_ind in range(len(cells_per_image_tensors)):\n\n            dh_probs_image_and_cell = []\n            sh_probs = []\n            for classifier_ind in range(len(all_classifiers['classifierHandler'])):\n                classifier = all_classifiers['classifierHandler'][classifier_ind]\n                is_dh = all_classifiers['is_dual_head'][classifier_ind]\n                model_name = all_classifiers['model_names'][classifier_ind]\n\n                cells2process = cells_per_image_tensors[tta_ind]\n\n                if is_dh:\n                    dh_probs_image_and_cell.append(classifier.predict_cell_from_tensor(cells2process,\n                                                                                       out_probs=IMG_AND_CELL_PROBS))\n                else:\n                    if model_name == 'swinT':\n                        cells2process = torch.nn.functional.interpolate(cells2process,\n                                                                        size=inference_setup.SWINT_SHAPE[:2])\n\n                    if model_name == 'eff_b4Hv3_3':\n                        probs = classifier.predict_cell_from_tensor_hybrid(cells2process,\n                                                                           scat_layer1=scatNet['sc1'],\n                                                                           scat_layer2=scatNet['sc2'])\n                    else:\n                        probs = classifier.predict_cell_from_tensor(cells2process,\n                                                                    out_probs=ONLY_CELL_PROBS)\n                        \n                    if model_name in inference_setup.NET_WEIGHT2ADJUST.keys():\n                        for cl_weight in inference_setup.NET_WEIGHT2ADJUST[model_name]:\n                            cl = cl_weight[0]\n                            weight = cl_weight[1]\n                            for ind in range(probs.shape[0]):\n                                probs[ind][cl] *= weight\n                        \n                    sh_probs.append(probs)\n\n            tta_cell_conf = combine_net_probabilities(dh_probs_image_and_cell, sh_probs)\n            tta_cell_conf = np.expand_dims(tta_cell_conf, axis=0)\n\n            if image_confs[tta_ind] is None:\n                image_confs[tta_ind] = tta_cell_conf\n            else:\n                image_confs[tta_ind] = np.concatenate((image_confs[tta_ind], tta_cell_conf))\n\n        # Averaging\n        for tta_ind in range(len(cells_per_image_tensors)):\n            if image_confs[tta_ind].shape[0] > 1:\n                image_confs[tta_ind] = np.expand_dims(image_confs[tta_ind].mean(0), 0)\n\n        image_confs = np.stack(image_confs).mean(0).squeeze()\n\n        # Dark cell detector\n        red_sums = []\n        blue_sums = []\n        blue_val = np.zeros(len(image['cells']))\n        blue_yellow_products = []\n        for cell_ind in range(len(image['cells'])):\n            cell = image['cells'][cell_ind]\n            [red_sum,blue_yellow,blue_sum] = calculate_darkcell_multiplier(cell)\n            red_sums.append(red_sum)\n            blue_sums.append(blue_sum)\n            blue_yellow_products.append(blue_yellow)\n        \n        red_minimum = mean(red_sums) * 0.1\n        blue_yellow_products_minimum = mean(blue_yellow_products) * 0.1     #\n        \n        dark_weight = np.ones(len(image['cells']))\n        for i in range(len(dark_weight)):\n            if red_sums[i] < red_minimum or blue_yellow_products[i] < blue_yellow_products_minimum:\n                dark_weight[i] *= 0.5\n                \n        blue_weight = np.ones(len(image['cells']))\n        for i in range(len(blue_weight)):\n            if blue_sums[i] < 12000:\n                blue_weight[i] *= 0.03\n                \n        # Assign cell labels\n        for cell_ind in range(len(image['cells'])):\n            cell = image['cells'][cell_ind]\n            confidence = image_confs[cell_ind]\n            cell_dark= dark_weight[cell_ind]\n            cell_not_blue=blue_weight[cell_ind]\n            cell_multiplier = calculate_cell_multiplier(bcp=bcp_classifier, cell=cell, orig_h=image_h, orig_w=image_w,\n                                                            boundary_cells_only=inference_setup.BCP_BOUNDARY_CELLS_ONLY)\n            cell_multiplierxg = calculate_xgboostcell_multiplier(cell, image_h, image_w)\n            cell_multiplierxg = (1 - inference_setup.BCP_LIMIT) * cell_multiplierxg + inference_setup.BCP_LIMIT\n            confidence *= cell_multiplierxg\n            confidence *= cell_multiplier\n            confidence *= cell_dark\n            confidence *= cell_not_blue\n\n            confidence = list(confidence)\n            cell['pred_label'] += [i for i in range(len(confidence))]\n            cell['confidence'] += confidence\n        submission_handler.update_submission_file(image_with_cells=image)\n        image_count += 1\n\n    batch_num += 1\n\nsubmission_handler.save_submission_file()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T11:52:25.746237Z","iopub.execute_input":"2022-08-08T11:52:25.746594Z","iopub.status.idle":"2022-08-08T11:53:14.203043Z","shell.execute_reply.started":"2022-08-08T11:52:25.746562Z","shell.execute_reply":"2022-08-08T11:53:14.202018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(all_classifiers['model_names'])\nif False:\n    df = pd.read_csv('/kaggle/working/submission.csv')\n    for i, row in df.iterrows():\n        print(row['PredictionString'])","metadata":{"execution":{"iopub.status.busy":"2022-08-08T11:50:27.049237Z","iopub.execute_input":"2022-08-08T11:50:27.049749Z","iopub.status.idle":"2022-08-08T11:50:27.060493Z","shell.execute_reply.started":"2022-08-08T11:50:27.049689Z","shell.execute_reply":"2022-08-08T11:50:27.058331Z"},"trusted":true},"execution_count":null,"outputs":[]}]}