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 callbacks = [ EpochCheckpoint(args["checkpoints"], every=5, startAt=args["start_epoch"]), TrainingMonitor(config.FIG_PATH, jsonPath=config.JSON_PATH, startAt=args["start_epoch"]) ] # train the network model.fit_generator(trainGen.generator(), steps_per_epoch=trainGen.numImages // 64, validation_data=valGen.generator(), validation_steps=valGen.numImages // 64, epochs=10, max_queue_size=10, callbacks=callbacks, verbose=1) # close the databases trainGen.close()
# otherwise, load the checkpoint from disk else: print("[INFO] loding {}...".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 figPath = os.path.sep.join([config.OUTPUT_PATH, "vggnet_emotion_4.png"]) jsonPath = os.path.sep.join([config.OUTPUT_PATH, "vggnet_emotion_4.json"]) callbacks = [ EpochCheckpoint(args["checkpoints"], every = 5, startAt = args["start_epoch"]), TrainingMonitor(figPath, jsonPath = jsonPath, startAt = args["start_epoch"]) ] # train 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 dataset
iap = ImageToArrayPreprocessor() # initialize the training and validation dataset generators trainGen = HDF5DatasetGenerator(config.TRAIN_HDF5, 64, aug=aug, preprocessors=[sp, mp, iap], classes=config.NUM_CLASSES) valGen = HDF5DatasetGenerator(config.VAL_HDF5, 64, preprocessors=[sp, mp, iap], classes=config.NUM_CLASSES) # initialize the set of callbacks callbacks = [ TrainingMonitor(config.FIG_PATH, jsonPath=config.JSON_PATH), LearningRateScheduler(poly_decay) ] # initialize the optimizer and model (ResNet56) print("[INFO] compiling model...") model = ResNet.build(64, 64, 3, config.NUM_CLASSES, (3, 4, 6), (64, 128, 256, 512), reg=0.0005, dataset="tiny_imagenet") opt = SGD(lr=INIT_LR, momentum=0.9, nesterov=True) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
# initialize the training and validation dataset generators trainGen = HDF5DatasetGenerator(config.TRAIN_HDF5, 128, aug = aug, preprocessors = [pp, mp, iap], classes = 2) valGen = HDF5DatasetGenerator(config.VAL_HDF5, 128, 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 // 128, validation_data = valGen.generator(), validation_steps = valGen.numImages // 128, epochs = 75, max_queue_size = 10, callbacks = callbacks, verbose = 1 ) # save the model to file print("[INFO] serializing model...") model.save(config.MODEL_PATH, overwrite = True)
lb = LabelBinarizer() trainY = lb.fit_transform(trainY) testY = lb.transform(testY) # initialize the label name for CIFAR-10 dataset labelNames = ["airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"] # construct the image generator for data augmentation aug = ImageDataGenerator(width_shift_range = 0.1, height_shift_range = 0.1, 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, jsonPath = jsonPath), LearningRateScheduler(poly_decay)] # initialize the optimizer and model print("[INFO] compiling model...") # opt = SGD(lr = INIT_LR, momentum = 0.9, nesterov = True) opt = Adam(lr = INIT_LR) model = MiniVGGNet.build(width = 32, height = 32, depth = 3, classes = 10) model.compile(loss = "categorical_crossentropy", optimizer = opt, metrics = ["accuracy"]) # train the network print("[INFO] training network...") model.fit_generator(aug.flow(trainX, trainY, batch_size = 64), validation_data = (testX, testY), steps_per_epoch = len(trainX) // 64, epochs = NUM_EPOCHS, callbacks = callbacks, verbose = 1)
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-3) print("[INFO] new learning rate: {}".format(K.get_value( model.optimizer.lr))) # construct the set of callbacks callbacks = [ EpochCheckpoint(args["checkpoints"], every=5, startAt=args["start_epoch"]), TrainingMonitor("output/resnet56_cifar10_3.png", jsonPath="output/resnet56_cifar10_3.json", startAt=args["start_epoch"]) ] # train the network print("[INFO] training network...") model.fit_generator(aug.flow(trainX, trainY, batch_size=64), validation_data=(testX, testY), steps_per_epoch=len(trainX) // 64, epochs=10, callbacks=callbacks, verbose=1) # evaluate network print("[INFO] evaluating network...") predictions = model.predict(testX, batch_size=64)
labelNames = [ "airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck" ] # construct the image generator for data augmentation aug = ImageDataGenerator(width_shift_range=0.1, height_shift_range=0.1, 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, jsonPath=jsonPath), LearningRateScheduler(poly_decay) ] # initialize the optimizer and model print("[INFO] compiling model...") opt = SGD(lr=INIT_LR, momentum=0.9) model = MiniGoogLeNet.build(width=32, height=32, depth=3, classes=10) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the network print("[INFO] training network...") model.fit_generator(aug.flow(trainX, trainY, batch_size=64), validation_data=(testX, testY),