我有两个类别,每个类别包含相等数量的图片
火车
测试
我的代码
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")
答案 0 :(得分:0)
由于某些原因,我无法访问您的训练脚本-只能检查您的屏幕截图。
我认为您的问题不是过拟合-您的模型实际上不是在学习,因为损失不会减少,而准确性却保持在同一水平。请仔细检查您的网络,学习率(最重要)和预处理。同样,您可以考虑加载VGG16预训练砝码,或者如果完成则不加载。
或者只是发布您的代码,我有机会看看。
更新:
根据您的代码,我发现您没有在VGG16内部进行任何更改-这很容易调试。
最好