由于不同的图片形状,Tensorflow 中的调整层崩溃
Resizing layer in Tensorflow crashes because of different picture shapes
我是 TensorFlow 的新手。我有不同图像大小的图像分类问题。在 documentation 中,我了解到在模型内部而不是在 dataset.map()
函数中调整大小有何好处。
我像这样批处理我的数据集:
ds_train = ds_train\
.batch(BATCH_SIZE)\
.prefetch(tf.data.experimental.AUTOTUNE)
我的模型很简单:
base_model = tf.keras.applications.ResNet50V2(
include_top=True, weights=None, input_tensor=None, input_shape=(224,224,3),
pooling=None, classes=NUM_CLASSES, classifier_activation='softmax')
seed = 42
model = tf.keras.Sequential([
tf.keras.Input(shape=(None, None, 3)),
tf.keras.layers.experimental.preprocessing.Resizing(224, 224),
tf.keras.layers.experimental.preprocessing.Rescaling(1./255),
tf.keras.layers.experimental.preprocessing.RandomFlip(mode='horizontal_and_vertical', seed=seed),
base_model
])
这给了我错误:
InvalidArgumentError: Cannot add tensor to the batch: number of elements does not match. Shapes are: [tensor]: [95,116,3], [batch]: [108,112,3]
。如何将调整大小层与批处理一起使用?
错误是您不能批量处理不同大小的元素。不幸的是,没有办法解决这个问题。该文档指定模型内部的预处理在推理时很有用(即当您调用 model.predict()
时)。
The key benefit to doing this is that it makes your model portable [...] When all data preprocessing is part of the model, other people can load and use your model without having to be aware of how each feature is expected to be encoded & normalized. Your inference model will be able to process raw images or raw structured data, and will not require users of the model to be aware of the details of e.g. the tokenization scheme used for text, the indexing scheme used for categorical features, whether image pixel values are normalized to [-1, +1] or to [0, 1], etc.
在训练过程中,如果你想使用大于1的batch size,你需要自己做预处理,如果图像有不同的大小。您可以使用 tf.data.Dataset.map()
.
我是 TensorFlow 的新手。我有不同图像大小的图像分类问题。在 documentation 中,我了解到在模型内部而不是在 dataset.map()
函数中调整大小有何好处。
我像这样批处理我的数据集:
ds_train = ds_train\
.batch(BATCH_SIZE)\
.prefetch(tf.data.experimental.AUTOTUNE)
我的模型很简单:
base_model = tf.keras.applications.ResNet50V2(
include_top=True, weights=None, input_tensor=None, input_shape=(224,224,3),
pooling=None, classes=NUM_CLASSES, classifier_activation='softmax')
seed = 42
model = tf.keras.Sequential([
tf.keras.Input(shape=(None, None, 3)),
tf.keras.layers.experimental.preprocessing.Resizing(224, 224),
tf.keras.layers.experimental.preprocessing.Rescaling(1./255),
tf.keras.layers.experimental.preprocessing.RandomFlip(mode='horizontal_and_vertical', seed=seed),
base_model
])
这给了我错误:
InvalidArgumentError: Cannot add tensor to the batch: number of elements does not match. Shapes are: [tensor]: [95,116,3], [batch]: [108,112,3]
。如何将调整大小层与批处理一起使用?
错误是您不能批量处理不同大小的元素。不幸的是,没有办法解决这个问题。该文档指定模型内部的预处理在推理时很有用(即当您调用 model.predict()
时)。
The key benefit to doing this is that it makes your model portable [...] When all data preprocessing is part of the model, other people can load and use your model without having to be aware of how each feature is expected to be encoded & normalized. Your inference model will be able to process raw images or raw structured data, and will not require users of the model to be aware of the details of e.g. the tokenization scheme used for text, the indexing scheme used for categorical features, whether image pixel values are normalized to [-1, +1] or to [0, 1], etc.
在训练过程中,如果你想使用大于1的batch size,你需要自己做预处理,如果图像有不同的大小。您可以使用 tf.data.Dataset.map()
.