<jupyter_start><jupyter_text><jupyter_code># Clone iSegFormer and install dependencies.
# It may take 16 mins to run this cell.
!git clone https://github.com/qinliuliuqin/iSegFormer.git

import os
os.chdir('/content/iSegFormer')
!pip install -r ./requirements.txt
# Download OAI-ZIB test set
URL_ISEGFORMER = "https://github.com/qinliuliuqin/iSegFormer/releases/download/v0.1"
DATA_FOLDER = "./datasets" 

!mkdir -p {DATA_FOLDER}

for dataset in ['OAI-ZIB-test']:
  dataset_url = f"{URL_ISEGFORMER}/{dataset}.zip"
  dataset_path = f"{DATA_FOLDER}/{dataset}.zip"
  !wget -q -O {dataset_path} {dataset_url}
  !unzip -q {dataset_path} -d {DATA_FOLDER}
  !rm {dataset_path}

# Download weights 
WEIGHTS_FOLDER = "./weights"
!mkdir -p {WEIGHTS_FOLDER}

MODEL_NAME_SWINB = "imagenet21k_pretrain_oaizib_finetune_swin_base_epoch_54"
MODEL_NAME_HR32 = "oai_pretrain_oaizib_finetune_hr32_epoch_54"

WEIGHTS_URL_SWINB = f"{URL_ISEGFORMER}/{MODEL_NAME_SWINB}.pth"
WEIGHTS_URL_HR32 = f"{URL_ISEGFORMER}/{MODEL_NAME_HR32}.pth"
!wget -q -P {WEIGHTS_FOLDER} {WEIGHTS_URL_SWINB}
!wget -q -P {WEIGHTS_FOLDER} {WEIGHTS_URL_HR32}
%matplotlib inline
import matplotlib.pyplot as plt

import sys
import torch
import numpy as np
from google.colab import drive

sys.path.insert(0, './')

from isegm.utils import vis, exp
from isegm.inference import utils
from isegm.inference.evaluation import evaluate_dataset, evaluate_sample
from isegm.inference.predictors import get_predictor

device = torch.device('cuda:0')
cfg = exp.load_config_file('./config.yml', return_edict=True)

EVAL_MAX_CLICKS = 20
MODEL_THRESH = 0.49
brs_mode = 'NoBRS'
TARGET_IOU = 0.9

model_path_swinb = utils.find_checkpoint("./weights", f"{MODEL_NAME_SWINB}.pth")
model_path_hr32 = utils.find_checkpoint("./weights", f"{MODEL_NAME_HR32}.pth")
model_swinb = utils.load_is_model(model_path_swinb, device)
model_hr32 = utils.load_is_model(model_path_hr32, device)

DATASET = 'OAIZIB'
dataset = utils.get_dataset(DATASET, cfg)

for model in [model_swinb, model_hr32]:
  predictor = get_predictor(model, brs_mode, device, prob_thresh=MODEL_THRESH)

  all_ious, elapsed_time = evaluate_dataset(dataset, predictor, pred_thr=MODEL_THRESH, 
                                            max_iou_thr=TARGET_IOU, max_clicks=EVAL_MAX_CLICKS)
  mean_spc, mean_spi = utils.get_time_metrics(all_ious, elapsed_time)
  noc_list, over_max_list = utils.compute_noc_metric(all_ious,
                                                    iou_thrs=[0.8, 0.85, 0.9],
                                                    max_clicks=EVAL_MAX_CLICKS)

  header, table_row = utils.get_results_table(noc_list, over_max_list, brs_mode, DATASET,
                                              mean_spc, elapsed_time, EVAL_MAX_CLICKS)
  print(header)
  print(table_row)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1064435742257031, 'fraction_numerical': 0.016375934496262016, 'mean_word_length': 4.213358070500927, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '620770', 'license': 'MIT', 'n_tokens_mistral': 1133, 'n_tokens_neox': 1102, 'n_words': 187, 'path': 'notebooks/colab_test_isegformer.ipynb', 'repo_name': 'qinliuliuqin/iSegFormer', 'size': '2809'}