Add a resizing layer to a keras sequential model

后端 未结 5 893
失恋的感觉
失恋的感觉 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条回答
  •  清酒与你
    2020-12-17 18:55

    Normally you would use the Reshape layer for this:

    model.add(Reshape((224,224,3), input_shape=(160,320,3))
    

    but since your target dimensions don't allow to hold all the data from the input dimensions (224*224 != 160*320), this won't work. You can only use Reshape if the number of elements does not change.

    If you are fine with losing some data in your image, you can specify your own lossy reshape:

    model.add(Reshape(-1,3), input_shape=(160,320,3))
    model.add(Lambda(lambda x: x[:50176])) # throw away some, so that #data = 224^2
    model.add(Reshape(224,224,3))
    

    That said, often these transforms are done before applying the data to the model because this is essentially wasted computation time if done in every training step.

提交回复
热议问题