Esempio n. 1
0
    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
Esempio n. 2
0
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])
Esempio n. 3
0
def load_and_preprocess_single(processable):
    file_path = processable["path"]
    target_shape = processable["target_shape"] 
    feature_size = processable["image_feature_size"]
    feature_shape = np.array([np.sqrt(feature_size), np.sqrt(feature_size)])
    feature_shape = feature_shape.astype("uint8")
    
    image_id = processable["image_id"]
    
    with load_img(file_path) as image:
        attention_maps = []
        bounding_boxes = processable["boxes"]
        for bounding_box in bounding_boxes:
            with Image.new(mode="L", size=(image.size)) as bbx:
                draw = ImageDraw.Draw(bbx)
                draw_box_on_image(draw, bounding_box["box"], fill=True)
                #bbx = bbx.resize(target_shape)
                bbx = bbx.resize(feature_shape, resample=NEAREST)
                attention_map = np.array(bbx)
                attention_map = np.reshape(attention_map, feature_size)
                attention_maps.append(attention_map)
    attention_maps = np.array(attention_maps)
    
    attention_ids = [bounding_box["box_id"] for bounding_box in processable["boxes"]]
    attention_ids = np.array(attention_ids)
    
    attention_labels = [bounding_box["category_id"]   for bounding_box in processable["boxes"]]
    attention_labels = np.array(attention_labels)
    
    directory_path = os.path.dirname(file_path)
    image_prefix = processable["image_prefix"]
    
    attention_map_path = to_image_path_by_id(image_prefix, image_id, directory_path, file_ending="bbx")
    store_numpy_to(attention_maps, attention_map_path)
    
    attention_label_path = to_image_path_by_id(image_prefix, image_id, directory_path, file_ending="lbx")
    store_numpy_to(attention_labels, attention_label_path)

    attention_id_path = to_image_path_by_id(image_prefix, image_id, directory_path, file_ending="ibx")
    store_numpy_to(attention_ids, attention_id_path)
    
Esempio n. 4
0
 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
Esempio n. 5
0
 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
     """
     image_file_paths = [
         to_image_path_by_id(self.prefix, image_id, self.directory_path)
         for image_id in image_ids
     ]
     images = np.array([
         _get_image(image_file_path, self.target_size)
         for image_file_path in image_file_paths
     ])
     if self.vgg_like:
         images = preprocess_input(images, mode="caffe")
     return images
Esempio n. 6
0
 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
     """
     image_file_paths = [
         to_image_path_by_id(self.prefix, image_id, self.directory_path)
         for image_id in image_ids
     ]
     """ original sized images b.c. boxes are from there """
     images = [
         get_image(image_file_path) for image_file_path in image_file_paths
     ]
     image_boxes = self.get_boxes_by_image_ids(image_ids)
     images = draw_boxes_on_images(images, image_boxes)
     images = resize_images(images, self.target_size)
     return images, image_boxes
Esempio n. 7
0
def __store_feature_maps_to_many(image_ids,
                                 feature_maps,
                                 directory_path,
                                 image_prefix,
                                 split_name=None):
    """
        @param ids: array 1d  (batches)
            A list of image ids in the same order as the feature maps
        @param maps: array 3d (batches, features, maps)
            An array of feature maps in the same order as the ids
    """
    if split_name:
        directory_path = "/".join([directory_path, split_name])
    return [
        store_numpy_to(
            feature_map,
            to_image_path_by_id(image_prefix,
                                image_id,
                                directory_path,
                                file_ending="npy"))
        for image_id, feature_map in zip(image_ids, feature_maps)
    ]
Esempio n. 8
0
 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)