我有一系列小图像,并希望使用它们来进行XCeption CNN的训练。训练和验证集分别具有以下形状:
>(63787, 72, 72, 3)
>(155, 72, 72, 3)
换句话说,我的图像满足Xceptions输入的最小X71,71,3形状的Xception要求。
这是我建立模型的方式
base_model=xception.Xception(include_top=False, weights=None, input_shape=(72, 72, 3))
x = base_model.output
x = Dense(256, activation='relu')(x)
predictions = Dense(classes, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)
opt= SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss="sparse_categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
这是我训练模型的方式
checkpoint = ModelCheckpoint(filename, monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='max', period=self.__model_history_period)
lr_reducer = ReduceLROnPlateau(factor=np.sqrt(0.1), cooldown=0, patience=1000, min_lr=0.5e-6)
early_stopper = EarlyStopping(min_delta=0.0001, patience=10000)
callbacks_list = [checkpoint, lr_reducer, early_stopper]
datagen = ImageDataGenerator(width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True, vertical_flip=True)
datagen.fit(x_train)
# Fit the model on the batches generated by datagen.flow().
H=model.fit_generator(datagen.flow(X_train, y_train,batch_size=self.__bs), steps_per_epoch=X_train.shape[0] // self.__bs, validation_data=(x_val, y_val), epochs=self.__epochs, verbose=1, max_q_size=100, callbacks=[checkpoint, lr_reducer, early_stopper])
但是,当我进行CNN训练时,出现以下错误:
> Traceback (most recent call last):
File "esperimento_paper.py", line 86, in <module>
vgg.run_2D()
File "Desktop/PhD-Market-Nets/src/classes/VggHandler.py", line 662, in run_2D
model, H, n_epochs = self.__train_2D(x_train=x_train, y_train=y_train, x_val=x_val, y_val=y_val, index_net=index_net, index_walk=index_walk)
File "Desktop/PhD-Market-Nets/src/classes/VggHandler.py", line 267, in __train_2D
callbacks=[checkpoint, lr_reducer, early_stopper])
File "Desktop/PhD-Market-Nets/venv/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "Desktop/PhD-Market-Nets/venv/lib/python3.6/site-packages/keras/engine/training.py", line 1418, in fit_generator
initial_epoch=initial_epoch)
File "Desktop/PhD-Market-Nets/venv/lib/python3.6/site-packages/keras/engine/training_generator.py", line 144, in fit_generator
val_x, val_y, val_sample_weight)
File "Desktop/PhD-Market-Nets/venv/lib/python3.6/site-packages/keras/engine/training.py", line 789, in _standardize_user_data
exception_prefix='target')
File "Desktop/PhD-Market-Nets/venv/lib/python3.6/site-packages/keras/engine/training_utils.py", line 128, in standardize_input_data
'with shape ' + str(data_shape))
ValueError: Error when checking target: expected dense_2 to have 4 dimensions, but got array with shape (155, 1)
我在这里想念什么?
答案 0 :(得分:3)
问题出在这一行:
x = base_model.output
x = Dense(256, activation='relu')(x)
设置include_top=False
时-返回的是形状为[number of examples, h, w, number of features]
的特征图。将Dense
应用于此要素地图时-您仅将其应用于最后一个维度(类似于1x1卷积)。这就是为什么输出为4D
的原因。为了克服这种尝试:
x = base_model.output
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
Flatten
将h
,w
和number of features
压缩为一个尺寸。多亏了您的网络可以正常工作。
PS。您也可以尝试使用GlobalMaxPooling2D
或其平均版本。这样可以跳过过滤器的空间位置,但会大大降低模型的内存占用量。