def load_image_from_file()

in tcav/activation_generator.py [0:0]


  def load_image_from_file(self, filename, shape):
    """Given a filename, try to open the file.

    If failed, return None.

    Args:
      filename: location of the image file
      shape: the shape of the image file to be scaled

    Returns:
      the image if succeeds, None if fails.

    Rasies:
      exception if the image was not the right shape.
    """
    if not tf.io.gfile.exists(filename):
      tf.compat.v1.logging.error('Cannot find file: {}'.format(filename))
      return None
    try:
      # ensure image has no transparency channel
      img = np.array(
          PIL.Image.open(tf.io.gfile.GFile(filename,
                                           'rb')).convert('RGB').resize(
                                               shape, PIL.Image.BILINEAR),
          dtype=np.float32)
      if self.normalize_image:
        # Normalize pixel values to between 0 and 1.
        img = img / 255.0
      if not (len(img.shape) == 3 and img.shape[2] == 3):
        return None
      else:
        return img

    except Exception as e:
      tf.compat.v1.logging.info(e)
      return None
    return img