CNN数据增强时添加宽度、高度时出错

我正在尝试按照TensorFlow文档中提到的方式对二分类图像分类问题应用数据增强:https://www.tensorflow.org/tutorials/images/classification#data_augmentation

我的模型如下:

Sequential([  data_augmentation,  layers.experimental.preprocessing.Rescaling(1./255),  layers.Conv2D(16, 3, padding='same', activation='relu'),  layers.MaxPooling2D(),  layers.Dropout(0.2),  layers.Conv2D(32, 3, padding='same', activation='relu'),  layers.MaxPooling2D(),  layers.Dropout(0.2),  layers.Conv2D(64, 3, padding='same', activation='relu'),  layers.MaxPooling2D(),  layers.Flatten(),  layers.Dense(128, activation='relu'),  layers.Dropout(0.5),  layers.Dense(1, activation='sigmoid')])

当我的数据增强层如下设置时,模型可以无错误地编译:

data_augmentation = keras.Sequential(  [    layers.experimental.preprocessing.RandomFlip("horizontal",                                                  input_shape=(150,                                                               150,                                                              3)),    layers.experimental.preprocessing.RandomRotation(0.2),    layers.experimental.preprocessing.RandomZoom(0.2)  ])

如果我在增强层中尝试引入RandomHeight()和/或RandomWidth(),在创建模型时会收到以下错误:

ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None`.

您知道这是为什么吗?如何解决这个问题?


回答:

您可以检查RandomWidth-Height输出的形状。RandomWidth类的源代码

 return tensor_shape.TensorShape(        [input_shape[0], None, input_shape[2], input_shape[3]])

假设我将RandomHeight作为第一层使用,并且input_shape为150 x 150的RGB图像。我们可以通过以下方式确认输出形状:

data_augmentation.summary()Layer (type)                 Output Shape              Param #   =================================================================random_height_2 (RandomHeigh (None, None, 150, 3)      0         _________________________________________________________________random_flip_2 (RandomFlip)   (None, None, 150, 3)      0         _________________________________________________________________random_rotation_2 (RandomRot (None, None, 150, 3)      0         _________________________________________________________________random_zoom_2 (RandomZoom)   (None, None, 150, 3)      0         

当您这样使用它,并且如果您在没有密集层的情况下编译模型,您将在模型摘要中看到:

dropout_6 (Dropout)          (None, None, 18, 64)      0         _________________________________________________________________flatten_6 (Flatten)          (None, None)              0         

(None,None)在这里引起了错误。您可以通过使用tf.keras.layers.GlobalMaxPooling2D()代替Flatten()来解决这个问题

虽然这解决了Flatten()层引起的维度问题,但GlobalMaxPooling2D的行为略有不同。

您可以查看这个问题来了解差异。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注