我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tensorflow.minimum()。
def log_variable(variable, gradient=None): r''' We introduce a function for logging a tensor variable's current state. It logs scalar values for the mean, standard deviation, minimum and maximum. Furthermore it logs a histogram of its state and (if given) of an optimization gradient. ''' name = variable.name mean = tf.reduce_mean(variable) tf.summary.scalar(name='%s/mean' % name, tensor=mean) tf.summary.scalar(name='%s/sttdev' % name, tensor=tf.sqrt(tf.reduce_mean(tf.square(variable - mean)))) tf.summary.scalar(name='%s/max' % name, tensor=tf.reduce_max(variable)) tf.summary.scalar(name='%s/min' % name, tensor=tf.reduce_min(variable)) tf.summary.histogram(name=name, values=variable) if gradient is not None: if isinstance(gradient, tf.IndexedSlices): grad_values = gradient.values else: grad_values = gradient if grad_values is not None: tf.summary.histogram(name='%s/gradients' % name, values=grad_values)
def resize_axis(tensor, axis, new_size, fill_value=0): tensor = tf.convert_to_tensor(tensor) shape = tf.unstack(tf.shape(tensor)) pad_shape = shape[:] pad_shape[axis] = tf.maximum(0, new_size - shape[axis]) shape[axis] = tf.minimum(shape[axis], new_size) shape = tf.stack(shape) resized = tf.concat([ tf.slice(tensor, tf.zeros_like(shape), shape), tf.fill(tf.stack(pad_shape), tf.cast(fill_value, tensor.dtype)) ], axis) # Update shape. new_shape = tensor.get_shape().as_list() # A copy is being made. new_shape[axis] = new_size resized.set_shape(new_shape) return resized
def mu_law_encode_nonlinear(audio, quantization_channels=256): ''' Compress the waveform amplitudes using mu-law non-linearity. NOTE: This mu-law functions as a non-linear function as opposed to quantization. ''' with tf.name_scope('encode'): mu = tf.to_float(quantization_channels - 1) # Perform mu-law companding transformation (ITU-T, 1988). # Minimum operation is here to deal with rare large amplitudes caused # by resampling. safe_audio_abs = tf.minimum(tf.abs(audio), 1.0) magnitude = tf.log1p(mu * safe_audio_abs) / tf.log1p(mu) signal = tf.multiply(tf.sign(audio), magnitude, name='mulaw') # Quantize signal to the specified number of levels. # return tf.to_int32((signal + 1) / 2 * mu + 0.5) return signal
def _clip_boxes(self, boxes, image): height = tf.shape(image)[1] width = tf.shape(image)[2] # TODO: what TF will do with tensors that will not be used anymore? x1_over_0 = tf.reshape(tf.maximum(tf.minimum(boxes[:, 0::4], tf.cast(width - 1, tf.float32)), 0), (-1,)) y1_over_0 = tf.reshape(tf.maximum(tf.minimum(boxes[:, 1::4], tf.cast(height - 1, tf.float32)), 0), (-1,)) x2_below_width = tf.reshape(tf.maximum(tf.minimum(boxes[:, 2::4], tf.cast(width - 1, tf.float32)), 0), (-1,)) y2_below_height = tf.reshape(tf.maximum(tf.minimum(boxes[:, 3::4], tf.cast(height - 1, tf.float32)), 0), (-1,)) boxes = tf.pack( [x1_over_0, # x1 >= 0 y1_over_0, # y1 >= 0 x2_below_width, # x2 < im_shape[1] y2_below_height], # y2 < im_shape[0] axis=1 ) return boxes # bbox_transform_inv
def process_image(img, scale, isotropic, crop, mean): '''Crops, scales, and normalizes the given image. scale : The image wil be first scaled to this size. If isotropic is true, the smaller side is rescaled to this, preserving the aspect ratio. crop : After scaling, a central crop of this size is taken. mean : Subtracted from the image ''' # Rescale if isotropic: img_shape = tf.to_float(tf.shape(img)[:2]) min_length = tf.minimum(img_shape[0], img_shape[1]) new_shape = tf.to_int32((scale / min_length) * img_shape) else: new_shape = tf.pack([scale, scale]) img = tf.image.resize_images(img, new_shape[0], new_shape[1]) # Center crop # Use the slice workaround until crop_to_bounding_box supports deferred tensor shapes # See: https://github.com/tensorflow/tensorflow/issues/521 offset = (new_shape - crop) / 2 img = tf.slice(img, begin=tf.pack([offset[0], offset[1], 0]), size=tf.pack([crop, crop, -1])) # Mean subtraction return tf.to_float(img) - mean
def prepare_reader(self, filename_queue, max_quantized_value=2, min_quantized_value=-2): """Creates a single reader thread for YouTube8M SequenceExamples. Args: filename_queue: A tensorflow queue of filename locations. max_quantized_value: the maximum of the quantized value. min_quantized_value: the minimum of the quantized value. Returns: A tuple of video indexes, video features, labels, and padding data. """ reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) return self.prepare_serialized_examples(serialized_example, max_quantized_value, min_quantized_value)
def apply_perturbations(i, j, X, increase, theta, clip_min, clip_max): """ TensorFlow implementation for apply perturbations to input features based on salency maps :param i: index of first selected feature :param j: index of second selected feature :param X: a matrix containing our input features for our sample :param increase: boolean; true if we are increasing pixels, false otherwise :param theta: delta for each feature adjustment :param clip_min: mininum value for a feature in our sample :param clip_max: maximum value for a feature in our sample : return: a perturbed input feature matrix for a target class """ # perturb our input sample if increase: X[0, i] = np.minimum(clip_max, X[0, i] + theta) X[0, j] = np.minimum(clip_max, X[0, j] + theta) else: X[0, i] = np.maximum(clip_min, X[0, i] - theta) X[0, j] = np.maximum(clip_min, X[0, j] - theta) return X
def OHNM_single_image(scores, n_pos, neg_mask): """Online Hard Negative Mining. scores: the scores of being predicted as negative cls n_pos: the number of positive samples neg_mask: mask of negative samples Return: the mask of selected negative samples. if n_pos == 0, no negative samples will be selected. """ def has_pos(): n_neg = n_pos * 3 max_neg_entries = tf.reduce_sum(tf.cast(neg_mask, tf.int32)) n_neg = tf.minimum(n_neg, max_neg_entries) n_neg = tf.cast(n_neg, tf.int32) neg_conf = tf.boolean_mask(scores, neg_mask) vals, _ = tf.nn.top_k(-neg_conf, k=n_neg) threshold = vals[-1]# a negtive value selected_neg_mask = tf.logical_and(neg_mask, scores <= -threshold) return tf.cast(selected_neg_mask, tf.float32) def no_pos(): return tf.zeros_like(neg_mask, tf.float32) return tf.cond(n_pos > 0, has_pos, no_pos)
def batch_iou_tf(proposals, gt): bboxes = tf.reshape(tf.transpose(proposals), [4, -1, 1]) bboxes_x1 = bboxes[0] bboxes_x2 = bboxes[0]+bboxes[2] bboxes_y1 = bboxes[1] bboxes_y2 = bboxes[1]+bboxes[3] gt = tf.reshape(tf.transpose(gt), [4, 1, -1]) gt_x1 = gt[0] gt_x2 = gt[0]+gt[2] gt_y1 = gt[1] gt_y2 = gt[1]+gt[3] widths = tf.maximum(0.0, tf.minimum(bboxes_x2, gt_x2) - tf.maximum(bboxes_x1, gt_x1)) heights = tf.maximum(0.0, tf.minimum(bboxes_y2, gt_y2) - tf.maximum(bboxes_y1, gt_y1)) intersection = widths*heights union = bboxes[2]*bboxes[3] + gt[2]*gt[3] - intersection return (intersection / union)
def batch_iou(proposals, gt): bboxes = np.transpose(proposals).reshape((4, -1, 1)) bboxes_x1 = bboxes[0] bboxes_x2 = bboxes[0]+bboxes[2] bboxes_y1 = bboxes[1] bboxes_y2 = bboxes[1]+bboxes[3] gt = np.transpose(gt).reshape((4, 1, -1)) gt_x1 = gt[0] gt_x2 = gt[0]+gt[2] gt_y1 = gt[1] gt_y2 = gt[1]+gt[3] widths = np.maximum(0, np.minimum(bboxes_x2, gt_x2) - np.maximum(bboxes_x1, gt_x1)) heights = np.maximum(0, np.minimum(bboxes_y2, gt_y2) - np.maximum(bboxes_y1, gt_y1)) intersection = widths*heights union = bboxes[2]*bboxes[3] + gt[2]*gt[3] - intersection return (intersection / union)
def decode_bboxes(tcoords, anchors): var_x, var_y, var_w, var_h = config['prior_variance'] t_x = tcoords[:, 0]*var_x t_y = tcoords[:, 1]*var_y t_w = tcoords[:, 2]*var_w t_h = tcoords[:, 3]*var_h a_w = anchors[:, 2] a_h = anchors[:, 3] a_x = anchors[:, 0]+a_w/2 a_y = anchors[:, 1]+a_h/2 x = t_x*a_w + a_x y = t_y*a_h + a_y w = tf.exp(t_w)*a_w h = tf.exp(t_h)*a_h x1 = tf.maximum(0., x - w/2) y1 = tf.maximum(0., y - h/2) x2 = tf.minimum(1., w + x1) y2 = tf.minimum(1., h + y1) return tf.stack([y1, x1, y2, x2], axis=1)
def center_crop_resize_image(image, image_size): """Center-crop into a square and resize to image_size. Args: image: A 3-D image `Tensor`. image_size: int, Desired size. Crops the image to a square and resizes it to the requested size. Returns: A 4-D tensor of shape [1, image_size, image_size, 3] and dtype float32, with values in [0, 1]. """ shape = tf.shape(image) small_side = tf.minimum(shape[0], shape[1]) image = tf.image.resize_image_with_crop_or_pad(image, small_side, small_side) image = tf.to_float(image) / 255.0 image = tf.image.resize_images(image, tf.constant([image_size, image_size])) return tf.expand_dims(image, 0)
def get_acceptance_rate(q, p, new_q, new_p, log_posterior, mass, data_axes): old_hamiltonian, old_log_prob = hamiltonian( q, p, log_posterior, mass, data_axes) new_hamiltonian, new_log_prob = hamiltonian( new_q, new_p, log_posterior, mass, data_axes) old_log_prob = tf.check_numerics( old_log_prob, 'HMC: old_log_prob has numeric errors! Try better initialization.') acceptance_rate = tf.exp( tf.minimum(-new_hamiltonian + old_hamiltonian, 0.0)) is_finite = tf.logical_and(tf.is_finite(acceptance_rate), tf.is_finite(new_log_prob)) acceptance_rate = tf.where(is_finite, acceptance_rate, tf.zeros_like(acceptance_rate)) return old_hamiltonian, new_hamiltonian, old_log_prob, new_log_prob, \ acceptance_rate
def scanline_error(tensor, shape): """ """ height, width, channels = shape value_shape = [height, width, 1] error_line = tf.maximum(basic([int(height * .75), 1], value_shape, distrib=ValueDistribution.exp) - .5, 0) error_swerve = tf.maximum(basic([int(height * .01), 1], value_shape, distrib=ValueDistribution.exp) - .5, 0) error_line *= error_swerve error_swerve *= 2 white_noise = basic([int(height * .75), 1], value_shape) white_noise = effects.blend(0, white_noise, error_swerve) error = error_line + white_noise y_index = effects.column_index(shape) x_index = (effects.row_index(shape) - tf.cast(effects.value_map(error, value_shape) * width * .025, tf.int32)) % width return tf.minimum(tf.gather_nd(tensor, tf.stack([y_index, x_index], 2)) + error_line * white_noise * 4, 1)
def density_map(tensor, shape): """ """ height, width, channels = shape bins = max(height, width) # values = value_map(tensor, shape, keep_dims=True) # values = tf.minimum(tf.maximum(tensor, 0.0), 1.0) # TODO: Get this to work with HDR data values = tensor # https://stackoverflow.com/a/34143927 binned_values = tf.cast(tf.reshape(values * (bins - 1), [-1]), tf.int32) ones = tf.ones_like(binned_values, dtype=tf.int32) counts = tf.unsorted_segment_sum(ones, binned_values, bins) out = tf.gather(counts, tf.cast(values[:, :] * (bins - 1), tf.int32)) return tf.ones(shape) * normalize(tf.cast(out, tf.float32))
def conv_feedback(tensor, shape, iterations=50, alpha=.5): """ Conv2d feedback loop :param Tensor tensor: :return: Tensor """ iterations = 100 half_shape = [int(shape[0] * .5), int(shape[1] * .5), shape[2]] convolved = offset(_downsample(tensor, shape, half_shape), half_shape, x=iterations * -3, y=iterations * -3) for i in range(iterations): convolved = convolve(ConvKernel.blur, convolved, half_shape) convolved = convolve(ConvKernel.sharpen, convolved, half_shape) convolved = normalize(convolved) up = tf.maximum((convolved - .5) * 2, 0.0) down = tf.minimum(convolved * 2, 1.0) return blend(tensor, resample(up + (1.0 - down), shape), alpha)
def blend_layers(control, shape, feather=1.0, *layers): layer_count = len(layers) control = normalize(control) control *= layer_count control_floor = tf.cast(control, tf.int32) x_index = row_index(shape) y_index = column_index(shape) layers = tf.stack(list(layers) + [layers[-1]]) layer_count += 1 floor_values = control_floor[:, :, 0] # I'm not sure why the mod operation is needed, but tensorflow-cpu explodes without it. combined_layer_0 = tf.gather_nd(layers, tf.stack([floor_values % layer_count, y_index, x_index], 2)) combined_layer_1 = tf.gather_nd(layers, tf.stack([(floor_values + 1) % layer_count, y_index, x_index], 2)) control_floor_fract = control - tf.floor(control) control_floor_fract = tf.minimum(tf.maximum(control_floor_fract - (1.0 - feather), 0.0) / feather, 1.0) return blend(combined_layer_0, combined_layer_1, control_floor_fract)
def yuv2rgb(yuv): """ Convert YUV image into RGB https://en.wikipedia.org/wiki/YUV """ yuv = tf.multiply(yuv, 255) yuv2rgb_filter = tf.constant([[[[1., 1., 1.], [0., -0.34413999, 1.77199996], [1.40199995, -0.71414, 0.]]]]) yuv2rgb_bias = tf.constant([-179.45599365, 135.45983887, -226.81599426]) yuv = tf.expand_dims(yuv, 0) temp = tf.nn.conv2d(yuv, yuv2rgb_filter, [1, 1, 1, 1], 'SAME') temp = tf.nn.bias_add(temp, yuv2rgb_bias) temp = tf.maximum(temp, tf.zeros(temp.get_shape(), dtype=tf.float32)) temp = tf.minimum(temp, tf.multiply( tf.ones(temp.get_shape(), dtype=tf.float32), 255)) temp = tf.divide(temp, 255) temp = tf.squeeze(temp, [0]) return temp
def pre_process_data(image,training): if training: image = tf.random_crop(image,size=[img_size_cropped,img_size_cropped,cifar10.num_channels]) image = tf.image.flip_left_right(image) image = tf.image.random_hue(image) image = tf.image.random_contrast(image) image = tf.image.random_saturation(image) image = tf.image.random_brightness(image) image = tf.maximum(image,1.0) image = tf.minimum(image,0.0) else: #for testing image image = tf.image.resize_image_with_crop_or_pad(image,img_size_cropped,img_size_cropped); return image
def f_inter_box(top_left_a, bot_right_a, top_left_b, bot_right_b): """Computes intersection area with boxes. Args: top_left_a: [B, T, 2] or [B, 2] bot_right_a: [B, T, 2] or [B, 2] top_left_b: [B, T, 2] or [B, 2] bot_right_b: [B, T, 2] or [B, 2] Returns: area: [B, T] """ top_left_max = tf.maximum(top_left_a, top_left_b) bot_right_min = tf.minimum(bot_right_a, bot_right_b) ndims = tf.shape(tf.shape(top_left_a)) # Check if the resulting box is valid. overlap = tf.to_float(top_left_max < bot_right_min) overlap = tf.reduce_prod(overlap, ndims - 1) area = tf.reduce_prod(bot_right_min - top_left_max, ndims - 1) area = overlap * tf.abs(area) return area
def f_iou_box_old(top_left_a, bot_right_a, top_left_b, bot_right_b): """Computes IoU of boxes. Args: top_left_a: [B, T, 2] or [B, 2] bot_right_a: [B, T, 2] or [B, 2] top_left_b: [B, T, 2] or [B, 2] bot_right_b: [B, T, 2] or [B, 2] Returns: iou: [B, T] """ inter_area = f_inter_box(top_left_a, bot_right_a, top_left_b, bot_right_b) inter_area = tf.maximum(inter_area, 1e-6) ndims = tf.shape(tf.shape(top_left_a)) # area_a = tf.reduce_prod(bot_right_a - top_left_a, ndims - 1) # area_b = tf.reduce_prod(bot_right_b - top_left_b, ndims - 1) check_a = tf.reduce_prod(tf.to_float(top_left_a < bot_right_a), ndims - 1) area_a = check_a * tf.reduce_prod(bot_right_a - top_left_a, ndims - 1) check_b = tf.reduce_prod(tf.to_float(top_left_b < bot_right_b), ndims - 1) area_b = check_b * tf.reduce_prod(bot_right_b - top_left_b, ndims - 1) union_area = (area_a + area_b - inter_area + 1e-5) union_area = tf.maximum(union_area, 1e-5) iou = inter_area / union_area iou = tf.maximum(iou, 1e-5) iou = tf.minimum(iou, 1.0) return iou
def get_learning_rate_decay(learning_rate, global_step, params): if params.learning_rate_decay == "noam": step = tf.to_float(global_step) warmup_steps = tf.to_float(params.warmup_steps) multiplier = params.hidden_size ** -0.5 decay = multiplier * tf.minimum((step + 1) * (warmup_steps ** -1.5), (step + 1) ** -0.5) return learning_rate * decay elif params.learning_rate_decay == "piecewise_constant": return tf.train.piecewise_constant(tf.to_int32(global_step), params.learning_rate_boundaries, params.learning_rate_values) elif params.learning_rate_decay == "none": return learning_rate else: raise ValueError("Unknown learning_rate_decay")
def _softmax_loss(self, y_true, y_pred): """Compute softmax loss. # Arguments y_true: Ground truth targets, tensor of shape (?, num_boxes, num_classes). y_pred: Predicted logits, tensor of shape (?, num_boxes, num_classes). # Returns softmax_loss: Softmax loss, tensor of shape (?, num_boxes). """ y_pred = tf.maximum(tf.minimum(y_pred, 1 - 1e-15), 1e-15) softmax_loss = -tf.reduce_sum(y_true * tf.log(y_pred), axis=-1) return softmax_loss
def min(x, axis=None, keepdims=False): """Minimum value in a tensor. # Arguments x: A tensor or variable. axis: An integer, the axis to find minimum values. keepdims: A boolean, whether to keep the dimensions or not. If `keepdims` is `False`, the rank of the tensor is reduced by 1. If `keepdims` is `True`, the reduced dimension is retained with length 1. # Returns A tensor with miminum values of `x`. """ axis = _normalize_axis(axis, ndim(x)) return tf.reduce_min(x, reduction_indices=axis, keep_dims=keepdims)
def iou_bbox(cls, bboxes1, bboxes2): """ Compute the IoUs between bounding boxes. """ bboxes1 = np.array(bboxes1, np.float32) bboxes2 = np.array(bboxes2, np.float32) intersection_min_y = np.maximum(bboxes1[:, 0], bboxes2[:, 0]) intersection_max_y = np.minimum(bboxes1[:, 0] + bboxes1[:, 2] - 1, bboxes2[:, 0] + bboxes2[:, 2] - 1) intersection_height = np.maximum(intersection_max_y - intersection_min_y + 1, np.zeros_like(bboxes1[:, 0])) intersection_min_x = np.maximum(bboxes1[:, 1], bboxes2[:, 1]) intersection_max_x = np.minimum(bboxes1[:, 1] + bboxes1[:, 3] - 1, bboxes2[:, 1] + bboxes2[:, 3] - 1) intersection_width = np.maximum(intersection_max_x - intersection_min_x + 1, np.zeros_like(bboxes1[:, 1])) area_intersection = intersection_height * intersection_width area_first = bboxes1[:, 2] * bboxes1[:, 3] area_second = bboxes2[:, 2] * bboxes2[:, 3] area_union = area_first + area_second - area_intersection iou = area_intersection * 1.0 / area_union iof = area_intersection * 1.0 / area_first ios = area_intersection * 1.0 / area_second return iou, iof, ios
def exponential_decay(learning_rate, warmup_steps=100, constant_steps=20000, decay_steps=20000, decay_rate=0.5): """Exponential decay.""" step = tf.to_float(tf.train.get_or_create_global_step()) if warmup_steps: learning_rate *= tf.minimum(1., (step + 1.0) / warmup_steps) step = tf.maximum(0., step - warmup_steps) if constant_steps: step = tf.maximum(0., step - constant_steps) if decay_steps: learning_rate *= decay_rate ** (step // decay_steps) return learning_rate
def _bbox_to_mask(yy, region_size, dtype): # trim bounding box exeeding region_size on top and left neg_part = tf.nn.relu(-yy[:2]) core = tf.ones(tf.to_int32(tf.round(yy[2:] - neg_part)), dtype=dtype) y1 = tf.maximum(yy[0], 0.) x1 = tf.maximum(yy[1], 0.) y2 = tf.minimum(region_size[0], yy[0] + yy[2]) x2 = tf.minimum(region_size[1], yy[1] + yy[3]) padding = (y1, region_size[0] - y2, x1, region_size[1] - x2) padding = tf.reshape(tf.stack(padding), (-1, 2)) padding = tf.to_int32(tf.round(padding)) mask = tf.pad(core, padding) # trim bounding box exeeding region_size on bottom and right rs = tf.to_int32(tf.round(region_size)) mask = mask[:rs[0], :rs[1]] mask.set_shape((None, None)) return mask
def compute_jaccard(gt_bboxes, anchors): gt_bboxes = tf.reshape(gt_bboxes, (-1,1,4)) anchors = tf.reshape(anchors, (1,-1,4)) inter_ymin = tf.maximum(gt_bboxes[:,:,0], anchors[:,:,0]) inter_xmin = tf.maximum(gt_bboxes[:,:,1], anchors[:,:,1]) inter_ymax = tf.minimum(gt_bboxes[:,:,2], anchors[:,:,2]) inter_xmax = tf.minimum(gt_bboxes[:,:,3], anchors[:,:,3]) h = tf.maximum(inter_ymax - inter_ymin, 0.) w = tf.maximum(inter_xmax - inter_xmin, 0.) inter_area = h * w anchors_area = (anchors[:,:,3] - anchors[:,:,1]) * (anchors[:,:,2] - anchors[:,:,0]) gt_bboxes_area = (gt_bboxes[:,:,3] - gt_bboxes[:,:,1]) * (gt_bboxes[:,:,2] - gt_bboxes[:,:,0]) union_area = anchors_area - inter_area + gt_bboxes_area jaccard = inter_area/union_area return jaccard
def get_hint_pool_idxs(self, normalized_query): """Get small set of idxs to compute nearest neighbor queries on. This is an expensive look-up on the whole memory that is used to avoid more expensive operations later on. Args: normalized_query: A Tensor of shape [None, key_dim]. Returns: A Tensor of shape [None, choose_k] of indices in memory that are closest to the queries. """ # get hash of query vecs hash_slot_idxs = self.get_hash_slots(normalized_query) # grab mem idxs in the hash slots hint_pool_idxs = [ tf.maximum(tf.minimum( tf.gather(self.hash_slots[i], idxs), self.memory_size - 1), 0) for i, idxs in enumerate(hash_slot_idxs)] return tf.concat(hint_pool_idxs, axis=1)
def resize_axis(tensor, axis, new_size, fill_value=0): """Truncates or pads a tensor to new_size on on a given axis. Truncate or extend tensor such that tensor.shape[axis] == new_size. If the size increases, the padding will be performed at the end, using fill_value. Args: tensor: The tensor to be resized. axis: An integer representing the dimension to be sliced. new_size: An integer or 0d tensor representing the new value for tensor.shape[axis]. fill_value: Value to use to fill any new entries in the tensor. Will be cast to the type of tensor. Returns: The resized tensor. """ tensor = tf.convert_to_tensor(tensor) shape = tf.unstack(tf.shape(tensor)) pad_shape = shape[:] pad_shape[axis] = tf.maximum(0, new_size - shape[axis]) shape[axis] = tf.minimum(shape[axis], new_size) shape = tf.stack(shape) resized = tf.concat([ tf.slice(tensor, tf.zeros_like(shape), shape), tf.fill(tf.stack(pad_shape), tf.cast(fill_value, tensor.dtype)) ], axis) # Update shape. new_shape = tensor.get_shape().as_list() # A copy is being made. new_shape[axis] = new_size resized.set_shape(new_shape) return resized
def get_video_matrix(self, features, feature_size, max_frames, max_quantized_value, min_quantized_value): """Decodes features from an input string and quantizes it. Args: features: raw feature values feature_size: length of each frame feature vector max_frames: number of frames (rows) in the output feature_matrix max_quantized_value: the maximum of the quantized value. min_quantized_value: the minimum of the quantized value. Returns: feature_matrix: matrix of all frame-features num_frames: number of frames in the sequence """ decoded_features = tf.reshape( tf.cast(tf.decode_raw(features, tf.uint8), tf.float32), [-1, feature_size]) num_frames = tf.minimum(tf.shape(decoded_features)[0], max_frames) feature_matrix = utils.Dequantize(decoded_features, max_quantized_value, min_quantized_value) feature_matrix = resize_axis(feature_matrix, 0, max_frames) return feature_matrix, num_frames
def SampleRandomSequence(model_input, num_frames, num_samples): """Samples a random sequence of frames of size num_samples. Args: model_input: A tensor of size batch_size x max_frames x feature_size num_frames: A tensor of size batch_size x 1 num_samples: A scalar Returns: `model_input`: A tensor of size batch_size x num_samples x feature_size """ batch_size = tf.shape(model_input)[0] frame_index_offset = tf.tile( tf.expand_dims(tf.range(num_samples), 0), [batch_size, 1]) max_start_frame_index = tf.maximum(num_frames - num_samples, 0) start_frame_index = tf.cast( tf.multiply( tf.random_uniform([batch_size, 1]), tf.cast(max_start_frame_index + 1, tf.float32)), tf.int32) frame_index = tf.minimum(start_frame_index + frame_index_offset, tf.cast(num_frames - 1, tf.int32)) batch_index = tf.tile( tf.expand_dims(tf.range(batch_size), 1), [1, num_samples]) index = tf.stack([batch_index, frame_index], 2) return tf.gather_nd(model_input, index)
def calculate_loss(self, predictions, labels, **unused_params): with tf.name_scope("loss_xent"): epsilon = 10e-6 origin_labels = tf.cast(labels, tf.float32) vocab_size = origin_labels.get_shape().as_list()[1] float_labels = tf.tile(tf.reshape(origin_labels,[-1, 1, vocab_size]),[1,FLAGS.top_k,1]) float_labels = tf.reshape(float_labels,[-1,vocab_size]) cross_entropy_loss = float_labels * tf.log(predictions + epsilon) + ( 1 - float_labels) * tf.log(1 - predictions + epsilon) cross_entropy_loss = tf.negative(cross_entropy_loss) num_labels = tf.minimum(tf.reduce_sum(origin_labels,axis=1),tf.constant(FLAGS.top_k,dtype=tf.float32)) mask = tf.reshape(tf.sequence_mask(num_labels,tf.constant(FLAGS.top_k,dtype=tf.float32),dtype=tf.float32),[-1]) cross_entropy_loss = tf.reduce_sum(tf.reduce_sum(cross_entropy_loss, 1)*mask)/(tf.reduce_sum(mask)+epsilon) return cross_entropy_loss
def get_video_matrix(self, features, feature_size, max_frames): reshaped_features = tf.reshape(features, [-1]) num_frames = tf.minimum(tf.shape(reshaped_features)[0], max_frames) feature_matrix = resize_axis(reshaped_features, 0, max_frames, fill_value="") return feature_matrix, num_frames
def __call__(self, inputs, state, scope=None): with tf.variable_scope(scope or type(self).__name__): c_prev, h_prev, update_prob_prev, cum_update_prob_prev = state # Parameters of gates are concatenated into one multiply for efficiency. concat = rnn_ops.linear([inputs, h_prev], 4 * self._num_units, True) # i = input_gate, j = new_input, f = forget_gate, o = output_gate i, j, f, o = tf.split(value=concat, num_or_size_splits=4, axis=1) if self._layer_norm: i = rnn_ops.layer_norm(i, name="i") j = rnn_ops.layer_norm(j, name="j") f = rnn_ops.layer_norm(f, name="f") o = rnn_ops.layer_norm(o, name="o") new_c_tilde = (c_prev * tf.sigmoid(f + self._forget_bias) + tf.sigmoid(i) * self._activation(j)) new_h_tilde = self._activation(new_c_tilde) * tf.sigmoid(o) # Compute value for the update prob with tf.variable_scope('state_update_prob'): new_update_prob_tilde = rnn_ops.linear(new_c_tilde, 1, True, bias_start=self._update_bias) new_update_prob_tilde = tf.sigmoid(new_update_prob_tilde) # Compute value for the update gate cum_update_prob = cum_update_prob_prev + tf.minimum(update_prob_prev, 1. - cum_update_prob_prev) update_gate = _binary_round(cum_update_prob) # Apply update gate new_c = update_gate * new_c_tilde + (1. - update_gate) * c_prev new_h = update_gate * new_h_tilde + (1. - update_gate) * h_prev new_update_prob = update_gate * new_update_prob_tilde + (1. - update_gate) * update_prob_prev new_cum_update_prob = update_gate * 0. + (1. - update_gate) * cum_update_prob new_state = SkipLSTMStateTuple(new_c, new_h, new_update_prob, new_cum_update_prob) new_output = SkipLSTMOutputTuple(new_h, update_gate) return new_output, new_state
def __call__(self, inputs, state, scope=None): with tf.variable_scope(scope or type(self).__name__): h_prev, update_prob_prev, cum_update_prob_prev = state # Parameters of gates are concatenated into one multiply for efficiency. with tf.variable_scope("gates"): concat = rnn_ops.linear([inputs, h_prev], 2 * self._num_units, bias=True, bias_start=1.0) # r = reset_gate, u = update_gate r, u = tf.split(value=concat, num_or_size_splits=2, axis=1) if self._layer_norm: r = rnn_ops.layer_norm(r, name="r") u = rnn_ops.layer_norm(u, name="u") # Apply non-linearity after layer normalization r = tf.sigmoid(r) u = tf.sigmoid(u) with tf.variable_scope("candidate"): new_c_tilde = self._activation(rnn_ops.linear([inputs, r * h_prev], self._num_units, True)) new_h_tilde = u * h_prev + (1 - u) * new_c_tilde # Compute value for the update prob with tf.variable_scope('state_update_prob'): new_update_prob_tilde = rnn_ops.linear(new_h_tilde, 1, True, bias_start=self._update_bias) new_update_prob_tilde = tf.sigmoid(new_update_prob_tilde) # Compute value for the update gate cum_update_prob = cum_update_prob_prev + tf.minimum(update_prob_prev, 1. - cum_update_prob_prev) update_gate = _binary_round(cum_update_prob) # Apply update gate new_h = update_gate * new_h_tilde + (1. - update_gate) * h_prev new_update_prob = update_gate * new_update_prob_tilde + (1. - update_gate) * update_prob_prev new_cum_update_prob = update_gate * 0. + (1. - update_gate) * cum_update_prob new_state = SkipGRUStateTuple(new_h, new_update_prob, new_cum_update_prob) new_output = SkipGRUOutputTuple(new_h, update_gate) return new_output, new_state
def calc_iou(self, boxes1, boxes2, scope='iou'): """calculate ious Args: boxes1: 4-D tensor [CELL_SIZE, CELL_SIZE, BOXES_PER_CELL, 4] ====> (x_center, y_center, w, h) boxes2: 1-D tensor [CELL_SIZE, CELL_SIZE, BOXES_PER_CELL, 4] ===> (x_center, y_center, w, h) Return: iou: 3-D tensor [CELL_SIZE, CELL_SIZE, BOXES_PER_CELL] """ with tf.variable_scope(scope): boxes1 = tf.stack([boxes1[:, :, :, :, 0] - boxes1[:, :, :, :, 2] / 2.0, boxes1[:, :, :, :, 1] - boxes1[:, :, :, :, 3] / 2.0, boxes1[:, :, :, :, 0] + boxes1[:, :, :, :, 2] / 2.0, boxes1[:, :, :, :, 1] + boxes1[:, :, :, :, 3] / 2.0]) boxes1 = tf.transpose(boxes1, [1, 2, 3, 4, 0]) boxes2 = tf.stack([boxes2[:, :, :, :, 0] - boxes2[:, :, :, :, 2] / 2.0, boxes2[:, :, :, :, 1] - boxes2[:, :, :, :, 3] / 2.0, boxes2[:, :, :, :, 0] + boxes2[:, :, :, :, 2] / 2.0, boxes2[:, :, :, :, 1] + boxes2[:, :, :, :, 3] / 2.0]) boxes2 = tf.transpose(boxes2, [1, 2, 3, 4, 0]) # calculate the left up point & right down point lu = tf.maximum(boxes1[:, :, :, :, :2], boxes2[:, :, :, :, :2]) rd = tf.minimum(boxes1[:, :, :, :, 2:], boxes2[:, :, :, :, 2:]) # intersection intersection = tf.maximum(0.0, rd - lu) inter_square = intersection[:, :, :, :, 0] * intersection[:, :, :, :, 1] # calculate the boxs1 square and boxs2 square square1 = (boxes1[:, :, :, :, 2] - boxes1[:, :, :, :, 0]) * \ (boxes1[:, :, :, :, 3] - boxes1[:, :, :, :, 1]) square2 = (boxes2[:, :, :, :, 2] - boxes2[:, :, :, :, 0]) * \ (boxes2[:, :, :, :, 3] - boxes2[:, :, :, :, 1]) union_square = tf.maximum(square1 + square2 - inter_square, 1e-10) return tf.clip_by_value(inter_square / union_square, 0.0, 1.0)
def get_bn_decay(batch): bn_momentum = tf.train.exponential_decay( BN_INIT_DECAY, batch*BATCH_SIZE, BN_DECAY_DECAY_STEP, BN_DECAY_DECAY_RATE, staircase=True) bn_decay = tf.minimum(BN_DECAY_CLIP, 1 - bn_momentum) return bn_decay