2017-08-26 250 views
1

我使用keras申請轉移與RESNET 50和V3開始學習,但一直在預測得到[[ 0.]]Keras model.predict始終爲0

下面的代碼是一個二元分類問題的時候。我也嘗試過vgg19和vgg16,但它們工作正常,它的正確資源和初始。數據集是50/50分割。而且我只更改每個模型的model = applications.resnet50.ResNet50代碼行。

下面

是代碼:

from keras.callbacks import EarlyStopping 
early_stopping = EarlyStopping(monitor='val_loss', patience=2) 

img_width, img_height = 256, 256 
train_data_dir = xxx 
validation_data_dir = xxx 
nb_train_samples = 14000 
nb_validation_samples = 6000 
batch_size = 16 
epochs = 50 

if K.image_data_format() == 'channels_first': 
    input_shape = (3, img_width, img_height) 
else: 
    input_shape = (img_width, img_height, 3) 

model = applications.resnet50.ResNet50(weights = "imagenet", include_top=False, input_shape = (img_width, img_height, 3)) 


    from keras.callbacks import EarlyStopping 
early_stopping = EarlyStopping(monitor='val_loss', patience=2) 

img_width, img_height = 256, 256 
train_data_dir = xxx 
validation_data_dir = xxx 
nb_train_samples = 14000 
nb_validation_samples = 6000 
batch_size = 16 
epochs = 50 

if K.image_data_format() == 'channels_first': 
    input_shape = (3, img_width, img_height) 
else: 
    input_shape = (img_width, img_height, 3) 

model = applications.resnet50.ResNet50(weights = "imagenet", include_top=False, input_shape = (img_width, img_height, 3)) 


#Freeze the layers which you don't want to train. Here I am freezing the first 5 layers. 
for layer in model.layers[:5]: 
    layer.trainable = False 

#Adding custom Layers 
x = model.output 
x = Flatten()(x) 
x = Dense(1024, activation="relu")(x) 
x = Dropout(0.5)(x) 
#x = Dense(1024, activation="relu")(x) 
predictions = Dense(1, activation="sigmoid")(x) 

# creating the final model 
model_final = Model(input = model.input, output = predictions) 

# compile the model 
model_final.compile(loss = "binary_crossentropy", optimizer = optimizers.SGD(lr=0.0001, momentum=0.9), metrics=["accuracy"]) 


# Initiate the train and test generators with data Augumentation 
train_datagen = ImageDataGenerator(
    rescale=1./255, 
    shear_range=0.2, 
    zoom_range=0.2, 
    horizontal_flip=True) 

test_datagen = ImageDataGenerator(
    rescale=1./255, 
    shear_range=0.2, 
    zoom_range=0.2, 
    horizontal_flip=True) 

train_generator = train_datagen.flow_from_directory(
    train_data_dir, 
    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), 
    batch_size=batch_size, 
    class_mode='binary') 

# Save the model according to the conditions 
#checkpoint = ModelCheckpoint("vgg16_1.h5", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1) 
#early = EarlyStopping(monitor='val_acc', min_delta=0, patience=10, verbose=1, mode='auto') 



model_final.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, 
    callbacks=[early_stopping]) 



from keras.models import load_model 
import numpy as np 
from keras.preprocessing.image import img_to_array, load_img 

#test_model = load_model('vgg16_1.h5') 
img = load_img('testn7.jpg',False,target_size=(img_width,img_height)) 
x = img_to_array(img) 
x = np.expand_dims(x, axis=0) 
#preds = model_final.predict_classes(x) 
prob = model_final.predict(x, verbose=0) 
#print(preds) 
print(prob) 

注意model_final.evaluate_generator(validation_generator, nb_validation_samples)提供了諸如80%的預期準確性它只是預測,始終是0

只是覺得奇怪,vgg19和vgg16做工精細但不resnet50和成立。這些模型是否需要別的東西來工作?

任何見識都會很棒。

在此先感謝。

+0

你如何做你的預處理?我在你的代碼中看不到它,這可能是你獲得這些結果的原因。您需要從Inception3或ResNet導入適當的預處理函數,並使用它來準備圖像(即從inception_v3導入InceptionV3,preprocess_input')。 – petezurich

+0

還需要刪除'rescale = 1。/255'。否則,圖像數組將重新調整兩次。 ('inception_v3.preprocess_input()'已經爲你完成了) –

+0

謝謝我會試試這個,preprocessing_input還有什麼功能呢?我找不到任何文檔。 – Stig

回答

0

謝謝! preprocessing_input()作品:)

+0

你可以[編輯]你的答案來解釋* preprocessing_input()是如何工作的?它有什麼作用?你是怎麼稱呼它的? –

0

我遇到了類似的問題。訓練期間,您將所有RGB值從0-255縮放到0-1。

在預測時應該做同樣的事情。 嘗試 x = img_to_array(img) x = x/255