问题过度拟合模型VGG16小型数据集

时间:2020-04-01 19:03:15

标签: python-3.x

Screenshot of the problem

我有两个类别,每个类别包含相等数量的图片

火车

  • 第一堂课的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")

Enddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd

1 个答案:

答案 0 :(得分:0)

由于某些原因,我无法访问您的训练脚本-只能检查您的屏幕截图。

我认为您的问题不是过拟合-您的模型实际上不是在学习,因为损失不会减少,而准确性却保持在同一水平。请仔细检查您的网络,学习率(最重要)和预处理。同样,您可以考虑加载VGG16预训练砝码,或者如果完成则不加载。

或者只是发布您的代码,我有机会看看。

更新:

根据您的代码,我发现您没有在VGG16内部进行任何更改-这很容易调试。

  1. 检查您的培训和测试集,并确保课程均匀分布
  2. 打印出标签(Y测试和培训),仔细检查其是否正确。
  3. 尝试标准化X训练并进行测试,而不是除以255。x =(x-mean)/ std
  4. 尝试将学习率设为0.0001(我发现这对于基于VGG16的网络通常来说是很好的选择)
  5. 在第一时间保持简单。不要使用衰减的优化,只需先尝试使用标准ADAM

最好