我尝试实现Keras模型的VGG-16架构,但是我收到错误消息,抱怨检查目标形状。
img_width, img_height = 512, 560
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
# build the VGG16 network
model = Sequential([
Conv2D(64, (3, 3), input_shape=input_shape, padding='same', activation='relu'),
Conv2D(64, (3, 3), activation='relu', padding='same'),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
Conv2D(128, (3, 3), activation='relu', padding='same'),
Conv2D(128, (3, 3), activation='relu', padding='same',),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
Conv2D(256, (3, 3), activation='relu', padding='same',),
Conv2D(256, (3, 3), activation='relu', padding='same',),
Conv2D(256, (3, 3), activation='relu', padding='same',),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
Conv2D(512, (3, 3), activation='relu', padding='same',),
Conv2D(512, (3, 3), activation='relu', padding='same',),
Conv2D(512, (3, 3), activation='relu', padding='same',),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
Conv2D(512, (3, 3), activation='relu', padding='same',),
Conv2D(512, (3, 3), activation='relu', padding='same',),
Conv2D(512, (3, 3), activation='relu', padding='same',),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
Flatten(),
Dense(4096, activation='relu'),
Dense(4096, activation='relu'),
Dense(1000, activation='softmax')
])
model.summary()
model.compile(loss = 'binary_crossentropy',
optimizer = 'rmsprop',
metrics = ['accuracy'])
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_datagen = ImageDataGenerator(
rotation_range = 180,
width_shift_range = 0.2,
height_shift_range = 0.2,
brightness_range = (0.8, 1.2),
rescale = 1. / 255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
vertical_flip = True
)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
#target_size=(224, 224),
target_size = (img_width, img_height),
batch_size = batch_size,
class_mode ='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size = (img_width, img_height),
#target_size=(224, 224),
batch_size = batch_size,
class_mode = 'binary'
)
history = model.fit_generator(
train_generator,
steps_per_epoch = nb_train_samples // batch_size,
epochs = epochs,
validation_data = validation_generator,
validation_steps = nb_validation_samples // batch_size)
我试图训练并拟合模型,但是下面显示了错误消息,如何解决此问题?看来我最后一个致密层的尺寸为1000,为什么它仍在抱怨呢?
Found 576 images belonging to 2 classes.
Found 145 images belonging to 2 classes.
Epoch 1/50
Traceback (most recent call last):
File "Trimer_useful_life_VGG.py", line 109, in <module>
validation_steps = nb_validation_samples // batch_size)
File "/home/hliu/.conda/envs/hliuPython/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/home/hliu/.conda/envs/hliuPython/lib/python3.6/site-packages/keras/engine/training.py", line 1418, in fit_generator
initial_epoch=initial_epoch)
File "/home/hliu/.conda/envs/hliuPython/lib/python3.6/site-packages/keras/engine/training_generator.py", line 217, in fit_generator
class_weight=class_weight)
File "/home/hliu/.conda/envs/hliuPython/lib/python3.6/site-packages/keras/engine/training.py", line 1211, in train_on_batch
class_weight=class_weight)
File "/home/hliu/.conda/envs/hliuPython/lib/python3.6/site-packages/keras/engine/training.py", line 789, in _standardize_user_data
exception_prefix='target')
File "/home/hliu/.conda/envs/hliuPython/lib/python3.6/site-packages/keras/engine/training_utils.py", line 138, in standardize_input_data
str(data_shape))
ValueError: Error when checking target: expected dense_3 to have shape (1000,) but got array with shape (1,)
答案 0 :(得分:3)
此模式配置为输出1000个类别,要用于两个类别和binary_crossentropy
丢失,应将最后一层更改为:
Dense(1, activation='sigmoid')
此配置允许将二进制分类为0-1,如果需要更多类别,则需要将类别数放在最后的Dense
中,并使用softmax
激活。