Skip to content
This repository was archived by the owner on Apr 10, 2024. It is now read-only.

Commit a4bf39e

Browse files
author
Ludwig Schubert
committed
Add crop_or_pad_to(height, width) to transforms to more easily support models which require precise input shapes
1 parent 6bb7c9e commit a4bf39e

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

lucid/optvis/transform.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ def inner(t_image):
137137

138138

139139
def _rand_select(xs, seed=None):
140-
rand_n = tf.random_uniform((), 0, len(xs), "int32", seed=seed)
141-
return tf.constant(xs)[rand_n]
140+
xs_list = list(xs)
141+
rand_n = tf.random_uniform((), 0, len(xs_list), "int32", seed=seed)
142+
return tf.constant(xs_list)[rand_n]
142143

143144

144145
def _angle2rads(angle, units):
@@ -150,6 +151,15 @@ def _angle2rads(angle, units):
150151
return angle
151152

152153

154+
def crop_or_pad_to(height, width):
155+
"""Ensures the specified spatial shape by either padding or cropping.
156+
Meant to be used as a last transform for architectures insisting on a specific
157+
spatial shape of their inputs.
158+
"""
159+
def inner(t_image):
160+
return tf.image.resize_image_with_crop_or_pad(t_image, height, width)
161+
return inner
162+
153163
standard_transforms = [
154164
pad(12, mode="constant", constant_value=.5),
155165
jitter(8),

0 commit comments

Comments
 (0)