File size: 2,035 Bytes
201ab5d f040850 3b33d39 201ab5d dda292a f040850 44561fc f040850 1f5c99c 3f0adec df5a179 f040850 201ab5d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
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}")
# set random seed using time
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)
# set random seed usiing time
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
# Function to load words based on global variable
def load_words(idx):
words = [f"word_{idx}_{i}" for i in range(20)]
return words
def load_csv_concepts(data_dir):
# Load data from csv
data = pd.read_csv(os.path.join(data_dir, 'concepts_by_class.csv'))
return data
|