imagekerasmodellayerrescale

Add a rescaling layer to a keras model


Suppose I have an input image of shape (100, 100, 3) and I want to rescale the image, i.e., make it smaller and have a dimension say (50, 50, 3), i.e., rescaling it to half. Would be better if Gaussian smoothing is applied to the image within the layer after rescaling.


Solution

  • Ahh, this works fine, I figured it out,

    y = layers.Lambda( 
            lambda image: tf.image.resize( 
                inputs, 
                (int(80 * 1/2), int(80 * 1/2)), 
                method = tf.image.ResizeMethod.BICUBIC,
                #align_corners = True, # possibly important
                preserve_aspect_ratio = True
            )
        )(inputs)