|
|
import os |
|
|
import pandas as pd |
|
|
from huggingface_hub import HfApi, HfFolder |
|
|
import yaml |
|
|
import numpy as np |
|
|
import time |
|
|
|
|
|
config = yaml.safe_load(open("./config/config.yaml")) |
|
|
|
|
|
def get_random_image_id(class_idx, data_dir): |
|
|
print(f'getting random image id for class index: {class_idx}') |
|
|
path = os.path.join(data_dir, 'images', str(class_idx)) |
|
|
images = os.listdir(path) |
|
|
ids = [int(img.split('.')[0]) for img in images if img.endswith('.png')] |
|
|
if not ids: |
|
|
raise ValueError(f"No images found for class index {class_idx} in {path}") |
|
|
|
|
|
np.random.seed(int(time.time())) |
|
|
random_id = np.random.randint(0, len(ids)) |
|
|
return ids[random_id] |
|
|
|
|
|
def load_image_and_saliency(class_idx, data_dir, img_id): |
|
|
path = os.path.join(data_dir, 'images', str(class_idx)) |
|
|
print('ooooooooooo', path) |
|
|
images = os.listdir(path) |
|
|
id = img_id |
|
|
print(f"Loading image with ID: {id} from class index: {class_idx}") |
|
|
image = os.path.join(path, f'{id}.png') |
|
|
gradcam_image = os.path.join(data_dir, 'saliency', 'gradcam', f'{id}.png') |
|
|
lime_image = os.path.join(data_dir, 'saliency', 'lime', f'{id}.png') |
|
|
sidu_image = os.path.join(data_dir, 'saliency', 'sidu', f'{id}.png') |
|
|
rise_image = os.path.join(data_dir, 'saliency', 'rise', f'{id}.png') |
|
|
return image, gradcam_image, lime_image, sidu_image, rise_image |
|
|
|
|
|
def load_example_images(class_idx, data_dir, max_images=16): |
|
|
path = os.path.join(data_dir, 'images', str(class_idx)) |
|
|
images = os.listdir(path) |
|
|
|
|
|
np.random.seed(int(time.time())) |
|
|
ids = np.random.choice(len(images), max_images, replace=False) |
|
|
images = [os.path.join(path, images[id]) for id in ids] |
|
|
return images |
|
|
|
|
|
|
|
|
def load_words(idx): |
|
|
words = [f"word_{idx}_{i}" for i in range(20)] |
|
|
return words |
|
|
|
|
|
|
|
|
def load_csv_concepts(data_dir): |
|
|
|
|
|
data = pd.read_csv(os.path.join(data_dir, 'concepts_by_class.csv')) |
|
|
return data |
|
|
|
|
|
|