def get_images_for_image_ids(self, image_ids): """ returning a list because """ attention_map_file_paths = [ to_image_path_by_id(self.prefix, image_id, file_ending="bbx") for image_id in image_ids ] attention_maps = [ load_numpy_from(self.directory_path, image_file_path) for image_file_path in attention_map_file_paths ] attention_labels_file_paths = [ to_image_path_by_id(self.prefix, image_id, file_ending="lbx") for image_id in image_ids ] attention_labels = [ load_numpy_from(self.directory_path, image_file_path) for image_file_path in attention_labels_file_paths ] attention_ids_file_paths = [ to_image_path_by_id(self.prefix, image_id, file_ending="ibx") for image_id in image_ids ] attention_ids = [ load_numpy_from(self.directory_path, image_file_path) for image_file_path in attention_ids_file_paths ] """ we cannot simply cast to numpy array here b.c. there might be a different amount of attention maps per image """ """ what to do exactly (flatten, concatenate, sum) depends on the caller """ return attention_maps, attention_labels, attention_ids
def categorical_knearest_neighbors_from_model_dir(model_dir, categories, k=10): word_sequence = load_json_from(model_dir, "word_sequence.json") word_embeddings = load_numpy_from(model_dir, "word_embeddings.npy") categories_with_neighbors = categorical_knearest_neighbors( categories, word_sequence, word_embeddings, k) store_json_to(categories_with_neighbors, model_dir, "category_neighbors.json")
def load_attention_map_by_image_id_from_many(image_ids, directory_path, image_prefix, split_name=None): """ Returns a dict of {image_id : feature_map} for each given image_id. """ if split_name: directory_path = "/".join([directory_path, split_name]) return dict([(image_id, load_numpy_from(to_image_path_by_id(image_prefix, image_id, directory_path, file_ending="bbx"))) for image_id in image_ids])
def get_images_for_image_ids(self, image_ids): """ @param captions: the list of captions as dicts of { "caption", "image_id", "id" } @return: the image in the same order as captions """ feature_map_file_paths = [ to_image_path_by_id(self.prefix, image_id, file_ending="npy") for image_id in image_ids ] feature_maps = np.array([ load_numpy_from(self.directory_path, image_file_path) for image_file_path in feature_map_file_paths ]) return feature_maps
def load_feature_maps_by_image_id_from_single(directory_path_or_file, split_name=None, flat=True): lookup_filename = DEFAULT_FEATURES_FILE_NAME if split_name and not flat: directory_path_or_file = "/".join([directory_path_or_file, split_name]) if split_name and flat: lookup_filename = "shatt_feature_maps_{}.json".format(split_name) data = load_numpy_from(directory_path_or_file, lookup_filename) image_ids = data["id"] feature_maps = data["features"] return dict(zip(image_ids, feature_maps))
def _get_image_for_image_id(self, image_id): """ Overwritten to determine the file ending """ image_file_path = to_image_path_by_id(self.prefix, image_id, file_ending="bbx") return load_numpy_from(self.directory_path, image_file_path)