Add a resizing layer to a keras sequential model

后端 未结 5 881
失恋的感觉
失恋的感觉 2020-12-17 18:02

How can I add a resizing layer to

model = Sequential()

using

model.add(...)

To resize an image from sha

5条回答
  •  -上瘾入骨i
    2020-12-17 18:54

    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.

提交回复
热议问题