Beispiel #1
0
  def next(self):
    """For python 2.x.

    Returns:
        The next batch.
    """
    # Keeps under lock only the mechanism which advances
    # the indexing of each batch.
    with self.lock:
      index_array, current_index, current_batch_size = next(
          self.index_generator)
    # The transformation of images is not under thread lock
    # so it can be done in parallel
    batch_x = np.zeros(
        tuple([current_batch_size] + list(self.x.shape)[1:]), dtype=K.floatx())
    for i, j in enumerate(index_array):
      x = self.x[j]
      x = self.image_data_generator.random_transform(x.astype(K.floatx()))
      x = self.image_data_generator.standardize(x)
      batch_x[i] = x
    if self.save_to_dir:
      for i in range(current_batch_size):
        img = array_to_img(batch_x[i], self.data_format, scale=True)
        fname = '{prefix}_{index}_{hash}.{format}'.format(
            prefix=self.save_prefix,
            index=current_index + i,
            hash=np.random.randint(1e4),
            format=self.save_format)
        img.save(os.path.join(self.save_to_dir, fname))
    if self.y is None:
      return batch_x
    batch_y = self.y[index_array]
    return batch_x, batch_y
Beispiel #2
0
    def _get_batches_of_transformed_samples(self, index_array):
        batch_x = np.zeros(tuple([len(index_array)] + list(self.x.shape)[1:]),
                           dtype=K.floatx())
        batch_coords = [self.coords[i] for i in index_array]
        x, y = self.func_patch(self._x, self._y, batch_coords, self.patch_h,
                               self.patch_w)
        self.x = x
        self.y = y
        index_array = np.arange(len(self.y))

        for i, j in enumerate(index_array):
            x = self.x[j]
            x = self.image_data_generator.random_transform(x.astype(
                K.floatx()))
            x = self.image_data_generator.standardize(x)
            batch_x[i] = x
        if self.save_to_dir:
            for i, j in enumerate(index_array):
                img = array_to_img(batch_x[i], self.data_format, scale=True)
                fname = '{prefix}_{index}_{hash}.{format}'.format(
                    prefix=self.save_prefix,
                    index=j,
                    hash=np.random.randint(1e4),
                    format=self.save_format)
            img.save(os.path.join(self.save_to_dir, fname))
        if self.y is None:
            return batch_x
        batch_y = self.y[index_array]
        return batch_x, batch_y
    def next(self):
        """For python 2.x.
        Returns:
            The next batch.
        """
        # Keeps under lock only the mechanism which advances
        # the indexing of each batch.
        with self.lock:
          index_array, current_index, current_batch_size = next(
              self.index_generator)
        # The transformation of images is not under thread lock
        # so it can be done in parallel
        
        batch_x = np.zeros((current_batch_size,) + self.image_shape, dtype=K.floatx())


        if self.training:
            batch_y = np.zeros(
                    (current_batch_size,) + self.image_shape[:2] + (self.num_classes,),
                    dtype=K.floatx())
        
        # iterates over the indexes of one batch
        for e, i in enumerate(index_array):
            # TODO implement data loading and preperation logic, make a call to shift_aug at some point. 


        if not self.training:
          return batch_x

        else:
          return batch_x, batch_y
    def next(self):
        """For python 2.x.
        Returns:
            The next batch.
        """
        # Keeps under lock only the mechanism which advances
        # the indexing of each batch.
        with self.lock:
            index_array, current_index, current_batch_size = next(
                self.index_generator)
        # The transformation of images is not under thread lock
        # so it can be done in parallel

        batch_x = np.zeros((current_batch_size, ) + self.image_shape,
                           dtype=K.floatx())

        if self.training:
            batch_y = np.zeros((current_batch_size, ) + self.image_shape[:2] +
                               (self.num_classes, ),
                               dtype=K.floatx())

        for e, i in enumerate(index_array):
            # load labels of the person in focus
            file_tuple = self.file_tuples[i]
            image = misc.imread(file_tuple[0])

            if image.shape != self.image_shape:
                image = misc.imresize(image, self.image_shape)

            if not self.training:
                image = preprocess_input(image.astype(np.float32))
                batch_x[e, :, :, :] = image
                continue

            else:
                gt_image = misc.imread(file_tuple[1]).clip(0, 1)
                if gt_image.shape[0] != self.image_shape[0]:
                    gt_image = misc.imresize(gt_image, self.image_shape)

                if self.shift_aug:
                    image, gt_image = shift_and_pad_augmentation(
                        image, gt_image)

                image = preprocess_input(image.astype(np.float32))
                batch_x[e, :, :, :] = image
                batch_y[e, :, :, :] = gt_image

        if not self.training:
            return batch_x
        else:
            if self.seq is not None:
                seq = self.seq.to_deterministic()
                batch_x = seq.augment_images(batch_x)
                batch_y = seq.augment_images(batch_y)
            return batch_x, batch_y
