Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a resizing layer to a keras sequential model

How can I add a resizing layer to

model = Sequential()

using

model.add(...)

To resize an image from shape (160, 320, 3) to (224,224,3) ?

like image 806
user1934212 Avatar asked Jan 27 '17 22:01

user1934212


2 Answers

I thought I should post an updated answer, since the accepted answer is wrong and there are some major updates in the recent Keras release.

To add a resizing layer, according to documentation:

tf.keras.layers.experimental.preprocessing.Resizing(height, width, interpolation="bilinear", crop_to_aspect_ratio=False, **kwargs)

For you, it should be:

from tensorflow.keras.layers.experimental.preprocessing import Resizing

model = Sequential()
model.add(Resizing(224,224))
like image 75
Dawei Wang Avatar answered Sep 27 '22 00:09

Dawei Wang


The accepted answer uses the Reshape layer, which works like NumPy's reshape, which can be used to reshape a 4x4 matrix into a 2x8 matrix, but that will result in the image loosing locality information:

0 0 0 0
1 1 1 1    ->    0 0 0 0 1 1 1 1
2 2 2 2          2 2 2 2 3 3 3 3
3 3 3 3

Instead, image data should be rescaled / "resized" using, e.g., Tensorflows image_resize. But beware about the correct usage and the bugs! As shown in the related question, this can be used with a lambda layer:

model.add( keras.layers.Lambda( 
    lambda image: tf.image.resize_images( 
        image, 
        (224, 224), 
        method = tf.image.ResizeMethod.BICUBIC,
        align_corners = True, # possibly important
        preserve_aspect_ratio = True
    )
))

In your case, as you have a 160x320 image, you also have to decide whether to keep the aspect ratio, or not. If you want to use a pre-trained network, then you should use the same kind of resizing that the network was trained for.

like image 42
mxmlnkn Avatar answered Sep 26 '22 00:09

mxmlnkn