我试图建立一个模型来检测输入图像是否是某个东西(例如,狗或不是)。我在用凯拉斯编码,但准确度太差了。你知道要把这个调准吗?或者我应该使用除keras以外的其他工具来解决一个类分类问题?提前非常感谢。
这是我到目前为止写的代码和输出。
train_dir = './path/to/train_dir'
vali_dir = './path/to/validation_dir'
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=False)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(150, 150),
batch_size=20,
class_mode='binary')
vali_datagen = ImageDataGenerator(rescale=1./255)
vali_generator = vali_datagen.flow_from_directory(
vali_dir,
target_size=(150, 150),
batch_size=20,
class_mode='binary')
model = Sequential()
model.add(Conv2D(16, 3, activation='relu', input_shape=(150, 150, 3)))
model.add(MaxPool2D(pool_size=2))
model.add(Conv2D(32, 3, activation='relu'))
model.add(MaxPool2D(pool_size=2))
model.add(Conv2D(64, 3, activation='relu'))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))
model.compile(
loss='binary_crossentropy',
optimizer=RMSprop(lr=0.003),
metrics=['acc']
)
history = model.fit_generator(
train_generator,
steps_per_epoch=100,
epochs=8,
verbose=2,
validation_data=vali_generator,
validation_steps=20
)
输出:
Found 3379 images belonging to 2 classes.
Found 607 images belonging to 2 classes.
Epoch 1/8
- 136s - loss: 7.6617 - acc: 0.5158 - val_loss: 10.5220 - val_acc: 0.3400
Epoch 2/8
- 124s - loss: 7.7837 - acc: 0.5118 - val_loss: 10.5220 - val_acc: 0.3400
.......and this is just terrible.
最佳答案
类标签似乎有问题-它们与数据是否正确相关?您可以检查它或发布ImageDataGenerator代码