首页 > 解决方案 > 在 Tensorflow 2.3 中利用调整层的正确方法?

问题描述

Tensorflow 2.3 引入了新的预处理层,例如tf.keras.layers.experimental.preprocessing.Resizing.

但是,使用 Keras 对图像进行训练的典型流程使用tf.keras.preprocessing.image.ImageDataGenerator,它只能采用固定target_size参数。据我了解,根本原因是将keras图像作为numpy背景中的数组处理,其中所有图像必须具有相同的大小(这是真的吗?)。

虽然我可以使用一个具有调整大小层的模型,该模型在固定大小上进行训练,然后预测任意大小的图像,但这似乎是有风险的,因为训练数据和推理数据会有系统差异。一种解决方法可能是使用ImageDataGeneratortarget_size调整层匹配的插值方法,这样在训练期间调整层基本上什么都不做,但看起来调整层并没有任何好处。

所以问题是,有没有一种方法可以直接在混合尺寸的图像上进行训练,以充分利用调整层的优势?

标签: pythontensorflowkeras

解决方案


models needs to operate on images of a FIXED size. If you train a model with a fixed size for example (224 X 224) then if you want to use the trained model to make predictions on images you need to resize those images to 224 X 224. Specifically whatever pre-processing you did on the training images you should also do on the images that you wish to predict. For example if your model was trained on RGB images but the images you want to predict are say BGR images (like reading in images with CV2) the results will be incorrect. You would need to convert them to RGB . Similarly if you rescaled you training images by dividing by 255 you should also rescale the images you want to predict.


推荐阅读