示例#1
0
 def preprocess(self, x):
     """
     Default preprocessing method for is to convert the picture to black and white and resize to be 48x48
     """
     im = processing_utils.decode_base64_to_image(x)
     im = im.convert('RGB')
     im = processing_utils.resize_and_crop(
         im, (self.image_width, self.image_height))
     return np.array(im)
示例#2
0
 def embed(self, x):
     shape = (100, 100) if self.shape is None else self.shape  
     if self.type == "pil":
         im = x
     elif self.type == "numpy":
         im = PIL.Image.fromarray(x)
     elif self.type == "file":
         im = PIL.Image.open(x)
     else:
         raise ValueError("Unknown type: " + str(self.type) + ". Please choose from: 'numpy', 'pil', 'file'.")
     im = processing_utils.resize_and_crop(im, (shape[0], shape[1]))
     return np.asarray(im).flatten()
示例#3
0
 def preprocess(self, x):
     im = processing_utils.decode_base64_to_image(x)
     with warnings.catch_warnings():
         warnings.simplefilter("ignore")
         im = im.convert(self.image_mode)
     if self.shape is not None:
         im = processing_utils.resize_and_crop(
             im, (self.shape[0], self.shape[1]))
     if self.type == "pil":
         return im
     elif self.type == "numpy":
         return np.array(im)
     elif self.type == "file":
         file_obj = tempfile.NamedTemporaryFile()
         im.save(file_obj.name)
         return file_obj
示例#4
0
 def get_interpretation_neighbors(self, x):
     x = processing_utils.decode_base64_to_image(x)
     if self.shape is not None:
         x = processing_utils.resize_and_crop(x, self.shape)
     image = np.array(x)
     segments_slic = slic(image, self.interpretation_segments, compactness=10, sigma=1)
     leave_one_out_tokens, masks = [], []
     replace_color = np.mean(image, axis=(0, 1))
     for (i, segVal) in enumerate(np.unique(segments_slic)):
         mask = segments_slic == segVal
         white_screen = np.copy(image)
         white_screen[segments_slic == segVal] = replace_color
         leave_one_out_tokens.append(
             processing_utils.encode_array_to_base64(white_screen))
         masks.append(mask)
     return leave_one_out_tokens, {"masks": masks}, True
示例#5
0
    def get_interpretation_scores(self, x, neighbors, scores, masks):
        """
        Returns:
        (List[List[float]]): A 2D array representing the interpretation score of each pixel of the image.
        """
        x = processing_utils.decode_base64_to_image(x)
        if self.shape is not None:
            x = processing_utils.resize_and_crop(x, self.shape)
        x = np.array(x)
        output_scores = np.zeros((x.shape[0], x.shape[1]))

        for score, mask in zip(scores, masks):
            output_scores += score * mask

        max_val, min_val = np.max(output_scores), np.min(output_scores)
        if max_val > 0:
            output_scores = (output_scores - min_val) / (max_val - min_val)
        return output_scores.tolist()
示例#6
0
 def preprocess(self, x):
     im = processing_utils.decode_base64_to_image(x)
     fmt = im.format
     with warnings.catch_warnings():
         warnings.simplefilter("ignore")
         im = im.convert(self.image_mode)
     if self.shape is not None:
         im = processing_utils.resize_and_crop(im, self.shape)
     if self.invert_colors:
         im = PIL.ImageOps.invert(im)
     if self.type == "pil":
         return im
     elif self.type == "numpy":
         return np.array(im)
     elif self.type == "file":
         file_obj = tempfile.NamedTemporaryFile(suffix=("."+fmt.lower() if fmt is not None else ".png"))
         im.save(file_obj.name)
         return file_obj
     else:
         raise ValueError("Unknown type: " + str(self.type) + ". Please choose from: 'numpy', 'pil', 'file'.")