python - 在 Tensorflow 2.3 中利用调整层的正确方法?
问题描述
Tensorflow 2.3 引入了新的预处理层,例如tf.keras.layers.experimental.preprocessing.Resizing
.
但是,使用 Keras 对图像进行训练的典型流程使用tf.keras.preprocessing.image.ImageDataGenerator
,它只能采用固定target_size
参数。据我了解,根本原因是将keras
图像作为numpy
背景中的数组处理,其中所有图像必须具有相同的大小(这是真的吗?)。
虽然我可以使用一个具有调整大小层的模型,该模型在固定大小上进行训练,然后预测任意大小的图像,但这似乎是有风险的,因为训练数据和推理数据会有系统差异。一种解决方法可能是使用ImageDataGenerator
与target_size
调整层匹配的插值方法,这样在训练期间调整层基本上什么都不做,但看起来调整层并没有任何好处。
所以问题是,有没有一种方法可以直接在混合尺寸的图像上进行训练,以充分利用调整层的优势?
解决方案
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.
推荐阅读
- ios - TIC TCP 连接失败 [4:0x604000360300]:1:61 Err(61) <1> HTTP 加载失败(错误代码:-1004
- python - Django 无法上传多个文件并且 request.FILES 似乎为空
- python - tf.metrics.auc 产生的结果与 sklearn.metrics.roc_auc_score 非常不同
- laravel - 如何在 laravel 5.6 中回显选定的单选值
- signalr - HubCallerContext 中的 SignalR 缺少连接
- ios - 显示位置 Swift3
- azure - 我可以使用服务连接将 VSTS 连接到多个 Azure 帐户吗?
- java - Android 错误“IF”和“ELSE”
- javascript - 如何使用 JavaScript 在我的数据中检测字符串数组和对象数组?
- python - IronPython 在沙箱中运行时“未定义名称'locals'”,但在我直接运行脚本时没有