IBYDMT / precompute_results.py
jacopoteneggi's picture
Update
dffe47c verified
import json
import os
import numpy as np
from PIL import Image
from app_lib.defaults import Defaults as d
from app_lib.test import get_testing_config, test
assets_dir = "assets"
image_dir = os.path.join(assets_dir, "images")
results_dir = os.path.join(assets_dir, "results")
os.makedirs(results_dir, exist_ok=True)
testing_config = get_testing_config(
significance_level=d.SIGNIFICANCE_LEVEL_VALUE,
tau_max=d.TAU_MAX_VALUE,
r=d.R_VALUE,
)
image_presets = json.load(open(os.path.join(assets_dir, "image_presets.json")))
for _image_name, _image_presets in image_presets.items():
_image_name = f"{_image_name}.jpg"
_image_path = os.path.join(image_dir, _image_name)
_image = Image.open(_image_path)
_class_name = _image_presets["class_name"]
_concepts = _image_presets["concepts"]
_cardinality = d.CARDINALITY_VALUE
_results = test(
testing_config,
_image,
_class_name,
_concepts,
_cardinality,
d.DATASET_NAME,
d.MODEL_NAME,
with_streamlit=False,
)
np.save(
os.path.join(results_dir, _image_name.split(".")[0]),
_results,
allow_pickle=True,
)