<jupyter_start><jupyter_text>Visualize segmentations generated on different localization methods<jupyter_code>%load_ext autoreload
%autoreload 2
from plot_helper import *
import ipyplot
# choose a task from these localization tasks
LOCALIZATION_TASKS =  ["Enlarged Cardiomediastinum",
                  "Cardiomegaly",
                  "Lung Lesion",
                  "Airspace Opacity",
                  "Edema",
                  "Consolidation",
                  "Atelectasis",
                  "Pneumothorax",
                  "Pleural Effusion",
                  "Support Devices"
                  ]<jupyter_output><empty_output><jupyter_text>Test SetRed is Stanford annotation and yellow is one of gradcam, ig and vietnam*IG CAMS not ready*  Parameters to change<jupyter_code>task = 'Airspace Opacity' # choose from the localization tasks to avoid error
pos_only = False # true if only show positive instances; change to false to visualize all images

# change the indices to your only liking to visualize as many images as needed
start_idx = 0
end_idx = 10

# opacity
w = 0.92 # 1 if wants segmentation to be very light, 0 otherwise
img_width = 300 # width of image to display<jupyter_output><empty_output><jupyter_text>Plot<jupyter_code># get image ids; can choose to load positive instances only
img_ids = load_all_ids(phase='test',task=task,pos_only = pos_only)
ids = img_ids[start_idx:end_idx] # select images to visualize
print(f"Showing positive only is {pos_only} with {len(img_ids)} images to visualize; task is {task} \n\n")

# plot everything
for img_id in ids:
    print(f"{img_id}\n")
    img1 = visualize_segmentations(img_id,task,phase='test',method='gradcam',model='single',w=w)
    img2 = visualize_segmentations(img_id,task,phase='test',method='gradcam',model='ensemble',w=w)
    img3 = visualize_segmentations(img_id,task,phase='test',method='ig',model='ensemble',w=w)
    img4 = visualize_segmentations(img_id,task,phase='test',method='vietnam',w=w)
    ipyplot.plot_images([img1,img2,img3,img4], labels = ['gradcam_single','gradcam_ensemble','ig_ensemble','human'],img_width=img_width)<jupyter_output><empty_output><jupyter_text>Validation set (not relevant for now)<jupyter_code># directories of CAMS
ig_dir = '/deep/u/ashwinagrawal/result/ensemble_results/cams/'
gradcam_dir = '/deep/u/asaporta/densenet_ckpts/willow/ensemble_results/cams/'
gt_dir = '/deep/group/aihc-bootcamp-spring2020/localize/annotations/gt_imgs/'

# original x-ray images
CXR_VALID_PATH = '/deep/group/CheXpert/CheXpert-v1.0/valid'
CXR_TEST_PATH = '/deep/group/anujpare/CheXpert_original_test500/test'

# vietnam v.s. localization 


# side by side 
# left: choose from vietnam, gradcam_single, gradcam_ensemble and ig with stanford overlay
# right: choose from vietnam, gradcam, and IG with stanford overlay

# change to see diff patient and pathology
patientid = 'patient64593'  # on validation set 64541:64740
task = 'Cardiomegaly'

# Integrated gradients
w = 0.92
visualize_cam_segmentation(ig_dir,CXR_VALID_PATH,gt_dir,patientid,task,w)
dataset = 'valid'
path_group = '/deep/group/aihc-bootcamp-spring2020/localize'
gt_file = f'{path_group}/annotations/{dataset}_annotations_merged.json'
with open(gt_file) as f:
    gt = json.load(f)
len(gt.keys())
gt[list(gt.keys())[0]]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10675512665862485, 'fraction_numerical': 0.015681544028950542, 'mean_word_length': 4.7387543252595155, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '504106', 'license': 'MIT', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1093, 'n_words': 254, 'path': 'chexpert-model/localization_eval/segmentation_demo.ipynb', 'repo_name': 'stanfordmlgroup/CheXseg', 'size': '3316'}