Image resize in keras model

I'm training a neural network model, which takes an input with shape (batch, 256, 256, 3), but I want to run my model on images with different sizes. Is there a way to put a resize block into keras model?

neural-networkskerasimage
2 votesJW234.00
1 Answers
NN138.00
1

First you need to create a new Keras layer for input resize and here is how you can do it.

import tensorflow as tf
from keras.engine.topology import Layer


# The class is prepared to change the size of model's input dynamically
class MyInput(Layer):
    def __init__(self, image_size=(512, 512), **kwargs):
        self.image_size = image_size[0], image_size[1]
        super(MyInput, self).__init__(**kwargs)

    def call(self, inputs, **kwargs):
        return tf.image.resize_images(inputs, self.image_size)

    def compute_output_shape(self, input_shape):
        return input_shape[0], self.image_size[0], self.image_size[1], input_shape[-1]

Then you can just use this layer as any other regular keras layer and it will resize the inputs for you. (Remember this is just only for tensorflow. You can rewrite call function for other backends)

Reply
Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?