Beispiel #5
0
    def next(self):
        """For python 2.x.

    Returns:
        The next batch.
    """
        with self.lock:
            index_array, current_index, current_batch_size = next(
                self.index_generator)
        # The transformation of images is not under thread lock
        # so it can be done in parallel
        batch_x = np.zeros((current_batch_size, ) + self.image_shape,
                           dtype=K.floatx())
        grayscale = self.color_mode == 'grayscale'
        # build batch of image data
        for i, j in enumerate(index_array):
            fname = self.filenames[j]
            img = load_img(os.path.join(self.directory, fname),
                           grayscale=grayscale,
                           target_size=self.target_size)
            x = img_to_array(img, data_format=self.data_format)
            x = self.image_data_generator.random_transform(x)
            x = self.image_data_generator.standardize(x)
            batch_x[i] = x
        # optionally save augmented images to disk for debugging purposes
        if self.save_to_dir:
            for i in range(current_batch_size):
                img = array_to_img(batch_x[i], self.data_format, scale=True)
                fname = '{prefix}_{index}_{hash}.{format}'.format(
                    prefix=self.save_prefix,
                    index=current_index + i,
                    hash=np.random.randint(1e4),
                    format=self.save_format)
                img.save(os.path.join(self.save_to_dir, fname))
        # build batch of labels
        if self.class_mode == 'input':
            batch_y = batch_x.copy()
        elif self.class_mode == 'sparse':
            batch_y = self.classes[index_array]
        elif self.class_mode == 'binary':
            batch_y = self.classes[index_array].astype(K.floatx())
        elif self.class_mode == 'categorical':
            batch_y = np.zeros((len(batch_x), self.num_class),
                               dtype=K.floatx())
            for i, label in enumerate(self.classes[index_array]):
                batch_y[i, label] = 1.
        else:
            return batch_x
        return batch_x, batch_y
Beispiel #6
0
  def next(self):
    """For python 2.x.

    Returns:
        The next batch.
    """
    with self.lock:
      index_array, current_index, current_batch_size = next(
          self.index_generator)
    # The transformation of images is not under thread lock
    # so it can be done in parallel
    batch_x = np.zeros(
        (current_batch_size,) + self.image_shape, dtype=K.floatx())
    grayscale = self.color_mode == 'grayscale'
    # build batch of image data
    for i, j in enumerate(index_array):
      fname = self.filenames[j]
      img = load_img(
          os.path.join(self.directory, fname),
          grayscale=grayscale,
          target_size=self.target_size)
      x = img_to_array(img, data_format=self.data_format)
      x = self.image_data_generator.random_transform(x)
      x = self.image_data_generator.standardize(x)
      batch_x[i] = x
    # optionally save augmented images to disk for debugging purposes
    if self.save_to_dir:
      for i in range(current_batch_size):
        img = array_to_img(batch_x[i], self.data_format, scale=True)
        fname = '{prefix}_{index}_{hash}.{format}'.format(
            prefix=self.save_prefix,
            index=current_index + i,
            hash=np.random.randint(1e4),
            format=self.save_format)
        img.save(os.path.join(self.save_to_dir, fname))
    # build batch of labels
    if self.class_mode == 'input':
      batch_y = batch_x.copy()
    elif self.class_mode == 'sparse':
      batch_y = self.classes[index_array]
    elif self.class_mode == 'binary':
      batch_y = self.classes[index_array].astype(K.floatx())
    elif self.class_mode == 'categorical':
      batch_y = np.zeros((len(batch_x), self.num_class), dtype=K.floatx())
      for i, label in enumerate(self.classes[index_array]):
        batch_y[i, label] = 1.
    else:
      return batch_x
    return batch_x, batch_y
