def main_worker(gpu, ngpus_per_node, args):
    global best_acc1
    args.gpu = gpu

    if args.gpu is not None:
        print("Use GPU: {} for training".format(args.gpu))

    if args.distributed:
        if args.dist_url == "env://" and args.rank == -1:
            args.rank = int(os.environ["RANK"])
        if args.multiprocessing_distributed:
            # For multiprocessing distributed training, rank needs to be the
            # global rank among all the processes
            args.rank = args.rank * ngpus_per_node + gpu
        dist.init_process_group(backend=args.dist_backend,
                                init_method=args.dist_url,
                                world_size=args.world_size,
                                rank=args.rank)
    # create model
    if args.pretrained:
        print("=> using pre-trained model '{}'".format(args.arch))
        model = models.__dict__[args.arch](pretrained=True)
    else:
        print("=> creating model '{}'".format(args.arch))
        #model = models.__dict__[args.arch]()
        model = yolonet_classification()

    if args.distributed:
        # For multiprocessing distributed, DistributedDataParallel constructor
        # should always set the single device scope, otherwise,
        # DistributedDataParallel will use all available devices.
        if args.gpu is not None:
            torch.cuda.set_device(args.gpu)
            model.cuda(args.gpu)
            # When using a single GPU per process and per
            # DistributedDataParallel, we need to divide the batch size
            # ourselves based on the total number of GPUs we have
            args.batch_size = int(args.batch_size / ngpus_per_node)
            args.workers = int(
                (args.workers + ngpus_per_node - 1) / ngpus_per_node)
            model = torch.nn.parallel.DistributedDataParallel(
                model, device_ids=[args.gpu])
        else:
            model.cuda()
            # DistributedDataParallel will divide and allocate batch_size to all
            # available GPUs if device_ids are not set
            model = torch.nn.parallel.DistributedDataParallel(model)
    elif args.gpu is not None:
        torch.cuda.set_device(args.gpu)
        model = model.cuda(args.gpu)
    else:
        # DataParallel will divide and allocate batch_size to all available GPUs
        if args.arch.startswith('alexnet') or args.arch.startswith('vgg'):
            model.features = torch.nn.DataParallel(model.features)
            model.cuda()
        else:
            model = torch.nn.DataParallel(model).cuda()

    # define loss function (criterion) and optimizer
    criterion = nn.CrossEntropyLoss().cuda(args.gpu)

    optimizer = torch.optim.SGD(model.parameters(),
                                args.lr,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)

    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            args.start_epoch = checkpoint['epoch']
            best_acc1 = checkpoint['best_acc1']
            if args.gpu is not None:
                # best_acc1 may be from a checkpoint from a different GPU
                best_acc1 = best_acc1.to(args.gpu)
            model.load_state_dict(checkpoint['state_dict'])
            optimizer.load_state_dict(checkpoint['optimizer'])
            print("=> loaded checkpoint '{}' (epoch {})".format(
                args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    cudnn.benchmark = True

    # Data loading code
    traindir = os.path.join(args.data, 'train')
    valdir = os.path.join(args.data, 'val')

    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    download = not os.path.exists(traindir)

    data = datasets.CIFAR100(args.data,
                             train=True,
                             transform=transforms.Compose([
                                 transforms.RandomResizedCrop(448),
                                 transforms.RandomHorizontalFlip(),
                                 transforms.ToTensor()
                             ]),
                             download=download)
    train_dataset, val_dataset = torch.utils.data.random_split(
        data, lengths=[int(0.7 * data.__len__()),
                       int(0.3 * data.__len__())])
    #val_data = datasets.CIFAR100(args.data, train=False,
    #																						download=download_val)
    #train_dataset = datasets.ImageFolder(
    #    traindir,
    #    transforms.Compose([
    #        transforms.RandomResizedCrop(448),
    #        transforms.RandomHorizontalFlip(),
    #        transforms.ToTensor(),
    #        normalize,
    #    ]))

    if args.distributed:
        train_sampler = torch.utils.data.distributed.DistributedSampler(
            train_dataset)
    else:
        train_sampler = None

    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=args.batch_size,
                                               shuffle=(train_sampler is None),
                                               num_workers=args.workers,
                                               pin_memory=True,
                                               sampler=train_sampler)

    val_loader = torch.utils.data.DataLoader(
        val_dataset,
        #datasets.ImageFolder(valdir, transforms.Compose([
        #    transforms.Resize(500),
        #    transforms.CenterCrop(448),
        #    transforms.ToTensor(),
        #    normalize,
        #])),
        batch_size=args.batch_size,
        shuffle=False,
        num_workers=args.workers,
        pin_memory=True)

    if args.evaluate:
        validate(val_loader, model, criterion, args)
        return

    for epoch in range(args.start_epoch, args.epochs):
        if args.distributed:
            train_sampler.set_epoch(epoch)
        adjust_learning_rate(optimizer, epoch, args)

        # train for one epoch
        train(train_loader, model, criterion, optimizer, epoch, args)

        # evaluate on validation set
        acc1 = validate(val_loader, model, criterion, args)

        # remember best acc@1 and save checkpoint
        is_best = acc1 > best_acc1
        best_acc1 = max(acc1, best_acc1)

        if not args.multiprocessing_distributed or (
                args.multiprocessing_distributed
                and args.rank % ngpus_per_node == 0):
            save_checkpoint(
                {
                    'epoch': epoch + 1,
                    'arch': args.arch,
                    'state_dict': model.state_dict(),
                    'best_acc1': best_acc1,
                    'optimizer': optimizer.state_dict(),
                }, is_best)
                            [i]] = semkittiyaml['labels'][i]

    return SemKITTI_label_name


