text
stringlengths
1
93.6k
else:
spamwatch = None
# ================================================
if not os.path.isdir(Config.TMP_DOWNLOAD_DIRECTORY):
os.makedirs(Config.TMP_DOWNLOAD_DIRECTORY)
# thumb image
if Config.THUMB_IMAGE is not None:
check = url(Config.THUMB_IMAGE)
if check:
try:
with open(thumb_image_path, "wb") as f:
f.write(requests.get(Config.THUMB_IMAGE).content)
except Exception as e:
LOGS.info(str(e))
def set_key(dictionary, key, value):
if key not in dictionary:
dictionary[key] = value
elif isinstance(dictionary[key], list):
if value in dictionary[key]:
return
dictionary[key].append(value)
else:
dictionary[key] = [dictionary[key], value]
async def make_gif(event, reply, quality=None, fps=None):
fps = fps or 1
quality = quality or 256
result_p = os.path.join("temp", "animation.gif")
animation = lottie.parsers.tgs.parse_tgs(reply)
with open(result_p, "wb") as result:
await _lionutils.run_sync(
lottie.exporters.gif.export_gif, animation, result, quality, fps
)
return result_p
# <FILESEP>
#############################################
## Artemis ##
## Copyright (c) 2022-present NAVER Corp. ##
## CC BY-NC-SA 4.0 ##
#############################################
"""
This script enables to produce heatmaps for the EM & IS scores of the ARTEMIS
model.
Change the global parameters below to precise what / how many heatmaps should be
generated, and run this script with the same arguments as when evaluating a
model, with `--gradcam` in addition.
"""
import os
import cv2
import numpy as np
import copy
import json
import torch
from torch.autograd import grad
import data
from vocab import Vocabulary
from utils import params_require_grad
from artemis_model import ARTEMIS
from evaluate import load_model, compute_and_process_compatibility_scores
from option import parser, verify_input_args
################################################################################
# *** GLOBAL PARAMETERS
################################################################################
# whether to generate heatmaps for the queries yielding the best results (the
# ground truth target image is well ranked)
ONLY_BEST_RESULTS = True
# number of queries to study
NUMBER_OF_EXAMPLES = 5
# number of coefficients contributing the most to a given score, that should be
# considered for backpropagation, in the GradCAM algorithm. If the score is
# computed as the dot product of two vectors `a` and `b`, the coefficients are
# given by the point-wise product of `a` and `b`.
NUMBER_OF_MAIN_COEFF = 3
################################################################################
# *** GENERATE & SAVE HEATMAPS
################################################################################
def main_generate_heatmaps(args, model, vocab):
"""
Potentially find the indices of the most relevant data examples (i.e. data
examples whose expected target image is well ranked by the model), and
generate heatmaps for them.