def get_section_tutorial(OurFaceNet):

    st.write()
    '''
    
    # Tutorial

    In this section we want to visualize how the face recognition is done. For this purpose we wil first select
    a few images of people (IDs) which will serve as the anchor images. Those images are taken from the
    'Large-scale CelebFaces Attributes' dataset [(CelebA)](http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html).
    After that, we then use a couple example images of the same people but in different settings.

    The list of variations includes the following:
    * Different backgrounds
    * Different lighting, bright, dark, color, etc.
    * Facing different angles
    * Wearing hats, accessories, etc.
    * Covering face with eyeglasses, sun-glasses, mustache, etc.
    * Different hair cuts, e.g. long hair, short hair, bangs, etc.
    * Same person in different age (young vs old)
    * Different facial expressions
    
    '''

    st.write()
    st.write()
    load_tutorial_anchor_images()
    test_images = load_tutorial_test_images()
    st.write()
    st.write()

    """

    In the inference phase, the test image is feed into the trained model. It calculates an embedding which is 
    a numerical vector that represents the person (face). This embedding should include all relevant facial features 
    in order to identify a person. The generated embedding is then used to compare it with all known people. 
    This is done pairwise (test person <-> known person) with a distance function which calculates a similarity 
    value. You are free to use any distance function that maps two vectors to one scalar value. In our case, we 
    decide to train our model with the use of the euclidean distance (L2 Norm). 

    """

    st.write()
    st.write()
    st.image(Image.open(Path('documentation/images/InferenceProcess.jpeg')),
             caption=f"Inference Process",
             use_column_width=True
             )
    st.write()
    st.write()

    '''

    '''

    if len(test_images) > 0:
        st.subheader("Inference")
        start_inference = st.button(label="Start Inference")

        if start_inference:
            col_1, col_2, col_3 = st.beta_columns(3)
            visited = [False, False]
            for img_path in test_images:
                img_tensor = transforms.ToTensor()(Image.open(img_path).resize([224, 224]))
                pred_id, msg = OurFaceNet.inference(
                    img_tensor, use_threshold=False, fuzzy_matches=False
                )

                msg = f"{msg.replace('!', ' -')} Person (ID): {pred_id}"
                temp_test_img_label = img_path.stem.split(" ")[0]

                if str(pred_id) == temp_test_img_label:
                    t = "<div><span class='highlight green'><span class='bold'>Passed</span> </span></div>"
                else:
                    t = "<div> <span class='highlight red'><span class='bold'>Failed</span></span></div>"

                if not visited[0]:
                    col_1.markdown(t, unsafe_allow_html=True)
                    col_1.image(Image.open(img_path), caption=f"{msg}", use_column_width=True)
                    visited[0] = True
                elif not visited[1]:
                    col_2.markdown(t, unsafe_allow_html=True)
                    col_2.image(Image.open(img_path), caption=f"{msg}", use_column_width=True)
                    visited[1] = True
                else:
                    col_3.markdown(t, unsafe_allow_html=True)
                    col_3.image(Image.open(img_path), caption=f"{msg}", use_column_width=True)
                    visited = [False, False]
Exemplo n.º 2
0
def run(opt):
    torch.manual_seed(opt.seed)
    torch.backends.cudnn.benchmark = not opt.not_cuda_benchmark and not opt.test

    print('Setting up data...')
    Dataset = get_dataset(opt.dataset,
                          opt.task)  # if opt.task==mot -> JointDataset

    f = open(opt.data_cfg)  # 选择哪一个数据集进行训练测试 '../src/lib/cfg/mot15.json',
    data_config = json.load(f)
    trainset_paths = data_config['train']  # 训练集路径
    dataset_root = data_config['root']  # 数据集所在目录
    print("Dataset root: %s" % dataset_root)
    f.close()

    # Image data transformations
    transforms = T.Compose([T.ToTensor()])

    # Dataset
    dataset = Dataset(opt=opt,
                      root=dataset_root,
                      paths=trainset_paths,
                      img_size=(1088, 608),
                      augment=True,
                      transforms=transforms)
    opt = opts().update_dataset_info_and_set_heads(opt, dataset)
    print("opt:\n", opt)

    logger = Logger(opt)

    # os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
    # os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpus_str  # 多GPU训练
    # print("opt.gpus_str: ", opt.gpus_str)

    # opt.device = torch.device('cuda:0' if opt.gpus[0] >= 0 else 'cpu')  # 设置GPU

    opt.device = device
    opt.gpus = my_visible_devs

    print('Creating model...')
    model = create_model(opt.arch, opt.heads, opt.head_conv)

    # 初始化优化器
    optimizer = torch.optim.Adam(model.parameters(), opt.lr)

    start_epoch = 0
    if opt.load_model != '':
        model, optimizer, start_epoch = load_model(model, opt.load_model,
                                                   optimizer, opt.resume,
                                                   opt.lr, opt.lr_step)

    # Get dataloader
    if opt.is_debug:
        train_loader = torch.utils.data.DataLoader(
            dataset=dataset,
            batch_size=opt.batch_size,
            shuffle=True,
            pin_memory=True,
            drop_last=True)  # debug时不设置线程数(即默认为0)
    else:
        train_loader = torch.utils.data.DataLoader(dataset=dataset,
                                                   batch_size=opt.batch_size,
                                                   shuffle=True,
                                                   num_workers=opt.num_workers,
                                                   pin_memory=True,
                                                   drop_last=True)

    print('Starting training...')
    Trainer = train_factory[opt.task]
    trainer = Trainer(opt=opt, model=model, optimizer=optimizer)
    # trainer.set_device(opt.gpus, opt.chunk_sizes, opt.device)
    trainer.set_device(opt.gpus, opt.chunk_sizes, device)

    best = 1e10
    for epoch in range(start_epoch + 1, opt.num_epochs + 1):
        mark = epoch if opt.save_all else 'last'

        # Train an epoch
        log_dict_train, _ = trainer.train(epoch, train_loader)

        logger.write('epoch: {} |'.format(epoch))
        for k, v in log_dict_train.items():
            logger.scalar_summary('train_{}'.format(k), v, epoch)
            logger.write('{} {:8f} | '.format(k, v))

        if opt.val_intervals > 0 and epoch % opt.val_intervals == 0:
            save_model(os.path.join(opt.save_dir, 'model_{}.pth'.format(mark)),
                       epoch, model, optimizer)
        else:  # mcmot_last_track or mcmot_last_det
            if opt.id_weight > 0:  # do tracking(detection and re-id)
                save_model(
                    os.path.join(opt.save_dir,
                                 'mcmot_last_track_' + opt.arch + '.pth'),
                    epoch, model, optimizer)
            else:  # only do detection
                # save_model(os.path.join(opt.save_dir, 'mcmot_last_det_' + opt.arch + '.pth'),
                #        epoch, model, optimizer)
                save_model(
                    os.path.join(opt.save_dir,
                                 'mcmot_last_det_' + opt.arch + '.pth'), epoch,
                    model, optimizer)
        logger.write('\n')

        if epoch in opt.lr_step:
            save_model(
                os.path.join(opt.save_dir, 'model_{}.pth'.format(epoch)),
                epoch, model, optimizer)

            lr = opt.lr * (0.1**(opt.lr_step.index(epoch) + 1))
            print('Drop LR to', lr)

            for param_group in optimizer.param_groups:
                param_group['lr'] = lr

        if epoch % 10 == 0:
            save_model(
                os.path.join(opt.save_dir, 'model_{}.pth'.format(epoch)),
                epoch, model, optimizer)
    logger.close()
Exemplo n.º 3
0
def train(
    cfg,
    data_cfg,
    weights_from="",
    weights_to="",
    save_every=10,
    img_size=(1088, 608),
    resume=False,
    epochs=100,
    batch_size=16,
    accumulated_batches=1,
    freeze_backbone=False,
    opt=None,
):
    # The function starts

    timme = strftime("%Y-%d-%m %H:%M:%S", gmtime())
    timme = timme[5:-3].replace('-', '_')
    timme = timme.replace(' ', '_')
    timme = timme.replace(':', '_')
    weights_to = osp.join(weights_to, 'run' + timme)
    mkdir_if_missing(weights_to)
    if resume:
        latest_resume = osp.join(weights_from, 'latest.pt')

    torch.backends.cudnn.benchmark = True  # unsuitable for multiscale

    # Configure run
    f = open(data_cfg)
    data_config = json.load(f)
    trainset_paths = data_config['train']
    dataset_root = data_config['root']
    f.close()

    transforms = T.Compose([T.ToTensor()])
    # Get dataloader
    dataset = JointDataset(dataset_root,
                           trainset_paths,
                           img_size,
                           augment=True,
                           transforms=transforms)
    dataloader = torch.utils.data.DataLoader(dataset,
                                             batch_size=batch_size,
                                             shuffle=True,
                                             num_workers=8,
                                             pin_memory=True,
                                             drop_last=True,
                                             collate_fn=collate_fn)
    # Initialize model
    model = Darknet(cfg, dataset.nID)

    cutoff = -1  # backbone reaches to cutoff layer
    start_epoch = 0
    if resume:
        checkpoint = torch.load(latest_resume, map_location='cpu')

        # Load weights to resume from
        model.load_state_dict(checkpoint['model'])
        model.cuda().train()

        # Set optimizer
        optimizer = torch.optim.SGD(filter(lambda x: x.requires_grad,
                                           model.parameters()),
                                    lr=opt.lr,
                                    momentum=.9)

        start_epoch = checkpoint['epoch'] + 1
        if checkpoint['optimizer'] is not None:
            optimizer.load_state_dict(checkpoint['optimizer'])

        del checkpoint  # current, saved

    else:
        # Initialize model with backbone (optional)
        if cfg.endswith('yolov3.cfg'):
            load_darknet_weights(model,
                                 osp.join(weights_from, 'darknet53.conv.74'))
            cutoff = 75
        elif cfg.endswith('yolov3-tiny.cfg'):
            load_darknet_weights(model,
                                 osp.join(weights_from, 'yolov3-tiny.conv.15'))
            cutoff = 15

        model.cuda().train()

        # Set optimizer
        optimizer = torch.optim.SGD(filter(lambda x: x.requires_grad,
                                           model.parameters()),
                                    lr=opt.lr,
                                    momentum=.9,
                                    weight_decay=1e-4)

    model = torch.nn.DataParallel(model)
    # Set scheduler
    scheduler = torch.optim.lr_scheduler.MultiStepLR(
        optimizer,
        milestones=[int(0.5 * opt.epochs),
                    int(0.75 * opt.epochs)],
        gamma=0.1)

    # An important trick for detection: freeze bn during fine-tuning
    if not opt.unfreeze_bn:
        for i, (name, p) in enumerate(model.named_parameters()):
            p.requires_grad = False if 'batch_norm' in name else True

    # model_info(model)
    t0 = time.time()
    for epoch in range(epochs):
        epoch += start_epoch
        logger.info(
            ('%8s%12s' + '%10s' * 6) % ('Epoch', 'Batch', 'box', 'conf', 'id',
                                        'total', 'nTargets', 'time'))

        # Freeze darknet53.conv.74 for first epoch
        if freeze_backbone and (epoch < 2):
            for i, (name, p) in enumerate(model.named_parameters()):
                if int(name.split('.')[2]) < cutoff:  # if layer < 75
                    p.requires_grad = False if (epoch == 0) else True

        ui = -1
        rloss = defaultdict(float)  # running loss
        optimizer.zero_grad()
        for i, (imgs, targets, _, _, targets_len) in enumerate(dataloader):
            if sum([len(x) for x in targets]) < 1:  # if no targets continue
                continue

            # SGD burn-in
            burnin = min(1000, len(dataloader))
            if (epoch == 0) & (i <= burnin):
                lr = opt.lr * (i / burnin)**4
                for g in optimizer.param_groups:
                    g['lr'] = lr

            # Compute loss, compute gradient, update parameters
            loss, components = model(imgs.cuda(), targets.cuda(),
                                     targets_len.cuda())
            components = torch.mean(components.view(-1, 5), dim=0)
            loss = torch.mean(loss)
            loss.backward()

            # accumulate gradient for x batches before optimizing
            if ((i + 1) % accumulated_batches
                    == 0) or (i == len(dataloader) - 1):
                optimizer.step()
                optimizer.zero_grad()

            # Running epoch-means of tracked metrics
            ui += 1

            for ii, key in enumerate(model.module.loss_names):
                rloss[key] = (rloss[key] * ui + components[ii]) / (ui + 1)

            # rloss indicates running loss values with mean updated at every epoch
            s = ('%8s%12s' + '%10.3g' * 6) % (
                '%g/%g' % (epoch, epochs - 1), '%g/%g' %
                (i, len(dataloader) - 1), rloss['box'], rloss['conf'],
                rloss['id'], rloss['loss'], rloss['nT'], time.time() - t0)
            t0 = time.time()
            if i % opt.print_interval == 0:
                logger.info(s)

        # Save latest checkpoint
        checkpoint = {
            'epoch': epoch,
            'model': model.module.state_dict(),
            'optimizer': optimizer.state_dict()
        }

        #copyfile(cfg, weights_to + '/cfg/yolo3.cfg')
        #copyfile(data_cfg, weights_to + '/cfg/ccmcpe.json')

        latest = osp.join(weights_to, 'latest.pt')
        torch.save(checkpoint, latest)
        if epoch % save_every == 0 and epoch != 0:
            # making the checkpoint lite
            checkpoint["optimizer"] = []
            torch.save(
                checkpoint,
                osp.join(weights_to, "weights_epoch_" + str(epoch) + ".pt"))

        # Calculate mAP
        if epoch % opt.test_interval == 0:
            with torch.no_grad():
                mAP, R, P = test.test(cfg,
                                      data_cfg,
                                      weights=latest,
                                      batch_size=batch_size)
                test.test_emb(cfg,
                              data_cfg,
                              weights=latest,
                              batch_size=batch_size)

        # Call scheduler.step() after opimizer.step() with pytorch > 1.1.0
        scheduler.step()
