训练图像的准确性好,但测试的准确性差

时间:2020-04-20 22:41:12

标签: python tensorflow machine-learning keras

我遵循了Adrian Rosebrock(多标签分类tutorial)教程对手数字符号进行分类,我已经使用VGGnet模型和Keras库训练了一个模型,训练后显示的图表明该模型应该可以正常工作。

该模型仅在训练图像上给出了良好的预测,但是当我从网络上测试图像时,它给出了错误的预测,

  • 对训练图像的良好预测:

enter image description here

  • 对测试图像的错误预测(来自网络)

enter image description here

enter image description here

  • 源代码:
EPOCHS = 30
INIT_LR = 1e-3
BS = 16
IMAGE_DIMS = (130, 130, 3)

# grab the image paths and randomly shuffle them
print("[INFO] loading images...")
imagePaths = sorted(list(paths.list_images(args["dataset"])))
random.seed(42)
random.shuffle(imagePaths)

data = []
labels = []

for imagePath in imagePaths:
    # load the image, pre-process it, and store it in the data list
    image = cv2.imread(imagePath)
    image = cv2.resize(image, (IMAGE_DIMS[1], IMAGE_DIMS[0]))
    image = img_to_array(image)
    data.append(image)


    l = label = imagePath.split(os.path.sep)[-2].split("_")
    labels.append(l)

data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)
print("[INFO] data matrix: {} images ({:.2f}MB)".format(
    len(imagePaths), data.nbytes / (1024 * 1000.0)))


print("[INFO] class labels:")
mlb = MultiLabelBinarizer()
labels = mlb.fit_transform(labels)

for (i, label) in enumerate(mlb.classes_):
    print("{}. {}".format(i + 1, label))


(trainX, testX, trainY, testY) = train_test_split(data,
    labels, test_size=0.2, random_state=42)

aug = ImageDataGenerator(rotation_range=25, width_shift_range=0.1,
    height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,
    horizontal_flip=False, fill_mode="nearest")


print("[INFO] compiling model...")
model = SmallerVGGNet.build(
    width=IMAGE_DIMS[1], height=IMAGE_DIMS[0],
    depth=IMAGE_DIMS[2], classes=len(mlb.classes_),
    finalAct="sigmoid")

opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)

# compile the model using binary cross-entropy rather than
# categorical cross-entropy -- this may seem counterintuitive for
# multi-label classification, but keep in mind that the goal here
# is to treat each output label as an independent Bernoulli
# distribution
model.compile(loss="binary_crossentropy", optimizer=opt,
    metrics=["accuracy"])

# train the network
print("[INFO] training network...")
H = model.fit_generator(
    aug.flow(trainX, trainY, batch_size=BS),
    validation_data=(testX, testY),
    steps_per_epoch=len(trainX) // BS,
    epochs=EPOCHS, verbose=1)

使模型对测试图像做出错误预测的原因是什么?

0 个答案:

没有答案