Skip to content

Commit

Permalink
change tf.to_ casts to tf.cast
Browse files Browse the repository at this point in the history
  • Loading branch information
sercant committed Apr 16, 2019
1 parent b657ca4 commit 63cc6fd
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
28 changes: 14 additions & 14 deletions core/preprocess_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def _crop(image, offset_height, offset_width, crop_height, crop_width):
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]))
offsets = tf.cast(tf.stack([offset_height, offset_width, 0]), tf.int32)

# Use tf.slice instead of crop_to_bounding box as it accepts tensors to
# define the crop size.
Expand Down Expand Up @@ -253,7 +253,7 @@ def get_random_scale(min_scale_factor, max_scale_factor, step_size):
raise ValueError('Unexpected value of min_scale_factor.')

if min_scale_factor == max_scale_factor:
return tf.to_float(min_scale_factor)
return tf.cast(min_scale_factor, tf.float32)

# When step_size = 0, we sample the value uniformly from [min, max).
if step_size == 0:
Expand Down Expand Up @@ -283,8 +283,8 @@ def randomly_scale_image_and_label(image, label=None, scale=1.0):
if scale == 1.0:
return image, label
image_shape = tf.shape(image)
new_dim = tf.to_int32(tf.to_float(
[image_shape[0], image_shape[1]]) * scale)
new_dim = tf.cast(tf.cast(
[image_shape[0], image_shape[1]], tf.float32) * scale, tf.int32)

# Need squeeze and expand_dims because image interpolation takes
# 4D tensors as input.
Expand Down Expand Up @@ -376,24 +376,24 @@ def resize_to_range(image,
"""
with tf.name_scope(scope, 'resize_to_range', [image]):
new_tensor_list = []
min_size = tf.to_float(min_size)
min_size = tf.cast(min_size, tf.float32)
if max_size is not None:
max_size = tf.to_float(max_size)
max_size = tf.cast(max_size, tf.float32)
# Modify the max_size to be a multiple of factor plus 1 and make sure the
# max dimension after resizing is no larger than max_size.
if factor is not None:
max_size = (max_size + (factor - (max_size - 1) % factor) % factor
- factor)

[orig_height, orig_width, _] = resolve_shape(image, rank=3)
orig_height = tf.to_float(orig_height)
orig_width = tf.to_float(orig_width)
orig_height = tf.cast(orig_height, tf.float32)
orig_width = tf.cast(orig_width, tf.float32)
orig_min_size = tf.minimum(orig_height, orig_width)

# Calculate the larger of the possible sizes
large_scale_factor = min_size / orig_min_size
large_height = tf.to_int32(tf.ceil(orig_height * large_scale_factor))
large_width = tf.to_int32(tf.ceil(orig_width * large_scale_factor))
large_height = tf.cast(tf.ceil(orig_height * large_scale_factor), tf.int32)
large_width = tf.cast(tf.ceil(orig_width * large_scale_factor), tf.int32)
large_size = tf.stack([large_height, large_width])

new_size = large_size
Expand All @@ -402,12 +402,12 @@ def resize_to_range(image,
# is too big.
orig_max_size = tf.maximum(orig_height, orig_width)
small_scale_factor = max_size / orig_max_size
small_height = tf.to_int32(
tf.ceil(orig_height * small_scale_factor))
small_width = tf.to_int32(tf.ceil(orig_width * small_scale_factor))
small_height = tf.cast(
tf.ceil(orig_height * small_scale_factor), tf.int32)
small_width = tf.cast(tf.ceil(orig_width * small_scale_factor), tf.int32)
small_size = tf.stack([small_height, small_width])
new_size = tf.cond(
tf.to_float(tf.reduce_max(large_size)) > max_size,
tf.cast(tf.reduce_max(large_size), tf.float32) > max_size,
lambda: small_size,
lambda: large_size)
# Ensure that both output sides are multiples of factor plus one.
Expand Down
2 changes: 1 addition & 1 deletion core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def scale_dimension(dim, scale):
Scaled dimension.
"""
if isinstance(dim, tf.Tensor):
return tf.cast((tf.to_float(dim) - 1.0) * scale + 1.0, dtype=tf.int32)
return tf.cast((tf.cast(dim, tf.float32) - 1.0) * scale + 1.0, dtype=tf.int32)
else:
return int((float(dim) - 1.0) * scale + 1.0)

Expand Down
2 changes: 1 addition & 1 deletion evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def main(unused_argv):
predictions = predictions[common.OUTPUT_TYPE]
predictions = tf.reshape(predictions, shape=[-1])
labels = tf.reshape(samples[common.LABEL], shape=[-1])
weights = tf.to_float(tf.not_equal(labels, dataset.ignore_label))
weights = tf.cast(tf.not_equal(labels, dataset.ignore_label), tf.float32)

# Set ignore_label regions to label 0, because metrics.mean_iou requires
# range of labels = [0, dataset.num_classes). Note the ignore_label regions
Expand Down
4 changes: 2 additions & 2 deletions utils/train_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def add_loss_for_each_scale(scales_to_logits,

if loss_function == 'sce':
scaled_labels = tf.reshape(scaled_labels, shape=[-1])
not_ignore_mask = tf.to_float(tf.not_equal(scaled_labels,
ignore_label)) * loss_weight
not_ignore_mask = tf.cast(tf.not_equal(
scaled_labels, ignore_label), tf.float32) * loss_weight
one_hot_labels = slim.one_hot_encoding(
scaled_labels, num_classes, on_value=1.0, off_value=0.0)
tf.losses.softmax_cross_entropy(
Expand Down
4 changes: 2 additions & 2 deletions visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ def main(unused_argv):
predictions,
[0, 0, 0],
[1, original_image_shape[0], original_image_shape[1]])
resized_shape = tf.to_int32([tf.squeeze(samples[common.HEIGHT]),
tf.squeeze(samples[common.WIDTH])])
resized_shape = tf.cast([tf.squeeze(samples[common.HEIGHT]),
tf.squeeze(samples[common.WIDTH])], tf.int32)
predictions = tf.squeeze(
tf.image.resize_images(tf.expand_dims(predictions, 3),
resized_shape,
Expand Down

0 comments on commit 63cc6fd

Please sign in to comment.