我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tensorflow.greater_equal()。
def _filter_negative_samples(labels, tensors): """keeps only samples with none-negative labels Params: ----- labels: of shape (N,) tensors: a list of tensors, each of shape (N, .., ..) the first axis is sample number Returns: ----- tensors: filtered tensors """ # return tensors keeps = tf.where(tf.greater_equal(labels, 0)) keeps = tf.reshape(keeps, [-1]) filtered = [] for t in tensors: tf.assert_equal(tf.shape(t)[0], tf.shape(labels)[0]) f = tf.gather(t, keeps) filtered.append(f) return filtered
def __init__(self, preds, labels, model, num_nodes, pos_weight, norm): preds_sub = preds labels_sub = labels self.cost = norm * tf.reduce_mean(tf.nn.weighted_cross_entropy_with_logits(logits=preds_sub, targets=labels_sub, pos_weight=pos_weight)) self.optimizer = tf.train.AdamOptimizer(learning_rate=FLAGS.learning_rate) # Adam Optimizer # Latent loss self.log_lik = self.cost self.kl = (0.5 / num_nodes) * tf.reduce_mean(tf.reduce_sum(1 + 2 * model.z_log_std - tf.square(model.z_mean) - tf.square(tf.exp(model.z_log_std)), 1)) self.cost -= self.kl self.opt_op = self.optimizer.minimize(self.cost) self.grads_vars = self.optimizer.compute_gradients(self.cost) self.correct_prediction = tf.equal(tf.cast(tf.greater_equal(tf.sigmoid(preds_sub), 0.5), tf.int32), tf.cast(labels_sub, tf.int32)) self.accuracy = tf.reduce_mean(tf.cast(self.correct_prediction, tf.float32))
def traditional_transition_loss_pred(self, i, j, combined_head, combined_dep): rel_trans_feat_ids = self.trans_feat_ids[i*self.args.beam_size+j] if not self.train else self.trans_feat_ids[i, j] rel_head = tf.reshape(tf.gather(combined_head, rel_trans_feat_ids[:4]), [4, self.args.rel_emb_dim]) rel_dep = tf.reshape(tf.gather(combined_dep, rel_trans_feat_ids[:4]), [4, self.args.rel_emb_dim]) mask = tf.cast(tf.reshape(tf.greater_equal(rel_trans_feat_ids[:4], 0), [4,1]), tf.float32) rel_head = tf.multiply(mask, rel_head) rel_dep = tf.multiply(mask, rel_dep) rel_hid = self.rel_merge(rel_head, rel_dep) rel_logit = self.rel_dense(tf.reshape(rel_hid, [1, -1])) rel_logit = tf.reshape(rel_logit, [-1]) log_partition = tf.reduce_logsumexp(rel_logit) if self.train: res = log_partition - rel_logit[self.trans_labels[i, j]] return res else: arc_pred = log_partition - rel_logit return arc_pred
def _crop(image, offset_height, offset_width, crop_height, crop_width): original_shape = tf.shape(image) rank_assertion = tf.Assert( tf.equal(tf.rank(image), 3), ['Rank of image must be equal to 3.']) cropped_shape = control_flow_ops.with_dependencies( [rank_assertion], tf.stack([crop_height, crop_width, original_shape[2]])) size_assertion = tf.Assert( tf.logical_and( tf.greater_equal(original_shape[0], crop_height), tf.greater_equal(original_shape[1], crop_width)), ['Crop size greater than the image size.']) offsets = tf.to_int32(tf.stack([offset_height, offset_width, 0])) # Use tf.slice instead of crop_to_bounding box as it accepts tensors to # define the crop size. image = control_flow_ops.with_dependencies([size_assertion], tf.slice(image, offsets, cropped_shape)) return tf.reshape(image, cropped_shape)
def bboxes_filter_labels(labels, bboxes, out_labels=[], num_classes=np.inf, scope=None): """Filter out labels from a collection. Typically used to get of DontCare elements. Also remove elements based on the number of classes. Return: labels, bboxes: Filtered elements. """ with tf.name_scope(scope, 'bboxes_filter_labels', [labels, bboxes]): mask = tf.greater_equal(labels, num_classes) for l in labels: mask = tf.logical_and(mask, tf.not_equal(labels, l)) labels = tf.boolean_mask(labels, mask) bboxes = tf.boolean_mask(bboxes, mask) return labels, bboxes # =========================================================================== # # Standard boxes computation. # =========================================================================== #
def average_precision_voc07(precision, recall, name=None): """Compute (interpolated) average precision from precision and recall Tensors. The implementation follows Pascal 2007 guidelines. See also: https://sanchom.wordpress.com/tag/average-precision/ """ with tf.name_scope(name, 'average_precision_voc07', [precision, recall]): # Convert to float64 to decrease error on cumulated sums. precision = tf.cast(precision, dtype=tf.float64) recall = tf.cast(recall, dtype=tf.float64) # Add zero-limit value to avoid any boundary problem... precision = tf.concat([precision, [0.]], axis=0) recall = tf.concat([recall, [np.inf]], axis=0) # Split the integral into 10 bins. l_aps = [] for t in np.arange(0., 1.1, 0.1): mask = tf.greater_equal(recall, t) v = tf.reduce_max(tf.boolean_mask(precision, mask)) l_aps.append(v / 11.) ap = tf.add_n(l_aps) return ap
def _crop(image, offset_height, offset_width, crop_height, crop_width): original_shape = tf.shape(image) rank_assertion = tf.Assert( tf.equal(tf.rank(image), 3), ['Rank of image must be equal to 3.']) cropped_shape = control_flow_ops.with_dependencies( [rank_assertion], tf.stack([crop_height, crop_width, original_shape[2]])) size_assertion = tf.Assert( tf.logical_and( tf.greater_equal(original_shape[0], crop_height), tf.greater_equal(original_shape[1], crop_width)), ['Crop size greater than the image size.']) offsets = tf.to_int32(tf.stack([offset_height, offset_width, 0])) # Use tf.slice instead of crop_to_bounding box as it accepts tensors to # define the crop size. image = control_flow_ops.with_dependencies( [size_assertion], tf.slice(image, offsets, cropped_shape)) return tf.reshape(image, cropped_shape)
def next_inputs(self, time, sample_ids=None, prev_finished=None): if sample_ids is None or self.teacher_rate > 0.: finished = tf.greater_equal(time + 1, self.sequence_length) else: finished = math_ops.logical_or( tf.greater_equal(time + 1, self.max_step), tf.equal(self.eos_id, sample_ids)) if self.teacher_rate == 1. or (sample_ids is None): next_input_ids = self._input_tas.read(time) return finished, self.lookup(next_input_ids) if self.teacher_rate > 0.: # scheduled teacher_rates = tf.less_equal( tf.random_uniform(tf.shape(sample_ids), minval=0., maxval=1.), self.teacher_rate) teacher_rates = tf.to_int32(teacher_rates) next_input_ids = (teacher_rates * self._input_tas.read(time) + (1 - teacher_rates) * sample_ids) else: next_input_ids = sample_ids return finished, self.lookup(next_input_ids)
def sample(self, logits, time): rl_time_steps = tf.floordiv(tf.maximum(self.global_step_tensor - self.burn_in_step, 0), self.increment_step) start_rl_step = self.sequence_length - rl_time_steps next_input_ids = tf.cond( tf.greater_equal(time, self.max_sequence_length), lambda: tf.tile([self.eos_id], [self.batch_size]), lambda: self._input_tas.read(time)) next_predicted_ids = tf.squeeze(tf.multinomial(logits, 1), axis=[-1]) mask = tf.to_int32(time >= start_rl_step) return (1 - mask) * tf.to_int32(next_input_ids) + mask * tf.to_int32( next_predicted_ids)
def atan2(x, y, epsilon = 1.0e-12): """ A hack until the TensorFlow developers implement a function that can find the angle from an x and y co- ordinate. :param x: :param epsilon: :return: """ # Add a small number to all zeros, to avoid division by zero: x = tf.where(tf.equal(x, 0.0), x + epsilon, x) y = tf.where(tf.equal(y, 0.0), y + epsilon, y) angle = tf.where(tf.greater(x, 0.0), tf.atan(y / x), tf.zeros_like(x)) angle = tf.where(tf.logical_and(tf.less(x, 0.0), tf.greater_equal(y, 0.0)), tf.atan(y / x) + np.pi, angle) angle = tf.where(tf.logical_and(tf.less(x, 0.0), tf.less(y, 0.0)), tf.atan(y / x) - np.pi, angle) angle = tf.where(tf.logical_and(tf.equal(x, 0.0), tf.greater(y, 0.0)), 0.5 * np.pi * tf.ones_like(x), angle) angle = tf.where(tf.logical_and(tf.equal(x, 0.0), tf.less(y, 0.0)), -0.5 * np.pi * tf.ones_like(x), angle) angle = tf.where(tf.logical_and(tf.equal(x, 0.0), tf.equal(y, 0.0)), tf.zeros_like(x), angle) return angle # List of faces for consistent ordering.
def _prepare_image(self, image): """Resize the image to a maximum height of `self.height` and maximum width of `self.width` while maintaining the aspect ratio. Pad the resized image to a fixed size of ``[self.height, self.width]``.""" img = tf.image.decode_png(image, channels=self.channels) dims = tf.shape(img) self.width = self.max_width max_width = tf.to_int32(tf.ceil(tf.truediv(dims[1], dims[0]) * self.height_float)) max_height = tf.to_int32(tf.ceil(tf.truediv(self.width, max_width) * self.height_float)) resized = tf.cond( tf.greater_equal(self.width, max_width), lambda: tf.cond( tf.less_equal(dims[0], self.height), lambda: tf.to_float(img), lambda: tf.image.resize_images(img, [self.height, max_width], method=tf.image.ResizeMethod.BICUBIC), ), lambda: tf.image.resize_images(img, [max_height, self.width], method=tf.image.ResizeMethod.BICUBIC) ) padded = tf.image.pad_to_bounding_box(resized, 0, 0, self.height, self.width) return padded
def block_shrinkage_conv(V,mu,rho): coef = 0.5 V_shape = tf.shape(V); one_val = tf.constant(1.0) b = tf.div(mu,rho) V_shape1 = tf.concat(0,[tf.mul(tf.slice(V_shape,[2],[1]),tf.slice(V_shape,[3],[1])),tf.mul(tf.slice(V_shape,[0],[1]),tf.slice(V_shape,[1],[1]))]) V = tf.reshape(tf.transpose(V,perm=[2,3,0,1]),V_shape1) norm_V = frobenius_norm_block(V,1) norm_V_per_dimension = tf.div(norm_V,tf.cast(tf.slice(V_shape1,[1],[1]),'float')) zero_part = tf.zeros(V_shape1) zero_ind = tf.greater_equal(b,norm_V_per_dimension) num_zero = tf.reduce_sum(tf.cast(zero_ind,'float')) # f4 = lambda: tf.greater_equal(tf.truediv(tf.add(tf.reduce_min(fro),tf.reduce_mean(fro)),2.0),fro) f4 = lambda: tf.greater_equal(tf.reduce_mean(norm_V),norm_V) f5 = lambda: zero_ind zero_ind = tf.cond(tf.greater(num_zero,tf.mul(coef,tf.cast(V_shape1[0],'float'))),f4,f5) G = tf.select(zero_ind,zero_part,tf.mul(tf.sub(one_val,tf.div(b,tf.reshape(norm_V,[-1,1]))),V)) G_shape = tf.concat(0,[tf.slice(V_shape,[2],[1]),tf.slice(V_shape,[3],[1]),tf.slice(V_shape,[0],[1]),tf.slice(V_shape,[1],[1])]) G = tf.transpose(tf.reshape(G,G_shape),perm=[2,3,0,1]) return G,zero_ind
def block_truncate_conv(V,mu,rho): coef = 0.5 V_shape = tf.shape(V) b = tf.sqrt(tf.div(tf.mul(2.,mu),rho)) #threshold # Reshape the 4D tensor of weights to a 2D matrix with rows containing the conv filters in vectorized form. V_shape1 = tf.concat(0,[tf.mul(tf.slice(V_shape,[2],[1]),tf.slice(V_shape,[3],[1])),tf.mul(tf.slice(V_shape,[0],[1]),tf.slice(V_shape,[1],[1]))]) V = tf.reshape(tf.transpose(V,perm=[2,3,0,1]),V_shape1) norm_V = frobenius_norm_block(V,1) norm_V_per_dimension = tf.div(norm_V,tf.cast(tf.slice(V_shape1,[1],[1]),'float')) # Implementation of Eq.10 in the paper using if condition inside the TensorFlow graph with tf.cond zero_part = tf.zeros(V_shape1) zero_ind = tf.greater_equal(b,norm_V_per_dimension) num_zero = tf.reduce_sum(tf.cast(zero_ind,'float')) # You can pass parameters to the functions in tf.cond() using lambda f4 = lambda: tf.greater_equal(tf.reduce_mean(norm_V),norm_V) f5 = lambda: zero_ind zero_ind = tf.cond(tf.greater(num_zero,tf.mul(coef,tf.cast(V_shape1[0],'float'))),f4,f5) G = tf.select(zero_ind,zero_part,V) G_shape = tf.concat(0,[tf.slice(V_shape,[2],[1]),tf.slice(V_shape,[3],[1]),tf.slice(V_shape,[0],[1]),tf.slice(V_shape,[1],[1])]) G = tf.transpose(tf.reshape(G,G_shape),perm=[2,3,0,1]) return G,zero_ind
def __call__(self, x, deterministic, train_clip=False, thresh=3): # Alpha is the dropout rate log_alpha = clip(self.log_sigma2 - tf.log(self.W**2 + eps)) # Values of log_alpha that are above the threshold clip_mask = tf.greater_equal(log_alpha, thresh) def true_path(): # For inference # If log_alpha >= thresh, return 0 # If log_alpha < thresh, return tf.matmul(x,self.W) return tf.matmul(x, tf.where(clip_mask, tf.zeros_like(self.W), self.W)) def false_path(): # For training # Sample from a normal distribution centred on tf.matmul(x,W) # and with variance roughly proportional to the size of tf.matmul(x,W)*tf.exp(log_alpha) W = self.W if train_clip: raise NotImplementedError mu = tf.matmul(x,W) si = tf.matmul(x*x, tf.exp(log_alpha) * self.W * self.W) si = tf.sqrt(si + eps) return mu + tf.random_normal(tf.shape(mu), mean=0.0, stddev=1.0) * si h = tf.cond(deterministic, true_path, false_path) return self.nonlinearity(h + self.b)
def __call__(self, x, deterministic, train_clip=False, thresh=3): # Alpha is the dropout rate log_alpha = clip(self.log_sigma2 - tf.log(self.W**2 + eps)) # Values of log_alpha that are above the threshold clip_mask = tf.greater_equal(log_alpha, thresh) def true_path(): # For inference return tf.nn.conv2d(x, tf.where(clip_mask, tf.zeros_like(self.W), self.W), strides=self.strides, padding=self.padding) def false_path(): # For training W = self.W if train_clip: raise NotImplementedError mu = tf.nn.conv2d(x, W, strides=self.strides, padding=self.padding) si = tf.nn.conv2d(x*x, tf.exp(log_alpha) * W*W, strides=self.strides, padding=self.padding) si = tf.sqrt(si + eps) return mu + tf.random_normal(tf.shape(mu), mean=0.0, stddev=1.0) * si h = tf.cond(deterministic, true_path, false_path) return self.nonlinearity(h + self.b)
def normal_ccdf(x, mu, sigma2): """Normal CCDF""" # Check for degenerate distributions when sigma2 == 0 # if x >= mu, n = 0 # if x < mu, n = 1 # sigma2_le_0 = tf.less_equal(sigma2, 0.) # x_gte_mu = tf.greater_equal(x, mu) # x_lt_mu = tf.less(x, mu) # Never divide by zero, instead the logic below handles degenerate distribution cases # sigma2 = tf.cond(sigma2_le_0, lambda: tf.ones_like(sigma2), lambda: sigma2) p = (1. - 0.5 * (1. + tf.erf((x - mu) / tf.sqrt(2. * sigma2)))) # p = tf.cond(tf.logical_and(sigma2_le_0, x_gte_mu), lambda: tf.zeros_like(p), lambda: p) # p = tf.cond(tf.logical_and(sigma2_le_0, x_lt_mu), lambda: tf.ones_like(p), lambda: p) return p
def testRandomPixelValueScale(self): preprocessing_options = [] preprocessing_options.append((preprocessor.normalize_image, { 'original_minval': 0, 'original_maxval': 255, 'target_minval': 0, 'target_maxval': 1 })) preprocessing_options.append((preprocessor.random_pixel_value_scale, {})) images = self.createTestImages() tensor_dict = {fields.InputDataFields.image: images} tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) images_min = tf.to_float(images) * 0.9 / 255.0 images_max = tf.to_float(images) * 1.1 / 255.0 images = tensor_dict[fields.InputDataFields.image] values_greater = tf.greater_equal(images, images_min) values_less = tf.less_equal(images, images_max) values_true = tf.fill([1, 4, 4, 3], True) with self.test_session() as sess: (values_greater_, values_less_, values_true_) = sess.run( [values_greater, values_less, values_true]) self.assertAllClose(values_greater_, values_true_) self.assertAllClose(values_less_, values_true_)
def prune_small_boxes(boxlist, min_side, scope=None): """Prunes small boxes in the boxlist which have a side smaller than min_side. Args: boxlist: BoxList holding N boxes. min_side: Minimum width AND height of box to survive pruning. scope: name scope. Returns: A pruned boxlist. """ with tf.name_scope(scope, 'PruneSmallBoxes'): height, width = height_width(boxlist) is_valid = tf.logical_and(tf.greater_equal(width, min_side), tf.greater_equal(height, min_side)) return gather(boxlist, tf.reshape(tf.where(is_valid), [-1]))
def __init__(self, preds, labels, pos_weight, norm): preds_sub = preds labels_sub = labels self.cost = norm * tf.reduce_mean(tf.nn.weighted_cross_entropy_with_logits(logits=preds_sub, targets=labels_sub, pos_weight=pos_weight)) self.optimizer = tf.train.AdamOptimizer(learning_rate=FLAGS.learning_rate) # Adam Optimizer self.opt_op = self.optimizer.minimize(self.cost) self.grads_vars = self.optimizer.compute_gradients(self.cost) self.correct_prediction = tf.equal(tf.cast(tf.greater_equal(tf.sigmoid(preds_sub), 0.5), tf.int32), tf.cast(labels_sub, tf.int32)) self.accuracy = tf.reduce_mean(tf.cast(self.correct_prediction, tf.float32))
def segment_sample_select(probs, segment_ids): num_segments = tf.reduce_max(segment_ids) + 1 sampled = tf.random_uniform([num_segments]) def scan_fn(acc, x): p, i = x[0], x[1] prev_v = tf.gather(acc[0], i) new_probs = acc[0] + tf.one_hot(i, num_segments, p) select = tf.logical_and(tf.less(prev_v, 0.0), tf.greater_equal(prev_v + p, 0.0)) return new_probs, select _, selection = tf.scan(scan_fn, (probs, segment_ids), initializer=(-sampled, False)) return selection
def greater_equal(x, y): '''Element-wise truth value of (x >= y). Returns a bool tensor. ''' return tf.greater_equal(x, y)
def apply_stats(self, statsUpdates): """ compute stats and update/apply the new stats to the running average """ def updateAccumStats(): if self._full_stats_init: return tf.cond(tf.greater(self.sgd_step, self._cold_iter), lambda: tf.group(*self._apply_stats(statsUpdates, accumulate=True, accumulateCoeff=1. / self._stats_accum_iter)), tf.no_op) else: return tf.group(*self._apply_stats(statsUpdates, accumulate=True, accumulateCoeff=1. / self._stats_accum_iter)) def updateRunningAvgStats(statsUpdates, fac_iter=1): # return tf.cond(tf.greater_equal(self.factor_step, # tf.convert_to_tensor(fac_iter)), lambda: # tf.group(*self._apply_stats(stats_list, varlist)), tf.no_op) return tf.group(*self._apply_stats(statsUpdates)) if self._async_stats: # asynchronous stats update update_stats = self._apply_stats(statsUpdates) queue = tf.FIFOQueue(1, [item.dtype for item in update_stats], shapes=[ item.get_shape() for item in update_stats]) enqueue_op = queue.enqueue(update_stats) def dequeue_stats_op(): return queue.dequeue() self.qr_stats = tf.train.QueueRunner(queue, [enqueue_op]) update_stats_op = tf.cond(tf.equal(queue.size(), tf.convert_to_tensor( 0)), tf.no_op, lambda: tf.group(*[dequeue_stats_op(), ])) else: # synchronous stats update update_stats_op = tf.cond(tf.greater_equal( self.stats_step, self._stats_accum_iter), lambda: updateRunningAvgStats(statsUpdates), updateAccumStats) self._update_stats_op = update_stats_op return update_stats_op
def __ge__(self, other): return tf.greater_equal(self, other) # slicing and indexing
def multi_label(prediction_batch, labels_batch, threshold=0.5, moving_average=True): with tf.variable_scope('metrics'): threshold_graph = tf.constant(threshold, name='threshold') zero_point_five = tf.constant(0.5) predicted_bool = tf.greater_equal(prediction_batch, threshold_graph) real_bool = tf.greater_equal(labels_batch, zero_point_five) return _metrics(predicted_bool, real_bool, moving_average)
def remove_padding(self, input_text): # calculate max length of the input_text mask = tf.greater_equal(input_text, 0) # true for words false for padding sequence_length = tf.reduce_sum(tf.cast(mask, tf.int32), 1) # truncate the input text to max length max_sequence_length = tf.reduce_max(sequence_length) input_text_length = tf.shape(input_text)[1] empty_padding_lenght = input_text_length - max_sequence_length input_text, _ = tf.split(input_text, [max_sequence_length, empty_padding_lenght], axis=1) return input_text, sequence_length
def finished(self, time, output): """Check which sentences are finished. Arguments: time: a `Tensor` of rank `0D` (i.e. a scalar) with the 0-based value of the current step in the loop. output: a `Tensor` of rank `2D` and shape `[batch_size, num_classes]` representing the current output of the model, i.e. abatch of probability distribution estimations over the output classes. Returns: a `Tensor` of shape `[batch_size]` of `tf.bool` elements, indicating for each position if the corresponding sequence has terminated or not. A sequence is has terminated if the current step is greater or equal the number of steps allowed (defined in the `lengths` input argument) and if the `argmax` over the output probability distribution ends up in the class that has id equal to the `EOS` symbol (if provided). """ length = time + 1 finished = tf.greater_equal(length, self._lengths) if finished.get_shape().ndims == 0: batch = [utils.get_dimension(output, 0)] finished = tf.tile([finished], batch) if self._EOS is not None: ids = tf.cast(tf.argmax(output, axis=-1), tf.int32) eos = tf.equal(ids, self._EOS) finished = tf.logical_or(finished, eos) return finished
def test_iterations(self): """Test the number of iterations.""" lengths = tf.constant([1, 2, 3], dtype=tf.int32) def _helper_finished(time, _): return tf.greater_equal(time + 1, lengths) helper = mock.Mock() helper.finished.side_effect = _helper_finished batch_size = utils.get_dimension(lengths, 0) inp_size, state_size, output_size = 2, 5, 2 decoder = mock.Mock() decoder.init_input.side_effect = lambda: tf.zeros([batch_size, inp_size]) decoder.init_state.side_effect = lambda: tf.ones([batch_size, state_size]) decoder.zero_output.side_effect = lambda: tf.zeros([batch_size, output_size]) decoder.step.side_effect = lambda t, i, s:\ ((i + 1), 3 * (i + 1), (s + 2), tf.tile([False], [batch_size])) output_exp = np.asarray( [[[1, 1], [0, 0], [0, 0]], [[1, 1], [4, 4], [0, 0]], [[1, 1], [4, 4], [13, 13]]], dtype=np.float32) # pylint: disable=E1101,I0011 state_exp = np.asarray( [[7, 7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7]], dtype=np.float32) # pylint: disable=E1101,I0011 dyndec = layers.DynamicDecoder(decoder, helper) output_t, state_t = dyndec.decode() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) output_act, state_act = sess.run([output_t, state_t]) self.assertAllEqual(output_exp, output_act) self.assertAllEqual(state_exp, state_act)
def optimize(self, G_loss, D_Y_loss, F_loss, D_X_loss): def make_optimizer(loss, variables, name='Adam'): """ Adam optimizer with learning rate 0.0002 for the first 100k steps (~100 epochs) and a linearly decaying rate that goes to zero over the next 100k steps """ global_step = tf.Variable(0, trainable=False) starter_learning_rate = self.learning_rate end_learning_rate = 0.0 start_decay_step = 100000 decay_steps = 100000 beta1 = self.beta1 learning_rate = ( tf.where( tf.greater_equal(global_step, start_decay_step), tf.train.polynomial_decay(starter_learning_rate, global_step-start_decay_step, decay_steps, end_learning_rate, power=1.0), starter_learning_rate ) ) tf.summary.scalar('learning_rate/{}'.format(name), learning_rate) learning_step = ( tf.train.AdamOptimizer(learning_rate, beta1=beta1, name=name) .minimize(loss, global_step=global_step, var_list=variables) ) return learning_step G_optimizer = make_optimizer(G_loss, self.G.variables, name='Adam_G') D_Y_optimizer = make_optimizer(D_Y_loss, self.D_Y.variables, name='Adam_D_Y') F_optimizer = make_optimizer(F_loss, self.F.variables, name='Adam_F') D_X_optimizer = make_optimizer(D_X_loss, self.D_X.variables, name='Adam_D_X') with tf.control_dependencies([G_optimizer, D_Y_optimizer, F_optimizer, D_X_optimizer]): return tf.no_op(name='optimizers')
def greater_equal(x, y): """Element-wise truth value of (x >= y). # Returns A bool tensor. """ return tf.greater_equal(x, y)
def _get_valid_sample_fraction(labels, p=0): """return fraction of non-negative examples, the ignored examples have been marked as negative""" num_valid = tf.reduce_sum(tf.cast(tf.greater_equal(labels, p), tf.float32)) num_example = tf.cast(tf.size(labels), tf.float32) frac = tf.cond(tf.greater(num_example, 0), lambda:num_valid / num_example, lambda: tf.cast(0, tf.float32)) frac_ = tf.cond(tf.greater(num_valid, 0), lambda:num_example / num_valid, lambda: tf.cast(0, tf.float32)) return frac, frac_
def _crop(image, offset_height, offset_width, crop_height, crop_width): """Crops the given image using the provided offsets and sizes. Note that the method doesn't assume we know the input image size but it does assume we know the input image rank. Args: image: an image of shape [height, width, channels]. offset_height: a scalar tensor indicating the height offset. offset_width: a scalar tensor indicating the width offset. crop_height: the height of the cropped image. crop_width: the width of the cropped image. Returns: the cropped (and resized) image. Raises: InvalidArgumentError: if the rank is not 3 or if the image dimensions are less than the crop size. """ original_shape = tf.shape(image) rank_assertion = tf.Assert( tf.equal(tf.rank(image), 3), ['Rank of image must be equal to 3.']) with tf.control_dependencies([rank_assertion]): cropped_shape = tf.stack([crop_height, crop_width, original_shape[2]]) size_assertion = tf.Assert( tf.logical_and( tf.greater_equal(original_shape[0], crop_height), tf.greater_equal(original_shape[1], crop_width)), ['Crop size greater than the image size.']) offsets = tf.to_int32(tf.stack([offset_height, offset_width, 0])) # Use tf.slice instead of crop_to_bounding box as it accepts tensors to # define the crop size. with tf.control_dependencies([size_assertion]): image = tf.slice(image, offsets, cropped_shape) return tf.reshape(image, cropped_shape)
def _crop(self, image, offset_height, offset_width, crop_height, crop_width): """Crops the given image using the provided offsets and sizes. Note that the method doesn't assume we know the input image size but it does assume we know the input image rank. Args: image: an image of shape [height, width, channels]. offset_height: a scalar tensor indicating the height offset. offset_width: a scalar tensor indicating the width offset. crop_height: the height of the cropped image. crop_width: the width of the cropped image. Returns: the cropped (and resized) image. Raises: InvalidArgumentError: if the rank is not 3 or if the image dimensions are less than the crop size. """ original_shape = tf.shape(image) rank_assertion = tf.Assert( tf.equal(tf.rank(image), 3), ['Rank of image must be equal to 3.']) with tf.control_dependencies([rank_assertion]): cropped_shape = tf.stack( [crop_height, crop_width, original_shape[2]]) size_assertion = tf.Assert( tf.logical_and( tf.greater_equal(original_shape[0], crop_height), tf.greater_equal(original_shape[1], crop_width)), ['Crop size greater than the image size.']) offsets = tf.to_int32(tf.stack([offset_height, offset_width, 0])) with tf.control_dependencies([size_assertion]): image = tf.slice(image, offsets, cropped_shape) return tf.reshape(image, cropped_shape)
def crop_to_fixed_size(img_tensor,annotation_tensor,output_shape): """ the output_shape must be smaller than the input_shape :param img_tensor: [w,h,depth] :param annotation_tensor: [w,h,1] :param output_shape: :param mask_out_num: :return: (output_shape,output_shape,3) (output_shape,output_shape,1) """ original_shape = tf.shape(img_tensor) crop_width, crop_height = output_shape[0],output_shape[1] image_width, image_height = original_shape[0],original_shape[1] img_cropped_shape = tf.stack([output_shape[0], output_shape[1], 3]) annotate_cropped_shape = tf.stack([output_shape[0], output_shape[1], 1]) size_assertion = tf.Assert( tf.logical_and( tf.greater_equal(original_shape[0], crop_width), tf.greater_equal(original_shape[1], crop_height)), ['Crop size greater than the image size.']) max_offset_height = tf.reshape(image_height - crop_height + 1, []) max_offset_width = tf.reshape(image_width - crop_width + 1, []) offset_height = tf.random_uniform( [], maxval=max_offset_height, dtype=tf.int32) offset_width = tf.random_uniform( [], maxval=max_offset_width, dtype=tf.int32) offsets = tf.to_int32(tf.stack([offset_width, offset_height, 0])) annotation_tensor = tf.to_int32(annotation_tensor) # Use tf.slice instead of crop_to_bounding box as it accepts tensors to # define the crop size. with tf.control_dependencies([size_assertion]): image = tf.slice(img_tensor, offsets, img_cropped_shape) annotate = tf.slice(annotation_tensor,offsets,annotate_cropped_shape) return tf.reshape(image, img_cropped_shape),tf.reshape(annotate,annotate_cropped_shape)
def crop_or_resize_to_fixed_size_and_rotate_output(img_tensor, annotation_tensor, output_shape, mask_out_num=None): """Returns tensor of a size (output_shape, output_shape, depth) and (output_shape, output_shape, 1). The function returns tensor that is of a size (output_shape, output_shape, depth) which is randomly cropped and rotate Parameters ---------- img_tensor : Tensor of size (width, height, depth) Tensor with image annotation_tensor : Tensor of size (width, height, 1) Tensor with respective annotation output_shape : Tensor or list [int, int] Tensor of list representing desired output shape mask_out_number : int Number representing the mask out value. Returns ------- cropped_padded_img : Tensor of size (output_shape[0], output_shape[1], 3). Image Tensor that was randomly scaled cropped_padded_annotation : Tensor of size (output_shape[0], output_shape[1], 1) Respective annotation Tensor that was randomly scaled with the same parameters """ input_shape = tf.shape(img_tensor)[0:2] image_width, image_height = input_shape[0],input_shape[1] crop_width, crop_height = output_shape[0],output_shape[1] cropped_padded_img,cropped_padded_annotaion = control_flow_ops.cond( tf.logical_and( tf.greater_equal(image_height, crop_height), tf.greater_equal(image_width, crop_width)), fn1=lambda:crop_to_fixed_size(img_tensor,annotation_tensor,output_shape), fn2=lambda:resize_to_fixed_size(img_tensor,annotation_tensor,output_shape,mask_out_num=mask_out_num)) return cropped_padded_img,cropped_padded_annotaion
def training_control(global_step, print_span, evaluation_span, max_step, name=None): with tf.name_scope(name, "training_control"): return { "step": global_step, "time_to_print": tf.equal(tf.mod(global_step, print_span), 0), "time_to_evaluate": tf.equal(tf.mod(global_step, evaluation_span), 0), "time_to_stop": tf.greater_equal(global_step, max_step), }