问题描述
如何向
model = Sequential()
使用
model.add(...)
要将图像的大小从 (160, 320, 3) 调整为 (224,224,3) 吗?
To resize an image from shape (160, 320, 3) to (224,224,3) ?
推荐答案
通常你会使用 Reshape
图层:
Normally you would use the Reshape
layer for this:
model.add(Reshape((224,224,3), input_shape=(160,320,3))
但是由于您的目标维度不允许包含来自输入维度 (224*224 !=160*320
) 的所有数据,这将不起作用.如果元素数量不变,您只能使用 Reshape
.
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.
这篇关于向 keras 序列模型添加调整大小层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!