sdl = SimpleDatasetLoader(preprocessors=[aap, iap])
(data, labels) = sdl.load(imagePaths, verbose=500)
data = data.astype("float") / 255.0

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

trainY = LabelBinarizer().fit_transform(trainY)
testY = LabelBinarizer().fit_transform(testY)

print("[INFO] compiling model ...")
opt = SGD(lr=0.05)
model = MiniVGGNet.build(width=64, height=64, depth=3, classes=len(classNames))
model.compile(loss="categorical_crossentropy",
              optimizer=opt,
              metrics=["accuracy"])

print("[INFO] training network ...")
H = model.fit(trainX,
              trainY,
              validation_data=(testX, testY),
              batch_size=32,
              epochs=100,
              verbose=1)

print("[INFO] evaluating network ...")
predictions = model.predict(testX, batch_size=32)
print(
    "ship", "truck"
]

# construct the image generator for data augmentation
aug = ImageDataGenerator(rotation_range=10,
                         width_shift_range=0.1,
                         height_shift_range=0.1,
                         horizontal_flip=True,
                         fill_mode="nearest")

# loop over the number of models to train
for i in np.arange(0, args["num_models"]):
    # initialize the optimizer and model
    print("[INFO] training model {}/{}".format(i + 1, args["num_models"]))
    opt = SGD(lr=0.01, decay=0.01 / 40, momentum=0.9, nesterov=True)
    model = MiniVGGNet.build(width=32, height=32, depth=3, classes=10)
    model.compile(loss="categorical_crossentropy",
                  optimizer=opt,
                  metrics=["accuracy"])

    # train the network
    H = model.fit(aug.flow(trainX, trainY, batch_size=64),
                  validation_data=(testX, testY),
                  epochs=40,
                  steps_per_epoch=len(trainX) // 64,
                  verbose=1)

    # save the model to disk
    p = [args["models"], "model_{}.model".format(i)]
    model.save(os.path.sep.join(p))
Example #3
0
((trainX, trainY), (testX, testY)) = cifar10.load_data()
trainX = trainX.astype('float') / 255.0
testX = testX.astype('float') / 255.0
lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)
labelNames = [
    'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse',
    'ship', 'truck'
]

print('[INFO] compiling model...')
opt = SGD(lr=0.01, decay=0.01 / 40, momentum=0.9, nesterov=True)
model = MiniVGGNet.build(width=32,
                         height=32,
                         depth=3,
                         classes=10,
                         useBatchNormalization=useBatchNormalization)
model.compile(loss='categorical_crossentropy',
              optimizer=opt,
              metrics=['accuracy'])

print('[INFO] training network...')
H = model.fit(trainX,
              trainY,
              validation_data=(testX, testY),
              batch_size=64,
              epochs=40,
              verbose=2)

print('[INFO] serializing network...')