optimizer=keras.optimizers.Adam(lr=lr)) model_all.compile(loss={ 'classification': cls_loss(), 'regression': smooth_l1(), 'dense_class_{}'.format(NUM_CLASSES): class_loss_cls, 'dense_regress_{}'.format(NUM_CLASSES): class_loss_regr(NUM_CLASSES - 1) }, optimizer=keras.optimizers.Adam(lr=lr)) gen = Generator(bbox_util, lines[:num_train], NUM_CLASSES, Batch_size, input_shape=[input_shape[0], input_shape[1]]).generate() gen_val = Generator(bbox_util, lines[num_train:], NUM_CLASSES, Batch_size, input_shape=[input_shape[0], input_shape[1]]).generate() epoch_size = num_train // Batch_size epoch_size_val = num_val // Batch_size if epoch_size == 0 or epoch_size_val == 0: raise ValueError("数据集过小,无法进行训练,请扩充数据集。")
np.random.seed(10101) np.random.shuffle(lines) np.random.seed(None) num_val = int(len(lines)*val_split) num_train = len(lines) - num_val if True: lr = 1e-4 Init_Epoch = 0 Freeze_Epoch = 25 optimizer = optim.Adam(model.parameters(),lr) lr_scheduler = optim.lr_scheduler.StepLR(optimizer,step_size=1,gamma=0.9) gen = Generator(lines[:num_train],(IMAGE_SHAPE[0],IMAGE_SHAPE[1])).generate() gen_val = Generator(lines[num_train:],(IMAGE_SHAPE[0],IMAGE_SHAPE[1])).generate() epoch_size = EPOCH_LENGTH epoch_size_val = int(EPOCH_LENGTH/10) # ------------------------------------# # 冻结一定部分训练 # ------------------------------------# for param in model.extractor.parameters(): param.requires_grad = False for epoch in range(Init_Epoch,Freeze_Epoch): fit_ont_epoch(model,epoch,epoch_size,epoch_size_val,gen,gen_val,Freeze_Epoch) lr_scheduler.step() if True:
model_all.summary() model_rpn.load_weights(base_net_weights, by_name=True) model_classifier.load_weights(base_net_weights, by_name=True) with open(annotation_path) as f: lines = f.readlines() np.random.seed(10101) np.random.shuffle(lines) np.random.seed(None) # 每个世代训练数据集长度的步数 # 根据数据集大小进行指定 EPOCH_LENGTH = len(lines) gen = Generator(bbox_util, lines, NUM_CLASSES, solid=True) rpn_train = gen.generate() log_dir = "logs" # 训练参数设置 logging = TensorBoard(log_dir=log_dir) callback = logging callback.set_model(model_all) model_rpn.compile(loss={ 'regression': smooth_l1(), 'classification': cls_loss() }, optimizer=keras.optimizers.Adam(lr=Learning_rate)) model_classifier.compile( loss=[class_loss_cls, class_loss_regr(NUM_CLASSES - 1)], metrics={'dense_class_{}'.format(NUM_CLASSES): 'accuracy'},