def create_model(input, num_class=2): """构建训练模型. Parameters ---------- input : paddle.data 输入的 placeholder. num_class : int 输出分类有几类. Returns ------- type 构建好的模型. """ if cfg.TRAIN.ARCHITECTURE == "unet_plain": return unet_plain(input, num_class, cfg.TRAIN.INPUT_SIZE) if cfg.TRAIN.ARCHITECTURE == "res_unet": return res_unet(input, num_class, cfg.TRAIN.INPUT_SIZE) if cfg.TRAIN.ARCHITECTURE == "hrnet": return hrnet(input, num_class) if cfg.TRAIN.ARCHITECTURE == "deeplabv3p": return deeplabv3p(input, num_class) raise Exception("错误的网络类型: {}".format(cfg.TRAIN.ARCHITECTURE))
def create_network(train_image, train_label, classes, network='unet_simple', image_size=(1024, 384), for_test=False): if network == 'unet_base': predict = unet_base(train_image, classes, image_size) elif network == 'unet_simple': predict = unet_simple(train_image, classes, image_size) elif network == 'deeplabv3p': predict = deeplabv3p(train_image, classes) else: raise Exception('Not support this model:', network) print('The program will run', network) if for_test == False: loss, miou = create_loss(predict, train_label, classes) return loss, miou, predict elif for_test == True: return predict else: raise Exception('Wrong Status:', for_test)
def main(): batch_size = 16 # 训练ckpt和inf模型路径 param_base_dir = os.path.join(code_base_dir, "params") param_base_dir = os.path.join(param_base_dir, args.net) infer_param_path = os.path.join(param_base_dir, "inf") ckpt_param_path = os.path.join(param_base_dir, "ckpt") print(infer_param_path) print(ckpt_param_path) test_reader = paddle.batch(data_reader(8, 10, True), batch_size) train_program = fluid.Program() train_init = fluid.Program() with fluid.program_guard(train_program, train_init): image = fluid.layers.data(name="image", shape=[3, 512, 512], dtype="float32") label = fluid.layers.data(name="label", shape=[1, 512, 512], dtype="int32") if args.net == "unet_simple": prediction = unet_simple(image, 2, [512, 512]) elif args.net == "unet_base": prediction = unet_base(image, 2, [512, 512]) elif args.net == "deeplabv3": prediction = deeplabv3p(image, 2) else: print("错误的网络类型") sys.exit(0) avg_loss = create_loss(prediction, label, 2) miou = mean_iou(prediction, label, 2) decay = paddle.fluid.regularizer.L2Decay(0.0001) optimizer = fluid.optimizer.AdamOptimizer(learning_rate=0.003, regularization=decay) # optimizer = fluid.optimizer.MomentumOptimizer(learning_rate=0.006, momentum=0.8,regularization=decay) optimizer.minimize(avg_loss) place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace() exe = fluid.Executor(place) exe.run(train_init) # fluid.io.load_persistables(exe, ckpt_param_path, train_init) exe_test = fluid.Executor(place) test_program = train_program.clone(for_test=True) train_program = fluid.CompiledProgram(train_program).with_data_parallel( loss_name=avg_loss.name) # test_program = fluid.CompiledProgram(test_program).with_data_parallel(loss_name=avg_loss.name) train_feeder = fluid.DataFeeder(place=place, feed_list=[image, label]) test_feeder = fluid.DataFeeder(place=place, feed_list=[image, label]) step = 1 best_miou = 0 train_prompt = "Train_miou" test_prompt = "Test_miou" # plot_prompt = Ploter(train_prompt, test_prompt) writer = LogWriter(logdir="../log/{}".format(datetime.now())) train_reader = fluid.io.xmap_readers(aug_mapper, data_reader(0, 8), 8, 32, False) step = 0 eval_step = 1000 for pass_id in range(args.num_epochs): batch = [] for record in train_reader(): batch.append(record) if len(batch) == batch_size: step += 1 avg_loss_value, miou_value = exe.run( train_program, feed=train_feeder.feed(batch), fetch_list=[avg_loss, miou]) batch = [] writer.add_scalar(tag="train_loss", step=step, value=avg_loss_value[0]) if step % 10 == 0: print( "\t\tTrain pass {}, Step {}, Cost {}, Miou {}".format( pass_id, step, avg_loss_value[0], miou_value[0])) eval_miou = 0 if step % eval_step == 0: auc_metric = fluid.metrics.Auc("AUC") test_losses = [] test_mious = [] for test_data in test_reader(): _, test_loss, test_miou = exe_test.run( test_program, feed=test_feeder.feed(test_data), fetch_list=[prediction, avg_loss, miou]) test_losses.append(test_loss[0]) test_mious.append(test_miou[0]) eval_miou = np.average(np.array(test_mious)) writer.add_scalar(tag="test_loss", step=step / eval_step, value=np.average(np.array(test_losses))) print("Test loss: {} ,miou: {}".format( np.average(np.array(test_losses)), eval_miou)) if math.isnan(float(avg_loss_value[0])): sys.exit("got NaN loss, training failed.") if step % eval_step == 0 and eval_miou > best_miou: best_miou = eval_miou print("Saving params of step: %d" % step) fluid.io.save_inference_model( infer_param_path, feeded_var_names=["image"], target_vars=[prediction], executor=exe, main_program=train_program, ) # fluid.io.save_persistables(exe, ckpt_param_path, train_program) print(best_miou)
def main(): batch_size = 32 num_epochs = args.num_epochs net_type = args.net # 训练ckpt和inf模型路径 param_base_dir = os.path.join(code_base_dir, 'params') param_base_dir = os.path.join(param_base_dir, net_type) infer_param_path = os.path.join(param_base_dir, "inf") ckpt_param_path = os.path.join(param_base_dir, "ckpt") print(infer_param_path) print(ckpt_param_path) train_reader = paddle.batch( paddle.reader.shuffle(data_reader(), int(batch_size * 1.5)), batch_size) test_reader = paddle.batch( paddle.reader.shuffle(data_reader(8, 10), int(batch_size * 1.5)), batch_size) train_program = fluid.Program() train_init = fluid.Program() with fluid.program_guard(train_program, train_init): image = fluid.layers.data(name='image', shape=[3, 512, 512], dtype='float32') label = fluid.layers.data(name='label', shape=[1, 512, 512], dtype='int32') train_loader = fluid.io.DataLoader.from_generator( feed_list=[image, label], capacity=batch_size) test_loader = fluid.io.DataLoader.from_generator( feed_list=[image, label], capacity=batch_size) if net_type == "unet_simple": prediction = unet_simple(image, 2, [512, 512]) elif net_type == "unet_base": prediction = unet_base(image, 2, [512, 512]) elif net_type == "deeplabv3": prediction = deeplabv3p(image, 2) else: print("错误的网络类型") sys.exit(0) avg_loss = create_loss(prediction, label, 2) miou = mean_iou(prediction, label, 2) # decay=paddle.fluid.regularizer.L2Decay(0.1) # optimizer = fluid.optimizer.SGD(learning_rate=0.0005,regularization=decay) # optimizer = fluid.optimizer.DecayedAdagradOptimizer(learning_rate=0.02,regularization=decay) # optimizer = fluid.optimizer.RMSProp(learning_rate=0.1,momentum=0.8,centered=True, regularization=decay) optimizer = fluid.optimizer.AdamOptimizer(learning_rate=0.003) # optimizer = fluid.optimizer.MomentumOptimizer(learning_rate=0.006, momentum=0.8,regularization=decay) optimizer.minimize(avg_loss) place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace() exe = fluid.Executor(place) exe.run(train_init) # fluid.io.load_persistables(exe, ckpt_param_path, train_init) exe_test = fluid.Executor(place) test_program = train_program.clone(for_test=True) # train_program=fluid.CompiledProgram(train_program).with_data_parallel(loss_name=avg_loss.name) test_program = fluid.CompiledProgram(test_program).with_data_parallel( loss_name=avg_loss.name) train_loader.set_sample_list_generator(train_reader, places=place) test_loader.set_sample_list_generator(test_reader, places=place) feeder = fluid.DataFeeder(place=place, feed_list=[image, label]) step = 1 best_miou = 0 train_prompt = "Train_miou" test_prompt = "Test_miou" plot_prompt = Ploter(train_prompt, test_prompt) for pass_id in range(num_epochs): for data_train in train_loader(): avg_loss_value, miou_value = exe.run(train_program, feed=data_train, fetch_list=[avg_loss, miou]) if step % 10 == 0: print("\t\tTrain pass %d, Step %d, Cost %f, Miou %f" % (pass_id, step, avg_loss_value[0], miou_value[0])) # if step % 10 ==0: # plot_prompt.append(train_prompt, step, miou_value[0]) # plot_prompt.plot() eval_miou = 0 if step % 100 == 0: auc_metric = fluid.metrics.Auc("AUC") test_losses = [] test_mious = [] for _, test_data in enumerate(test_loader()): # print(test_data) # input("pause") _, test_loss, test_miou = exe_test.run( test_program, feed=test_data, fetch_list=[prediction, avg_loss, miou]) test_losses.append(test_loss[0]) test_mious.append(test_miou[0]) eval_miou = np.average(np.array(test_mious)) # plot_prompt.append(test_prompt, step, eval_miou) # plot_prompt.plot() print("Test loss: %f ,miou: %f" % (np.average(np.array(test_losses)), eval_miou)) if math.isnan(float(avg_loss_value[0])): sys.exit("got NaN loss, training failed.") if step % 100 == 0 and param_base_dir is not None and eval_miou > best_miou: best_miou = eval_miou print("Saving params of step: %d" % step) fluid.io.save_inference_model(infer_param_path, feeded_var_names=['image'], target_vars=[prediction], executor=exe, main_program=train_program) fluid.io.save_persistables(exe, ckpt_param_path, train_program) step += 1 print(best_miou)