Exemplo n.º 4
0
def train_one_epoch(
        model,
        arch,
        optimizer,
        lr_scheduler,
        data_loader,
        device,
        epoch,
        print_freq,
        ngpus_per_node,
        model_without_ddp,
        args
    ):
    model.train()
    metric_logger = utils.MetricLogger(delimiter="  ")
    metric_logger.add_meter("lr", utils.SmoothedValue(window_size=1, fmt="{value:.6f}"))
    # header = "Epoch: [{}]".format(epoch)

    for images, targets in metric_logger.log_every(
            iterable=data_loader,
            print_freq=print_freq,
            # header=header,
            iter_num=args.iter_num
        ):

        images = list(image.to(device) for image in images)
        targets = [{k: v.to(device) for k, v in t.items()} for t in targets]

        """
        [{"boxes": tensor([], device="cuda:0"), "labels": tensor([], device="cuda:0", dtype=torch.int64), "masks": tensor([], device="cuda:0", dtype=torch.uint8), "iscrowd": tensor([], device="cuda:0", dtype=torch.int64)}]
        """

        try:
            loss_dict = model(images, targets) 
            losses = sum(loss for loss in loss_dict.values())

            # reduce losses over all GPUs for logging purposes
            loss_dict_reduced = utils.reduce_dict(loss_dict)
            losses_reduced = sum(loss for loss in loss_dict_reduced.values())
            loss_value = losses_reduced.item()

            if not math.isfinite(loss_value):
                logger.fatal("Loss is {}, stopping training".format(loss_value))
                logger.fatal(loss_dict_reduced)
                sys.exit(1)

            optimizer.zero_grad()
            losses.backward()
            optimizer.step()
            lr_scheduler.step()

            metric_logger.update(loss=losses_reduced, **loss_dict_reduced)
            metric_logger.update(lr=optimizer.param_groups[0]["lr"])

        except Exception as e:
            logger.warning(e, exc_info=True)
            # logger.info("print target for debug")
            # print(targets)

        args.iter_num += 1

        # save checkpoint here
        if not args.multiprocessing_distributed or (args.multiprocessing_distributed and args.rank % ngpus_per_node == 0):
            if args.iter_num % 1000 == 0:
                utils.save_on_master({
                        "model": model_without_ddp.state_dict(),
                        "optimizer": optimizer.state_dict(),
                        "lr_scheduler": lr_scheduler.state_dict(),
                        "epoch": epoch,
                        "iter_num": args.iter_num,
                        "args": args,
                    },
                    "{}/{}_{}.pth".format(checkpoint_dir, arch.__name__, args.iter_num)
                )

                os.makedirs("{}/debug_image/".format(checkpoint_dir), exist_ok=True)

                if args.iter_num < 5000:
                    continue

                model.eval()

                # from barez import overlay_ann	
                debug_image = None
                debug_image_list = []
                cnt = 0
                for image_path in glob.glob("./table_test/*"):
                    cnt += 1
                    image_name = os.path.basename(image_path)
                    # print(image_name)
                    image = cv2.imread(image_path)
                    rat = 1300 / image.shape[0]
                    image = cv2.resize(image, None, fx=rat, fy=rat)

                    transform = transforms.Compose([transforms.ToTensor()])
                    image = transform(image)

                    # put the model in evaluation mode
                    with torch.no_grad():
                        tensor = [image.to(device)]
                        prediction = model(tensor)
                        
                    image = torch.squeeze(image, 0).permute(1, 2, 0).mul(255).numpy().astype(np.uint8)

                    for pred in prediction:
                        for idx, mask in enumerate(pred['masks']):
                            if pred['scores'][idx].item() < 0.5:
                                continue
                        
                            m =  mask[0].mul(255).byte().cpu().numpy()
                            box = list(map(int, pred["boxes"][idx].tolist())) 
                            score = pred["scores"][idx].item()
                            # image = overlay_ann(image, m, box, "", score)

                    # if debug_image is None:
                    #     debug_image = image
                    # else:
                    #     debug_image = np.concatenate((debug_image, image), axis=1)

                    # if cnt == 10:
                    #     cnt = 0
                    #     debug_image_list.append(debug_image)
                    #     debug_image = None
                    
                avg_length = np.mean([i.shape[1] for i in debug_image_list])

                
                di = None

                
                for debug_image in debug_image_list:
                    rat = avg_length / debug_image.shape[1]
                    debug_image = cv2.resize(debug_image, None, fx=rat, fy=rat)

                    if di is None:
                        di = debug_image
                    else:
                        
                        di = np.concatenate((di, debug_image), axis=0)
            

                di = cv2.resize(di, None, fx=0.4, fy=0.4)
                cv2.imwrite("{}/debug_image/{}.jpg".format(checkpoint_dir, args.iter_num), di)

                model.train()

        # hard stop
        if args.iter_num == 50000:
            logger.info("ITER NUM == 50k, training successfully!")
            raise SystemExit
Exemplo n.º 5
0
def simple_augment():
    simple_augment_ = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
    ])
    return simple_augment_
Exemplo n.º 6
0
def test_emb(
        opt,
        batch_size=16,
        img_size=(1088, 608),
        print_interval=40,
):
    """
    :param opt:
    :param batch_size:
    :param img_size:
    :param print_interval:
    :return:
    """
    data_cfg = opt.data_cfg
    f = open(data_cfg)
    data_cfg_dict = json.load(f)
    f.close()
    nC = 1
    test_paths = data_cfg_dict['test_emb']
    dataset_root = data_cfg_dict['root']
    if opt.gpus[0] >= 0:
        opt.device = torch.device('cuda')
    else:
        opt.device = torch.device('cpu')

    print('Creating model...')
    model = create_model(opt.arch, opt.heads, opt.head_conv)
    model = load_model(model, opt.load_model)
    # model = torch.nn.DataParallel(model)
    model = model.to(opt.device)
    model.eval()

    # Get data loader
    transforms = T.Compose([T.ToTensor()])
    dataset = JointDataset(opt,
                           dataset_root,
                           test_paths,
                           img_size,
                           augment=False,
                           transforms=transforms)
    data_loader = torch.utils.data.DataLoader(dataset,
                                              batch_size=batch_size,
                                              shuffle=False,
                                              num_workers=8,
                                              drop_last=False)
    embedding, id_labels = [], []
    print('Extracting features...')
    for batch_i, batch in enumerate(data_loader):
        t = time.time()
        output = model(batch['input'].cuda())[-1]
        id_head = _tranpose_and_gather_feat(output['id'], batch['ind'].cuda())
        id_head = id_head[batch['reg_mask'].cuda() > 0].contiguous()
        emb_scale = math.sqrt(2) * math.log(opt.nID - 1)
        id_head = emb_scale * F.normalize(id_head)
        id_target = batch['ids'].cuda()[batch['reg_mask'].cuda() > 0]

        for i in range(0, id_head.shape[0]):
            if len(id_head.shape) == 0:
                continue
            else:
                feat, label = id_head[i], id_target[i].long()
            if label != -1:
                embedding.append(feat)
                id_labels.append(label)

        if batch_i % print_interval == 0:
            print(
                'Extracting {}/{}, # of instances {}, time {:.2f} sec.'.format(
                    batch_i, len(data_loader), len(id_labels),
                    time.time() - t))

    print('Computing pairwise similarity...')
    if len(embedding) < 1:
        return None

    embedding = torch.stack(embedding, dim=0).cuda()
    id_labels = torch.LongTensor(id_labels)
    n = len(id_labels)
    print(n, len(embedding))
    assert len(embedding) == n

    embedding = F.normalize(embedding, dim=1)
    p_dist = torch.mm(embedding, embedding.t()).cpu().numpy()
    gt = id_labels.expand(n, n).eq(id_labels.expand(n, n).t()).numpy()

    up_triangle = np.where(np.triu(p_dist) - np.eye(n) * p_dist != 0)
    p_dist = p_dist[up_triangle]
    gt = gt[up_triangle]

    far_levels = [1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1]
    far, tar, threshold = metrics.roc_curve(gt, p_dist)
    interp = interpolate.interp1d(far, tar)
    tar_at_far = [interp(x) for x in far_levels]
    for f, fa in enumerate(far_levels):
        print('TPR@FAR={:.7f}: {:.4f}'.format(fa, tar_at_far[f]))
    return tar_at_far
