嗨,我是Keras的新手,有后端张量流。我已经构建了两个可能类的图像的训练和验证集;我的网络必须以两个类是或否结束。我使用ImageDatagenerator从文件夹中读取图像并准备培训和验证集。最后,我得到了标题中描述的问题。我的猜测是ImageDatagenerator没有像我想的那样准备数据;任何机构都可以向我解释如何解决它,这里是代码(谢谢):
# Data Preparation
# dimensions of our images.
img_width, img_height = 256, 256
#top_model_weights_path = 'bottleneck_fc_model.h5'
train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
nb_train_samples = 2
nb_validation_samples = 2
epochs = 50
batch_size = 1
num_classes = 2
# prepare data augmentation configuration
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
data_format=K.image_data_format(),
horizontal_flip=True)
test_datagen = ImageDataGenerator(
rescale=1. / 255,
data_format=K.image_data_format())
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='binary')
# create the CNN model
model = Sequential()
model.add(Conv2D(24, kernel_size=(20, 20), strides=(2,2), padding='valid', activation='relu', input_shape=(256,256,3)))
model.add(MaxPooling2D(pool_size=(7, 7), strides=(2,2), padding='valid'))
# Avoiding overfitting
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
# Avoiding overfitting
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
print(model.summary())
# Compile model
sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mean_squared_error', optimizer='sgd', metrics=['accuracy'])
# Fit the model
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,
verbose=0)
# Save the weights
model.save_weights('first_try.h5')
答案 0 :(得分:3)
特别针对“两个班级”,有两种方法:
每个人都需要不同的模型输出:
Dense(1,....)
Dense(2,....)
你似乎是第一种情况,所以,改变你的最后一层。
该错误意味着什么?
您的模型输出形状(BatchSize,2),但您的类标签具有形状(BatchSize,1)。
答案 1 :(得分:0)
如果您需要单个输出来预测0或1,则只需将最后一层更改为
model.add(Dense(1, activation='softmax'))
但是,如果每个类都需要两个输出,则对训练和验证生成器使用分类类模式,即
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical')
<...>
model.add(Dense(2, activation='softmax'))
总结: