Skip to content

Commit f9110bb

Browse files
author
Saumitro Dasgupta
committed
Fixed isotropic rescaling
1 parent 8178f69 commit f9110bb

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

examples/imagenet/dataset.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@ def _load_image(path, scale, isotropic, crop, mean):
3737
img = read_image(path)
3838
# Rescale
3939
if isotropic:
40-
scale = scale / float(min(w, h))
41-
new_height, new_width = (h * scale, w * scale)
40+
img_shape = tf.to_float(tf.shape(img)[:2])
41+
min_length = tf.minimum(img_shape[0], img_shape[1])
42+
new_shape = tf.to_int32((scale / min_length) * img_shape)
4243
else:
43-
new_height, new_width = (scale, scale)
44-
img = tf.image.resize_images(img, new_height, new_width)
45-
# Crop
46-
img = tf.image.crop_to_bounding_box(img, (new_height - crop) / 2,
47-
(new_width - crop) / 2, crop, crop)
44+
new_shape = tf.pack([scale, scale])
45+
img = tf.image.resize_images(img, new_shape[0], new_shape[1])
46+
# Center crop
47+
# Use the slice workaround until crop_to_bounding_box supports deferred tensor shapes
48+
# See: https://github.com/tensorflow/tensorflow/issues/521
49+
offset = (new_shape - crop) / 2
50+
img = tf.slice(img, begin=tf.pack([offset[0], offset[1], 0]), size=tf.pack([crop, crop, -1]))
4851
# Mean subtraction
4952
return tf.to_float(img) - mean
5053

0 commit comments

Comments
 (0)