# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:python3]
#     language: python
#     name: conda-env-python3-py
# ---

import pickle

from utils import load_vocab, decode_caption, load_caption, rrv_votes, load_annotations, print_image

vocab = load_vocab(dict_file = "../../outputs/vocab/5000/coco2014_vocab.json")
image_id_to_index, index_to_image_id, annotations_dict = load_annotations(annotations_dir="../../annotations/", 
                                                                          annotations_file='captions_val2014.json',
                                                                         map_file = "../../outputs/val_image_id_to_idx.csv")
print("Processed {} images".format(len(image_id_to_index)))
print("Processed {} images".format(len(annotations_dict.keys())))


def load_best_beam_captions(beam_size):
    file_name = '../../outputs/voted_captions/{}/best_beam.pickle'.format(beam_size)
    with open(file_name, 'rb') as file:
        best_beams = pickle.load(file)
    return {im_id: decode_caption(best_beams[im_id], vocab) for im_id in sorted(best_beams)}


def load_voted_captions(beam_size, similarity):
    file_name = '../../outputs/voted_captions/{}/{}.pickle'.format(beam_size, similarity)
    with open(file_name, 'rb') as file:
        return pickle.load(file)


beam_captions = load_best_beam_captions(10)

str_beam_captions = { (im_id, " ".join(caption)) for (im_id, caption) in beam_captions.items()}

# +
beam_10_captions_file = "../../outputs/str_captions_for_analysis/beam_10.pickle" 

with open(beam_10_captions_file, 'wb') as file:
    pickle.dump(str_beam_captions, file, pickle.HIGHEST_PROTOCOL)
# -

with open(beam_10_captions_file, 'rb') as file:
    str_captions =  pickle.load(file)



bigram_prec_100_captions = load_voted_captions(100, "bigram_precision")
bigram_prec_100_captions[100][0][0]



bigram_prec_100_captions_str = {
    im_id: " ".join(decode_caption(bigram_prec_100_captions[im_id][0][0], vocab))
    for im_id in sorted(bigram_prec_100_captions.keys())
}

bigram_prec_100_captions_str

bigram_precision_100_captions_file = "../../outputs/str_captions_for_analysis/bigram_precision_100.pickle" 
with open(bigram_precision_100_captions_file, "wb") as file:
    pickle.dump(bigram_prec_100_captions_str, file, pickle.HIGHEST_PROTOCOL)

bigram_prec_100_captions_str



StarcoderdataJupyterScriptsDedupFiltered