def random_square_crop(image_size, min_scale): """Generates a random square crop within an image. Args: image_size: a [height, width] tensor. min_scale: how much the minimum dimension can be scaled down when taking a crop. (e.g. if the image is 480 x 640, a min_scale of 0.8 means the output crop can have a height and width between 480 and 384, which is 480 * 0.8.) Returns: output_begin, output_size and image_size. output_begin and output_size are three element tensors specifying the shape to crop using crop_sequence below. image_size is a two element [height, width] tensor from the input. """ min_dim = tf.reduce_min(image_size[0:2]) sampled_size = tf.to_int32( tf.to_float(min_dim) * tf.random_uniform([], min_scale, 1.0)) output_size = tf.stack([sampled_size, sampled_size, -1]) height_offset = tf.random_uniform([], 0, image_size[0] - sampled_size + 1, dtype=tf.int32) width_offset = tf.random_uniform([], 0, image_size[1] - sampled_size + 1, dtype=tf.int32) output_begin = tf.stack([height_offset, width_offset, 0]) return output_begin, output_size, image_size
def get_squared_loss(logits, labels): onehot_labels = tf.one_hot(indices=labels, depth=get_dataset().NUM_LABELS) diff = logits - tf.to_float(onehot_labels) loss_vector = tf.reduce_mean(tf.square(diff), axis=1) return tf.reduce_mean(loss_vector), loss_vector