model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # otherwise, load the checkpoint from disk else: 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-5) print("[INFO] new learning rate: {}...".format(K.get_value(model.optimizer.lr))) # construct the set of callbacks fname = os.path.sep.join([args['checkpoints'], "weights.{epoch:03d}-{val_loss:.4f}.hdf5"]) checkpoint = ModelCheckpoint(fname, monitor="val_loss", mode="min", save_best_only=True, verbose=1) monitor = TrainingMonitor(config.FIG_PATH, json_path=config.JSON_PATH, start_at=args["start_epoch"]) callbacks = [checkpoint, monitor] # train the network model.fit_generator( trainGen.generator(), steps_per_epoch=trainGen.numImages // bs, validation_data=valGen.generator(), validation_steps=valGen.numImages // bs, epochs=10, max_queue_size=bs * 2, callbacks=callbacks, verbose=1) # close the HDF5 datasets trainGen.close()
model = load_model(args["model"]) # update the learning rate print("Old learning rate: {}".format(K.get_value(model.optimizer.lr))) K.set_value(model.optimizer.lr, 1e-3) print("New learning rate: {}".format(K.get_value(model.optimizer.lr))) # construct the set of callbacks figPath = os.path.sep.join([config.OUTPUT_PATH, "vggnet_emotion.png"]) jsonPath = os.path.sep.join([config.OUTPUT_PATH, "vggnet_emotion.json"]) callbacks = [ EpochCheckpoint(args["checkpoints"], every=5, startAt=args["start_epoch"]), TrainingMonitor(figPath, jsonPath=jsonPath, startAt=args["start_epoch"]) ] # train the network model.fit_generator(trainGen.generator(), steps_per_epoch=trainGen.numImages // config.BATCH_SIZE, validation_data=valGen.generator(), validation_steps=valGen.numImages // config.BATCH_SIZE, epochs=15, max_queue_size=config.BATCH_SIZE * 2, callbacks=callbacks, verbose=1) # close the databases trainGen.close() valGen.close()
# loop over all layers in the base model and freeze them so they # will not be updated during the training process for layer in baseModel.layers: layer.trainable = False # compile our model print("[INFO]: Compiling model....") optimizer = RMSprop(lr=0.001) model.compile(loss="categorical_crossentropy", optimizer=optimizer, 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)] # Construct the callback to save only the 'best' model to disk based on the validation loss checkpoint = ModelCheckpoint(args['weights'], monitor="val_loss", mode="min", save_best_only=True, verbose=1) callbacks.append(checkpoint) # train the head of the network for a few epoches (all other # laysers are frozen) -- this will allow the new FC layers to # start to become initialize with actual "learned" values # versus pure random print("[INFO]: Training head....") H = model.fit_generator(aug.flow(trainX, trainY, batch_size=batch_size),
horizontal_flip=True, fill_mode="nearest") # initialize the optimizer and model print("[INFO] compiling model...") opt = SGD(lr=INIT_LR, momentum=0.9) model = ResNet.build(32, 32, 3, 10, (9, 9, 9), (64, 64, 128, 256), reg=0.0005) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # construct the set of callbacks fig_path = os.path.sep.join([args["output"], "{}.png".format(os.getpid())]) json_path = os.path.sep.join([args["output"], "{}.json".format(os.getpid())]) callbacks = [ TrainingMonitor(fig_path, json_path), LearningRateScheduler(poly_decay) ] # train the network bs = 128 model.fit_generator(aug.flow(trainX, trainY, batch_size=bs), validation_data=(testX, testY), steps_per_epoch=len(trainX) // bs, epochs=100, callbacks=callbacks, verbose=1) # save the network do disk print("[INFO] serializing network...") model.save(args["model"])
K.set_value(model.optimizer.lr, args['learning_rate']) print("[INFO] new learning rate: {}...".format( K.get_value(model.optimizer.lr))) # construct the set of callbacks os.mkdir( os.path.sep.join([args['checkpoints'], "lr_" + str(args['learning_rate'])])) fname = os.path.sep.join([ args['checkpoints'], "lr_" + str(args['learning_rate']), "{epoch:03d}-{val_loss:.4f}.hdf5" ]) checkpoint = ModelCheckpoint(fname, monitor="val_loss", mode="min", save_best_only=True, verbose=1) monitor = TrainingMonitor("output/resnet56_cifar10.png", json_path="output/resnet56_cifar10.json", start_at=args["start_epoch"]) callbacks = [checkpoint, monitor] # train the network bs = 128 model.fit_generator(aug.flow(trainX, trainY, batch_size=bs), validation_data=(testX, testY), steps_per_epoch=len(trainX) // bs, epochs=100, callbacks=callbacks, verbose=1)
testY = lb.transform(testY) # construct the image generator for data augmentation aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode="nearest") # 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, json_path=jsonPath), LearningRateScheduler(poly_decay) ] # Initialize the optimizer and model optimizer = SGD(lr=INIT_LR, momentum=0.9, nesterov=True) model = MiniGoogLeNet.build(width=32, height=32, depth=3, classes=10) model.compile(loss="categorical_crossentropy", optimizer=optimizer, metrics=["accuracy"]) # Train the network model.fit_generator(aug.flow(trainX, trainY, batch_size=32), validation_data=(testX, testY), steps_per_epoch=len(trainX) // 32, epochs=NUM_EPOCHS,
test_y = lb.transform(test_y) # Initialize the label names for the CIFAR-10 dataset label_names = [ "airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck" ] # Initialize the SGD optimizer, but without any learning rate decay print("[INFO]: Compiling model....") optimizer = SGD(lr=0.01, momentum=0.9, nesterov=True) model = MiniVGGNet.build(width=32, height=32, depth=3, classes=10) model.compile(loss="categorical_crossentropy", optimizer=optimizer, metrics=["accuracy"]) # Construct the set of callbacks fig_path = os.path.sep.join([args["output"], "{}.png".format(os.getpid())]) json_path = os.path.sep.join([args["output"], "{}.json".format(os.getpid())]) callbacks = [TrainingMonitor(fig_path, json_path)] # train the network print("[INFO]: Training....") model.fit(train_x, train_y, validation_data=(test_x, test_y), batch_size=64, epochs=100, callbacks=callbacks, verbose=1)
preprocessors=[pp, mp, iap], classes=2) valGen = HDF5DatasetGenerator(config.VAL_HDF5, batchSize=bs, 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 // bs, validation_data=valGen.generator(), validation_steps=valGen.numImages // bs, epochs=75, max_queue_size=bs * 2, callbacks=callbacks, verbose=1) # save the model to file print("[INFO] serializing model...") model.save(config.MODEL_PATH, overwrite=True)