![](/img/trans.png)
[英]Invalid Shape Error when trying to leverage Keras's VGG16 pretrained model
[英]Shape error when fitting my data to VGG16 cnn --Keras
我想使用 VGG16 作為 cnn 使用數據增強和遷移學習對狗品種進行分類。
首先,我使用 keras 中的 ImageDataGenerator 進行一些數據增強
train_datagen = ImageDataGenerator(rotation_range = 30,
width_shift_range = 0.2,
height_shift_range = 0.2,
rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
fill_mode = 'nearest')
train_generator = train_datagen.flow_from_directory('../data/train/',
target_size = (224, 224),
batch_size = batch_size,
class_mode = 'categorical')
flow_from_directory
方法返回一個 DirectoryIterator 產生 (x, y) 的元組,其中 x 是 numpy 數組,其中包含一批形狀為 (batch_size, *target_size, channels) 的圖像,y 是對應標簽的 numpy 數組。 因為這里的 class_mode 是分類模式,所以它應該返回 y 的 2D one-hot 編碼標簽。
然后我進行遷移學習,只刪除最后一層,用一個帶有 softmax 激活的密集層替換它。
model = VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3))
for layer in model.layers:
layer.trainable = False
x = model.output
predictions = Dense(120, activation='softmax')(x)
new_model = Model(inputs=model.input, outputs=predictions)
然后我將我的數據擬合到 model:
new_model.fit_generator(train_generator,
steps_per_epoch = 6680 // batch_size,
epochs = 50,
validation_data = validation_generator,
validation_steps = 835 // batch_size,
verbose=2)
我得到錯誤: ValueError: Error when checks target: expected dense_3 to have 4 dimensions, but got array with shape (16, 120)
我不知道問題出在哪里:(
謝謝你的幫助 !
VGG16的總結給出:
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 224, 224, 3)] 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
_________________________________________________________________
block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
_________________________________________________________________
block2_conv2 (Conv2D) (None, 112, 112, 128) 147584
_________________________________________________________________
block2_pool (MaxPooling2D) (None, 56, 56, 128) 0
_________________________________________________________________
block3_conv1 (Conv2D) (None, 56, 56, 256) 295168
_________________________________________________________________
block3_conv2 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_conv3 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_pool (MaxPooling2D) (None, 28, 28, 256) 0
_________________________________________________________________
block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160
_________________________________________________________________
block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_pool (MaxPooling2D) (None, 14, 14, 512) 0
_________________________________________________________________
block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_pool (MaxPooling2D) (None, 7, 7, 512) 0
=================================================================
Total params: 14,714,688
Trainable params: 14,714,688
Non-trainable params: 0
_________________________________________________________________
最后一層具有 3-d 特征,您需要在應用 Dense 和 softmax 之前將其展平。
在最后一個密集層之前添加一個Flatten()
。
x = model.output
x = Flatten()(x) # add this line
predictions = Dense(120, activation='softmax')(x)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.