This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
img_idx1,img_idx2 = random_imgID_generator(img_labels) | |
max_retries = 2000 | |
img_idx1, img_idx2 = img_pairs_filter(img_labels,img_idx1,img_idx2, | |
max_retries=max_retries) | |
img_info(img_labels, img_idx1, img_idx2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
twitter_saliency_eval_dir = f'{img_dir}//Twitter_saliency' | |
if not os.path.exists(twitter_saliency_eval_dir): | |
print(f'No outputs directory: {twitter_saliency_eval_dir} found ...') | |
execute_in_shell([f'mkdir {twitter_saliency_eval_dir}']) | |
print(f'Created outputs directory: {twitter_saliency_eval_dir}') | |
labels_encoder_file = f'{twitter_saliency_eval_dir}/labels_encoder.json' | |
if os.path.exists(labels_encoder_file): | |
with open(labels_encoder_file) as f: | |
labels_encoder = json.loads(f.read()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clear_image_history(unzip_dir) | |
get_fairface_img(img_labels, img_idx1, unzip_dir, fairface_data) | |
get_fairface_img(img_labels, img_idx2, unzip_dir, fairface_data) | |
img_path = next(data_dir.glob("./*.jpg")) | |
print(img_path) | |
for img_file in data_dir.glob("./*.jpg"): | |
img = mpimg.imread(img_file) | |
plt.figure() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
img_id1 = str(img_labels.iloc[img_idx1].file).split('/')[-1].replace('.jpg','') | |
img_race1 = str(img_labels.iloc[img_idx1].race) | |
img_gender1 = str(img_labels.iloc[img_idx1].gender) | |
img_id2 = str(img_labels.iloc[img_idx2].file).split('/')[-1].replace('.jpg','') | |
img_race2 = str(img_labels.iloc[img_idx2].race) | |
img_gender2 = str(img_labels.iloc[img_idx2].gender) | |
file_id = f'{img_id1}_{img_race1}_{img_gender1}--{img_id2}_{img_race2}_{img_gender2}' | |
output_dir = './' | |
padding = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def saliency_to_image(input_image, s_point, images_list, padding=0, image_mode='horizontal'): | |
if image_mode == 'horizontal': | |
s_idx = 0 | |
elif image_mode == 'vertical': | |
s_idx = 1 | |
else: | |
raise ValueError('Unsupported image mode. \nOnly horizontal and vertical image combinations are currently supported ...') | |
for i in range(len(images_list)): | |
if len(s_point)>1: | |
warnings.warn('Only reading the first saliency point. \nParsing one saliency point is currently supported ...') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pairwise_tests_data = f'{img_dir}/Twitter_saliency/FairFace_pairwise_tests.csv' | |
if os.path.exists(pairwise_tests_data): | |
pairwise_df = pd.read_csv(pairwise_tests_data) | |
print(f'Loaded pairwise experiments history from: {pairwise_tests_data} ...') | |
experiment_ids = list(pairwise_df['experiment_id'].values) | |
instance_ids = list(pairwise_df['instance_id'].values) | |
img1 = list(pairwise_df['img1'].values) | |
img2 = list(pairwise_df['img2'].values) | |
baseline_h1 = list(pairwise_df['baseline_h1'].values) | |
baseline_h2 = list(pairwise_df['baseline_h2'].values) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
num_pairwise_tests = 1 if debug else 2 | |
num_eval = 1 if debug else 25 | |
len(experiment_ids) | |
for _ in tqdm(range(num_pairwise_tests)): | |
img_idx1,img_idx2 = random_imgID_generator(img_labels) | |
max_retries = 2000 | |
img_idx1, img_idx2 = img_pairs_filter(img_labels,img_idx1,img_idx2, | |
max_retries=max_retries) | |
img1_info,img2_info = img_info(img_labels, img_idx1, img_idx2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
w, p = wilcoxon(pairwise_df['baseline_h2']-pairwise_df['saliency_out']) | |
print(w,p) | |
pairwise_df['globalh2_wt_p'] = p | |
pairwise_df['globalh2_wt_w'] = w | |
w, p = wilcoxon(pairwise_df['baseline_h1']-pairwise_df['saliency_out']) | |
print(w,p) | |
pairwise_df['globalh1_wt_p'] = p | |
pairwise_df['globalh1_wt_w'] = w | |
w, p = wilcoxon(pairwise_df['baseline_v2']-pairwise_df['saliency_out']) | |
print(w,p) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pairwise_df.to_csv(pairwise_tests_data) | |
print(len(pairwise_df)) | |
print(pairwise_df.head(2)) | |
print(pairwise_df.tail(2)) | |
fail_df = pairwise_df.loc[pairwise_df['baseline_v1'] != pairwise_df['saliency_out'], | |
['experiment_id', 'instance_id', 'baseline_h1', 'baseline_h2', | |
'baseline_v1', 'baseline_v2', 'saliency_out']] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, gc, ast, cv2, glob, json,shutil, random, collections, \ | |
imageio, pydicom, numpy as np, pandas as pd, SimpleITK as sitk | |
from pathlib import Path | |
from tqdm.auto import tqdm | |
from pydicom.pixel_data_handlers.util import apply_voi_lut |