我正在嘗試將資料增強作為一個層添加到模型中,但我遇到了我認為是形狀問題的問題。我也嘗試在增強層中指定輸入形狀。當我從模型中取出data_augmentation圖層時,它運行良好。
preprocessing.RandomFlip('horizontal', input_shape=(224, 224, 3))
data_augmentation_layer = keras.Sequential([
preprocessing.RandomFlip('horizontal'),
preprocessing.RandomRotation(0.2),
preprocessing.RandomZoom(0.2),
preprocessing.RandomWidth(0.2),
preprocessing.RandomHeight(0.2),
preprocessing.RandomContrast(0.2)
], name='data_augmentation')
model = keras.Sequential([
data_augmentation_layer,
Conv2D(filters=32,
kernel_size=1,
strides=1,
input_shape=(224, 224, 3)),
Activation(activation='relu'),
MaxPool2D(),
Conv2D(filters=32,
kernel_size=1,
strides=1),
Activation(activation='relu'),
MaxPool2D(),
Flatten(),
Dense(1, activation='sigmoid')
])```
The last dimension of the inputs to a Dense layer should be defined. Found None. Full input shape received: (None, None)
Call arguments received:
? inputs=tf.Tensor(shape=(None, 224, 224, 3), dtype=float32)
? training=True
? mask=None
uj5u.com熱心網友回復:
層RandomWidth和RandomHeight導致此錯誤,因為它們導致None尺寸:請參閱此處的評論:
[...]RandomHeight 將導致高度維度上的 None 形狀,因為并非圖層的所有輸出都將具有相同的高度(按設計)。這對于像 Conv2D 層這樣的東西是可以的,它可以接受可變形狀的影像輸入(在某些維度上沒有形狀)。
這不適用于然后呼叫 Flatten 后跟 Dense,因為展平的批次也將具有可變大小(因為高度可變),并且 Dense 層需要最后一個維度的固定形狀。您可能會在密集之前填充 flatten 的輸出,但如果您想要這種架構,您可能只想避免導致可變輸出形狀的影像增強層。
因此Flatten,例如,您可以使用GlobalMaxPool2D不需要事先知道其他維度的層,而不是使用層:
import tensorflow as tf
data_augmentation_layer = tf.keras.Sequential([
tf.keras.layers.RandomFlip('horizontal',
input_shape=(224, 224, 3)),
tf.keras.layers.RandomRotation(0.2),
tf.keras.layers.RandomZoom(0.2),
tf.keras.layers.RandomWidth(0.2),
tf.keras.layers.RandomHeight(0.2),
tf.keras.layers.RandomContrast(0.2)
], name='data_augmentation')
model = tf.keras.Sequential([
data_augmentation_layer,
tf.keras.layers.Conv2D(filters=32,
kernel_size=1,
strides=1),
tf.keras.layers.Activation(activation='relu'),
tf.keras.layers.MaxPool2D(),
tf.keras.layers.Conv2D(filters=32,
kernel_size=1,
strides=1),
tf.keras.layers.Activation(activation='relu'),
tf.keras.layers.GlobalMaxPool2D(),
tf.keras.layers.Dense(1, activation='sigmoid')
])
print(model.summary())
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
data_augmentation (Sequenti (None, None, None, 3) 0
al)
conv2d_8 (Conv2D) (None, None, None, 32) 128
activation_8 (Activation) (None, None, None, 32) 0
max_pooling2d_6 (MaxPooling (None, None, None, 32) 0
2D)
conv2d_9 (Conv2D) (None, None, None, 32) 1056
activation_9 (Activation) (None, None, None, 32) 0
global_max_pooling2d_1 (Glo (None, 32) 0
balMaxPooling2D)
dense_4 (Dense) (None, 1) 33
=================================================================
Total params: 1,217
Trainable params: 1,217
Non-trainable params: 0
_________________________________________________________________
None
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/443836.html
上一篇:'KerasClassifier'物件沒有屬性'summary'-嘗試從KerasClassifier構建的lstm模型中獲取摘要
下一篇:一張一張保存GAN生成的圖片
