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) ?
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))
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With