Beispiel #7
0
  def get_updates(self, loss, params):
    grads = self.get_gradients(loss, params)
    self.updates = [K.update_add(self.iterations, 1)]

    lr = self.lr
    if self.initial_decay > 0:
      lr *= (1. / (1. + self.decay * K.cast(self.iterations,
                                            K.dtype(self.decay))))

    t = K.cast(self.iterations, K.floatx()) + 1
    lr_t = lr * (K.sqrt(1. - K.pow(self.beta_2, t)) /
                 (1. - K.pow(self.beta_1, t)))

    ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
    vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
    self.weights = [self.iterations] + ms + vs

    for p, g, m, v in zip(params, grads, ms, vs):
      m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
      v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g)
      p_t = p - lr_t * m_t / (K.sqrt(v_t) + self.epsilon)

      self.updates.append(K.update(m, m_t))
      self.updates.append(K.update(v, v_t))
      new_p = p_t

      # Apply constraints.
      if getattr(p, 'constraint', None) is not None:
        new_p = p.constraint(new_p)

      self.updates.append(K.update(p, new_p))
    return self.updates
Beispiel #8
0
  def get_updates(self, loss, params):
    grads = self.get_gradients(loss, params)
    self.updates = [K.update_add(self.iterations, 1)]

    lr = self.lr
    if self.initial_decay > 0:
      lr *= (1. / (1. + self.decay * K.cast(self.iterations,
                                            K.dtype(self.decay))))

    t = K.cast(self.iterations, K.floatx()) + 1
    lr_t = lr / (1. - K.pow(self.beta_1, t))

    shapes = [K.int_shape(p) for p in params]
    # zero init of 1st moment
    ms = [K.zeros(shape) for shape in shapes]
    # zero init of exponentially weighted infinity norm
    us = [K.zeros(shape) for shape in shapes]
    self.weights = [self.iterations] + ms + us

    for p, g, m, u in zip(params, grads, ms, us):

      m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
      u_t = K.maximum(self.beta_2 * u, K.abs(g))
      p_t = p - lr_t * m_t / (u_t + self.epsilon)

      self.updates.append(K.update(m, m_t))
      self.updates.append(K.update(u, u_t))
      new_p = p_t

      # Apply constraints.
      if getattr(p, 'constraint', None) is not None:
        new_p = p.constraint(new_p)

      self.updates.append(K.update(p, new_p))
    return self.updates
Beispiel #9
0
def img_to_array(img, data_format=None):
  """Converts a PIL Image instance to a Numpy array.

  Arguments:
      img: PIL Image instance.
      data_format: Image data format.

  Returns:
      A 3D Numpy array.

  Raises:
      ValueError: if invalid `img` or `data_format` is passed.
  """
  if data_format is None:
    data_format = K.image_data_format()
  if data_format not in {'channels_first', 'channels_last'}:
    raise ValueError('Unknown data_format: ', data_format)
  # Numpy array x has format (height, width, channel)
  # or (channel, height, width)
  # but original PIL image has format (width, height, channel)
  x = np.asarray(img, dtype=K.floatx())
  if len(x.shape) == 3:
    if data_format == 'channels_first':
      x = x.transpose(2, 0, 1)
  elif len(x.shape) == 2:
    if data_format == 'channels_first':
      x = x.reshape((1, x.shape[0], x.shape[1]))
    else:
      x = x.reshape((x.shape[0], x.shape[1], 1))
  else:
    raise ValueError('Unsupported image shape: ', x.shape)
  return x
Beispiel #10
0
 def dropped_inputs(inputs=inputs, rate=self.rate, seed=self.seed):
     alpha_p = -alpha * scale
     kept_idx = K.greater_equal(
         K.random_uniform(noise_shape, seed=seed), rate)
     kept_idx = K.cast(kept_idx, K.floatx())
     a = ((1 - rate) * (1 + rate * alpha_p**2))**-0.5
     b = -a * alpha_p * rate
     x = inputs * kept_idx + alpha_p * (1 - kept_idx)
     return a * x + b
Beispiel #11
0
 def dropped_inputs(inputs=inputs, rate=self.rate, seed=self.seed):
   alpha_p = -alpha * scale
   kept_idx = K.greater_equal(K.random_uniform(noise_shape, seed=seed),
                              rate)
   kept_idx = K.cast(kept_idx, K.floatx())
   a = ((1 - rate) * (1 + rate * alpha_p ** 2)) ** -0.5
   b = -a * alpha_p * rate
   x = inputs * kept_idx + alpha_p * (1 - kept_idx)
   return a * x + b
