将尺寸调整层添加到keras顺序模型中 [英] Add a resizing layer to a keras sequential model

查看:102
本文介绍了将尺寸调整层添加到keras顺序模型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将调整大小图层添加到

How can I add a resizing layer to

model = Sequential()

使用

model.add(...)

要将图像的形状从形状(160、320、3)调整为(224,224,3)吗?

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

推荐答案

通常,您会使用> c0> 层:

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顺序模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