Exemplo n.º 7
0
def main(opt):
    torch.manual_seed(opt.seed)
    torch.backends.cudnn.benchmark = not opt.not_cuda_benchmark and not opt.test

    print('Setting up data...')
    Dataset = get_dataset(opt.dataset, opt.task)
    f = open(opt.data_cfg)
    data_config = json.load(f)
    trainset_paths = data_config['train']
    dataset_root = data_config['root']
    f.close()
    transforms = T.Compose([T.ToTensor()])
    dataset = Dataset(opt, dataset_root, trainset_paths, (1088, 608), augment=True, transforms=transforms)
    opt = opts().update_dataset_info_and_set_heads(opt, dataset)
    print(opt)

    logger = Logger(opt)

    os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpus_str
    opt.device = torch.device('cuda' if opt.gpus[0] >= 0 else 'cpu')

    print('Creating model...')
    model = create_model(opt.arch, opt.heads, opt.head_conv)
    optimizer = torch.optim.Adam(model.parameters(), opt.lr)
    start_epoch = 0
    if opt.load_model != '':
        model, optimizer, start_epoch = load_model(
            model, opt.load_model, optimizer, opt.resume, opt.lr, opt.lr_step)

    # Get dataloader

    train_loader = torch.utils.data.DataLoader(
        dataset,
        batch_size=opt.batch_size,
        shuffle=True,
        num_workers=opt.num_workers,
        pin_memory=True,
        drop_last=True
    )

    print('Starting training...')
    Trainer = train_factory[opt.task]
    trainer = Trainer(opt, model, optimizer)
    trainer.set_device(opt.gpus, opt.chunk_sizes, opt.device)
    best = 1e10
    for epoch in range(start_epoch + 1, opt.num_epochs + 1):
        mark = epoch if opt.save_all else 'last'
        log_dict_train, _ = trainer.train(epoch, train_loader)
        logger.write('epoch: {} |'.format(epoch))
        for k, v in log_dict_train.items():
            logger.scalar_summary('train_{}'.format(k), v, epoch)
            logger.write('{} {:8f} | '.format(k, v))

        if opt.val_intervals > 0 and epoch % opt.val_intervals == 0:
            save_model(os.path.join(opt.save_dir, 'model_{}.pth'.format(mark)),
                       epoch, model, optimizer)
        else:
            save_model(os.path.join(opt.save_dir, 'model_last.pth'),
                       epoch, model, optimizer)
        logger.write('\n')
        if epoch in opt.lr_step:
            save_model(os.path.join(opt.save_dir, 'model_{}.pth'.format(epoch)),
                       epoch, model, optimizer)
            lr = opt.lr * (0.1 ** (opt.lr_step.index(epoch) + 1))
            print('Drop LR to', lr)
            for param_group in optimizer.param_groups:
                param_group['lr'] = lr
        if epoch % 5 == 0:
            save_model(os.path.join(opt.save_dir, 'model_{}.pth'.format(epoch)),
                       epoch, model, optimizer)
    logger.close()
Exemplo n.º 8
0
import torch
from dataset import Dataset
from torch.utils.data import DataLoader
import numpy as np
import os, sys
from osgeo import gdal_array
import gdal
import argparse
from torchvision.transforms import transforms as T
sys.path.append(r'/home/zhoudengji/ghx/data/code/mycnn')
from model import MyCNN

x_transform = T.Compose([
    T.ToTensor(),
    # 标准化至[-1,1],规定均值和标准差
    # T.Normalize([0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5])  # torchvision.transforms.Normalize(mean, std, inplace=False)
])
# mask只需要转换为tensor
y_transform = T.ToTensor()


def nomlize(array):
    return np.divide(array, np.max(array))


# 测试
def test():
    model = MyCNN(4, 512, 512)
    # model = models.UNet(2)
    model.load_state_dict(torch.load(args.weight, map_location='cpu'))
    model.eval()
Exemplo n.º 9
0
def train():
    start_time = time.time()
    learning_rate = 0.001
    Epoch_num = 50
    Batch_size = 1024
    Resolution = 36
    save_path = 'save/'

    # https://tutorials.pytorch.kr/intermediate/tensorboard_tutorial.html
    # The direction for tensorboard
    writer = SummaryWriter(save_path)
    writer.add_scalar('epoch', Epoch_num)
    writer.add_scalar('batch size', Batch_size)
    writer.add_scalar('learning_rate', learning_rate)
    writer.add_scalar('resolution', Resolution)

    tr_transform = transforms.Compose([
        transforms.Resize((Resolution, Resolution)),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize((0.4914, 0.4822, 0.4465),
                             (0.2023, 0.1994, 0.2010))
    ])

    te_transform = transforms.Compose([
        transforms.Resize((Resolution, Resolution)),
        transforms.ToTensor(),
        transforms.Normalize((0.4914, 0.4822, 0.4465),
                             (0.2023, 0.1994, 0.2010))
    ])

    # https://gldmg.tistory.com/43
    # This reference is to store and get .npy file

    # https://wikidocs.net/14304
    # Check if the file is located.
    train_path = 'data/pre_train'
    train_file = 'data/train.npy'

    test_path = 'data/pre_test'
    test_file = 'data/test.npy'

    if os.path.isfile(train_file):
        load = np.load(train_file, allow_pickle=True)
        tr_x, tr_y = load[0], load[1]
    else:
        tr_x, tr_y = make_tensor(train_path)
        np.save(train_file, np.array([tr_x, tr_y]))

    if os.path.isfile(test_file):
        load = np.load(test_file, allow_pickle=True)
        te_x, te_y = load[0], load[1]
    else:
        te_x, te_y = make_tensor(test_path)
        np.save(test_file, np.array([te_x, te_y]))

    processing_time = time.time()
    time_check('processing', start_time, processing_time)
    tr_data = CustomDataset(tr_x, tr_y, tr_transform)

    tr_loader = DataLoader(tr_data, batch_size=Batch_size, shuffle=True)

    te_data = CustomDataset(te_x, te_y, te_transform)
    te_loader = DataLoader(te_data, batch_size=Batch_size, shuffle=False)

    loader_time = time.time()
    time_check('loading', processing_time, loader_time)

    # model
    model = models.resnet18()

    model.fc = nn.Linear(512, 4)
    model = model.cuda()

    # Summary Model
    summary(model, input_size=(3, Resolution, Resolution))

    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

    # Start training
    model.train()
    for epoch in range(Epoch_num):
        train_loss = 0
        correct = 0
        total = 0
        for inputs, targets in tr_loader:
            inputs = inputs.cuda()
            targets = targets.cuda()

            outputs = model(inputs)
            # Ground True vs Prediction value
            # print(outputs)
            # print(targets)

            loss = criterion(outputs, targets)

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

            train_loss += loss.item()
            _, predicted = outputs.max(1)
            total += targets.size(0)
            correct += predicted.eq(targets).sum().item()

        loss = train_loss / len(tr_loader)
        acc = 100 * correct / total

        if epoch % 5 == 0:
            print('Train - Epoch : %d, Loss : %.2f, Accuracy : %.2f' %
                  (epoch, loss, acc))
            writer.add_scalar('Training loss', loss, epoch)
            writer.add_scalar('Accuracy', acc, epoch)

    train_time = time.time()
    time_check('train', loader_time, train_time)

    # https://tutorials.pytorch.kr/beginner/saving_loading_models.html
    torch.save(model, save_path + '/model.pt')

    # Evaluation
    model.eval()

    test_loss = 0
    correct = 0
    total = 0

    with torch.no_grad():
        for batch_idx, (inputs, targets) in enumerate(te_loader):
            inputs = inputs.cuda()
            targets = targets.cuda()

            outputs = model(inputs)
            loss = criterion(outputs, targets)

            test_loss += loss.item()
            _, predicted = outputs.max(1)
            total += targets.size(0)
            correct += predicted.eq(targets).sum().item()

    loss = test_loss / len(te_loader)
    acc = 100 * correct / total

    print('Evaluation - Loss : %.2f, Accuracy : %.2f' % (loss, acc))

    test_time = time.time()
    time_check('test', train_time, test_time)
    writer.close()
Exemplo n.º 10
0
def pixelshuffle_upsample(lr, scale, upsample_model):
    lr = transforms.ToTensor()(lr).unsqueeze(0)
    lr = lr.cuda()
    lr = upsample_model(lr)
    return lr.squeeze(0)
Exemplo n.º 11
0
def bicubic_upsample(lr, scale):
    lr = lr.resize([lr.size[0] * scale, lr.size[1] * scale],
                   resample=Image.BICUBIC)
    return transforms.ToTensor()(lr)
Exemplo n.º 12
0
    def __init__(self):
        """Initialize ResMaskNet

        @misc{luanresmaskingnet2020,
        Author = {Luan Pham & Tuan Anh Tran},
        Title = {Facial Expression Recognition using Residual Masking Network},
        url = {https://github.com/phamquiluan/ResidualMaskingNetwork},
        Year = {2020}
        }

        """
        self.transform = transforms.Compose(
            [transforms.ToPILImage(),
             transforms.ToTensor()])

        self.FER_2013_EMO_DICT = {
            0: "angry",
            1: "disgust",
            2: "fear",
            3: "happy",
            4: "sad",
            5: "surprise",
            6: "neutral",
        }

        # load configs and set random seed
        configs = json.load(
            open(
                os.path.join(get_resource_path(),
                             "ResMaskNet_fer2013_config.json")))
        self.image_size = (configs["image_size"], configs["image_size"])
        self.use_gpu = torch.cuda.is_available()
        # if self.use_gpu:
        #     self.state = torch.load(
        #         os.path.join(
        #             get_resource_path(), "ResMaskNet_Z_resmasking_dropout1_rot30.pth"
        #         )
        #     )
        # else:
        #     self.state = torch.load(
        #         os.path.join(
        #             get_resource_path(), "ResMaskNet_Z_resmasking_dropout1_rot30.pth"
        #         ),
        #         map_location={"cuda:0": "cpu"},
        #     )

        self.model = resmasking_dropout1(in_channels=3, num_classes=7)

        if self.use_gpu:
            self.model.load_state_dict(
                torch.load(
                    os.path.join(
                        get_resource_path(),
                        "ResMaskNet_Z_resmasking_dropout1_rot30.pth"))['net'])
            self.model.cuda()

        else:
            self.model.load_state_dict(
                torch.load(
                    os.path.join(get_resource_path(),
                                 "ResMaskNet_Z_resmasking_dropout1_rot30.pth"),
                    map_location={"cuda:0": "cpu"},
                )['net'])
        self.model.eval()
def main():

    # img_folders = ["../coco_img/bg2_0/", "../coco_img/bg2_127/", "../coco_img/bg2_255/",
    # "../coco_img/obj2_0/", "../coco_img/obj2_127/", "../coco_img/obj2_255/"]

    img_folders = [
        "../coco_img/merged_bg2_0/", "../coco_img/merged_bg2_127/",
        "../coco_img/merged_bg2_255/", "../coco_img/merged_obj2_0/",
        "../coco_img/merged_obj2_127/", "../coco_img/merged_obj2_255/"
    ]
    img_folders = ["../coco_img/org/"]

    model_name = "MLCCOCO"

    model = MultilabelObject(None, 80).cuda()

    log_dir = "./"
    checkpoint = torch.load(os.path.join(log_dir, 'model_best.pth.tar'),
                            encoding='bytes')
    new_checkpoint = OrderedDict()
    for k in checkpoint[b'state_dict']:
        new_checkpoint[k.decode('utf-8')] = checkpoint[b'state_dict'][k]

    model.load_state_dict(new_checkpoint)
    model.eval()

    with open("classes_list.pickle", "rb") as f:
        classes_list = pickle.load(f)

    for img_folder in img_folders:

        crop_size = 224
        image_size = 256
        batch_size = 64
        normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                         std=[0.229, 0.224, 0.225])

        val_transform = transforms.Compose([
            transforms.Scale(image_size),
            transforms.CenterCrop(crop_size),
            transforms.ToTensor(), normalize
        ])

        cocomlc = COCO_MLC(img_folder, val_transform)

        test_loader = torch.utils.data.DataLoader(cocomlc,
                                                  batch_size=batch_size,
                                                  shuffle=False,
                                                  num_workers=8,
                                                  pin_memory=True)

        t = tqdm(test_loader, desc='testing {}'.format(img_folder))

        result = {}

        for batch_idx, (imgs, paths) in enumerate(t):

            images = imgs.cuda()
            # print(images.shape)

            probs, labels, labels_probs = infer_batch(model,
                                                      classes_list,
                                                      inputs=images,
                                                      threshold=0.5)

            for i in range(0, len(paths)):
                path = paths[i]
                result[path] = {
                    "prob": probs[i],
                    "labels": labels[i],
                    "labels_probs": labels_probs[i]
                }

        pickle_file_name = "{}_{}.pickle".format(
            model_name, os.path.basename(os.path.normpath(img_folder)))
        pickle_path = os.path.join(".", "result_pickle", pickle_file_name)
        with open(pickle_path, 'wb') as handle:
            pickle.dump(result, handle)
        print("Done, Saved to {}".format(pickle_path))
def get_section_one_shot_learning(OurFaceNet):
    # Sidebar
    st.sidebar.header("One-Shot Learning")
    st.sidebar.write("You can add a new person (ID) to the model.\
                 Afterwards, you can test the verification with some test images.")
    st.sidebar.subheader("Add Person (ID)")
    person_name = st.sidebar.text_input("Enter name of person (ID):", max_chars=40)
    anchor_file = st.sidebar.file_uploader("Select anchor image:", type=['jpg', 'png', 'jpeg'])

    # Main page
    '''
    # One-Shot Learning
    '''

    st.subheader(f"Step 1: Add new Person!")

    '''
    Check the sidebar, in order to add a new person (ID) to 
    the database. After you entered a name and uploaded the image, it will be added and preprocessed 
    automatically into the database.     
    '''

    if person_name is not None and anchor_file is not None:
        # add anchor to known IDs
        OurFaceNet.create_anchor_embeddings(anchor_dict={person_name: anchor_file})
        st.subheader("Done. New Person Added!")
        anchor_image = Image.open(anchor_file)
        st.image(anchor_image, caption=f"Person (ID): {person_name}", use_column_width=True)

        st.subheader("Step 2: Select Test Images")
        st.write("Now, after getting setup you can select some random images to test out the verification system. \
                Select a directory with all (face) images you want to verify."
                 )

        test_images_path = st.text_input("Relative Path to test directory: ")
        if test_images_path == "":
            st.warning("Path to directory is missing! Please add path to test images.")
            return
        test_image_dir = Path(test_images_path)
        if test_image_dir.is_dir():
            test_images = load_tutorial_test_images(test_image_dir)
        else:
            st.warning("Path to directory is missing! Please add path to test images.")

        # Step 3: Inference
        if len(test_images) > 0:
            st.subheader("Step 3: Inference")
            st.write(f"Now we will verify the given test images w.r.t. given Person (ID): {person_name}.")
            st.write("")
            start_inference = st.button(label="Start Inference")
            if start_inference:
                col_1, col_2, col_3 = st.beta_columns(3)
                visited = [False, False]
                for img_path in test_images:
                    test_image = Image.open(img_path).resize([224, 224])
                    img_tensor = transforms.ToTensor()(test_image)

                    # Prediction
                    pred_id, msg = OurFaceNet.inference(
                        img_tensor, use_threshold=False, fuzzy_matches=False
                    )

                    temp_test_img_label = ' '.join([img_path.stem.split(" ")[0], img_path.stem.split(" ")[1]])
                    if str(pred_id) == temp_test_img_label:
                        t = "<div><span class='highlight green'><span class='bold'>Passed</span> </span></div>"
                        msg = f"{msg} Person (ID): {pred_id}"
                    else:
                        t = "<div> <span class='highlight red'><span class='bold'>Failed</span></span></div>"
                        msg = "Unknown person. No identity match found!"

                    if not visited[0]:
                        col_1.markdown(t, unsafe_allow_html=True)
                        col_1.image(test_image, caption=f"{msg}", use_column_width=True)
                        visited[0] = True
                    elif not visited[1]:
                        col_2.markdown(t, unsafe_allow_html=True)
                        col_2.image(test_image, caption=f"{msg}", use_column_width=True)
                        visited[1] = True
                    else:
                        col_3.markdown(t, unsafe_allow_html=True)
                        col_3.image(test_image, caption=f"{msg}", use_column_width=True)
                        visited = [False, False]
def test():
    transform_test = transforms.Compose([
        transforms.ToTensor(),
        # transforms.Normalize(0.41,2)
    ])

    # testset1 = datasets.ImageFolder(args.test_root1, transform=transform_test)
    #
    # testset2 = datasets.ImageFolder(args.test_root2, transform=transform_test)
    #
    # test_loader1 = DataLoader(dataset=testset1,
    #                           batch_size=1,
    #                           shuffle=False)
    #
    # test_loader2 = DataLoader(dataset=testset2,
    #                           batch_size=1,
    #                           shuffle=False)
    # transform = transforms.Compose([transforms.ToTensor(),
    #                                 # transforms.Lambda(lambda x: global_contrast_normalization(x, scale='l1')),
    #                                 transforms.Normalize([0.5], [0.5])])
    encoder, decoder = Encoder(args), Decoder(args)
    # model_dict = torch.load(args.model)

    # encoder_dict = model_dict['encoder']
    # decoder_dict = model_dict['decoder']
    # encoder.load_state_dict(encoder_dict)
    # decoder.load_state_dict(decoder_dict)
    encooder, decoder = load_model(encoder, decoder, args.model)
    encoder.to('cuda')
    decoder.to('cuda')
    encoder.eval()
    decoder.eval()
    # all_data=torch.zeros((3,128,128))
    thr = 0.09000  #0.0908

    import numpy as np

    Tensor = torch.cuda.FloatTensor
    from PIL import Image
    cnt = 0
    # thr=0.06 for part1
    for img in tqdm(img_list1):
        points = {}
        img_path = os.path.join(args.test_root1, img)
        image = Image.open(img_path)
        image = transform_test(image)
        image = image.unsqueeze(0)
        if image.shape[1] != 3:
            image = image.repeat(1, 3, 1, 1)
        image = image.type(Tensor)
        data_rebuild1 = encoder.forward(image)
        data_rebuild1 = decoder.forward(data_rebuild1)
        # criterion=nn.MSELoss()
        # recon_loss = criterion(data_rebuild1,image)
        # recon_loss=recon_loss.detach().to('cpu').numpy()
        residual = torch.abs(data_rebuild1.squeeze()[0, :, :] -
                             image.squeeze()[0, :, :])
        point_set = residual.ge(thr)
        # point_set=point_set.detach().to('cpu').numpy()
        point = point_set.nonzero().cpu().numpy()
        points['points'] = ['{},{}'.format(p[0], p[1]) for p in point]
        if (point.shape[0] > 5):
            save_json(img, points, args.json_part1)
        # print(points)
        # all_data+=residual.detach().cpu().squeeze()
        if args.show:
            data_rebuild1 = data_rebuild1.detach().to('cpu').squeeze().permute(
                1, 2, 0).numpy()
            data = image.to('cpu')
            data = data.squeeze().detach().permute(1, 2, 0)
            data = data.numpy()
            points = point_set.to('cpu')
            im_show = data.copy()
            im_show[points, :] = 1
            ###########
            muti_img = np.hstack([data, data_rebuild1, im_show])
            #cv2.imshow('flaw_locate',im_show)
            #cv2.imshow('ori',data)
            #cv2.imshow('rebuild1', data_rebuild1)
            cv2.imshow("muti_img", muti_img)
            #cv2.waitKey(0)
            # cv2.destoryAllWindows()
            cnt = cnt + 1
            # cv2.imwrite('/home/zww/cv/defect/auto-encoder-net/image/%s.jpg'%str(cnt),data_rebuild1*255)
            cv2.waitKey(0)
Exemplo n.º 16
0
def do_marking_run(class_marking_percentage,
                   experiment_directory,
                   tensorboard_log_directory,
                   augment=True):

    # Setup experiment directory
    if os.path.isdir(experiment_directory):
        error_message = f"Directory {experiment_directory} already exists. By default we assume you don't want to "\
                        "repeat the marking stage."
        logger.info(error_message)
        return

    logfile_path = os.path.join(experiment_directory, 'marking.log')
    setup_logger_tqdm(filepath=logfile_path)

    # Load randomly sampled images from random class along with list of original indexes
    training_set = torchvision.datasets.CIFAR10(root="experiments/datasets",
                                                download=True)
    class_id, images, original_indexes = get_images_for_marking_cifar10(
        training_set,
        class_marking_percentage=class_marking_percentage,
        tensorboard_log_directory=tensorboard_log_directory)

    # Marking network is the resnet18 we trained on CIFAR10
    marking_network = torchvision.models.resnet18(pretrained=False,
                                                  num_classes=10)
    checkpoint_path = "experiments/table1/step1/checkpoint.pth"
    marking_network_checkpoint = torch.load(checkpoint_path)
    marking_network.load_state_dict(
        marking_network_checkpoint["model_state_dict"])

    # Carriers
    marking_network_fc_feature_size = 512
    carriers = torch.randn(len(training_set.classes),
                           marking_network_fc_feature_size)
    carriers /= torch.norm(carriers, dim=1, keepdim=True)
    torch.save(carriers, os.path.join(experiment_directory, "carriers.pth"))

    # Run!
    optimizer = lambda x: torch.optim.AdamW(x, lr=0.1)
    epochs = 100
    batch_size = 32
    output_directory = os.path.join(experiment_directory, "marked_images")
    if not augment:
        augmentation = None
    marked_images = do_marking(output_directory,
                               marking_network,
                               images,
                               original_indexes,
                               carriers,
                               class_id,
                               NORMALIZE_CIFAR10,
                               optimizer,
                               tensorboard_log_directory,
                               epochs=epochs,
                               batch_size=batch_size,
                               overwrite=True,
                               augmentation=augmentation)

    # Show marked images in Tensorboard
    tensorboard_summary_writer = SummaryWriter(
        log_dir=tensorboard_log_directory)
    images_for_tensorboard = [transforms.ToTensor()(x) for x in marked_images]
    img_grid = torchvision.utils.make_grid(images_for_tensorboard, nrow=16)
    tensorboard_summary_writer.add_image('marked_images', img_grid)

    # Record marking completion
    with open(os.path.join(experiment_directory, "marking.complete"),
              "w") as fh:
        fh.write("1")
Exemplo n.º 17
0
def main():
    parser = argparse.ArgumentParser(description='Chainer example: MNIST')
    parser.add_argument('--batchsize',
                        '-b',
                        type=int,
                        default=100,
                        help='Number of images in each mini-batch')
    parser.add_argument('--epoch',
                        '-e',
                        type=int,
                        default=20,
                        help='Number of sweeps over the dataset to train')
    parser.add_argument('--frequency',
                        '-f',
                        type=int,
                        default=-1,
                        help='Frequency of taking a snapshot')
    parser.add_argument('--device',
                        '-d',
                        type=str,
                        default='-1',
                        help='Device specifier. Either ChainerX device '
                        'specifier or an integer. If non-negative integer, '
                        'CuPy arrays with specified device id are used. If '
                        'negative integer, NumPy arrays are used')
    parser.add_argument('--out',
                        '-o',
                        default='result',
                        help='Directory to output the result')
    parser.add_argument('--resume',
                        '-r',
                        type=str,
                        help='Resume the training from snapshot')
    parser.add_argument('--autoload',
                        action='store_true',
                        help='Automatically load trainer snapshots in case'
                        ' of preemption or other temporary system failure')
    parser.add_argument('--unit',
                        '-u',
                        type=int,
                        default=1000,
                        help='Number of units')
    group = parser.add_argument_group('deprecated arguments')
    group.add_argument('--gpu',
                       '-g',
                       dest='device',
                       type=int,
                       nargs='?',
                       const=0,
                       help='GPU ID (negative value indicates CPU)')
    args = parser.parse_args()

    device = torch.device(args.device)

    print('Device: {}'.format(device))
    print('# Minibatch-size: {}'.format(args.batchsize))
    print('# epoch: {}'.format(args.epoch))
    print('')

    # Set up a neural network to train
    # Classifier reports softmax cross entropy loss and accuracy at every
    # iteration, which will be used by the PrintReport extension below.
    model = Classifier(MLP(784, args.unit, 10))
    model.to(device)

    # Setup an optimizer
    optimizer = torch.optim.Adam(model.parameters())

    # Load the MNIST dataset
    transform = transforms.ToTensor()
    train = datasets.MNIST('data',
                           train=True,
                           download=True,
                           transform=transform)
    test = datasets.MNIST('data', train=False, transform=transform)

    train_iter = pytorch_trainer.iterators.SerialIterator(
        train, args.batchsize)
    test_iter = pytorch_trainer.iterators.SerialIterator(test,
                                                         args.batchsize,
                                                         repeat=False,
                                                         shuffle=False)

    # Set up a trainer
    updater = training.updaters.StandardUpdater(train_iter,
                                                optimizer,
                                                model,
                                                device=device)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)

    # Evaluate the model with the test dataset for each epoch
    trainer.extend(extensions.Evaluator(test_iter, model, device=device),
                   call_before_training=True)

    # Dump a computational graph from 'loss' variable at the first iteration
    # The "main" refers to the target link of the "main" optimizer.
    # trainer.extend(extensions.DumpGraph('main/loss'))

    # Take a snapshot for each specified epoch
    frequency = args.epoch if args.frequency == -1 else max(1, args.frequency)
    # Take a snapshot each ``frequency`` epoch, delete old stale
    # snapshots and automatically load from snapshot files if any
    # files are already resident at result directory.
    trainer.extend(extensions.snapshot(n_retains=1, autoload=args.autoload),
                   trigger=(frequency, 'epoch'))

    # Write a log of evaluation statistics for each epoch
    trainer.extend(extensions.LogReport(), call_before_training=True)

    # Save two plot images to the result dir
    trainer.extend(extensions.PlotReport(['main/loss', 'validation/main/loss'],
                                         'epoch',
                                         file_name='loss.png'),
                   call_before_training=True)
    trainer.extend(extensions.PlotReport(
        ['main/accuracy', 'validation/main/accuracy'],
        'epoch',
        file_name='accuracy.png'),
                   call_before_training=True)

    # Print selected entries of the log to stdout
    # Here "main" refers to the target link of the "main" optimizer again, and
    # "validation" refers to the default name of the Evaluator extension.
    # Entries other than 'epoch' are reported by the Classifier link, called by
    # either the updater or the evaluator.
    trainer.extend(extensions.PrintReport([
        'epoch', 'main/loss', 'validation/main/loss', 'main/accuracy',
        'validation/main/accuracy', 'elapsed_time'
    ]),
                   call_before_training=True)

    # Print a progress bar to stdout
    trainer.extend(extensions.ProgressBar())

    if args.resume is not None:
        # Resume from a snapshot (Note: this loaded model is to be
        # overwritten by --autoload option, autoloading snapshots, if
        # any snapshots exist in output directory)
        trainer.load_state_dict(torch.load(args.resume))

    # Run the training
    trainer.run()
Exemplo n.º 18
0
from torchvision.transforms import transforms
from torch.utils.data import DataLoader
import torchvision.datasets as datasets

# //TODO: Custom Transforms
transform = transforms.Compose([
    transforms.Resize((30, 30)),
    transforms.ToTensor(),  # //* 0-255 to 0-1, numpy to tensors
    transforms.Normalize(
        [0.5, 0.5, 0.5],  # //* 0-1 to [-1,1] , formula (x-mean)/std
        [0.5, 0.5, 0.5])
])


# //! custom Dataloader function
def load_data(train_path, test_path):
    train_loader = DataLoader(datasets.ImageFolder(train_path,
                                                   transform=transform),
                              batch_size=64,
                              shuffle=True)

    test_loader = DataLoader(datasets.ImageFolder(test_path,
                                                  transform=transform),
                             batch_size=32,
                             shuffle=True)

    # //TODO: load all classes(43)
    classes = datasets.ImageFolder(train_path, transform=transform).classes

    return train_loader, test_loader, classes
Exemplo n.º 19
0
class NetShell:
    default_scale = 224
    default_transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])

    class _Path:
        def __init__(self):
            self.root = '../..'
            self.dataset_root = 'cookie/data'
            self.dataset_dir = 'CIFAR10'
            self.save_root = 'cookie/save'
            self.save_dir = None

        def dataset_path(self):
            dataset_path = f'{self.root}/{self.dataset_root}'
            if self.dataset_dir:
                dataset_path = dataset_path + '/' + self.dataset_dir
            if not os.path.exists(dataset_path):
                os.makedirs(dataset_path)
            return dataset_path

        def save_path(self):
            save_path = f'{self.root}/{self.save_root}'
            if self.save_dir:
                save_path = save_path + '/' + self.save_dir
            if not os.path.exists(save_path):
                os.makedirs(save_path)
            return save_path

    class _Train:
        def __init__(self):
            self.transform = NetShell.default_transform
            self.shuffle = True
            self.max_epoch = 200
            self.save_checkpoint = -1
            self.save_epoch = -1
            self.early_stop_max = 5

    class _Test:
        def __init__(self):
            self.transform = NetShell.default_transform
            self.shuffle = True

    class _Save:
        def __init__(self):
            self.optimizer = True
            self.state = True

    class _Options:
        def __init__(self):
            self.train = NetShell._Train()
            self.test = NetShell._Test()
            self.save = NetShell._Save()

    def __init__(self,
                 net,
                 Dataset=None,
                 criterion=torch.nn.CrossEntropyLoss()):
        self.net = net
        self.cuda = True
        self.options = NetShell._Options()
        self.path = NetShell._Path()
        self.criterion = criterion
        self.Dataset = Dataset if Dataset else torchvision.datasets.CIFAR10
        self.optimizer = optim.SGD(self.net.parameters(),
                                   lr=0.001,
                                   momentum=0.9)
        self.state = {
            'epoch': 0,
            'iter': 0,
            'special_mark': None,
        }

    @staticmethod
    def sav_loader(path=None,
                   auto_load_path=None,
                   Dataset=None,
                   load_optimizer=False):
        if auto_load_path:
            files = os.listdir(auto_load_path)
            files.sort()
            path = f'{auto_load_path}/{files[-1]}'
        with open(path, 'rb') as f:
            sav = torch.load(f)
        net = sav['net']
        ns = NetShell(net, Dataset=Dataset)
        state = sav.get('state')
        if state:
            ns.state = state
        optimizer = sav.get('optimizer')
        if load_optimizer and optimizer:
            ns.optimizer.load_state_dict(optimizer)
        return ns

    def train(self,
              batch_size=8,
              num_workers=2,
              checkpoint=2000,
              sampling_test=True,
              early_stop=False):
        if self.Dataset:
            dataset = self.Dataset(root=self.path.dataset_path(),
                                   transform=self.options.train.transform,
                                   train=True)
        else:
            dataset = torchvision.datasets.CIFAR10(
                root=self.path.dataset_path(),
                transform=self.options.train.transform,
                download=True)

        trainloader = torch.utils.data.DataLoader(
            dataset,
            batch_size=batch_size,
            shuffle=self.options.train.shuffle,
            num_workers=num_workers)
        base_epoch = self.state['epoch']
        cuda = self.cuda
        net = self.net
        optimizer = self.optimizer
        criterion = self.criterion
        checkpoint_timer = 0
        checkpoint_save = self.options.train.save_checkpoint
        epoch_save = self.options.train.save_epoch
        if cuda:
            net.cuda()
        es_history = 0
        es_flag = 0
        for epoch in range(base_epoch, self.options.train.max_epoch):
            then = datetime.now()
            running_loss = 0
            total_itr = 0
            for itr, data in enumerate(trainloader, 0):
                xs, truth = data
                if cuda:
                    xs, truth = Variable(xs.cuda()), Variable(truth.cuda())
                else:
                    xs, truth = Variable(xs), Variable(truth)
                optimizer.zero_grad()
                ys = net(xs)
                loss = criterion(ys, truth)
                loss.backward()
                optimizer.step()
                running_loss += loss.data.item()
                if itr != 0 and itr % checkpoint == 0:
                    now = datetime.now()
                    average_loss = running_loss / checkpoint
                    print(
                        f'[{epoch + 1}, {itr:5d}] loss:{average_loss:.10f} | {(now - then).seconds}s'
                    )
                    running_loss = 0.0
                    then = now
                    checkpoint_timer += 1
                    if checkpoint_save > 0 and checkpoint_timer % checkpoint_save == 0:
                        self.state['iter'] = itr
                        self.save()
                total_itr = itr
            self.state['iter'] = total_itr
            self.state['epoch'] = epoch + 1
            if epoch_save > 0 and (epoch + 1) % epoch_save == 0:
                self.save()
            if sampling_test:
                accuracy = self.test(batch_size=batch_size,
                                     num_workers=num_workers)
                if early_stop:
                    if accuracy <= es_history:
                        es_flag += 1
                        if es_flag >= self.options.train.early_stop_max:
                            return
                    else:
                        es_flag = 0
                    es_history = accuracy

    def test(self, batch_size=8, num_workers=2):
        if self.Dataset:
            dataset = self.Dataset(root=self.path.dataset_path(),
                                   transform=self.options.test.transform,
                                   train=False)
        else:
            dataset = torchvision.datasets.CIFAR10(
                root=self.path.dataset_root,
                train=False,
                transform=self.options.test.transform,
                download=True)

        testloader = torch.utils.data.DataLoader(
            dataset,
            batch_size=batch_size,
            shuffle=self.options.test.shuffle,
            num_workers=num_workers)
        net = self.net
        cuda = self.cuda
        if cuda:
            net.cuda()
        correct = 0
        total = 0
        for data in testloader:
            xs, truth = data
            if cuda:
                xs, truth = Variable(xs.cuda()), Variable(truth.cuda())
            else:
                xs, truth = Variable(xs), Variable(truth)
            ys = net(xs)
            _, predicted = torch.max(ys.data, 1)
            total += truth.size(0)
            correct += (predicted == truth).sum()
        acc = 100.0 * correct.float() / total
        print(f'Accuracy of the network on the test dataset_root: {acc:.2f} %')
        return acc

    def save(self):
        pkg = {'net': self.net}
        suffix = 'n'
        if self.options.save.optimizer:
            pkg['optimizer'] = self.optimizer.state_dict()
            suffix += 'o'
        if self.options.save.state:
            pkg['state'] = self.state
            suffix += 's'
        suffix += '.sav'
        nhash = self.net_hash()
        fp = f"{self.path.save_path()}/{nhash}.{suffix}"
        torch.save(pkg, fp)
        print(f'saved as {fp}')

    def net_hash(self):
        special_mark = self.state.get('special_mark')
        if special_mark:
            special_mark = f'x{special_mark}'
        else:
            special_mark = ''
        return f"{self.net._get_name()}{special_mark}_ep{self.state['epoch']}_{self.state['iter']}"