Beispiel #12
0
def array_to_img(x, data_format=None, scale=True):
    """Converts a 3D Numpy array to a PIL Image instance.

  Arguments:
      x: Input Numpy array.
      data_format: Image data format.
      scale: Whether to rescale image values
          to be within [0, 255].

  Returns:
      A PIL Image instance.

  Raises:
      ImportError: if PIL is not available.
      ValueError: if invalid `x` or `data_format` is passed.
  """
    if pil_image is None:
        raise ImportError('Could not import PIL.Image. '
                          'The use of `array_to_img` requires PIL.')
    x = np.asarray(x, dtype=K.floatx())
    if x.ndim != 3:
        raise ValueError(
            'Expected image array to have rank 3 (single image). '
            'Got array with shape:', x.shape)

    if data_format is None:
        data_format = K.image_data_format()
    if data_format not in {'channels_first', 'channels_last'}:
        raise ValueError('Invalid data_format:', data_format)

    # Original Numpy array x has format (height, width, channel)
    # or (channel, height, width)
    # but target PIL image has format (width, height, channel)
    if data_format == 'channels_first':
        x = x.transpose(1, 2, 0)
    if scale:
        x = x + max(-np.min(x), 0)  # pylint: disable=g-no-augmented-assignment
        x_max = np.max(x)
        if x_max != 0:
            x /= x_max
        x *= 255
    if x.shape[2] == 3:
        # RGB
        return pil_image.fromarray(x.astype('uint8'), 'RGB')
    elif x.shape[2] == 1:
        # grayscale
        return pil_image.fromarray(x[:, :, 0].astype('uint8'), 'L')
    else:
        raise ValueError('Unsupported channel number: ', x.shape[2])
Beispiel #13
0
    def __init__(self,
                 x,
                 y,
                 image_data_generator,
                 batch_size=32,
                 shuffle=False,
                 seed=None,
                 data_format=None,
                 save_to_dir=None,
                 save_prefix='',
                 save_format='png'):
        if y is not None and len(x) != len(y):
            raise ValueError('X (images tensor) and y (labels) '
                             'should have the same length. '
                             'Found: X.shape = %s, y.shape = %s' %
                             (np.asarray(x).shape, np.asarray(y).shape))

        if data_format is None:
            data_format = K.image_data_format()
        self.x = np.asarray(x, dtype=K.floatx())

        if self.x.ndim != 4:
            raise ValueError(
                'Input data in `NumpyArrayIterator` '
                'should have rank 4. You passed an array '
                'with shape', self.x.shape)
        channels_axis = 3 if data_format == 'channels_last' else 1
        if self.x.shape[channels_axis] not in {1, 3, 4}:
            raise ValueError('NumpyArrayIterator is set to use the '
                             'data format convention "' + data_format + '" '
                             '(channels on axis ' + str(channels_axis) +
                             '), i.e. expected '
                             'either 1, 3 or 4 channels on axis ' +
                             str(channels_axis) + '. '
                             'However, it was passed an array with shape ' +
                             str(self.x.shape) + ' (' +
                             str(self.x.shape[channels_axis]) + ' channels).')
        if y is not None:
            self.y = np.asarray(y)
        else:
            self.y = None
        self.image_data_generator = image_data_generator
        self.data_format = data_format
        self.save_to_dir = save_to_dir
        self.save_prefix = save_prefix
        self.save_format = save_format
        super(NumpyArrayIterator, self).__init__(x.shape[0], batch_size,
                                                 shuffle, seed)
Beispiel #14
0
def array_to_img(x, data_format=None, scale=True):
  """Converts a 3D Numpy array to a PIL Image instance.

  Arguments:
      x: Input Numpy array.
      data_format: Image data format.
      scale: Whether to rescale image values
          to be within [0, 255].

  Returns:
      A PIL Image instance.

  Raises:
      ImportError: if PIL is not available.
      ValueError: if invalid `x` or `data_format` is passed.
  """
  if pil_image is None:
    raise ImportError('Could not import PIL.Image. '
                      'The use of `array_to_img` requires PIL.')
  x = np.asarray(x, dtype=K.floatx())
  if x.ndim != 3:
    raise ValueError('Expected image array to have rank 3 (single image). '
                     'Got array with shape:', x.shape)

  if data_format is None:
    data_format = K.image_data_format()
  if data_format not in {'channels_first', 'channels_last'}:
    raise ValueError('Invalid data_format:', data_format)

  # Original Numpy array x has format (height, width, channel)
  # or (channel, height, width)
  # but target PIL image has format (width, height, channel)
  if data_format == 'channels_first':
    x = x.transpose(1, 2, 0)
  if scale:
    x = x + max(-np.min(x), 0)  # pylint: disable=g-no-augmented-assignment
    x_max = np.max(x)
    if x_max != 0:
      x /= x_max
    x *= 255
  if x.shape[2] == 3:
    # RGB
    return pil_image.fromarray(x.astype('uint8'), 'RGB')
  elif x.shape[2] == 1:
    # grayscale
    return pil_image.fromarray(x[:, :, 0].astype('uint8'), 'L')
  else:
    raise ValueError('Unsupported channel number: ', x.shape[2])
