簡體   English   中英

Model 提供了很好的訓練和測試准確性,但在進行預測時效果不佳

[英]Model gives great training and testing accuracy but not working well when doing prediction

您好,我正在使用 VGG16 預訓練的 model 進行深度學習的遷移學習。 我想從 VGG16 中提取特征來構建我自己的 model,因為我只能訪問 CPU。 這是我的構建和訓練設置。

import os
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix

from keras.applications import Xception, VGG16, ResNet50
conv_base = VGG16(weights='imagenet',include_top=False,input_shape=(224, 224, 3))

conv_base.summary()
base_dir = 'NewDCDatatset'

train_dir = os.path.join(base_dir, 'Train')
validation_dir = os.path.join(base_dir, 'Validation')
test_dir = os.path.join(base_dir, 'Test')

datagen = ImageDataGenerator(rescale=1./255)
batch_size = 5

def extract_features(directory, sample_count):
    features = np.zeros(shape=(sample_count, 7 , 7 , 512))
    labels = np.zeros(shape=(sample_count,2))
    generator = datagen.flow_from_directory(directory,target_size=(224, 224),batch_size=batch_size,class_mode='categorical')
    i = 0
    for inputs_batch, labels_batch in generator:
        features_batch = conv_base.predict(inputs_batch)
        features[i * batch_size : (i + 1) * batch_size] = features_batch
        labels[i * batch_size : (i + 1) * batch_size] = labels_batch
        i += 1
        if i * batch_size >= sample_count:
            break
    return features, labels

train_features, train_labels = extract_features(train_dir, 2000)
validation_features, validation_labels = extract_features(validation_dir,420 )
test_features, test_labels = extract_features(test_dir, 420)

train_features = np.reshape(train_features, (2000, 7 * 7 * 512))
validation_features = np.reshape(validation_features, (420, 7 * 7 * 512))
test_features = np.reshape(test_features, (420, 7 * 7 * 512))

from keras import models
from keras import layers
from keras import optimizers
model = models.Sequential()
model.add(layers.Dense(64, activation='relu', input_dim=7 * 7 * 512))
model.add(layers.Dense(2, activation='softmax'))
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['acc'])
history = model.fit(train_features, train_labels,epochs=2,batch_size=5,shuffle=True)
print(model.evaluate(test_features,test_labels))

model.save('TLFACE.h5')

#predictions = model.predict_generator(test_features,steps = 5)

#print(predictions)

這是我通過 model 預測新圖像以對貓和狗進行分類的方式的設置,但我沒有得到那么准確的結果,我很少能夠正確分類任何圖像。 我不知道我在做什么錯誤,是調整圖像大小還是預測時發生了什么。

from keras.models import load_model
deep = load_model('TLFACE.h5')

from PIL import Image
import numpy as np
import cv2

file_nam = '4705.jpg'
img = cv2.imread(file_nam)
img = cv2.imshow('frame',img)
cv2.waitKey(1000)

img = Image.open(file_nam).convert("L")

img = img.resize((256,98))
im2arr = np.array(img)
im2arr = im2arr.reshape(1,25088)
# Predicting the Test set r1esults
y_pred = deep.predict(im2arr)
print(y_pred)
print(y_pred[0][0])

根據您的代碼,您的 model 接收大小為1 x 7*7*512 = 1 x 25088的特征向量。 此功能是conv_base model 中的圖像編碼(在您的extract_features方法中實現)。

但是,在預測時的示例中,您只需拍攝圖像,將其重塑為(256,98) ,然后展平為(1,25088) 您完全跳過了extract_features階段。 換句話說,您嘗試在完全不同的數據上進行預測,然后在訓練期間使用(以及fit方法下的自動化測試)。 您需要使用完全相同的預處理方法進行測試,這意味着首先將圖像重新縮放為224x224x3大小,然后使用conv_base model 提取特征,最后使用新的 model 對提取的特征執行預測。

話雖如此,我想建議您使用非常“骯臟”的實現,這沒有正確組織,而且很容易混淆。 您實際上想要構建一個對圖像執行分類的 model,而不是執行提取特征等所有繁重的編碼,因此您可以使用以下內容:

pretrained_vgg16 = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
pretrained_vgg16.trainable = False  # This is important if you don't want to modify your base model weights during training.
new_model = models.Sequential([pretrained_vgg16, 
                               layers.Flatten(),
                               layers.Dense(64, activation='relu'),
                               layers.Dense(2,activation='softmax')])

此外,請注意,在訓練期間決定是否要修改預訓練的 model 權重非常重要。 如果沒有,您需要調用pretrained_vgg16.trainable = False行。

現在您可以將new_model.fit與加載圖像並將其重新縮放為224x224x3的生成器一起使用,而無需顯式提取特征。 另一個重要的評論是原始 VGG16 model 可能是在標准化數據上訓練的(這意味着像素值被標准化為在[0,1]范圍內),您還需要對圖像執行相同的預處理以獲得理想結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM