Exemplo n.º 1
0
                help="Max overlap for NMS")
ap.add_argument("--top_k", default=200, type=int, help="Top k for NMS")
args = ap.parse_args()

batch_size = args.batch_size
workers = args.num_workers
data_folder = args.data_path
trained_model = torch.load(args.trained_model)
model = trained_model["model"]
model = model.to(device)

#Set eval model
model.eval()

#Load test dataset
test_dataset = VOCDataset(data_folder, split="TEST")
test_loader = torch.utils.data.DataLoader(test_dataset,
                                          batch_size=batch_size,
                                          shuffle=False,
                                          collate_fn=combine,
                                          num_workers=workers,
                                          pin_memory=True)


def evaluate(model, test_loader):
    '''
        Evaluate model by caculate mAP
        model: model SSD
        test_loader: Dataloader for test data
        
        Out: mAP for test data
Exemplo n.º 2
0
def main():
    batch_size = 4
    color_mean = (0.485, 0.456, 0.406)
    color_std = (0.229, 0.224, 0.225)

    # data_root_path = os.path.join('/Users', 'shirai1', 'work', 'pytorch_work', 'pytorch_advanced',
    #                               '3_semantic_segmentation', 'data', 'VOCdevkit', 'VOC2012')
    data_root_path = os.path.join('/home', 'yusuke', 'work', 'data',
                                  'VOCdevkit', 'VOC2012')
    train_img_list, train_anno_list, val_img_list, val_anno_list = make_datapath_list(
        data_root_path)
    train_dataset = VOCDataset(train_img_list,
                               train_anno_list,
                               phase='train',
                               transform=DataTransform(input_size=475,
                                                       color_mean=color_mean,
                                                       color_std=color_std))
    val_dataset = VOCDataset(val_img_list,
                             val_anno_list,
                             phase='val',
                             transform=DataTransform(input_size=475,
                                                     color_mean=color_mean,
                                                     color_std=color_std))

    train_dataloader = data.DataLoader(train_dataset,
                                       batch_size=batch_size,
                                       shuffle=True)
    val_dataloader = data.DataLoader(val_dataset,
                                     batch_size=batch_size,
                                     shuffle=False)

    dataloaders_dict = {'train': train_dataloader, 'val': val_dataloader}

    net = PSPNet(n_classes=150)
    net.load_state_dict(torch.load('./weights/pspnet50_ADE20K.pth'))

    n_classes = 21
    net.decode_feature.classification = nn.Conv2d(in_channels=512,
                                                  out_channels=n_classes,
                                                  kernel_size=1,
                                                  stride=1,
                                                  padding=0)
    net.aux.classification = nn.Conv2d(in_channels=256,
                                       out_channels=n_classes,
                                       kernel_size=1,
                                       stride=1,
                                       padding=0)

    def weights_init(m):
        if isinstance(m, nn.Conv2d):
            nn.init.xavier_normal_(m.weight.data)
            if m.bias is not None:
                nn.init.constant_(m.bias, 0.0)

    net.decode_feature.classification.apply(weights_init)
    net.aux.classification.apply(weights_init)

    print('ネットワークの設定完了。学習済みの重みのロード終了')

    # optimizerをネットワークの層の名前ごとにlrを変えて定義
    optimizer = optim.SGD([
        {
            'params': net.feature_conv.parameters(),
            'lr': 1e-3
        },
        {
            'params': net.feature_res_1.parameters(),
            'lr': 1e-3
        },
        {
            'params': net.feature_res_2.parameters(),
            'lr': 1e-3
        },
        {
            'params': net.feature_dilated_res_1.parameters(),
            'lr': 1e-3
        },
        {
            'params': net.feature_dilated_res_2.parameters(),
            'lr': 1e-3
        },
        {
            'params': net.pyramid_pooling.parameters(),
            'lr': 1e-3
        },
        {
            'params': net.decode_feature.parameters(),
            'lr': 1e-2
        },
        {
            'params': net.aux.parameters(),
            'lr': 1e-2
        },
    ],
                          momentum=0.9,
                          weight_decay=0.0001)

    # スケジューラーの設定
    def lambda_epoch(epoch):
        max_epoch = 30
        return math.pow((1 - epoch / max_epoch), 0.9)

    scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda_epoch)

    criterion = PSPLoss(aux_weight=0.4)

    epochs = 30

    train_model(net,
                dataloaders_dict,
                criterion,
                scheduler,
                optimizer,
                num_epochs=epochs)
Exemplo n.º 3
0
from datasets import VOCDataset, load_dataset_from_dokki_jar, load_dataset_from_icdar_jar
import os
import logging
import drawer

os.environ["DATASET_TMP"] = 'C:/Users/gugaime/Documents/Datasets/output'
logging.basicConfig(level=logging.INFO)

if __name__ == "__main__":
    dataset = load_dataset_from_icdar_jar(
        'C:/Users/gugaime/Documents/Datasets/icdar.task1train.zip', "TRAIN")
    dataset = VOCDataset('C:/Users/gugaime/Documents/Datasets/output', "TRAIN")
    dataset.turn_off_augment()
    image, timage, boxes, labels, difficulties = dataset[2]
    logging.info("-Image:%s", timage.shape)
    logging.info("-Boxed:%s", boxes.shape)
    logging.info("-Labels:%s", labels.shape)
    drawer.show_image(image)

    label_map = {}
    label_map[0] = 'x'
    label_map[1] = 'y'

    drawer.show_image_and_boxes(image, boxes, labels, difficulties, label_map)
Exemplo n.º 4
0
def main():
    global start_epoch, label_map, epoch, checkpoint, decay_lr_at
    #Init model or load checkpoint
    if checkpoint is None:
        start_epoch = 0
        model = SSD300(num_classes)
        biases = list()
        not_biases = list()
        for param_name, param in model.named_parameters():
            if param.requires_grad:
                if param_name.endswith(".bias"):
                    biases.append(param)
                else:
                    not_biases.append(param)
        optimizer = optim.SGD(params=[{
            'params': biases,
            "lr": 2 * lr
        }, {
            "params": not_biases
        }],
                              lr=lr,
                              momentum=momentum,
                              weight_decay=weight_decay)

    else:
        checkpoint = torch.load(checkpoint)
        start_epoch = checkpoint['epoch'] + 1
        print('\nLoaded checkpoint from epoch %d.\n' % start_epoch)
        model = checkpoint['model']
        optimizer = checkpoint['optimizer']
        if args.adjust_optim is not None:
            print("Adjust optimizer....")
            print(args.lr)
            biases = list()
            not_biases = list()
            for param_name, param in model.named_parameters():
                if param.requires_grad:
                    if param_name.endswith(".bias"):
                        biases.append(param)
                    else:
                        not_biases.append(param)
            optimizer = optim.SGD(params=[{
                'params': biases,
                "lr": 2 * lr
            }, {
                "params": not_biases
            }],
                                  lr=lr,
                                  momentum=momentum,
                                  weight_decay=weight_decay)

    #Move to default device
    model = model.to(device)
    criterion = MultiBoxLoss(model.default_boxes).to(device)

    train_dataset = VOCDataset(data_folder, split="train")
    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=batch_size,
                                               shuffle=True,
                                               collate_fn=combine,
                                               num_workers=workers,
                                               pin_memory=True)
    epochs = iterations // (len(train_dataset) // batch_size)
    decay_lr_at = [
        it // (len(train_dataset) // batch_size) for it in decay_lr_at
    ]

    for epoch in range(start_epoch, epochs):
        if epoch in decay_lr_at:
            print("Decay learning rate...")
            adjust_lr(optimizer, decay_lr_to)
        #One 's training
        train(train_loader=train_loader,
              model=model,
              criterion=criterion,
              optimizer=optimizer,
              epoch=epoch)

        #Save
        save_checkpoint(epoch, model, optimizer)
Exemplo n.º 5
0
def train():
    model = vgg.VGG16_LargeFOV()
    model.load_state_dict(torch.load(init_model_path))
    model = torch.nn.DataParallel(model, device_ids=device_ids)
    model = model.to(device)
    optimizer = torch.optim.SGD(
        params=[
            {
                'params': get_params(model, '1x'),
                'lr': lr,
                'weight_decay': weight_decay
            },
            {
                'params': get_params(model, '2x'),
                'lr': lr * 2,
                'weight_decay': 0
            },
            {
                'params': get_params(model, '10x'),
                'lr': lr * 10,
                'weight_decay': weight_decay
            },
            {
                'params': get_params(model, '20x'),
                'lr': lr * 20,
                'weight_decay': 0
            },
        ],
        momentum=0.9,
    )
    # optimizer = torch.optim.SGD(params=model.parameters(), lr=lr, momentum=0.9, weight_decay=weight_decay) # for val mIoU = 69.6

    print('Set data...')
    train_loader = torch.utils.data.DataLoader(
        VOCDataset(split='train_aug',
                   crop_size=321,
                   is_scale=False,
                   is_flip=True),
        # VOCDataset(split='train_aug', crop_size=321, is_scale=True, is_flip=True), # for val mIoU = 69.6
        batch_size=batch_size,
        shuffle=True,
        num_workers=4,
        drop_last=True)

    # Learning rate policy
    for group in optimizer.param_groups:
        group.setdefault('initial_lr', group['lr'])

    print('Start train...')
    iters = 0
    log_file = open(log_path, 'w')
    loss_iters, accuracy_iters = [], []
    for epoch in range(1, 100):
        for iter_id, batch in enumerate(train_loader):
            loss_seg, accuracy = losses.build_metrics(model, batch, device)
            optimizer.zero_grad()
            loss_seg.backward()
            optimizer.step()

            loss_iters.append(float(loss_seg.cpu()))
            accuracy_iters.append(float(accuracy))

            iters += 1
            if iters % num_print_iters == 0:
                cur_time = strftime("%Y-%m-%d %H:%M:%S", localtime())
                log_str = 'iters:{:4}, loss:{:6,.4f}, accuracy:{:5,.4}'.format(
                    iters, np.mean(loss_iters), np.mean(accuracy_iters))
                print(log_str)
                log_file.write(cur_time + ' ' + log_str + '\n')
                log_file.flush()
                loss_iters = []
                accuracy_iters = []

            if iters % num_save_iters == 0:
                torch.save(model.state_dict(),
                           model_path_save + str(iters) + '.pth')

            # step
            # if iters == num_update_iters or iters == num_update_iters + 1000:
            #     for group in optimizer.param_groups:
            #         group["lr"] *= 0.1

            # poly
            for group in optimizer.param_groups:
                group["lr"] = group['initial_lr'] * (
                    1 - float(iters) / num_max_iters)**0.9

            if iters == num_max_iters:
                exit()
Exemplo n.º 6
0
def test(model_path_test, use_crf):
    batch_size = 2
    is_post_process = use_crf
    crop_size = 513
    model = vgg.VGG16_LargeFOV(input_size=crop_size, split='test')
    model = torch.nn.DataParallel(model, device_ids=device_ids)
    model.load_state_dict(torch.load(model_path_test))
    model.eval()
    model = model.to(device)
    val_loader = torch.utils.data.DataLoader(VOCDataset(
        split='val',
        crop_size=crop_size,
        label_dir_path='SegmentationClassAug',
        is_scale=False,
        is_flip=False),
                                             batch_size=batch_size,
                                             shuffle=False,
                                             num_workers=4,
                                             drop_last=False)

    # DenseCRF
    post_processor = crf.DenseCRF(
        iter_max=10,  # 10
        pos_xy_std=3,  # 3
        pos_w=3,  # 3
        bi_xy_std=140,  # 121, 140
        bi_rgb_std=5,  # 5, 5
        bi_w=5,  # 4, 5
    )

    img_dir_path = root_dir_path + '/JPEGImages/'
    # class palette for test
    palette = []
    for i in range(256):
        palette.extend((i, i, i))
    palette[:3 * 21] = np.array(
        [[0, 0, 0], [128, 0, 0], [0, 128, 0], [128, 128, 0], [0, 0, 128],
         [128, 0, 128], [0, 128, 128], [128, 128, 128],
         [64, 0, 0], [192, 0, 0], [64, 128, 0], [192, 128, 0], [64, 0, 128],
         [192, 0, 128], [64, 128, 128], [192, 128, 128], [0, 64, 0],
         [128, 64, 0], [0, 192, 0], [128, 192, 0], [0, 64, 128]],
        dtype='uint8').flatten()
    times = 0.0
    index = 0
    loss_iters, accuracy_iters = [], []
    CEL = nn.CrossEntropyLoss(ignore_index=255).to(device)
    for iter_id, batch in enumerate(val_loader):
        image_ids, images, labels = batch
        images = images.to(device)
        labels = losses.resize_labels(labels,
                                      size=(crop_size, crop_size)).to(device)
        logits = model(images)
        probs = nn.functional.softmax(logits,
                                      dim=1)  # shape = [batch_size, C, H, W]

        outputs = torch.argmax(probs, dim=1)  # shape = [batch_size, H, W]

        loss_seg = CEL(logits, labels)
        accuracy = float(torch.eq(outputs, labels).sum().cpu()) / (
            len(image_ids) * logits.shape[2] * logits.shape[3])
        loss_iters.append(float(loss_seg.cpu()))
        accuracy_iters.append(float(accuracy))

        for i in range(len(image_ids)):
            if is_post_process:
                raw_image = cv2.imread(img_dir_path + image_ids[i] + '.jpg',
                                       cv2.IMREAD_COLOR)  # shape = [H, W, 3]
                h, w = raw_image.shape[:2]
                pad_h = max(513 - h, 0)
                pad_w = max(513 - w, 0)
                pad_kwargs = {
                    "top": 0,
                    "bottom": pad_h,
                    "left": 0,
                    "right": pad_w,
                    "borderType": cv2.BORDER_CONSTANT,
                }
                raw_image = cv2.copyMakeBorder(raw_image,
                                               value=[0, 0, 0],
                                               **pad_kwargs)
                raw_image = raw_image.astype(np.uint8)
                start_time = time.time()
                prob = post_processor(raw_image,
                                      probs[i].detach().cpu().numpy())
                times += time.time() - start_time
                output = np.argmax(prob, axis=0).astype(np.uint8)
                img_label = Image.fromarray(output)
            else:
                output = np.array(outputs[i].cpu(), dtype=np.uint8)
                img_label = Image.fromarray(output)
            img_label.putpalette(palette)
            img_label.save(pred_dir_path + image_ids[i] + '.png')

            accuracy = float(torch.eq(outputs[i], labels[i]).sum().cpu()) / (
                logits.shape[2] * logits.shape[3])
            index += 1
            if index % 200 == 0:
                print(image_ids[i], float('%.4f' % accuracy), index)
    if is_post_process:
        print('dense crf time = %s' % (times / index))
    print('val loss = %s, acc = %s' %
          (np.mean(loss_iters), np.mean(accuracy_iters)))
    print(model_path_test)