Beispiel #15
0
  def __init__(self,
               x,
               y,
               image_data_generator,
               batch_size=32,
               shuffle=False,
               seed=None,
               data_format=None,
               save_to_dir=None,
               save_prefix='',
               save_format='jpeg'):
    if y is not None and len(x) != len(y):
      raise ValueError('X (images tensor) and y (labels) '
                       'should have the same length. '
                       'Found: X.shape = %s, y.shape = %s' %
                       (np.asarray(x).shape, np.asarray(y).shape))

    if data_format is None:
      data_format = K.image_data_format()
    self.x = np.asarray(x, dtype=K.floatx())

    if self.x.ndim != 4:
      raise ValueError('Input data in `NumpyArrayIterator` '
                       'should have rank 4. You passed an array '
                       'with shape', self.x.shape)
    channels_axis = 3 if data_format == 'channels_last' else 1
    if self.x.shape[channels_axis] not in {1, 3, 4}:
      raise ValueError(
          'NumpyArrayIterator is set to use the '
          'data format convention "' + data_format + '" '
          '(channels on axis ' + str(channels_axis) + '), i.e. expected '
          'either 1, 3 or 4 channels on axis ' + str(channels_axis) + '. '
          'However, it was passed an array with shape ' + str(self.x.shape) +
          ' (' + str(self.x.shape[channels_axis]) + ' channels).')
    if y is not None:
      self.y = np.asarray(y)
    else:
      self.y = None
    self.image_data_generator = image_data_generator
    self.data_format = data_format
    self.save_to_dir = save_to_dir
    self.save_prefix = save_prefix
    self.save_format = save_format
    super(NumpyArrayIterator, self).__init__(x.shape[0], batch_size, shuffle,
                                             seed)
Beispiel #16
0
  def get_updates(self, loss, params):
    grads = self.get_gradients(loss, params)
    self.updates = [K.update_add(self.iterations, 1)]
    t = K.cast(self.iterations, K.floatx()) + 1

    # Due to the recommendations in [2], i.e. warming momentum schedule
    momentum_cache_t = self.beta_1 * (
        1. - 0.5 * (K.pow(K.cast_to_floatx(0.96), t * self.schedule_decay)))
    momentum_cache_t_1 = self.beta_1 * (
        1. - 0.5 *
        (K.pow(K.cast_to_floatx(0.96), (t + 1) * self.schedule_decay)))
    m_schedule_new = self.m_schedule * momentum_cache_t
    m_schedule_next = self.m_schedule * momentum_cache_t * momentum_cache_t_1
    self.updates.append((self.m_schedule, m_schedule_new))

    shapes = [K.int_shape(p) for p in params]
    ms = [K.zeros(shape) for shape in shapes]
    vs = [K.zeros(shape) for shape in shapes]

    self.weights = [self.iterations] + ms + vs

    for p, g, m, v in zip(params, grads, ms, vs):
      # the following equations given in [1]
      g_prime = g / (1. - m_schedule_new)
      m_t = self.beta_1 * m + (1. - self.beta_1) * g
      m_t_prime = m_t / (1. - m_schedule_next)
      v_t = self.beta_2 * v + (1. - self.beta_2) * K.square(g)
      v_t_prime = v_t / (1. - K.pow(self.beta_2, t))
      m_t_bar = (
          1. - momentum_cache_t) * g_prime + momentum_cache_t_1 * m_t_prime

      self.updates.append(K.update(m, m_t))
      self.updates.append(K.update(v, v_t))

      p_t = p - self.lr * m_t_bar / (K.sqrt(v_t_prime) + self.epsilon)
      new_p = p_t

      # Apply constraints.
      if getattr(p, 'constraint', None) is not None:
        new_p = p.constraint(new_p)

      self.updates.append(K.update(p, new_p))
    return self.updates
