簡體   English   中英

問題過擬合模型 VGG16 小數據集

[英]problem overfitting model VGG16 small dataset

問題截圖

我有兩個類,每個類都包含相同數量的圖片

火車

  • 一班360度火車圖片
  • 二班360度火車圖片

測試

  • 90張測試圖片第一類
  • 90張測試圖片二類

我的代碼

def load_split(basePath, csvPath):
    data = []
    labels = []

    rows = open(csvPath).read().strip().split("\n")[1:]
    random.shuffle(rows)

    for (i, row) in enumerate(rows):
        if i > 0:
            print("[INFO] processed {} total images".format(i))

        (label, imagePath) = row.strip().split(",")[-2:]

        imagePath = os.path.sep.join([basePath, imagePath])
        image = io.imread(imagePath)

        image = transform.resize(image, (224, 224))
        image = exposure.equalize_adapthist(image, clip_limit=0.1)

        data.append(image)
        labels.append(int(label))

    data = np.array(data)
    labels = np.array(labels)

    return (data, labels)

ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True,
    help="path to input GTSRB")
ap.add_argument("-m", "--model", required=True,
    help="path to output model")
ap.add_argument("-p", "--plot", type=str, default="plot.png",
    help="path to training history plot")
args = vars(ap.parse_args())

NUM_EPOCHS = 30
INIT_LR = 1e-4
BS = 64

labelNames = open("signnames.csv").read().strip().split("\n")[1:]
labelNames = [l.split(",")[1] for l in labelNames]

trainPath = os.path.sep.join([args["dataset"], "Train.csv"])
testPath = os.path.sep.join([args["dataset"], "Test.csv"])

print("[INFO] loading training and testing data...")
(trainX, trainY) = load_split(args["dataset"], trainPath)
(testX, testY) = load_split(args["dataset"], testPath)

trainX = (trainX-np.mean(trainX))/np.std(trainX)
testX = (testX-np.mean(testX))/np.std(testX)

numLabels = len(np.unique(trainY))
trainY = to_categorical(trainY, numLabels)
testY = to_categorical(testY, numLabels)

classTotals = trainY.sum(axis=0)
classWeight = classTotals.max() / classTotals

aug = ImageDataGenerator(
    rotation_range=10,
    zoom_range=0.15,
    width_shift_range=0.1,
    height_shift_range=0.1,
    shear_range=0.15,
    horizontal_flip=False,
    vertical_flip=False,
    fill_mode="nearest")

print("[INFO] compiling model...")
opt = Adam(lr=INIT_LR)
model = TrafficSignNet.build(width=224, height=224, depth=3,
    classes=numLabels)
model.compile(loss="categorical_crossentropy", optimizer=opt,
    metrics=["accuracy"])

print("[INFO] training network...")
H = model.fit_generator(
    aug.flow(trainX, trainY, batch_size=BS),
    validation_data=(testX, testY),
    steps_per_epoch=trainX.shape[0] // BS,
    epochs=NUM_EPOCHS,
    class_weight=classWeight,
    verbose=1)

print("[INFO] evaluating network...")
predictions = model.predict(testX, batch_size=BS)
print(classification_report(testY.argmax(axis=1),
    predictions.argmax(axis=1), target_names=labelNames))

print("[INFO] serializing network to '{}'...".format(args["model"]))
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
model.save_weights("model.h5")
print("Saved model to disk")

Enddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd

由於某種原因,我無法訪問您的訓練腳本 - 只能查看您的屏幕截圖。

我認為您的問題不是過度擬合 - 您的模型實際上沒有學習,因為在准確性保持在同一水平的同時損失並沒有減少。 請仔細檢查您的網絡、學習率(最重要)和預處理。 此外,您可以考慮加載 VGG16 預訓練重量,或者如果您已經加載,則不加載。

或者直接貼出你的代碼,有機會我可以看看。

更新:

根據您的代碼,我發現您沒有在 VGG16 內部進行任何更改 - 這很容易調試。

  1. 檢查您的訓練和測試集,並確保課程分布均勻
  2. 打印標簽(Y 測試和訓練),仔細檢查它們是否正確。
  3. 嘗試標准化 X 訓練和測試,而不是除以 255。 x=(x-mean)/std
  4. 嘗試將學習率設為 0.0001(我發現它通常適用於基於 VGG16 的網絡)
  5. 第一次保持簡單。 不要使用衰減優化,只需先嘗試標准 ADAM

最好的,

暫無
暫無

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

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