Example #1
0
    fill_mode="nearest",
    horizontal_flip=True,
    preprocessing_function=train_preprocessors.preprocess)

val_aug = ImageDataGenerator(
    preprocessing_function=val_preprocessors.preprocess)

train_generator = train_aug.flow_from_directory(directory=configs.TRAIN_IMAGE,
                                                batch_size=configs.BATCH_SIZE)
val_generator = val_aug.flow_from_directory(directory=configs.TEST_IMAGE,
                                            batch_size=configs.BATCH_SIZE)

# callback
path = os.path.sep.join([configs.OUTPUT_PATH, f"{os.getpid()}.png"])
callbacks = [
    TrainingMonitor(plot_path=path),
]

# initialize optimizer and model
opt = Adam(lr=1e-3)
model = AlexNet.build(width=227, height=227, classes=configs.NUM_CLASSES)

# compile and train model
model.compile(opt, loss="binary_crossentropy", metrics=["accuracy"])
model.fit(
    train_generator,
    epochs=EPOCH,
    steps_per_epoch=train_generator.samples // configs.BATCH_SIZE,
    validation_data=val_generator,
    validation_steps=val_generator.samples // configs.BATCH_SIZE,
    max_queue_size=configs.BATCH_SIZE * 2,
Example #2
0
                                preprocessors=[pp, mp, iap],
                                classes=2)
valGen = HDF5DatasetGenerator(config.VAL_HDF5,
                              32,
                              preprocessors=[sp, mp, iap],
                              classes=2)

# initialize the optimizer
print("[INFO] compiling model...")
opt = Adam(lr=1e-3)
model = AlexNet.build(width=227, height=227, depth=3, classes=2, reg=0.0002)
model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"])

# construct the set of callbacks
path = os.path.sep.join([config.OUTPUT_PATH, "{}.png".format(os.getpid())])
callbacks = [TrainingMonitor(path)]

# train the network
model.fit_generator(trainGen.generator(),
                    steps_per_epoch=trainGen.numImages // 32,
                    validation_data=valGen.generator(),
                    validation_steps=valGen.numImages // 32,
                    epochs=75,
                    max_queue_size=128 * 2,
                    callbacks=callbacks,
                    verbose=1)

# save the model to file
print("[INFO] serializing model...")
model.save(config.MODEL_PATH, overwrite=True)
    'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse',
    'ship', 'truck'
]

# initialize the SGD optimizer, but without any learning rate decay
print("[INFO] compiling model...")
opt = SGD(lr=0.01, 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"])

# construct the set of callbacks
figPath = os.path.sep.join([args['output'], '{}.png'.format(os.getpid())])
jsonPath = os.path.sep.join([args['output'], '{}.json'.format(os.getpid())])
callbacks = [TrainingMonitor(figPath, jsonPath=jsonPath)]

# train the network
print('[INFO] training network...')
model.fit(trainX,
          trainY,
          validation_data=(testX, testY),
          batch_size=64,
          epochs=100,
          callbacks=callbacks,
          verbose=2)
Example #4
0
else:
    # load the checkpoint from disk
    print("[INFO] loading {}...".format(args["model"]))
    model = load_model(args["model"])

    # update the learning rate
    print("[INFO] old learning rate: {}".format(K.get_value(
        model.optimizer.lr)))
    K.set_value(model.optimizer.lr, 1e-2)
    print("[INFO] new learning rate: {}".format(K.get_value(
        model.optimizer.lr)))

# build the path to the training plot and training history
plotPath = os.path.sep.join(["output", "resnet_fashion_mnist.png"])
jsonPath = os.path.sep.join(["output", "resnet_fashion_mnist.json"])

# construct the set of callbacks
callbacks = [
    EpochCheckpoint(args["checkpoints"], every=2, startAt=args["start_epoch"]),
    TrainingMonitor(plotPath, jsonPath=jsonPath, startAt=args["start_epoch"])
]

# train the network
print("[INFO] training network...")
model.fit_generator(aug.flow(trainX, trainY, batch_size=128),
                    validation_data=(testX, testY),
                    steps_per_epoch=len(trainX) // 128,
                    epochs=80,
                    callbacks=callbacks,
                    verbose=1)
Example #5
0
checkpoint = ModelCheckpoint(filepath=frame,
                             monitor='val_loss',
                             mode='min',
                             save_best_only=True,
                             verbose=1)
figPath = os.path.sep.join([
    '/home/pavel/PycharmProjects/nn/pyimagesearch/plot/',
    "{}_.png".format(os.getpid())
])
jsonPath = os.path.sep.join([
    '/home/pavel/PycharmProjects/nn/pyimagesearch/plot/',
    "{}.json".format(os.getpid())
])
callbacks = [
    checkpoint,
    TrainingMonitor(jsonPath=jsonPath, figPath=figPath, val=False)
]
print("[INFO] training network...")
H = model.fit(x=trainX,
              y=trainY,
              validation_data=(testX, testY),
              batch_size=64,
              epochs=30,
              callbacks=callbacks,
              verbose=1)
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, 40), H.history["loss"], label="train_loss")
plt.plot(np.arange(0, 40), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, 40), H.history["acc"], label="train_acc")
plt.plot(np.arange(0, 40), H.history["val_acc"], label="val_acc")
Example #6
0
    "ship", "truck"
]

# initialize the optimizer and model
print("[INFO] compiling model...")
opt = SGD(lr=0.01, decay=0.01 / 50, momentum=0.9, nesterov=True)
model = MiniVGGNetwork.build(width=32, height=32, depth=3, classes=10)
model.compile(loss="categorical_crossentropy",
              optimizer=opt,
              metrics=["accuracy"])

# construct the set of  callbacks
# initialize the training monitor
figPath = os.path.sep.join([args["output"], "{}.png".format(os.getpid())])
jsonPath = os.path.sep.join([args["output"], "{}.json".format(os.getpid())])
training_monitor = TrainingMonitor(figPath=figPath, jsonPath=jsonPath)

# initialize the checkpoint improvements
checkpoint_improvements = ModelCheckpoint(args["weight"],
                                          monitor="val_loss",
                                          mode="min",
                                          save_best_only=True,
                                          verbose=1)

# initialize the callbacks
callbacks = [training_monitor, checkpoint_improvements]

# train the network
print("[INFO] training network...")
model.fit(X_train,
          y_train,