Python 使用Exception CNN训练自定义图像时出现奇怪错误
我有一系列的小图片,我想用它们来训练一个例外的CNN。培训和验证集分别具有以下形状:Python 使用Exception CNN训练自定义图像时出现奇怪错误,python,tensorflow,keras,neural-network,deep-learning,Python,Tensorflow,Keras,Neural Network,Deep Learning,我有一系列的小图片,我想用它们来训练一个例外的CNN。培训和验证集分别具有以下形状: >(63787, 72, 72, 3) >(155, 72, 72, 3) 换句话说,我的图像满足71,71,3最小异常输入形状的异常要求 这就是我构建模型的方式 base_model=xception.Xception(include_top=False, weights=None, input_shape=(72, 72, 3)) x = base_model.output
>(63787, 72, 72, 3)
>(155, 72, 72, 3)
换句话说,我的图像满足71,71,3最小异常输入形状的异常要求
这就是我构建模型的方式
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])
这就是我训练模特的方式
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)
我在这里遗漏了什么?问题在于这一行:
x = base_model.output
x = Dense(256, activation='relu')(x)
设置include_top=False时,返回的是形状的特征映射[示例数,h,w,特征数]。当你对这个特征映射应用稠密时——你只对最后一个维度应用它,它类似于1x1卷积。这就是为什么输出为4D。为了克服这种情况,请尝试:
x = base_model.output
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
展平将h、w和特征数维度挤压为单个维度。多亏了这一点,您的网络应该可以正常工作
另外,你也可以尝试使用或其平均版本。这将跳过过滤器的空间位置,但将显著减少模型的内存占用。问题在于这一行:
x = base_model.output
x = Dense(256, activation='relu')(x)
设置include_top=False时,返回的是形状的特征映射[示例数,h,w,特征数]。当你对这个特征映射应用稠密时——你只对最后一个维度应用它,它类似于1x1卷积。这就是为什么输出为4D。为了克服这种情况,请尝试:
x = base_model.output
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
展平将h、w和特征数维度挤压为单个维度。多亏了这一点,您的网络应该可以正常工作
另外,你也可以尝试使用或其平均版本。这将跳过过滤器的空间位置,但将显著减少模型的内存占用。我希望我能拥抱你以感谢你!成功了!谢谢我希望我能拥抱你来感谢你!成功了!谢谢