Spaces:
Runtime error
Runtime error
Download src/utils.py from MarcoParola/saliency-evaluation-exp2-intelimage: direct link, hf CLI and curl.
- Browser
- Download file 1.27 kB
-
https://huggingface.co/spaces/MarcoParola/saliency-evaluation-exp2-intelimage/resolve/main/src/utils.py
- Command line
-
hf download hf://spaces/MarcoParola/saliency-evaluation-exp2-intelimage/src/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/MarcoParola/saliency-evaluation-exp2-intelimage/resolve/main/src/utils.py
1.27 kB
| 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 load_example_images(class_idx, data_dir, max_images=16): | |
| path = os.path.join(data_dir, 'images', str(class_idx)) | |
| images = os.listdir(path) | |
| # pick max_images random images | |
| # set random seed using 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 | |
| def generate_random_ids(): | |
| # generate three sets of random ids (three among 1-4, 1-8, 1-12) | |
| # get time as seed | |
| seed = int(time.time()) | |
| np.random.seed(seed) | |
| ids1 = np.random.choice(np.arange(1, 5), 3, replace=False) | |
| ids2 = np.random.choice(np.arange(2, 10), 3, replace=False) | |
| ids3 = np.random.choice(np.arange(3, 15), 3, replace=False) | |
| return [ids1, ids2, ids3] | |