Exemple #1
0
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)
Exemple #2
0
def main():
    # network = 'deeplabv3p'
    # save_model_path = "./model_weights/" + network + "_"
    # model_path = "./model_weights/" + network + "_0_6000"
    data_dir = ''
    val_percent = .1

    epochs = 9

    kwargs = {
        'num_workers': 4,
        'pin_memory': True
    } if torch.cuda.is_available() else {}
    training_dataset = LaneDataset(
        "~/workspace/myDL/CV/week8/Lane_Segmentation_pytorch/data_list/train.csv",
        transform=transforms.Compose(
            [ImageAug(),
             DeformAug(),
             ScaleAug(),
             CutOut(32, 0.5),
             ToTensor()]))

    training_data_batch = DataLoader(training_dataset,
                                     batch_size=2,
                                     shuffle=True,
                                     drop_last=True,
                                     **kwargs)

    dataset = BasicDataset(data_dir,
                           img_size=cfg.IMG_SIZE,
                           crop_offset=cfg.crop_offset)

    n_val = int(len(dataset) * val_percent)
    n_train = len(dataset) - n_val
    train, val = random_split(dataset, [n_train, n_val])

    train_loader = DataLoader(train,
                              batch_size=cfg.batch_size,
                              shuffle=True,
                              num_workers=8,
                              pin_memory=True)
    val_loader = DataLoader(val,
                            batch_size=cfg.batch_size,
                            shuffle=False,
                            num_workers=8,
                            pin_memory=True)

    model = unet_base(cfg.num_classes, cfg.IMG_SIZE)
    model.cuda()

    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=cfg.base_lr,
                                 betas=(0.9, 0.99))

    bce_criterion = nn.BCEWithLogitsLoss()
    dice_criterion = MulticlassDiceLoss()

    model.train()
    epoch_loss = 0

    dataprocess = tqdm(training_data_batch)
    for batch_item in dataprocess:
        image, mask = batch_item['image'], batch_item['mask']
        if torch.cuda.is_available():
            image, mask = image.cuda(), mask.cuda()
            image = image.to(torch.float32).requires_grad_()
            mask = mask.to(torch.float32).requires_grad_()

            masks_pred = model(image)
            masks_pred = torch.argmax(masks_pred, dim=1)
            masks_pred = masks_pred.to(torch.float32)
            mask = mask.to(torch.float32)

            # print('mask_pred:', masks_pred)
            # print('mask:', mask)
            loss = bce_criterion(masks_pred, mask) + dice_criterion(
                masks_pred, mask)
            epoch_loss += loss.item()

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
Exemple #3
0
def main():
    # network = 'deeplabv3p'
    # save_model_path = "./model_weights/" + network + "_"
    # model_path = "./model_weights/" + network + "_0_6000"
    data_dir = '/env/data_list_for_Lane_Segmentation/train.csv'
    val_percent = .1

    epochs = 9

    dataset = BasicDataset(data_dir,
                           img_size=cfg.IMG_SIZE,
                           crop_offset=cfg.crop_offset)

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    n_val = int(len(dataset) * val_percent)
    n_train = len(dataset) - n_val
    train, val = random_split(dataset, [n_train, n_val])

    train_loader = DataLoader(train,
                              batch_size=cfg.batch_size,
                              shuffle=True,
                              num_workers=0,
                              pin_memory=True)
    val_loader = DataLoader(val,
                            batch_size=cfg.batch_size,
                            shuffle=False,
                            num_workers=0,
                            pin_memory=True)

    model = unet_base(cfg.num_classes, cfg.IMG_SIZE)

    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=cfg.base_lr,
                                 betas=(0.9, 0.99))

    bce_criterion = nn.BCEWithLogitsLoss()
    dice_criterion = MulticlassDiceLoss()

    for epoch in range(epochs):
        model.train()

        epoch_loss = 0
        with tqdm(total=n_train,
                  desc=f'Epoch {epoch + 1}/{epochs}',
                  unit='img') as pbar:
            for batch in train_loader:
                imgs = batch['image']
                true_masks = batch['mask']

                imgs = imgs.to(device=device, dtype=torch.float32)
                mask_type = torch.float32 if cfg.num_classes == 1 else torch.long
                true_masks = true_masks.to(device=device, dtype=mask_type)

                masks_pred = model(imgs)

                loss = bce_criterion(masks_pred, true_masks) + dice_criterion(
                    masks_pred, true_masks)
                epoch_loss += loss.item()

                pbar.set_postfix(**{'loss (batch)': loss.item()})

                optimizer.zero_grad()
                loss.backward()
                optimizer.step()

                pbar.update(imgs.shape[0])

                global_step += 1
                if global_step % (len(dataset) // (10 * cfg.batch_size)) == 0:
                    val_score = eval_net(model, val_loader, device, n_val)
                    print('val_score:', val_score)
Exemple #4
0
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)
Exemple #5
0
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)