valGen = HDF.HDF5DatasetGenerator(config.VAL_HDF5, 128, preprocessors=[sp, mp, iap], classes=2) # 初始化优化器 print("[INFO] compiling model...") opt = Adam(lr=1e-3) model = alexnet.AlexNet.build(width=227, height=227, depth=3, classes=2, reg=0.0002) model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy']) # callbacks path = os.path.sep.join([config.OUTPUT_PATH, "{}.png".format(os.getpid())]) callbacks = [TM.TrainingMonitor(path)] # 训练网络 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=128 * 2, callbacks=callbacks, verbose=1) # 保存模型文件 print("[INFO] serializing model ....") model.save(config.MODEL_PATH, overwrite=True)
optimizer=opt, metrics=['accuracy']) # 否则从磁盘中加载checkpoints模型 else: print("[INFO] loading {}...".format(args['model'])) model = load_model(args['model']) # 更新学习率 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))) # 回调函数列表 callbacks = [ # checkpoint EPO.EpochCheckpoint(args['checkpoints'], every=5, startAt=args['start_epoch']), # 监控训练过程 TM.TrainingMonitor("output/resnet56_cifar10.png", jsonPath="output/resnet56_cifar10.json", startAt=args['start_epoch']) ] # 训练网络 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=10, callbacks=callbacks, verbose=1)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) 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, experiment_learning_rate) print("[INFO] new learning rate: {}".format(K.get_value(model.optimizer.lr))) # construct the set of callbacks callbacks = [ ECP.EpochCheckpoint(args["checkpoints"], every=5, startAt=args["start_epoch"]), TM.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=experiment_epoch, max_queue_size=64 * 2, callbacks=callbacks, verbose=1) # close the databases trainGen.close() valGen.close()
# 初始化预处理 sp = SIP.SimplePreprocessor(64,64) mp = MP.MeanPreprocessor(means['R'],means['G'],means['B']) iap = ITAP.ImageToArrayPreprocessor() # 训练数据集和验证书籍生成器 trainGen = HDFG.HDF5DatasetGenerator(config.TRAIN_HDF5,64,aug = aug, preprocessors=[sp,mp,iap],classes=config.NUM_CLASSES) valGen = HDFG.HDF5DatasetGenerator(config.VAL_HDF5,64, preprocessors=[sp,mp,iap],classes=config.NUM_CLASSES) # 回调函数列表 figPath = os.path.sep.join([args['output'],"{}.png".format(os.getpid())]) jsonPath = os.path.sep.join([args['output'],"{}.json".format(os.getpid())]) callbacks = [ TM.TrainingMonitor(figPath,jsonPath), LearningRateScheduler(ploy_decay) ] # 初始化模型和优化器 print("[INFO] compiling model...") opt = SGD(lr=INIT_LR,momentum=0.9) model = resnet.ResNet.build(32, 32, 3, config.NUM_CLASSES, (3, 4, 6), (64, 128, 256, 512), reg=0.0005,dataset="tiny_imagenet") model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) #训练网络 print("[INFO] training network....") model.fit_generator( trainGen.generator(), steps_per_epoch = trainGen.numImages // 64, validation_data = valGen.generator(),
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 figPath = os.path.sep.join([config.OUTPUT_PATH, "vggnet_emotion.png"]) jsonPath = os.path.sep.join([config.OUTPUT_PATH, "vggnet_emotion.json"]) callbacks = [ epochcheckpoint.EpochCheckpoint(args["checkpoints"], every=5, startAt=args["start_epoch"]), trainingmonitor.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()
testY = lb.transform(testY) # initialize the label names for the CIFAR-10 dataset labelNames = [ "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.MiniVGGNet.build(width=32, height=32, depth=3, classes=10) 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.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=1)