Beispiel #17
0
    def __init__(self,
                 x,
                 y,
                 image_data_generator,
                 coords,
                 patch_h,
                 patch_w,
                 batch_size=32,
                 shuffle=False,
                 seed=None,
                 func_patch=_extract_patches,
                 data_format=None,
                 save_to_dir=None,
                 save_prefix='',
                 save_format='png'):
        self.coords = coords
        self.patch_h = patch_h
        self.patch_w = patch_w
        if isinstance(x, list):
            self._x, self._y = x[:], y[:]
            chnum = self._x[0].shape[-1]
        else:
            self._x, self._y = x.copy(), y.copy()
            self._x = self._x[0, :, :, :]
            chnum = self._x.shape[-1]
        self.x = np.asarray(np.zeros((1, patch_h, patch_w, chnum)),
                            dtype=K.floatx())
        if y is not None:
            self.y = np.zeros(1)
        else:
            self.y = None
        self.n = len(self.coords)
        self.func_patch = func_patch

        self.image_data_generator = image_data_generator
        self.data_format = data_format
        self.save_to_dir = save_to_dir
        self.save_prefix = save_prefix
        self.save_format = save_format
        super(CropIterator, self).__init__(len(self.coords), batch_size,
                                           shuffle, seed)
Beispiel #18
0
    def build(self, input_shape):
        assert len(input_shape) == 3
        n_classes = input_shape[2]
        n_steps = input_shape[1]
        assert n_steps is None or n_steps >= 2
        self.input_spec = [InputSpec(dtype=K.floatx(),
                           shape=(None, n_steps, n_classes))]

        # Transition params
        self.U = self.add_weight(
            (n_classes, n_classes),
            initializer=self.init,
            name='U',
            regularizer=self.U_regularizer,
            constraint=self.U_constraint)

        # if self.initial_weights is not None:
        #     self.set_weights(self.initial_weights)
        #     del self.initial_weights

        super(ChainCRF_tensorflow, self).build(input_shape)
Beispiel #19
0
def sparse_categorical_accuracy(y_true, y_pred):
    return K.cast(
        K.equal(K.max(y_true, axis=-1),
                K.cast(K.argmax(y_pred, axis=-1), K.floatx())), K.floatx())
Beispiel #20
0
 def call(self, inputs):
   boolean_mask = K.any(
       K.not_equal(inputs, self.mask_value), axis=-1, keepdims=True)
   return inputs * K.cast(boolean_mask, K.floatx())
Beispiel #21
0
 def call(self, inputs, mask=None):
     return inputs * K.cast(inputs > self.theta, K.floatx())
    def next(self):
        """For python 2.x.
        Returns:
            The next batch.
        """
        # Keeps under lock only the mechanism which advances
        # the indexing of each batch.
        with self.lock:
            index_array, current_index, current_batch_size = next(
                self.index_generator)
        # The transformation of images is not under thread lock
        # so it can be done in parallel

        batch_x = np.zeros((current_batch_size, ) + self.image_shape,
                           dtype=K.floatx())

        if self.training:
            batch_y = np.zeros((current_batch_size, ) + self.image_shape[:2] +
                               (self.num_classes, ),
                               dtype=K.floatx())

        for e, i in enumerate(index_array):
            # load labels of the person in focus
            file_tuple = self.file_tuples[i]
            image = misc.imread(file_tuple[0])

            if image.shape != self.image_shape:
                image = misc.imresize(image, self.image_shape)

            if not self.training:
                image = preprocess_input(image.astype(np.float32))
                batch_x[e, :, :, :] = image
                continue

            else:
                gt_image = misc.imread(file_tuple[1]).clip(0, 1)
                if gt_image.shape[0] != self.image_shape[0]:
                    gt_image = misc.imresize(gt_image, self.image_shape)

                # Do augmentation:
                if np.random.uniform() > 0.5:
                    if self.shift_aug:
                        image, gt_image = shift_and_pad_augmentation(
                            image, gt_image)

                    if self.flip_aug:
                        image, gt_image = flip_augmentation(image, gt_image)

                image = preprocess_input(image.astype(np.float32))
                batch_x[e, :, :, :] = image
                batch_y[e, :, :, :] = gt_image

        if not self.training:
            return batch_x

        else:
            """
            batch_sample_weights = compute_sample_weight(
                'balanced',
                (
                    batch_y.reshape(
                        (-1, self.num_classes)
                    ) * np.arange(self.num_classes)
                ).sum(axis = 1)
            ).reshape(
                (current_batch_size, -1)
            ).mean(axis = 1)
            return (batch_x, batch_y, batch_sample_weights)
            """
            return (batch_x, batch_y)