def train(
        cfg,
        data_cfg,
        resume=False,
        epochs=100,
        batch_size=16,
        accumulated_batches=1,
        freeze_backbone=False,
        opt=None,
):
    weights = '../weights'   # 改到上一层, 这样方便文件夹复制
    mkdir_if_missing(weights)
    latest = osp.join(weights, 'latest.pt') # 这个是为了resume上次存好的checkpoint,注意不要覆盖!

    torch.backends.cudnn.benchmark = True  # unsuitable for multiscale

    # Configure run
    print("loading data")
    sys.stdout.flush()
    f = open(data_cfg)
    data_config = json.load(f)
    trainset_paths = data_config['train']
    dataset_root = data_config['root']
    f.close()
    cfg_dict = parse_model_cfg(cfg) 
    img_size = [int(cfg_dict[0]['width']), int(cfg_dict[0]['height'])]

    # Get dataloader
    transforms = T.Compose([T.ToTensor()])
    dataset = JointDataset(dataset_root, trainset_paths, img_size, augment=True, transforms=transforms)
    dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True,
                                             num_workers=8, pin_memory=False, drop_last=True, collate_fn=collate_fn) 

    # Initialize model
    print("building model")
    sys.stdout.flush()
    model = Darknet(cfg_dict, dataset.nID)

    cutoff = -1  # backbone reaches to cutoff layer
    start_epoch = 0
    if resume:
        if opt.latest:
            latest_resume = "/home/master/kuanzi/weights/72_epoch_arcface.pt"
            print("Loading the latest weight...", latest_resume)
            checkpoint = torch.load(latest_resume, map_location='cpu')

            # Load weights to resume from
            model.load_state_dict(checkpoint['model'])
            model.cuda().train()

            # Set optimizer
            classifer_param_value = list(map(id, model.classifier.parameters()))
            classifer_param = model.classifier.parameters()
            base_params = filter(lambda p: id(p) not in classifer_param_value, model.parameters())
            print("classifer_param\n", classifer_param)  #  [2218660649072]
            print("classifer_param_value\n", classifer_param_value)  #  [2218660649072]
            print("base_params\n", base_params)  # <filter object at 0x0000020493D95048>
            sys.stdout.flush()
            # optimizer = torch.optim.SGD(filter(lambda x: x.requires_grad, model.parameters()), lr=opt.lr * 0.1, momentum=.9)
            optimizer = torch.optim.SGD([
                        {'params': filter(lambda x: x.requires_grad, base_params), 'lr': opt.lr * 0.01},
                        {'params': classifer_param, 'lr': opt.lr}], 
                        momentum=.9)
            # optimizer = torch.optim.SGD(filter(lambda x: x.requires_grad, model.parameters()), lr=opt.lr, momentum=.9)

            start_epoch = checkpoint['epoch'] + 1
            if checkpoint['optimizer'] is not None:
                # Anyway, if you’re “freezing” any part of your network, and your optimizer is only passed “unfrozen” model parameters 
                # (i.e. your optimizer filters out model parameters whose requires_grad is False), 
                # then when resuming, you’ll need to unfreeze the network again and re-instantiate the optimizer afterwards. 
                optimizer.load_state_dict(checkpoint['optimizer'])

            del checkpoint  # current, saved

        else:
            # pretrain = "/home/master/kuanzi/weights/jde_1088x608_uncertainty.pt"
            pretrain = "/home/master/kuanzi/weights/jde_864x480_uncertainty.pt" #576x320
            print("Loading jde finetune weight...", pretrain)
            sys.stdout.flush()
            checkpoint = torch.load(pretrain, map_location='cpu')
            
            model_dict = model.state_dict()
            pretrained_dict = {k: v for k, v in checkpoint['model'].items() if not k.startswith("classifier")}  # 去掉全连接层
            model_dict.update(pretrained_dict)
            model.load_state_dict(model_dict)
            model.cuda().train()
            print ("model weight loaded")
            sys.stdout.flush()

            classifer_param_value = list(map(id, model.classifier.parameters()))
            classifer_param = model.classifier.parameters()
            base_params = filter(lambda p: id(p) not in classifer_param_value, model.parameters())
            print("classifer_param\n", classifer_param)  #  [2218660649072]
            print("classifer_param_value\n", classifer_param_value)  #  [2218660649072]
            print("base_params\n", base_params)  # <filter object at 0x0000020493D95048>
            sys.stdout.flush()
            # optimizer = torch.optim.SGD(filter(lambda x: x.requires_grad, model.parameters()), lr=opt.lr * 0.1, momentum=.9)
            optimizer = torch.optim.SGD([
                        {'params': filter(lambda x: x.requires_grad, base_params), 'lr': opt.lr * 0.01},
                        {'params': classifer_param, 'lr': opt.lr}], 
                        momentum=.9)

            print("chk epoch:\n", checkpoint['epoch'])
            sys.stdout.flush()
            start_epoch = checkpoint['epoch'] + 1

    else:
        # Initialize model with backbone (optional)
        print("Loading backbone...")
        sys.stdout.flush()
        if cfg.endswith('yolov3.cfg'):
            load_darknet_weights(model, osp.join(weights ,'darknet53.conv.74'))
            cutoff = 75
        elif cfg.endswith('yolov3-tiny.cfg'):
            load_darknet_weights(model, osp.join(weights , 'yolov3-tiny.conv.15'))
            cutoff = 15

        model.cuda().train()

        # Set optimizer
        optimizer = torch.optim.SGD(filter(lambda x: x.requires_grad, model.parameters()), lr=opt.lr, momentum=.9, weight_decay=1e-4)

    model = torch.nn.DataParallel(model)
    # Set scheduler
    scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, 
            milestones=[int(0.5*opt.epochs), int(0.75*opt.epochs)], gamma=0.1)
    
    # An important trick for detection: freeze bn during fine-tuning 
    if not opt.unfreeze_bn:
        for i, (name, p) in enumerate(model.named_parameters()):
            p.requires_grad = False if 'batch_norm' in name else True

    model_info(model)
       
    t0 = time.time()
    print("begin training...")
    sys.stdout.flush()
    for epoch in range(epochs):
        epoch += start_epoch

        logger.info(('%8s%12s' + '%10s' * 6) % (
            'Epoch', 'Batch', 'box', 'conf', 'id', 'total', 'nTargets', 'time'))
        
        # Freeze darknet53.conv.74 for first epoch
        if freeze_backbone and (epoch < 2):
            for i, (name, p) in enumerate(model.named_parameters()):
                if int(name.split('.')[2]) < cutoff:  # if layer < 75
                    p.requires_grad = False if (epoch == 0) else True

        ui = -1
        rloss = defaultdict(float)  # running loss
        optimizer.zero_grad()
        for i, (imgs, targets, _, _, targets_len) in enumerate(dataloader):
            if sum([len(x) for x in targets]) < 1:  # if no targets continue
                continue
            
            # SGD burn-in
            burnin = min(1000, len(dataloader))
            if (epoch == 0) & (i <= burnin):
                lr = opt.lr * (i / burnin) **4 
                for g in optimizer.param_groups:
                    g['lr'] = lr
            
            # Compute loss, compute gradient, update parameters
            loss, components = model(imgs.cuda(), targets.cuda(), targets_len.cuda())
            components = torch.mean(components.view(-1, 5),dim=0)

            loss = torch.mean(loss)
            loss.backward()

            # accumulate gradient for x batches before optimizing
            if ((i + 1) % accumulated_batches == 0) or (i == len(dataloader) - 1):
                optimizer.step()
                optimizer.zero_grad()

            # Running epoch-means of tracked metrics
            ui += 1
            
            for ii, key in enumerate(model.module.loss_names):
                rloss[key] = (rloss[key] * ui + components[ii]) / (ui + 1)

            s = ('%8s%12s' + '%10.3g' * 6) % (
                '%g/%g' % (epoch, epochs - 1),
                '%g/%g' % (i, len(dataloader) - 1),
                rloss['box'], rloss['conf'],
                rloss['id'],rloss['loss'],
                rloss['nT'], time.time() - t0)
            t0 = time.time()
            if i % opt.print_interval == 0:
                logger.info(s)
        
        # # Save latest checkpoint
        # checkpoint = {'epoch': epoch,
        #               'model': model.module.state_dict(),
        #               'optimizer': optimizer.state_dict()}
        # torch.save(checkpoint, latest)

        # Calculate mAP
        if epoch % opt.test_interval ==0 and epoch != 0:
            epoch_chk = osp.join(weights, str(epoch) + '_epoch_arcface.pt')
            checkpoint = {'epoch': epoch,
                    'model': model.module.state_dict(),
                    'optimizer': optimizer.state_dict()}
            torch.save(checkpoint, epoch_chk)
            # """ 训练与测试解耦,以下工作单独进行 """
            # with torch.no_grad():
            #     # mAP, R, P = test.test(cfg, data_cfg, weights=latest, batch_size=batch_size, print_interval=40)
            #     # print ("test.test:\t", mAP, "\t", R, "\t", P)
            #     test_mapgiou.test_giou(cfg, data_cfg, weights=latest, batch_size=batch_size, print_interval=40)
            #     test_mapgiou.test_emb(cfg, data_cfg, weights=latest, batch_size=batch_size, print_interval=40)


        # Call scheduler.step() after opimizer.step() with pytorch > 1.1.0 
        scheduler.step()