def get_nuScenes_label_name(label_mapping):
    with open(label_mapping, 'r') as stream:
        nuScenesyaml = yaml.safe_load(stream)
    nuScenes_label_name = dict()
    for i in sorted(list(nuScenesyaml['learning_map'].keys()))[::-1]:
        val_ = nuScenesyaml['learning_map'][i]
        nuScenes_label_name[val_] = nuScenesyaml['labels_16'][val_]

    return nuScenes_label_name


if __name__ == "__main__":
    from collections import Counter
    # data = SemKITTI_sk(r"/home/jinwei/SemanticKitti/dataset/sequences",label_mapping="/mrtstorage/users/jinwei/Cylinder3D/config/label_mapping/semantic-kitti.yaml")
    # print(data.__len__())
    # xyz, label = data[0]
    # print(xyz.shape, label.shape)
    # print(Counter(label.flatten().tolist()))
    data = CycleDense(
        r"/home/jinwei/dense",
        label_mapping=
        r"/home/jinwei/Cylinder3D/config/label_mapping/cycledense.yaml")
    print(data.__len__())
    xyz, ref = data[0]
    print(xyz.shape, ref.shape)
Example #3
0
def q_evaluate(model, data, args):
    #load single mini-batch
    data_loader = torch.utils.data.DataLoader(data,
                                              batch_size=data.__len__(),
                                              shuffle=False,
                                              num_workers=4,
                                              drop_last=False)

    # train on each batch
    s_s, pos = None, None
    for batch in tqdm(data_loader):
        # first, get additional vectors per batch
        ids = batch['id']
        titles = batch['title']

        # for each id, look up associated questions
        titles, pos, neg = get_sample(data.idx_to_cand,
                                      lambda x: data.idx_to_vec[x][0], ids,
                                      titles)

        q = autograd.Variable(titles)
        ps = autograd.Variable(neg)

        # run the batch through the model, rough version dealing with mmd
        if 'use_mmd' not in args.__dict__ or not args.use_mmd:
            q = model(q)
            ps = ps.contiguous().view(-1, args.max_title)
            ps = model(ps)
        else:
            q, _ = model(q)
            ps = ps.contiguous().view(-1, args.max_title)
            ps, _ = model(ps)

        # for dev and test data, we want to evaluate on all data, not just
        # first neg_samples data points
        ns = neg.size(0)

        if args.model == 'cnn':
            ps = ps.contiguous().view(ns, -1,
                                      len(args.kernel_sizes) * args.kernel_num)
        elif args.model == 'lstm':
            ps = ps.contiguous().view(ns, -1, args.hidden_size)

        # if needed, run computation on body
        if args.use_body:
            bodies = batch['body']
            bodies, pos, neg = get_sample(data.idx_to_cand,
                                          lambda x: data.idx_to_vec[x][1], ids,
                                          bodies)

            q_b = autograd.Variable(bodies)
            ps_b = autograd.Variable(neg)

            if 'use_mmd' not in args.__dict__ or not args.use_mmd:
                q_b = model(q_b)
                ps_b = ps_b.contiguous().view(-1, args.max_body)
                ps_b = model(ps_b)
            else:
                q_b, _ = model(q_b)
                ps_b = ps_b.contiguous().view(-1, args.max_body)
                ps_b, _ = model(ps_b)

            if args.model == 'cnn':
                ps = ps.contiguous().view(
                    args.neg_samples + 1, -1,
                    len(args.kernel_sizes) * args.kernel_num)
            elif args.model == 'lstm':
                ps = ps.contiguous().view(args.neg_samples + 1, -1,
                                          args.hidden_size)

            q = (q + q_b) / 2.0
            ps = (ps + ps_b) / 2.0

        # get cosine similarities
        qs = q.repeat(ns, 1, 1)
        cos2 = nn.CosineSimilarity(dim=2)
        s_s = cos2(qs, ps)

    map, mrr, p1, p5, auc = score(s_s, pos)
    print(
        'Similarity Task:\nMAP: {}\nMRR: {}\nP@1: {}\nP@5: {}\nAUC(0.05): {}\n'
        .format(map, mrr, p1, p5, auc))