准确度停留在50%的Keras

时间:2018-07-29 14:48:52

标签: python machine-learning keras conv-neural-network pre-trained-model

代码

import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential,Model
from keras.layers import Dropout, Flatten, Dense,Input
from keras import applications
from keras.preprocessing import image
from keras import backend as K
K.set_image_dim_ordering('tf')


# dimensions of our images.
img_width, img_height = 150,150

top_model_weights_path = 'bottleneck_fc_model.h5'
train_data_dir = 'Cats and Dogs Dataset/train'
validation_data_dir = 'Cats and Dogs Dataset/validation'
nb_train_samples = 20000
nb_validation_samples = 5000
epochs = 50
batch_size = 16
input_tensor = Input(shape=(150,150,3))

base_model=applications.VGG16(include_top=False, weights='imagenet',input_tensor=input_tensor)
for layer in base_model.layers:
    layer.trainable = False

top_model=Sequential()
top_model.add(Flatten(input_shape=base_model.output_shape[1:]))
top_model.add(Dense(256,activation="relu"))
top_model.add(Dropout(0.5))
top_model.add(Dense(1,activation='softmax'))
top_model.load_weights(top_model_weights_path)
model = Model(inputs=base_model.input,outputs=top_model(base_model.output))


datagen = ImageDataGenerator(rescale=1. / 255)

train_data = datagen.flow_from_directory(train_data_dir,target_size=(img_width, img_height),batch_size=batch_size,classes=['dogs', 'cats'],class_mode="binary",shuffle=False)


validation_data = datagen.flow_from_directory(validation_data_dir,target_size=(img_width, img_height),classes=['dogs', 'cats'], batch_size=batch_size,class_mode="binary",shuffle=False)


model.compile(optimizer='adam',loss='binary_crossentropy', metrics=['accuracy'])

model.fit_generator(train_data, steps_per_epoch=nb_train_samples//batch_size, epochs=epochs,validation_data=validation_data, shuffle=False,verbose=

我已经使用keras(使用VGG16网络学习的转移)在猫和狗的数据集(https://www.kaggle.com/c/dogs-vs-cats/data)上实现了图像分类器。该代码运行时没有错误,但在大约一半的时期内,精度停留在0.0%处,而在一半之后,精度提高到50%。我正在将Atom与氢气一起使用。

My directory

Results of execution

我该如何解决。我真的不认为我对VGG16这样的数据集有偏见(尽管我在这个领域还比较陌生)。

1 个答案:

答案 0 :(得分:4)

将您在输出层的激活更改为S型

来自

top_model.add(Dense(1,activation='softmax')) 

top_model.add(Dense(1,activation='sigmoid'))