Exemplo n.º 21
0
def train_single_task(t_id, task, tasks, vis_p, learner, config,
                      transfer_matrix, total_steps):

    training_params = config.pop('training-params')
    learner_params = config.pop('learner-params', {})
    assert 'model-params' not in config, "Can't have model-specific " \
                                         "parameters while tuning at the " \
                                         "stream level."

    if learner_params:
        learner.set_h_params(**learner_params)

    batch_sizes = training_params.pop('batch_sizes')
    # optim_func = training_params.pop('optim_func')
    optim_func = learner.optim_func
    optim_params = config.pop('optim')
    schedule_mode = training_params.pop('schedule_mode')
    split_optims = training_params.pop('split_optims')

    dropout = config.pop('dropout') if 'dropout' in config else None

    stream_setting = training_params.pop('stream_setting')
    plot_all = training_params.pop('plot_all')
    normalize = training_params.pop('normalize')
    augment_data = training_params.pop('augment_data')
    transformations = []
    if augment_data:
        transformations.extend([
            transforms.ToPILImage(),
            transforms.RandomHorizontalFlip(),
            transforms.RandomCrop(32, 4),
            transforms.ToTensor()
        ])
    lca_n = training_params.pop('lca')

    if plot_all:
        vis_p = get_training_vis_conf(vis_p, tune.get_trial_dir())
        # print('NEW vis: ', vis_p)
        task_vis = visdom.Visdom(**vis_p)
        # env = [env[0], env[-1]]
        # vis_p['env'] = '_'.join(env)
        # vis_p['log_to_filename'] = os.path.join(vis_logdir, vis_p['env'])
        # g_task_vis = visdom.Visdom(**vis_p)

        logger.info(get_env_url(task_vis))
    else:
        task_vis = None

    t_trans = [[] for _ in range(len(task['split_names']))]
    t_trans[0] = transformations.copy()

    datasets_p = dict(task=task, transforms=t_trans, normalize=normalize)
    datasets = _load_datasets(**datasets_p)
    train_loader, eval_loaders = get_classic_dataloaders(datasets, batch_sizes)

    assert t_id == task['id']

    start1 = time.time()
    model = learner.get_model(task['id'],
                              x_dim=task['x_dim'],
                              n_classes=task['n_classes'],
                              descriptor=task['descriptor'],
                              dataset=eval_loaders[:2])
    model_creation_time = time.time() - start1

    loss_fn = task['loss_fn']
    training_params['loss_fn'] = loss_fn

    prepare_batch = _prepare_batch
    if hasattr(model, 'prepare_batch_wrapper'):
        prepare_batch = model.prepare_batch_wrapper(prepare_batch, t_id)

    if hasattr(model, 'loss_wrapper'):
        training_params['loss_fn'] = \
            model.loss_wrapper(training_params['loss_fn'])

    # if hasattr(model, 'backward_hook'):
    #     training_params[]

    # optim = set_optim_params(optim_func, optim_params, model, split_optims)
    optim_fact = partial(set_optim_params,
                         optim_func=optim_func,
                         optim_params=optim_params,
                         split_optims=split_optims)
    # if schedule_mode == 'steps':
    #     lr_scheduler = torch.optim.lr_scheduler.\
    #         MultiStepLR(optim[0], milestones=[25, 40])
    # elif schedule_mode == 'cos':
    #     lr_scheduler = torch.optim.lr_scheduler.\
    #         CosineAnnealingLR(optim[0], T_max=200, eta_min=0.001)
    # elif schedule_mode is None:
    #     lr_scheduler = None
    # else:
    #     raise NotImplementedError()
    if dropout is not None:
        set_dropout(model, dropout)

    assert not config, config
    start2 = time.time()
    rescaled, t, metrics, b_state_dict = train_model(
        model, datasets_p, batch_sizes, optim_fact, prepare_batch, task,
        train_loader, eval_loaders, training_params, config)

    training_time = time.time() - start2
    start3 = time.time()
    if not isinstance(model, ExhaustiveSearch):
        #todo Handle the state dict loading uniformly for all learners RN only
        # the exhaustive search models load the best state dict after training
        model.load_state_dict(b_state_dict['state_dict'])

    iterations = list(metrics.pop('training_iteration').values())
    epochs = list(metrics.pop('training_epoch').values())

    assert len(iterations) == len(epochs)
    index = dict(epochs=epochs, iterations=iterations)
    update_summary(index, task_vis, 'index', 0.5)

    grouped_xs = dict()
    grouped_metrics = defaultdict(list)
    grouped_legends = defaultdict(list)
    for metric_n, metric_v in metrics.items():
        split_n = metric_n.split()
        if len(split_n) < 2:
            continue
        name = ' '.join(split_n[:-1])
        grouped_metrics[split_n[-1]].append(list(metric_v.values()))
        grouped_legends[split_n[-1]].append(name)
        if split_n[-1] in grouped_xs:
            if len(metric_v) > len(grouped_xs[split_n[-1]]):
                longer_xs = list(metric_v.keys())
                assert all(a == b
                           for a, b in zip(longer_xs, grouped_xs[split_n[-1]]))
                grouped_xs[split_n[-1]] = longer_xs
        else:
            grouped_xs[split_n[-1]] = list(metric_v.keys())

    for (plot_name, val), (_, legends) in sorted(
            zip(grouped_metrics.items(), grouped_legends.items())):
        assert plot_name == _
        val = fill_matrix(val)
        if len(val) == 1:
            val = np.array(val[0])
        else:
            val = np.array(val).transpose()
        x = grouped_xs[plot_name]
        task_vis.line(val,
                      X=x,
                      win=plot_name,
                      opts={
                          'title': plot_name,
                          'showlegend': True,
                          'width': 500,
                          'legend': legends,
                          'xlabel': 'iterations',
                          'ylabel': plot_name
                      })

    avg_data_time = list(metrics['data time_ps'].values())[-1]
    avg_forward_time = list(metrics['forward time_ps'].values())[-1]
    avg_epoch_time = list(metrics['epoch time_ps'].values())[-1]
    avg_eval_time = list(metrics['eval time_ps'].values())[-1]
    total_time = list(metrics['total time'].values())[-1]

    entropies, ent_legend = [], []
    for metric_n, metric_v in metrics.items():
        if metric_n.startswith('Trainer entropy'):
            entropies.append(list(metric_v.values()))
            ent_legend.append(metric_n)

    if entropies:
        task_vis.line(np.array(entropies).transpose(),
                      X=iterations,
                      win='ENT',
                      opts={
                          'title': 'Arch entropy',
                          'showlegend': True,
                          'width': 500,
                          'legend': ent_legend,
                          'xlabel': 'Iterations',
                          'ylabel': 'Loss'
                      })

    if hasattr(learner, 'arch_scores') and hasattr(learner, 'get_top_archs'):
        update_summary(learner.arch_scores[t_id], task_vis, 'scores')
        archs = model.get_top_archs(5)
        list_top_archs(archs, task_vis)

    if 'training_archs' in metrics:
        plot_trajectory(model.ssn.graph, metrics['training_archs'],
                        model.ssn.stochastic_node_ids, task_vis)

    postproc_time = time.time() - start3
    start4 = time.time()
    save_path = tune.get_trial_dir()
    finish_res = learner.finish_task(datasets[0], t_id, task_vis, save_path)
    finish_time = time.time() - start4

    start5 = time.time()
    eval_tasks = tasks
    # eval_tasks = tasks[:t_id + 1] if stream_setting else tasks
    evaluation = evaluate_on_tasks(eval_tasks,
                                   learner,
                                   batch_sizes[1],
                                   training_params['device'], ['Val', 'Test'],
                                   normalize,
                                   cur_task=t_id)
    assert evaluation['Val']['accuracy'][t_id] == b_state_dict['value']

    stats = {}
    eval_time = time.time() - start5

    stats.update(finish_res)

    test_accs = metrics['Test accuracy_0']
    if not test_accs:
        lca = np.float('nan')
    else:
        if len(test_accs) <= lca_n:
            last_key = max(test_accs.keys())
            assert len(test_accs) == last_key + 1,\
                f"Can't compute LCA@{lca_n} if steps were skipped " \
                f"(got {list(test_accs.keys())})"
            test_accs = test_accs.copy()
            last_acc = test_accs[last_key]
            for i in range(last_key + 1, lca_n + 1):
                test_accs[i] = last_acc
        lca = np.mean([test_accs[i] for i in range(lca_n + 1)])

    accs = {}
    key = 'accuracy'
    # logger.warning(evaluation)
    for split in evaluation.keys():
        transfer_matrix[split].append(evaluation[split][key])
        for i in range(len(tasks)):
            split_acc = evaluation[split][key]
            if i < len(split_acc):
                accs['{}_T{}'.format(split, i)] = split_acc[i]
            else:
                accs['{}_T{}'.format(split, i)] = float('nan')
    plot_heatmaps(list(transfer_matrix.keys()),
                  list(map(fill_matrix, transfer_matrix.values())), task_vis)

    # logger.warning(t_id)
    # logger.warning(transfer_matrix)

    avg_val = np.mean(evaluation['Val']['accuracy'])
    avg_val_so_far = np.mean(evaluation['Val']['accuracy'][:t_id + 1])
    avg_test = np.mean(evaluation['Test']['accuracy'])
    avg_test_so_far = np.mean(evaluation['Test']['accuracy'][:t_id + 1])

    step_time_s = time.time() - start1
    step_sum = model_creation_time + training_time + postproc_time + \
               finish_time + eval_time
    best_it = b_state_dict.get('cum_best_iter', b_state_dict['iter'])
    tune.report(
        t=t_id,
        best_val=b_state_dict['value'],
        avg_acc_val=avg_val,
        avg_acc_val_so_far=avg_val_so_far,
        avg_acc_test_so_far=avg_test_so_far,
        lca=lca,
        avg_acc_test=avg_test,
        test_acc=evaluation['Test']['accuracy'][t_id],
        duration_seconds=step_time_s,
        duration_iterations=t,
        duration_best_it=best_it,
        duration_finish=finish_time,
        duration_model_creation=model_creation_time,
        duration_training=training_time,
        duration_postproc=postproc_time,
        duration_eval=eval_time,
        duration_sum=step_sum,
        # entropy=stats.pop('entropy'),
        new_params=learner.new_params(t_id),
        total_params=learner.n_params(t_id),
        total_steps=total_steps + t,
        fw_t=round(avg_forward_time * 1000) / 1000,
        data_t=round(avg_data_time * 1000) / 1000,
        epoch_t=round(avg_epoch_time * 1000) / 1000,
        eval_t=round(avg_eval_time * 1000) / 1000,
        total_t=round(total_time * 1000) / 1000,
        env_url=get_env_url(vis_p),
        **accs,
        **stats)
    return rescaled, t, metrics, b_state_dict, stats
 def __init__(self, clip_info):
     self.list = clip_info
     self.transform = transforms.Compose([
         transforms.ToTensor(),
         transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
     ])
Exemplo n.º 23
0
from torch.utils.data import DataLoader
from torch import nn, optim
from torchvision.transforms import transforms
from unet import Unet
from DataHelper import *
from tqdm import tqdm
import numpy as np
import skimage.io as io

PATH = './model/unet_model.pt'

# 是否使用cuda
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

x_transforms = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize([0.5], [0.5])])

# mask只需要转换为tensor
y_transforms = transforms.ToTensor()


def train_model(model, criterion, optimizer, dataload, num_epochs=10):
    best_model = model
    min_loss = 1000
    for epoch in range(num_epochs):
        print('Epoch {}/{}'.format(epoch, num_epochs - 1))
        # print('-' * 10)
        dt_size = len(dataload.dataset)
        epoch_loss = 0
        step = 0
Exemplo n.º 24
0
    def __init__(self, body_regressor_checkpoint, hand_regressor_checkpoint, smpl_dir, device=torch.device('cuda'), use_smplx=True):
        super().__init__('BodyMocap')
        self.device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
        print("Loading Body Pose Estimator")
        self.__load_body_estimator()
        self.visualizer = Visualizer('opengl')
        self.frame_id = 0	#count frames

        parser = argparse.ArgumentParser()
        parser.add_argument("--rot90", default=False, type= bool, help="clockwise rotate 90 degrees")
        #parser.add_argument("--camera_topic", default="/logi_c922_2/image_rect_color", help="choose a topic as input image")
        parser.add_argument("--body_only", default=False, type= bool, help="detect only body and save its result")
        parser.add_argument("--result_path", default="/home/student/result/", help="choose a topic as input image")
        parser.add_argument("--save_result", default=False, help="save result or not")
        args = parser.parse_args()
        self.rot90 = args.rot90
        #self.camera_topic = args.camera_topic
        self.body_only = args.body_only
        self.result_path = args.result_path
        self.save_result = args.save_result
        self.load = [0,0]
        self.angle_leg = 0
        self.angle_trunk = 0
        self.start = 0
        self.angles =  np.empty((1,20),dtype = float)
        self.body_side =  np.empty((25,3),dtype = float)
        # Load parametric model (SMPLX or SMPL)
        if use_smplx:
            smplModelPath = smpl_dir + '/SMPLX_NEUTRAL.pkl'
            self.smpl = SMPLX(smpl_dir,
                    batch_size=1,
                    num_betas = 10,
                    use_pca = False,
                    create_transl=False).to(self.device)
            self.use_smplx = True
        else:
            smplModelPath = smpl_dir + '/basicModel_neutral_lbs_10_207_0_v1.0.0.pkl'
            self.smpl = SMPL(smplModelPath, batch_size=1, create_transl=False).to(self.device)
            self.use_smplx = False
            
        #Load pre-trained neural network 
        SMPL_MEAN_PARAMS = '/home/student/frankmocap/extra_data/body_module/data_from_spin/smpl_mean_params.npz'
        self.model_regressor = hmr(SMPL_MEAN_PARAMS).to(self.device)
        body_checkpoint = torch.load(body_regressor_checkpoint)
        self.model_regressor.load_state_dict(body_checkpoint['model'], strict=False)
        self.model_regressor.eval()

       #hand module init
        
        transform_list = [ transforms.ToTensor(),
                          transforms.Normalize((0.5, 0.5, 0.5),
                                               (0.5, 0.5, 0.5))]
        self.normalize_transform = transforms.Compose(transform_list)

        #Load Hand network 
        self.opt = TestOptions().parse([])

        #Default options
        self.opt.single_branch = True
        self.opt.main_encoder = "resnet50"
        # self.opt.data_root = "/home/hjoo/dropbox/hand_yu/data/"
        self.opt.model_root = "/home/student/frankmocap/extra_data"
        self.opt.smplx_model_file = os.path.join(smpl_dir,'SMPLX_NEUTRAL.pkl')
      
        self.opt.batchSize = 1
        self.opt.phase = "test"
        self.opt.nThreads = 0
        self.opt.which_epoch = -1
        self.opt.checkpoint_path = hand_regressor_checkpoint

        self.opt.serial_batches = True  # no shuffle
        self.opt.no_flip = True  # no flip
        self.opt.process_rank = -1

        # self.opt.which_epoch = str(epoch)
        self.hand_model_regressor = H3DWModel(self.opt)
        # if there is no specified checkpoint, then skip
        assert self.hand_model_regressor.success_load, "Specificed checkpoints does not exists: {}".format(self.opt.checkpoint_path)
        self.hand_model_regressor.eval()

        self.hand_bbox_detector = HandBboxDetector('third_view', self.device)

 		#subscriber and publisher initialization
		#input subscriber
        self.br = CvBridge()
        self.subscription_img = self.create_subscription(Image, '/side_img', self.callback_side,10)
        self.subscription_img = self.create_subscription(Image, '/front_img', self.callback_front,10)
		
		#output publisher
        self.publisher_pose = self.create_publisher(Image,'/pose',10)	#images with keypoints annotation
        #self.publisher_keypoints = self.create_publisher(Float32MultiArray,'/keypoints',10)	#keypoints coordinates
        self.publisher_risk = self.create_publisher(Int64,'/risk',10)	#risk level
        self.publisher_angles = self.create_publisher(Float32MultiArray,'/angles',10)
