Exemplo n.º 1
0
 def testNoOp(self):
   # No random cropping is performed since the size is value.shape.
   for shape in (2, 1, 1), (2, 1, 3), (4, 5, 3):
     value = np.arange(0, np.prod(shape), dtype=np.int32).reshape(shape)
     with self.cached_session():
       crop = random_ops.random_crop(value, shape).eval()
       self.assertAllEqual(crop, value)
Exemplo n.º 2
0
 def testNoOp(self):
   # No random cropping is performed since the size is value.shape.
   for shape in (2, 1, 1), (2, 1, 3), (4, 5, 3):
     value = np.arange(0, np.prod(shape), dtype=np.int32).reshape(shape)
     with self.cached_session():
       crop = random_ops.random_crop(value, shape).eval()
       self.assertAllEqual(crop, value)
Exemplo n.º 3
0
def image_augmentation(inputs, batch_size):
    """image augmentation."""
    img = inputs
    img = image_ops_impl.resize_images_v2(img, size=[224, 224])
    img = random_ops.random_crop(img, size=[batch_size, 224, 224, 3])
    img = rotate(img)
    img = zoom(img)
    return img
Exemplo n.º 4
0
 def testContains(self):
   with self.cached_session():
     shape = (3, 5, 7)
     target = (2, 3, 4)
     value = np.random.randint(1000000, size=shape)
     value_set = set(
         tuple(value[i:i + 2, j:j + 3, k:k + 4].ravel())
         for i in range(2) for j in range(3) for k in range(4))
     crop = random_ops.random_crop(value, size=target)
     for _ in range(20):
       y = self.evaluate(crop)
       self.assertAllEqual(y.shape, target)
       self.assertTrue(tuple(y.ravel()) in value_set)
Exemplo n.º 5
0
 def testContains(self):
   with self.cached_session():
     shape = (3, 5, 7)
     target = (2, 3, 4)
     value = np.random.randint(1000000, size=shape)
     value_set = set(
         tuple(value[i:i + 2, j:j + 3, k:k + 4].ravel())
         for i in range(2) for j in range(3) for k in range(4))
     crop = random_ops.random_crop(value, size=target)
     for _ in range(20):
       y = self.evaluate(crop)
       self.assertAllEqual(y.shape, target)
       self.assertTrue(tuple(y.ravel()) in value_set)
Exemplo n.º 6
0
def random_crop(labeled_tensor, shape_map, seed=None, name=None):
    """Randomly crops a tensor to a given size.

  See tf.random_crop.

  Args:
    labeled_tensor: The input tensor.
    shape_map: A dictionary mapping axis names to the size of the random crop
      for that dimension.
    seed: An optional random seed.
    name: An optional op name.

  Returns:
    A tensor of the same rank as `labeled_tensor`, cropped randomly in the
    selected dimensions.

  Raises:
    ValueError: If the shape map contains an axis name not in the input tensor.
  """
    with ops.name_scope(name, 'lt_random_crop', [labeled_tensor]) as scope:
        labeled_tensor = core.convert_to_labeled_tensor(labeled_tensor)

        for axis_name in shape_map:
            if axis_name not in labeled_tensor.axes:
                raise ValueError('Selection axis %s not in axes %s' %
                                 (axis_name, labeled_tensor.axes))

        shape = []
        axes = []
        for axis in labeled_tensor.axes.values():
            if axis.name in shape_map:
                size = shape_map[axis.name]
                shape.append(size)
                # We lose labels for the axes we crop, leaving just the size.
                axes.append((axis.name, size))
            else:
                shape.append(len(axis))
                axes.append(axis)

        crop_op = random_ops.random_crop(labeled_tensor.tensor,
                                         shape,
                                         seed=seed,
                                         name=scope)

        return core.LabeledTensor(crop_op, axes)
Exemplo n.º 7
0
def random_crop(labeled_tensor, shape_map, seed=None, name=None):
  """Randomly crops a tensor to a given size.

  See tf.random_crop.

  Args:
    labeled_tensor: The input tensor.
    shape_map: A dictionary mapping axis names to the size of the random crop
      for that dimension.
    seed: An optional random seed.
    name: An optional op name.

  Returns:
    A tensor of the same rank as `labeled_tensor`, cropped randomly in the
    selected dimensions.

  Raises:
    ValueError: If the shape map contains an axis name not in the input tensor.
  """
  with ops.name_scope(name, 'lt_random_crop', [labeled_tensor]) as scope:
    labeled_tensor = core.convert_to_labeled_tensor(labeled_tensor)

    for axis_name in shape_map:
      if axis_name not in labeled_tensor.axes:
        raise ValueError('Selection axis %s not in axes %s' %
                         (axis_name, labeled_tensor.axes))

    shape = []
    axes = []
    for axis in labeled_tensor.axes.values():
      if axis.name in shape_map:
        size = shape_map[axis.name]
        shape.append(size)
        # We lose labels for the axes we crop, leaving just the size.
        axes.append((axis.name, size))
      else:
        shape.append(len(axis))
        axes.append(axis)

    crop_op = random_ops.random_crop(labeled_tensor.tensor,
                                     shape,
                                     seed=seed,
                                     name=scope)

    return core.LabeledTensor(crop_op, axes)
Exemplo n.º 8
0
  def testRandomization(self):
    # Run 1x1 crop num_samples times in an image and ensure that one finds each
    # pixel 1/size of the time.
    num_samples = 1000
    shape = [5, 4, 1]
    size = np.prod(shape)
    single = [1, 1, 1]
    value = np.arange(size).reshape(shape)

    with self.cached_session():
      crop = random_ops.random_crop(value, single, seed=7)
      counts = np.zeros(size, dtype=np.int32)
      for _ in range(num_samples):
        y = self.evaluate(crop)
        self.assertAllEqual(y.shape, single)
        counts[y] += 1

    # Calculate the mean and 4 * standard deviation.
    mean = np.repeat(num_samples / size, size)
    four_stddev = 4.0 * np.sqrt(mean)

    # Ensure that each entry is observed in 1/size of the samples
    # within 4 standard deviations.
    self.assertAllClose(counts, mean, atol=four_stddev)
Exemplo n.º 9
0
  def testRandomization(self):
    # Run 1x1 crop num_samples times in an image and ensure that one finds each
    # pixel 1/size of the time.
    num_samples = 1000
    shape = [5, 4, 1]
    size = np.prod(shape)
    single = [1, 1, 1]
    value = np.arange(size).reshape(shape)

    with self.cached_session():
      crop = random_ops.random_crop(value, single, seed=7)
      counts = np.zeros(size, dtype=np.int32)
      for _ in range(num_samples):
        y = self.evaluate(crop)
        self.assertAllEqual(y.shape, single)
        counts[y] += 1

    # Calculate the mean and 4 * standard deviation.
    mean = np.repeat(num_samples / size, size)
    four_stddev = 4.0 * np.sqrt(mean)

    # Ensure that each entry is observed in 1/size of the samples
    # within 4 standard deviations.
    self.assertAllClose(counts, mean, atol=four_stddev)