Beispiel #23
0
  def fit(self, x, augment=False, rounds=1, seed=None):
    """Fits internal statistics to some sample data.

    Required for featurewise_center, featurewise_std_normalization
    and zca_whitening.

    Arguments:
        x: Numpy array, the data to fit on. Should have rank 4.
            In case of grayscale data,
            the channels axis should have value 1, and in case
            of RGB data, it should have value 3.
        augment: Whether to fit on randomly augmented samples
        rounds: If `augment`,
            how many augmentation passes to do over the data
        seed: random seed.

    Raises:
        ValueError: in case of invalid input `x`.
        ImportError: if Scipy is not available.
    """
    x = np.asarray(x, dtype=K.floatx())
    if x.ndim != 4:
      raise ValueError('Input to `.fit()` should have rank 4. '
                       'Got array with shape: ' + str(x.shape))
    if x.shape[self.channel_axis] not in {1, 3, 4}:
      raise ValueError(
          'Expected input to be images (as Numpy array) '
          'following the data format convention "' + self.data_format + '" '
          '(channels on axis ' + str(self.channel_axis) + '), i.e. expected '
          'either 1, 3 or 4 channels on axis ' + str(self.channel_axis) + '. '
          'However, it was passed an array with shape ' + str(
              x.shape) + ' (' + str(x.shape[self.channel_axis]) + ' channels).')

    if seed is not None:
      np.random.seed(seed)

    x = np.copy(x)
    if augment:
      ax = np.zeros(
          tuple([rounds * x.shape[0]] + list(x.shape)[1:]), dtype=K.floatx())
      for r in range(rounds):
        for i in range(x.shape[0]):
          ax[i + r * x.shape[0]] = self.random_transform(x[i])
      x = ax

    if self.featurewise_center:
      self.mean = np.mean(x, axis=(0, self.row_axis, self.col_axis))
      broadcast_shape = [1, 1, 1]
      broadcast_shape[self.channel_axis - 1] = x.shape[self.channel_axis]
      self.mean = np.reshape(self.mean, broadcast_shape)
      x -= self.mean

    if self.featurewise_std_normalization:
      self.std = np.std(x, axis=(0, self.row_axis, self.col_axis))
      broadcast_shape = [1, 1, 1]
      broadcast_shape[self.channel_axis - 1] = x.shape[self.channel_axis]
      self.std = np.reshape(self.std, broadcast_shape)
      x /= (self.std + K.epsilon())

    if self.zca_whitening:
      if linalg is None:
        raise ImportError('Scipy is required for zca_whitening.')

      flat_x = np.reshape(x, (x.shape[0], x.shape[1] * x.shape[2] * x.shape[3]))
      sigma = np.dot(flat_x.T, flat_x) / flat_x.shape[0]
      u, s, _ = linalg.svd(sigma)
      self.principal_components = np.dot(
          np.dot(u, np.diag(1. / np.sqrt(s + 10e-7))), u.T)
Beispiel #24
0
 def __call__(self, w):
     w *= K.cast(w >= 0., K.floatx())
     return w
Beispiel #25
0
def iou_accuracy(y_true, y_pred):
  i = K.cast(K.cumsum(K.maximum(y_true*K.round(y_pred), 0.)), K.floatx())
  u = K.cast(K.cumsum(K.maximum(y_true+K.round(y_pred), 0.)), K.floatx())
  return i/u
Beispiel #26
0
 def call(self, inputs):
   boolean_mask = K.any(
       K.not_equal(inputs, self.mask_value), axis=-1, keepdims=True)
   return inputs * K.cast(boolean_mask, K.floatx())
Beispiel #27
0
 def __call__(self, w):
   w *= K.cast(w >= 0., K.floatx())
   return w
 def call(self, inputs, mask=None):
   return inputs * K.cast(inputs > self.theta, K.floatx())
Beispiel #29
0
def sparse_categorical_accuracy(y_true, y_pred):
  return K.equal(
      K.max(y_true, axis=-1), K.cast(K.argmax(y_pred, axis=-1), K.floatx()))