def preprocess(x): """Cast to float, normalize, and concatenate images along last axis.""" x = nest.map_structure( lambda image: tf.image.convert_image_dtype(image, tf.float32), x) x = nest.flatten(x) x = tf.concat(x, axis=-1) x = (tf.image.convert_image_dtype(x, tf.float32) - 0.5) * 2.0 return x
def preprocess(x): """Cast to float, normalize, and concatenate images along last axis.""" import tensorflow as tf x = nest.map_structure( lambda image: tf.image.convert_image_dtype(image, tf.float32), x) x = nest.flatten(x) x = tf.concat(x, axis=-1) # x = (tf.image.convert_image_dtype(x, tf.float32) - 0.5) * 2.0 # TODO: Why is the image being converted to float32 twice? Once in the # nest and once down here? x = (high - low) * tf.image.convert_image_dtype(x, tf.float32) + low return x
def feedforward_model(hidden_layer_sizes, output_size, activation='relu', output_activation='linear', preprocessors=None, dropout=None, name='feedforward_model', *args, **kwargs): def cast_and_concat(x): x = nest.map_structure(training_utils.cast_if_floating_dtype, x) x = nest.flatten(x) x = tf.concat(x, axis=-1) return x if dropout: layers = nest.flatten([[ tf.keras.layers.Dense(hidden_layer_size, *args, activation=activation, **kwargs), tf.keras.layers.Dropout(dropout) ] for hidden_layer_size in hidden_layer_sizes]) else: layers = [ tf.keras.layers.Dense(hidden_layer_size, *args, activation=activation, **kwargs) for hidden_layer_size in hidden_layer_sizes ] model = PicklableSequential( (tfkl.Lambda(cast_and_concat), *layers, tf.keras.layers.Dense( output_size, *args, activation=output_activation, **kwargs)), name=name) return model
def cast_and_concat(x): x = nest.map_structure(lambda element: tf.cast(element, tf.float32), x) x = nest.flatten(x) x = tf.concat(x, axis=-1) return x
def flatten_input_structure(inputs): inputs_flat = nest.flatten(inputs) return inputs_flat
def cast_and_concat(x): x = nest.map_structure(training_utils.cast_if_floating_dtype, x) x = nest.flatten(x) x = tf.concat(x, axis=-1) return x