def random_central_crop(image, minval, maxval): with ops.name_scope(None, 'central_crop', [image]): image = ops.convert_to_tensor(image, name='image') if (minval < 0 or maxval < 0 or minval > 1 or maxval > 1): raise ValueError('crop ratio range must be between 0 and 1.') _AssertAtLeast3DImage(image) rank = image.get_shape().ndims if rank != 3 and rank != 4: raise ValueError( '`image` should either be a Tensor with rank = 3 or ' 'rank = 4. Had rank = {}.'.format(rank)) # Helper method to return the `idx`-th dimension of `tensor`, along with # a boolean signifying if the dimension is dynamic. def _get_dim(tensor, idx): static_shape = tensor.get_shape()[idx].value if static_shape is not None: return static_shape, False return array_ops.shape(tensor)[idx], True # Get the height, width, depth (and batch size, if the image is a 4-D # tensor). if rank == 3: img_h, dynamic_h = _get_dim(image, 0) img_w, dynamic_w = _get_dim(image, 1) img_d = image.get_shape()[2] else: img_bs = image.get_shape()[0] img_h, dynamic_h = _get_dim(image, 1) img_w, dynamic_w = _get_dim(image, 2) img_d = image.get_shape()[3] central_fraction = tf.random_uniform([], minval=minval, maxval=maxval, dtype=tf.float64) # Compute the bounding boxes for the crop. The type and value of the # bounding boxes depend on the `image` tensor's rank and whether / not the # dimensions are statically defined. img_hd = math_ops.to_double(img_h) bbox_h_start = math_ops.to_int32( (img_hd - img_hd * central_fraction) / 2) img_wd = math_ops.to_double(img_w) bbox_w_start = math_ops.to_int32( (img_wd - img_wd * central_fraction) / 2) bbox_h_size = img_h - bbox_h_start * 2 bbox_w_size = img_w - bbox_w_start * 2 if rank == 3: bbox_begin = array_ops.stack([bbox_h_start, bbox_w_start, 0]) bbox_size = array_ops.stack([bbox_h_size, bbox_w_size, -1]) else: bbox_begin = array_ops.stack([0, bbox_h_start, bbox_w_start, 0]) bbox_size = array_ops.stack([-1, bbox_h_size, bbox_w_size, -1]) image = array_ops.slice(image, bbox_begin, bbox_size) # Reshape the `image` tensor to the desired size. if rank == 3: image.set_shape([None, None, img_d]) else: image.set_shape([img_bs, None, None, img_d]) return image
def convert_non_tensor(x): if x is None: raise ValueError("None values not supported") if isinstance(x, (np.ndarray, float, int)): return ops.convert_to_tensor(x) return x
def _resource_apply_dense(self, grad, var): var_dtype = var.dtype.base_dtype lr_t = self._decayed_lr(var_dtype) m = self.get_slot(var, 'm') v = self.get_slot(var, 'v') beta_1_t = self._get_hyper('beta_1', var_dtype) beta_2_t = self._get_hyper('beta_2', var_dtype) epsilon_t = ops.convert_to_tensor(self.epsilon, var_dtype) local_step = math_ops.cast(self.iterations + 1, var_dtype) beta_1_power = math_ops.pow(beta_1_t, local_step) beta_2_power = math_ops.pow(beta_2_t, local_step) if self._initial_total_steps > 0: total_steps = self._get_hyper('total_steps', var_dtype) warmup_steps = total_steps * \ self._get_hyper('warmup_proportion', var_dtype) min_lr = self._get_hyper('min_lr', var_dtype) decay_steps = K.maximum(total_steps - warmup_steps, 1) decay_rate = (min_lr - lr_t) / decay_steps lr_t = tf.where( local_step <= warmup_steps, lr_t * (local_step / warmup_steps), lr_t + decay_rate * K.minimum(local_step - warmup_steps, decay_steps), ) sma_inf = 2.0 / (1.0 - beta_2_t) - 1.0 sma_t = sma_inf - 2.0 * local_step * \ beta_2_power / (1.0 - beta_2_power) m_t = state_ops.assign(m, beta_1_t * m + (1.0 - beta_1_t) * grad, use_locking=self._use_locking) m_corr_t = m_t / (1.0 - beta_1_power) v_t = state_ops.assign(v, beta_2_t * v + (1.0 - beta_2_t) * math_ops.square(grad), use_locking=self._use_locking) if self.amsgrad: vhat = self.get_slot(var, 'vhat') vhat_t = state_ops.assign(vhat, math_ops.maximum(vhat, v_t), use_locking=self._use_locking) v_corr_t = math_ops.sqrt(vhat_t / (1.0 - beta_2_power)) else: vhat_t = None v_corr_t = math_ops.sqrt(v_t / (1.0 - beta_2_power)) r_t = math_ops.sqrt((sma_t - 4.0) / (sma_inf - 4.0) * (sma_t - 2.0) / (sma_inf - 2.0) * sma_inf / sma_t) var_t = tf.where(sma_t >= 5.0, r_t * m_corr_t / (v_corr_t + epsilon_t), m_corr_t) if self._initial_weight_decay > 0.0: var_t += self._get_hyper('weight_decay', var_dtype) * var var_update = state_ops.assign_sub(var, lr_t * var_t, use_locking=self._use_locking) updates = [var_update, m_t, v_t] if self.amsgrad: updates.append(vhat_t) return control_flow_ops.group(*updates)
def _resource_apply_sparse(self, grad, var, indices): var_dtype = var.dtype.base_dtype lr_t = self._decayed_lr(var_dtype) beta_1_t = self._get_hyper('beta_1', var_dtype) beta_2_t = self._get_hyper('beta_2', var_dtype) epsilon_t = ops.convert_to_tensor(self.epsilon, var_dtype) local_step = math_ops.cast(self.iterations + 1, var_dtype) beta_1_power = math_ops.pow(beta_1_t, local_step) beta_2_power = math_ops.pow(beta_2_t, local_step) if self._initial_total_steps > 0: total_steps = self._get_hyper('total_steps', var_dtype) warmup_steps = total_steps * self._get_hyper( 'warmup_proportion', var_dtype) min_lr = self._get_hyper('min_lr', var_dtype) lr_t = tf.where( local_step <= warmup_steps, lr_t * (local_step / warmup_steps), min_lr + (lr_t - min_lr) * (1.0 - tf.minimum(local_step, total_steps) / total_steps), ) sma_inf = 2.0 / (1.0 - beta_2_t) - 1.0 sma_t = sma_inf - 2.0 * local_step * beta_2_power / (1.0 - beta_2_power) m = self.get_slot(var, 'm') m_scaled_g_values = grad * (1 - beta_1_t) m_t = state_ops.assign(m, m * beta_1_t, use_locking=self._use_locking) with ops.control_dependencies([m_t]): m_t = self._resource_scatter_add(m, indices, m_scaled_g_values) m_corr_t = m_t / (1.0 - beta_1_power) v = self.get_slot(var, 'v') v_scaled_g_values = (grad * grad) * (1 - beta_2_t) v_t = state_ops.assign(v, v * beta_2_t, use_locking=self._use_locking) with ops.control_dependencies([v_t]): v_t = self._resource_scatter_add(v, indices, v_scaled_g_values) if self.amsgrad: vhat = self.get_slot(var, 'vhat') vhat_t = state_ops.assign(vhat, math_ops.maximum(vhat, v_t), use_locking=self._use_locking) v_corr_t = math_ops.sqrt(vhat_t / (1.0 - beta_2_power) + epsilon_t) else: v_corr_t = math_ops.sqrt(v_t / (1.0 - beta_2_power) + epsilon_t) r_t = math_ops.sqrt((sma_t - 4.0) / (sma_inf - 4.0) * (sma_t - 2.0) / (sma_inf - 2.0) * sma_inf / sma_t) var_t = tf.where(sma_t > 5.0, r_t * m_corr_t / v_corr_t, m_corr_t) if self._initial_weight_decay > 0.0: var_t += self._get_hyper('weight_decay', var_dtype) * var var_update = state_ops.assign_sub(var, lr_t * var_t, use_locking=self._use_locking) updates = [var_update, m_t, v_t] if self.amsgrad: updates.append(vhat_t) return control_flow_ops.group(*updates)
def hard_sigmoid(x): x = 0.2 * x + 0.5 zeros = ops.convert_to_tensor(0., dtype=x.dtype.base_dtype) ones = ops.convert_to_tensor(1., dtype=x.dtype.base_dtype) return clip_ops.clip_by_value(x, zeros, ones)
def pad_to_bounding_box(image, offset_height, offset_width, target_height, target_width, dynamic_shape=False): """Pad `image` with zeros to the specified `height` and `width`. Adds `offset_height` rows of zeros on top, `offset_width` columns of zeros on the left, and then pads the image on the bottom and right with zeros until it has dimensions `target_height`, `target_width`. This op does nothing if `offset_*` is zero and the image already has size `target_height` by `target_width`. Args: image: 3-D tensor with shape `[height, width, channels]` offset_height: Number of rows of zeros to add on top. offset_width: Number of columns of zeros to add on the left. target_height: Height of output image. target_width: Width of output image. dynamic_shape: Whether the input image has undertermined shape. If set to `True`, shape information will be retrieved at run time. Default to `False`. Returns: 3-D tensor of shape `[target_height, target_width, channels]` Raises: ValueError: If the shape of `image` is incompatible with the `offset_*` or `target_*` arguments, and `dynamic_shape` is set to `False`. """ image = ops.convert_to_tensor(image, name='image') _Check3DImage(image, require_static=(not dynamic_shape)) height, width, depth = _ImageDimensions(image, dynamic_shape=dynamic_shape) after_padding_width = target_width - offset_width - width after_padding_height = target_height - offset_height - height if not dynamic_shape: if target_width < width: raise ValueError('target_width must be >= width') if target_height < height: raise ValueError('target_height must be >= height') if after_padding_width < 0: raise ValueError('target_width not possible given ' 'offset_width and image width') if after_padding_height < 0: raise ValueError('target_height not possible given ' 'offset_height and image height') # Do not pad on the depth dimensions. if (dynamic_shape or offset_width or offset_height or after_padding_width or after_padding_height): paddings = array_ops.reshape( array_ops.pack([offset_height, after_padding_height, offset_width, after_padding_width, 0, 0]), [3, 2]) padded = array_ops.pad(image, paddings) if not dynamic_shape: padded.set_shape([target_height, target_width, depth]) else: padded = image return padded
def resize_image_with_crop_or_pad(image, target_height, target_width, dynamic_shape=False): """Crops and/or pads an image to a target width and height. Resizes an image to a target width and height by either centrally cropping the image or padding it evenly with zeros. If `width` or `height` is greater than the specified `target_width` or `target_height` respectively, this op centrally crops along that dimension. If `width` or `height` is smaller than the specified `target_width` or `target_height` respectively, this op centrally pads with 0 along that dimension. Args: image: 3-D tensor of shape [height, width, channels] target_height: Target height. target_width: Target width. dynamic_shape: Whether the input image has undertermined shape. If set to `True`, shape information will be retrieved at run time. Default to `False`. Raises: ValueError: if `target_height` or `target_width` are zero or negative. Returns: Cropped and/or padded image of shape `[target_height, target_width, channels]` """ image = ops.convert_to_tensor(image, name='image') _Check3DImage(image, require_static=(not dynamic_shape)) original_height, original_width, _ = _ImageDimensions(image, dynamic_shape=dynamic_shape) if target_width <= 0: raise ValueError('target_width must be > 0.') if target_height <= 0: raise ValueError('target_height must be > 0.') if dynamic_shape: max_ = math_ops.maximum min_ = math_ops.minimum else: max_ = max min_ = min width_diff = target_width - original_width offset_crop_width = max_(-width_diff // 2, 0) offset_pad_width = max_(width_diff // 2, 0) height_diff = target_height - original_height offset_crop_height = max_(-height_diff // 2, 0) offset_pad_height = max_(height_diff // 2, 0) # Maybe crop if needed. cropped = crop_to_bounding_box(image, offset_crop_height, offset_crop_width, min_(target_height, original_height), min_(target_width, original_width), dynamic_shape=dynamic_shape) # Maybe pad if needed. resized = pad_to_bounding_box(cropped, offset_pad_height, offset_pad_width, target_height, target_width, dynamic_shape=dynamic_shape) if resized.get_shape().ndims is None: raise ValueError('resized contains no shape.') if not resized.get_shape()[0].is_compatible_with(target_height): raise ValueError('resized height is not correct.') if not resized.get_shape()[1].is_compatible_with(target_width): raise ValueError('resized width is not correct.') return resized