Exemplo n.º 25
0
#!/usr/bin/python
# -*- coding:utf-8 -*-
"""
@author:fangpf
@time: 2020/11/30
"""
import torch
from torchvision.transforms import transforms

from model.inception_iccv import InceptionNet

normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
transform = transforms.Compose(
    [transforms.Resize(size=(256, 128)),
     transforms.ToTensor(), normalize])

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

classes = [
    'Female', 'AgeLess16', 'Age17-30', 'Age31-45', 'BodyFat', 'BodyNormal',
    'BodyThin', 'Customer', 'Clerk', 'BaldHead', 'LongHair', 'BlackHair',
    'Hat', 'Glasses', 'Muffler', 'Shirt', 'Sweater', 'Vest', 'TShirt',
    'Cotton', 'Jacket', 'Suit-Up', 'Tight', 'ShortSleeve', 'LongTrousers',
    'Skirt', 'ShortSkirt', 'Dress', 'Jeans', 'TightTrousers', 'LeatherShoes',
    'SportShoes', 'Boots', 'ClothShoes', 'CasualShoes', 'Backpack', 'SSBag',
    'HandBag', 'Box', 'PlasticBag', 'PaperBag', 'HandTrunk', 'OtherAttchment',
    'Calling', 'Talking', 'Gathering', 'Holding', 'Pusing', 'Pulling',
    'CarryingbyArm', 'CarryingbyHand'
]
classes_name = [
Exemplo n.º 26
0
def evaluate(args):
    '''
    This script is used to generate a evaluation figure.
    '''

    use_gpu = torch.cuda.is_available()
    if use_gpu:
        print("GPU Enabled")
        print(f"Current GPU Memory Usage: {torch.cuda.memory_allocated()}")

    #Create the data
    t = transforms.Compose([
        transforms.Resize(args.input_size),
        transforms.ToTensor(),
    ])

    t2 = transforms.Compose([
        transforms.Resize(args.mask_size),
        transforms.ToTensor(),
    ])

    data_set = landpy.MyDataLoader(args.data_dir,
                                   args.mask_size,
                                   image_transforms=t,
                                   mask_transforms=t2)

    #Set Batch Size
    batch_size = 1

    dataset_size = len(data_set)
    print(f"Number of Images {dataset_size}")

    train_loader, test_loader = landpy.create_data_loaders(
        data_set, 0.8, batch_size)

    unet_model = landpy.UNet(3, 7)
    if use_gpu:
        unet_model = unet_model.cuda()
        unet_model.load_state_dict(torch.load(args.model_path))
    else:
        unet_model.load_state_dict(
            torch.load(args.model_path, map_location=torch.device('cpu')))

    unet_model.eval()

    num_images = 3

    figure_row = 0
    sat_image, actual_mask, label = next(iter(test_loader))

    fig, ax = plt.subplots(nrows=num_images, ncols=3, figsize=(20, 20))
    for i, (test_image, labels, class_labels) in enumerate(test_loader):
        if use_gpu:
            test_image = test_image.cuda()
            torch.cuda.empty_cache()

        predictions = unet_model(test_image)
        soft_max_output = torch.nn.LogSoftmax(dim=1)(predictions)

        if use_gpu:
            soft_max_output = soft_max_output.cpu()

        numpy_output = soft_max_output.data.numpy()
        final_prediction = np.argmax(numpy_output, axis=1)
        prediction_img = landpy.construct_image(final_prediction[0],
                                                args.mask_size)

        ax[figure_row][0].imshow(
            np.transpose(test_image[0].data.numpy(), (1, 2, 0)))
        ax[figure_row][0].set_title("Real Image")
        ax[figure_row][1].imshow(
            np.transpose(labels[0].data.numpy(), (1, 2, 0)))
        ax[figure_row][1].set_title("Actual Mask")
        ax[figure_row][2].imshow(np.transpose(prediction_img, (1, 2, 0)))
        ax[figure_row][2].set_title("Prediction Mask")

        figure_row += 1
        if figure_row == num_images:
            break

    fig_path = os.path.join(args.figure_path, 'evaluation_figure.png')
    plt.savefig(fig_path)
Exemplo n.º 27
0
        x = ReLU(self.fc4(code))
        x = ReLU(self.fc5(x))
        x = self.fc6(x)

        return x

    def encoders(self, x):

        x = ReLU(self.fc1(x))
        x = ReLU(self.fc2(x))
        code = ReLU(self.fc3(x))

        return code


tr = transforms.Compose([transforms.ToTensor()])

train = MNIST('./data', transform=tr, download=True)
test = MNIST('./data', transform=tr, train=False, download=True)

epoch = 10
batch_size = 30
train = DataLoader(train, batch_size, True)
test = DataLoader(test, batch_size, False)
vae = VAE((1, 28, 28), 120)
sgd = tch.optim.RMSprop(vae.parameters())
mse = nn.MSELoss()
# dev = tch.device('cuda:0')
# vae.to(dev)

for j in range(epoch):
Exemplo n.º 28
0
from  torch.utils.data import DataLoader
import torchvision
from torchvision.transforms import transforms as tfs
from torchvision import models
from torchvision.models import ResNet
import numpy as np
from visdom import Visdom
import os

data_dir='./data/hymenoptera_data'

train_dataset=torchvision.datasets.ImageFolder(root=os.path.join(data_dir,'train'),
                                               transform=tfs.Compose([
                                                tfs.RandomResizedCrop(224),
                                                tfs.RandomHorizontalFlip(),
                                                tfs.ToTensor(),
                                                tfs.Normalize(
                                                mean=(0.485, 0.456, 0.406),
                                                std=(0.229, 0.224, 0.225)
                                                )
                                               ])
                                               )
val_dataset=torchvision.datasets.ImageFolder(root=os.path.join(data_dir,'val'),
                                               transform=tfs.Compose([
                                                tfs.RandomResizedCrop(224),
                                                tfs.RandomHorizontalFlip(),
                                                tfs.ToTensor(),
                                                tfs.Normalize(
                                                mean=(0.485, 0.456, 0.406),
                                                std=(0.229, 0.224, 0.225)
                                                )
Exemplo n.º 29
0
# -*- coding: utf-8 -*-
"""s15transform.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1aAmDlTSAc1ypmMIHgcHpYVlTSFQL3Dbo
"""

from torchvision.transforms import transforms

scale_transform = transforms.Compose([
    transforms.Resize((128, 128)),
    transforms.Grayscale(num_output_channels=3),
    transforms.ToTensor(),
])
Exemplo n.º 30
0
    def __init__(self):
        ##The top config
        #self.data_root = '/media/hhy/data/USdata/MergePhase1/test_0.3'
        #self.log_dir = '/media/hhy/data/code_results/MILs/MIL_H_Attention'

        self.root = '/remote-home/my/Ultrasound_CV/data/Ruijin/clean'
        self.log_dir = '/remote-home/my/hhy/Ultrasound_MIL/experiments/PLN1/base/fold4'
        if not os.path.exists(self.log_dir):
            os.makedirs(self.log_dir)
        ##training config
        self.lr = 1e-4
        self.epoch = 50
        self.resume = -1
        self.batch_size = 1
        self.net = Attention()
        self.net.cuda()

        self.optimizer = Adam(self.net.parameters(), lr=self.lr)
        self.lrsch = torch.optim.lr_scheduler.MultiStepLR(
            self.optimizer, milestones=[10, 30, 50, 70], gamma=0.5)

        self.logger = Logger(self.log_dir)
        self.train_transform = transforms.Compose([
            transforms.Resize((224, 224)),
            transforms.RandomResizedCrop((224, 224)),
            transforms.RandomHorizontalFlip(0.5),
            transforms.RandomVerticalFlip(0.5),
            transforms.ColorJitter(0.25, 0.25, 0.25, 0.25),
            transforms.ToTensor()
        ])
        self.test_transform = transforms.Compose(
            [transforms.Resize((224, 224)),
             transforms.ToTensor()])

        self.label_name = "手术淋巴结情况(0未转移;1转移)"
        self.trainbag = RuijinBags(self.root, [0, 1, 2, 3],
                                   self.train_transform,
                                   label_name=self.label_name)
        self.testbag = RuijinBags(self.root, [4],
                                  self.test_transform,
                                  label_name=self.label_name)

        train_label_list = list(
            map(lambda x: int(x['label']), self.trainbag.patient_info))
        pos_ratio = sum(train_label_list) / len(train_label_list)
        print(pos_ratio)
        train_weight = [(1 - pos_ratio) if x > 0 else pos_ratio
                        for x in train_label_list]

        # self.train_sampler = WeightedRandomSampler(weights=train_weight, num_samples=len(self.trainbag))
        self.train_loader = DataLoader(self.trainbag,
                                       batch_size=self.batch_size,
                                       num_workers=8,
                                       shuffle=True)
        self.val_loader = DataLoader(self.testbag,
                                     batch_size=self.batch_size,
                                     shuffle=False,
                                     num_workers=8)

        if self.resume > 0:
            self.net, self.optimizer, self.lrsch, self.loss, self.global_step = self.logger.load(
                self.net, self.optimizer, self.lrsch, self.loss, self.resume)
        else:
            self.global_step = 0

        # self.trainer = MTTrainer(self.net, self.optimizer, self.lrsch, self.loss, self.train_loader, self.val_loader, self.logger, self.global_step, mode=2)
        self.trainer = MILTrainer(self.net, self.optimizer, self.lrsch, None,
                                  self.train_loader, self.val_loader,
                                  self.logger, self.global_step)