def test(**kwargs):
    opt.parse(kwargs)
    if opt.dataset_name == 'iamgenet':
        test_loader = DataLoader(get_loader(opt.dataset_name,
                                            opt.data_path + 'val/'),
                                 batch_size=opt.test_batch_size,
                                 shuffle=True)
    else:
        test_loader = DataLoader(get_loader(opt.dataset_name,
                                            opt.data_path,
                                            train=False),
                                 batch_size=opt.test_batch_size,
                                 shuffle=True)
    model = get_model(opt.model_name,
                      in_channels=opt.in_channels,
                      img_rows=opt.img_rows,
                      num_classes=opt.num_classes)
    model.cuda(0)
    model.eval()
    model.load(opt.model_save_path)
    criterion = nn.CrossEntropyLoss().cuda()
    optimizer = optim.SGD(model.parameters(), lr=opt.lr, momentum=opt.momentum)
    test_loss = 0
    correct = 0
    for data, target in test_loader:
        data, target = Variable(data.cuda(0)), Variable(target.cuda(0))
        output = model(data)
        test_loss += criterion(output, target).data[0]
        pred = output.data.max(1, keepdim=True)[1]
        correct += pred.eq(target.data.view_as(pred)).cpu().sum()
    test_loss /= len(test_loader.dataset)
    print('Test set: Average loss:{:.4f}, Accuracy: {}/{} ({:.0f}%)'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))
Exemple #2
0
def main(config):
    # For fast training
    cudnn.benchmark = True

    # Create directories if not exist.
    if not os.path.exists(config.log_dir):
        os.makedirs(config.log_dir)
    if not os.path.exists(config.model_save_dir):
        os.makedirs(config.model_save_dir)
    if not os.path.exists(config.extractor_save_dir):
        os.makedirs(config.extractor_save_dir)

    # Data loader
    train_loader = get_loader(config.dataset,
                              config.data_dir,
                              batch_size=config.batch_size,
                              mode='train',
                              shuffle=True,
                              num_workers=config.num_workers)
    val_loader = get_loader(config.dataset,
                            config.data_dir,
                            batch_size=config.batch_size,
                            mode='val',
                            shuffle=False,
                            num_workers=config.num_workers)

    # Training model
    Solver = solver_selector[config.mode]
    model = get_model(config.dataset)
    solver = Solver(train_loader, val_loader, config)
    solver.train(model, config.attack_method)
Exemple #3
0
def train(cfg):
    # Setup seeds
    torch.manual_seed(cfg.get("seed", 1337))
    torch.cuda.manual_seed(cfg.get("seed", 1337))
    np.random.seed(cfg.get("seed", 1337))
    random.seed(cfg.get("seed", 1337))

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

    # Setup Dataloader
    loader_src = get_loader(cfg, "train", pathname="path_source")
    loader_tgt = get_loader(cfg, "train", pathname="path_target")

    # Setup model
    aan_src = AAN()
    aan_tgt = AAN()
    aan_transfer = AAN()

    # Train
    ws = [1,2,4]
    wt = [4, 2]
    alpha = 1e-4
    for img_src, _ in loader_src:
        # generate input noise image
        img_src = img_src[:,:3,:,:]
        noise = torch.randn_like(img_src)
        out_noise = aan_transfer(noise)
        out_src = aan_src(img_src)
        content_features = []
        noise_features = []
        for i in range(5,8):
            content_features.append(aan_src.outputsF[i].output)
            noise_features.append(aan_transfer.outputsF[i].output)
        loss_content = dist.content_loss(content_features, noise_features, weights=ws)
        conv1_sum = 0 # gram matrices sum
        conv2_sum = 0
        for img_tgt, _ in loader_tgt:
            img_tgt = img_tgt[:,:3,:,:]
            out_tgt = aan_tgt(img_tgt)
            conv1 = dist.GramMatrix(aan_src.outputsF[3].output)
            conv2 = dist.GramMatrix(aan_src.outputsF[4].output)
            conv1_sum = conv1_sum + conv1
            conv2_sum = conv2_sum + conv2

        con1_mean = conv1_sum / len(loader_tgt)
        con2_mean = conv2_sum / len(loader_tgt)
        loss_style = dist.style_loss(noise_features, [con1_mean, con2_mean], weights=wt)
        
        loss = loss_content + alpha * loss_style
        loss.backward()
        noise = aan_transfer.outputsB.input[0]
Exemple #4
0
def evaluate(config):
    # Data loader
    val_loader = get_loader(config.dataset,
                            config.data_dir,
                            batch_size=np.inf,
                            mode='val',
                            shuffle=False,
                            num_workers=config.num_workers)
    test_loader = get_loader(config.dataset,
                             config.data_dir,
                             batch_size=np.inf,
                             mode='test',
                             shuffle=False,
                             num_workers=config.num_workers)
    # Evaluating model
    model = get_model(config.dataset, model_save_dir=config.model_save_dir)
    evaluator = Evaluator(model, config.dataset)

    val_acc = {}
    print('Val set:')
    val_acc['cln'] = evaluator.evaluate(val_loader)
    print('Clean:', val_acc['cln'])
    val_acc['fgsm'] = evaluator.evaluate_fgsm(val_loader)
    print('FGSM:', val_acc['fgsm'])
    val_acc['pgd'] = evaluator.evaluate_pgd(val_loader, num_iter=50)
    print('PGD:', val_acc['pgd'])

    test_acc = {}
    print('Test set:')
    test_acc['cln'] = evaluator.evaluate(test_loader)
    print('Clean:', test_acc['cln'])
    test_acc['fgsm'] = evaluator.evaluate_fgsm(test_loader)
    print('FGSM:', test_acc['fgsm'])
    test_acc['pgd'] = evaluator.evaluate_pgd(test_loader, num_iter=50)
    print('PGD:', test_acc['pgd'])
    test_acc['cw'] = evaluator.evaluate_cw(test_loader)
    print('CW:', test_acc['cw'])

    test_acc['loss_sensitivity'] = evaluator.evaluate_robust(test_loader)
    print('loss_sensitivity:', test_acc['loss_sensitivity'])

    for i in [
            5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150
    ]:
        acc_pgd = evaluator.evaluate_pgd(test_loader, num_iter=i)
        print('PGD_{}: {}'.format(i, acc_pgd))

    return val_acc, test_acc
def test():
    import argparse
    parser = argparse.ArgumentParser()
    config = parser.parse_args()
    config.temperature = 100
    config.knn = 16
    config.extractor = 'AE'
    config.dataset = 'MNIST'
    config.extractor_save_dir = './results/extractor'
    config.data_dir = '../data'
    if not os.path.exists(config.extractor_save_dir):
        os.makedirs(config.extractor_save_dir)
    graph = GraphConstructor(config)

    indexes = graph.get_knn_index(1)
    print(indexes)

    from loader import get_loader
    import numpy as np
    data_loader = get_loader(config.dataset,
                             config.data_dir,
                             batch_size=128,
                             mode='train',
                             shuffle=False)
    data_iter = iter(data_loader)
    idx, (x, y) = next(data_iter)
    intrinsic, penalty = graph.get_similarity(idx, y)
    print(intrinsic)
    print(penalty)
Exemple #6
0
def test(args):

    data_loader = get_loader(args.dataset)
    data_path = get_data_path(args.dataset)
    loader = data_loader(data_path,
                         is_transform=True,
                         split="test",
                         img_size=(240, 320))
    testloader = data.DataLoader(loader)

    # Setup Model
    model = Model_2b_depgd_GAP_MS(ResidualBlock, UpProj_Block, 1, num_class)
    from collections import OrderedDict
    state_dict = torch.load(args.model_path,
                            map_location=lambda storage, location: storage)
    new_state_dict = OrderedDict()
    for k, v in state_dict.items():
        name = k[7:]  # remove `module.`
        new_state_dict[name] = v
    model.load_state_dict(new_state_dict)

    model.eval()
    if torch.cuda.is_available():
        model.cuda()

    Aiou, Am_acc, acc_lbl, class_Aiou = accuracy(model, testloader)
    print("PixAcc, mAcc, and mIoU are: %f, %f, %f" %
          (acc_lbl, Am_acc, np.sum(class_Aiou[1:]) / float(num_class - 1)))
    print("class Aiou:", class_Aiou[1:])
Exemple #7
0
def main():
    parser = argparse.ArgumentParser(description=APP.description)
    parser.add_argument('action', choices=['apply', 'add'], default='apply',
                        nargs='?',
                        help="Action to be performed")
    parser.add_argument('--template-path', dest="path", required=True,
                        help='Path to template to be applied.')

    parser.add_argument('-o', '--output',
                        help='Where the output should be put.')

    parser.add_argument('--verbose', action="store_true", default=False,
                        help='Verbose mode.')

    args = parser.parse_args()

    logging_setup(args.verbose)

    if args.action == 'apply':
        loader = get_loader(args.path)
        runner = Runner(loader)
        runner.run(args.output)
    elif args.action == 'add':
        fm = downloader.FileManager()
        fm.save(args.path)
Exemple #8
0
def main():
    parser = argparse.ArgumentParser(description=APP.description)
    parser.add_argument('action',
                        choices=['apply', 'add'],
                        default='apply',
                        nargs='?',
                        help="Action to be performed")
    parser.add_argument('--template-path',
                        dest="path",
                        required=True,
                        help='Path to template to be applied.')

    parser.add_argument('-o',
                        '--output',
                        help='Where the output should be put.')

    parser.add_argument('--verbose',
                        action="store_true",
                        default=False,
                        help='Verbose mode.')

    args = parser.parse_args()

    logging_setup(args.verbose)

    if args.action == 'apply':
        loader = get_loader(args.path)
        runner = Runner(loader)
        runner.run(args.output)
    elif args.action == 'add':
        fm = downloader.FileManager()
        fm.save(args.path)
Exemple #9
0
def eval(cfg):
    # Setup seeds
    torch.manual_seed(cfg.get("seed", 1337))
    torch.cuda.manual_seed(cfg.get("seed", 1337))
    np.random.seed(cfg.get("seed", 1337))
    random.seed(cfg.get("seed", 1337))

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

    # Setup evaluation data
    loader_source = get_loader(cfg, "train")
    # data_eval_labels = utils.recursive_glob(os.path.join(cfg["data"]["path"], 'labels'))

    # Setup model
    model = Unet(cfg).to(device)
    checkpoint = torch.load(cfg["training"]["checkpoint"])
    model.load_state_dict(checkpoint["model_state"])
    stats = None
    model.eval()
    for images, labels in tqdm.tqdm(loader_source):
        model.set_input(images, labels)
        model.forward()
        if stats is None:
            stats = [StatsRecorder() for i in range(len(model.hooks))]
        for i, hook in enumerate(model.hooks):
            activation = hook.output
            b, c, h, w = activation.shape
            activation = activation.transpose(0,
                                              1).reshape(c,
                                                         -1).transpose(0, 1)
            stats[i].update(activation.cpu().data.numpy())

    print([s.mean for s in stats])
    print([s.std for s in stats])
Exemple #10
0
def attack(model, dataroot, device):
    ################
    # Data Loader
    ################
    test_loader = get_loader('letters',
                             32,
                             batch_size=1,
                             dataroot=dataroot,
                             train=False)

    #fgsm = FGSM(predict=model, eps=0.3)
    #bim = LinfBasicIterativeAttack(predict=model)
    #pgd = PGDAttack(predict=model, eps=0.8)

    total, correct = 0, 0
    for i, (image, label) in enumerate(test_loader, 1):
        image, label = image.to(device), label.to(device)
        #pert = fgsm.perturb(image)

        #out = model(pert)

        torchvision.utils.save_image(image.data,
                                     './results/letters/{}.png'.format(i))
        #torchvision.utils.save_image(pert.data, './results/mnist/fgsm/adv/{}.png'.format(i))

        #_, predicted = torch.max(out.data, 1)
        #correct += (predicted == label - 1).sum().item()
        #total += label.size(0)
        #if i == 100:
        #    break

    print('=======> Test Accuracy: {}'.format(correct / total))
def evaluation(net, face):
    dataloader = get_loader(batch_size=128).dataloader
    features_total = torch.Tensor(
        np.zeros((args.num_faces, 512), dtype=np.float32)).to(args.device)
    labels = torch.Tensor(np.zeros((args.num_faces, 1),
                                   dtype=np.float32)).to(args.device)
    with torch.no_grad():
        bs_total = 0
        for index, (img, targets) in enumerate(dataloader):
            bs = len(targets)
            img = img.to(args.device)
            features = net(img)
            features_total[bs_total:bs_total + bs] = features
            labels[bs_total:bs_total + bs] = targets
            bs_total += bs
        assert bs_total == args.num_faces, print(
            'Database should have {} faces!'.format(args.num_faces))

    input_feature = net(
        face_ToTensor(face).to(args.device).view([1, 3, 112, 96]))

    input_feature = input_feature.cpu().detach().numpy()
    features_total = features_total.cpu().detach().numpy()
    scores = 1 - cdist(input_feature, features_total, 'cosine')

    return get_result(scores)
Exemple #12
0
    def run(self):
        if os.path.exists(self.default_path):
            print('Using trained extractor checkpoints: {}'.format(
                self.default_path))
        else:
            print('Training feature extractor...')
            self.train_extractor()

        if os.path.exists(self.feature_path):
            print('Using computed features: {}'.format(self.feature_path))
        else:
            self.data_loader = get_loader(self.dataset,
                                          self.data_dir,
                                          batch_size=256,
                                          mode='train',
                                          shuffle=False)
            self.model = get_extractor_model(
                self.dataset,
                self.method,
                extractor_save_dir=self.extractor_save_dir)
            features = []
            for idx, (X, y) in self.data_loader:
                x = X.to(self.device)
                z = self.model(x, only_logits=False)[1]
                features.append(z.detach())
            tensor = torch.cat(tuple(features), dim=0)
            torch.save(tensor, self.feature_path)
            print('Saved features into {}...'.format(self.feature_path))
Exemple #13
0
def main(args):
    cudnn.benchmark = True

    if not os.path.exists(args.save_path):
        os.makedirs(args.save_path)
        print('Create path : {}'.format(args.save_path))

    if args.result_fig:
        fig_path = os.path.join(args.save_path, 'fig')
        if not os.path.exists(fig_path):
            os.makedirs(fig_path)
            print('Create path : {}'.format(fig_path))

    data_loader = get_loader(
        mode=args.mode,
        load_mode=args.load_mode,
        saved_path=args.saved_path,
        test_patient=args.test_patient,
        patch_n=(args.patch_n if args.mode == 'train' else None),
        patch_size=(args.patch_size if args.mode == 'train' else None),
        transform=args.transform,
        batch_size=(args.batch_size if args.mode == 'train' else 1),
        num_workers=args.num_workers)

    solver = Solver(args, data_loader)
    if args.mode == 'train':
        solver.train()
    elif args.mode == 'test':
        solver.test()
def main(config):
    cudnn.benchmark = True
    if config.model_type not in ['UNet', 'R2U_Net', 'AttU_Net', 'R2AttU_Net', 'Iternet', 'AttUIternet', 'R2UIternet',
                                 'NestedUNet']:
        print('ERROR!! model_type should be selected in U_Net/R2U_Net/AttU_Net/R2AttU_Net/Iternet/AttUIternet/R2UIternet/NestedUNet')
        print('Your input for model_type was %s' % config.model_type)
        return

    # Create directories if not exist
    if not os.path.exists(config.model_path):
        os.makedirs(config.model_path)
    if not os.path.exists(config.result_path):
        os.makedirs(config.result_path)
    config.result_path = os.path.join(config.result_path, config.model_type)
    if not os.path.exists(config.result_path):
        os.makedirs(config.result_path)

    print(config)

    train_loader = get_loader(image_path=config.train_path,
                              image_size=config.image_size,
                              batch_size=config.batch_size,
                              num_workers=config.num_workers,
                              mode='train',
                              augmentation_prob=config.augmentation_prob)
    valid_loader = get_loader(image_path=config.valid_path,
                              image_size=config.image_size,
                              batch_size=config.batch_size,
                              num_workers=config.num_workers,
                              mode='valid',
                              augmentation_prob=0.)
    test_loader = get_loader(image_path=config.test_path,
                             image_size=config.image_size,
                             batch_size=config.batch_size,
                             num_workers=config.num_workers,
                             mode='test',
                             augmentation_prob=0.)

    solver = Solver(config, train_loader, valid_loader, test_loader)

    start = time.time()
    history, results = solver.train()
    stop = time.time()
    training_time = stop - start
    print(history)
    print(results)
    print("Training time: {}".format(training_time))
Exemple #15
0
    def predict(self,
                data,
                targets,
                idx=None,
                batch_size=64,
                cuda=False,
                minmax=(0, 255)):
        """
        Infers from the given data

        :param data: A `numpy.ndarray` of data
        :param tragets: A `numpy.ndarray` of target data
        :param idx: A `list` of index to use for prediction
        :param batch_size: An `int` of the batch size 
        :param cuda: A `bool` wheter to load the model on the GPU
        :param minmax: A `tuple` of normalization 
        
        :returns : A `torch.tensor` of the images
        :returns : A `torch.tensor` of the targets
        :returns : A `torch.tensor` of the predictions
        :returns : A `torch.tensor` of the indices 
        """
        if isinstance(idx, type(None)):
            idx = numpy.arange(len(data))
        predict_loader = loader.get_loader(data[idx],
                                           targets[idx],
                                           batch_size=batch_size,
                                           validation=True,
                                           minmax=minmax)

        self.eval()
        for X, y, index in tqdm(predict_loader, leave=False,
                                desc="Prediction"):

            # Verifies the shape of the data
            if X.ndim == 3:
                X = X.unsqueeze(1)

            # Send on GPU
            if cuda:
                X = X.cuda()

            # Prediction and loss computation
            pred = self.forward(X)

            if cuda:
                X = X.cpu().data.numpy()
                pred = pred.cpu().data.numpy()
            else:
                X = X.data.numpy()
                pred = pred.data.numpy()
            y = y.data.numpy()

            yield X, y, pred, index

            # To avoid memory leak
            del X, y, pred
Exemple #16
0
 def process_tracker(self,tracker):
     Filter = get_filter(tracker.filter)
     Loader = get_loader(tracker.loader)
     loader = Loader(tracker,Filter(self.interest_list,tracker.publisher))
     media_list = loader.fetch()
     for media in media_list:
         if not media in self.submitted_list and \
            not media in self.wait_list:
             media.fetch()
             self.wait_list.append(media)
Exemple #17
0
    def __init__(self, root_dir, work_dir):
        self.root_dir = root_dir
        self.work_dir = work_dir
        self.loader = loader.get_loader(self.root_dir)
        self.loader.load_root_file()
        self.loader.set_work_dir(self.work_dir)
        logging.debug('init done')

        self.sorted_targets = []  # targets in order decreased by dependencies
        self.rules = []
Exemple #18
0
    def load(self, name, version=None):
        version = version or max(self.get_list_of_versions(name))
        path = os.path.join(self.REPO_PATH, name, version)
        zipfile = os.path.join(path, 'package.zip')

        if not os.path.exists(path):
            raise VersionNotFoundError()
        if not os.path.exists(zipfile):
            raise PackageNotFoundError()
        return loader.get_loader(zipfile)
def train(**kwargs):
    opt = Config()
    for k, v in kwargs.items(): #设置参数
        setattr(opt, k, v)   
    if(opt.use_gpu):
        torch.cuda.empty_cache()#清空缓存
    # 数据
    dataloader,datas = get_loader(opt) 
    datas = dataloader.dataset.datas
    word2ix = datas['word2ix']
    sos = word2ix.get(datas.get('sos'))
    voc_length = len(word2ix)
    #定义模型
    encoder = EncoderRNN(opt, voc_length)
    decoder = AttentionDecoderRNN(opt, voc_length)
    #加载断点,从上次结束地方开始
    if opt.model_ckpt:
        checkpoint = torch.load(opt.model_ckpt)
        encoder.load_state_dict(checkpoint['en'])
        decoder.load_state_dict(checkpoint['de']) 
    #切换模式
    encoder = encoder.to(opt.device)
    decoder = decoder.to(opt.device)
    encoder.train()
    decoder.train()
    #定义优化器(注意与encoder.to(device)前后不要反)
    encoder_optimizer = torch.optim.Adam(encoder.parameters(), lr=opt.learning_rate)
    decoder_optimizer = torch.optim.Adam(decoder.parameters(), lr=opt.learning_rate * opt.decoder_learning_ratio)
    if opt.model_ckpt:
        encoder_optimizer.load_state_dict(checkpoint['en_opt'])
        decoder_optimizer.load_state_dict(checkpoint['de_opt']) 
    #定义打印loss的变量
    print_loss = 0   
    for epoch in range(opt.epoch):
        for i, data in enumerate(dataloader):
            #取一个batch训练
            loss = train_by_batch(sos, opt, data, encoder_optimizer, decoder_optimizer, encoder, decoder)
            print_loss += loss
            #打印损失   
            if i % opt.print_every == 0:
                print_loss_avg = print_loss / opt.print_every
                print("Epoch: {}; Epoch Percent complete: {:.4f}%; Average loss: {:.8f}"
                .format(epoch, epoch / opt.epoch * 100, print_loss_avg))
                print_loss = 0                
        # 保存checkpoint
        if epoch % opt.save_every == 0:
            checkpoint_path = '{prefix}_{time}'.format(prefix=opt.prefix,time=time.strftime('%m%d_%H%M'))
            torch.save({
                'en': encoder.state_dict(),
                'de': decoder.state_dict(),
                'en_opt': encoder_optimizer.state_dict(),
                'de_opt': decoder_optimizer.state_dict(),
            }, checkpoint_path)
Exemple #20
0
def main(config):
    # For fast training
    cudnn.benchmark = True

    # Create directories if not exist.
    if not os.path.exists(config.log_dir):
        os.makedirs(config.log_dir)
    if not os.path.exists(config.model_save_dir):
        os.makedirs(config.model_save_dir)
    if not os.path.exists(config.extractor_save_dir):
        os.makedirs(config.extractor_save_dir)

    # Feature extractor
    extractor = FeatureExtractor(config)
    extractor.run()

    # Graph constructor
    graph = GraphConstructor(config)

    # Data loader
    train_loader = get_loader(config.dataset,
                              config.data_dir,
                              batch_size=config.batch_size,
                              mode='train',
                              shuffle=True,
                              num_workers=config.num_workers,
                              graph=graph,
                              anchor_size=config.anchor_size)
    val_loader = get_loader(config.dataset,
                            config.data_dir,
                            batch_size=config.batch_size,
                            mode='val',
                            shuffle=False,
                            num_workers=config.num_workers)

    # Training model
    Solver = Solver_ATMR
    model = get_model(config.dataset)
    solver = Solver(train_loader, val_loader, graph, config)
    solver.train(model, config.attack_method)
Exemple #21
0
    def save(self, source):
        zloader = loader.get_loader(source)
        zloader.validate()
        path = os.path.join(self.REPO_PATH, zloader.name, zloader.version_str)
        zipfile = os.path.join(path, 'package.zip')

        if not os.path.exists(path):
            os.makedirs(path)
        LOGGER.debug('Storing in %s', path)
        if os.path.isfile(source):
            shutil.copy2(source, path)
        elif os.path.isdir(source):
            shutil.copytree(source, path)
def run(net, feature_dim, device, N =1, step=None):
    net.eval()

    dataloader = get_loader('LFWDataset_gz_lr', batch_size=256, N=N).dataloader
    # 6000 Image Pairs ( 3000 genuine and 3000 impostor matches)
    features11_total = torch.Tensor(np.zeros((6000, feature_dim), dtype=np.float32)).to(device)
    features12_total, features21_total, features22_total = torch.zeros_like(features11_total), \
                                                           torch.zeros_like(features11_total), \
                                                           torch.zeros_like(features11_total)
    labels = torch.Tensor(np.zeros((6000, 1), dtype=np.float32)).to(device)
    with torch.no_grad():
        bs_total = 0
        for index, (img1, img2, img1_flip, img2_flip, targets) in enumerate(dataloader):
            bs = len(targets)
            img1, img1_flip = img1.to(device), img1_flip.to(device)
            img2, img2_flip = img2.to(device), img2_flip.to(device)
            features11 = net(img1)
            features12 = net(img1_flip)
            features21 = net(img2)
            features22 = net(img2_flip)
            features11_total[bs_total:bs_total + bs] = features11
            features12_total[bs_total:bs_total + bs] = features12
            features21_total[bs_total:bs_total + bs] = features21
            features22_total[bs_total:bs_total + bs] = features22
            labels[bs_total:bs_total + bs] = targets
            bs_total += bs
        assert bs_total == 6000, print('LFW pairs should be 6,000!')
    labels = labels.cpu().numpy()
    for cal_type in ['concat']:  # cal_type: concat/sum/normal
        scores = tensor_pair_cosine_distance(features11_total, features12_total, features21_total, features22_total, type=cal_type)
        fpr, tpr, _ = roc_curve(labels, scores) # false positive rate / true positive rate
        roc_auc = auc(fpr, tpr)
        accuracy = []
        thd = []
        folds = KFold(n=6000, n_folds=10, shuffle=False) # 1 for test, 9 for train
        thresholds = np.linspace(-10000, 10000, 10000 + 1)
        thresholds = thresholds / 10000
        predicts = np.hstack((scores, labels))
        for idx, (train, test) in enumerate(folds):
            best_thresh = find_best_threshold(thresholds, predicts[train]) # find the best threshold to say if these two persons are same or not
            accuracy.append(eval_acc(best_thresh, predicts[test]))
            thd.append(best_thresh)
        mean_acc, std = np.mean(accuracy), np.std(accuracy)  # have 10 accuracies and then find the mean and std
        if step is not None:
            message = 'DF={} LFWACC={:.4f} std={:.4f} auc={:.4f} type:{} at {}iter.'.format(N, mean_acc, std, roc_auc, cal_type, step)
            logging.info(message)
        else:
            message = 'DF={} LFWACC={:.4f} std={:.4f} auc={:.4f} type:{} at testing.'.format(N, mean_acc, std, roc_auc, cal_type)
            logging.info(message)
        print(message)
Exemple #23
0
def train(**kwargs):
    opt.parse(kwargs)
    if opt.dataset_name == 'imagenet':
        train_loader = DataLoader(get_loader(opt.dataset_name,
                                             opt.data_path + 'train/'),
                                  batch_size=opt.batch_size,
                                  shuffle=True)
    else:
        train_loader = DataLoader(get_loader(opt.dataset_name,
                                             opt.data_path,
                                             train=True),
                                  batch_size=opt.batch_size,
                                  shuffle=True)

    model = get_model(opt.model_name,
                      in_channels=opt.in_channels,
                      img_rows=opt.img_rows,
                      num_classes=opt.num_classes)
    model.cuda(0)
    #model.load(opt.model_save_path)
    model.train()
    criterion = nn.CrossEntropyLoss().cuda()
    optimizer = optim.SGD(model.parameters(), lr=opt.lr, momentum=opt.momentum)

    for epoch in range(1, opt.max_epochs + 1):
        for batch_idx, (data, target) in enumerate(train_loader):
            data, target = Variable(data.cuda(0)), Variable(target.cuda(0))
            optimizer.zero_grad()
            output = model(data)
            loss = criterion(output, target)
            loss.backward()
            optimizer.step()
        print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
            epoch, batch_idx * len(data), len(train_loader.dataset),
            100. * batch_idx / len(train_loader), loss.data[0]))
        if epoch % opt.save_freq == 0:
            model.save()
Exemple #24
0
def boxplotvis(cfg):

    # device = torch.device("cuda:{}".format(cfg["other"]["gpu_idx"]) if torch.cuda.is_available() else "cpu")
    data_loader = get_loader(cfg["data"]["dataset"])
    data_path = cfg["data"]["path"]
    v_loader = data_loader(data_path, split='val')

    n_classes = v_loader.n_classes
    n_val = len(v_loader.files['val'])

    # test differnet models' prediction
    vgg16lstm_metric = runningScore(n_classes, n_val)
    vgg16gru_metric = runningScore(n_classes, n_val)
    segnet_metric = runningScore(n_classes, n_val)

    with torch.no_grad():
        for i_val, (images_val, labels_val,
                    img_name_val) in tqdm(enumerate(v_loader)):
            gt = np.squeeze(labels_val.data.cpu().numpy())
            vgg16lstm_pred = m.imread(
                pjoin(cfg["data"]["pred_path"], 'vgg16_lstm_brainweb',
                      img_name_val + '.bmp'))
            vgg16gru_pred = m.imread(
                pjoin(cfg["data"]["pred_path"], 'vgg16_gru_brainweb',
                      img_name_val + '.bmp'))
            segnet_pred = m.imread(
                pjoin(cfg["data"]["pred_path"], 'segnet_brainweb',
                      img_name_val + '.bmp'))

            vgg16lstm_encode = v_loader.encode_segmap(vgg16lstm_pred)
            vgg16gru_encode = v_loader.encode_segmap(vgg16gru_pred)
            segnet_encode = v_loader.encode_segmap(segnet_pred)

            vgg16lstm_metric.update(gt, vgg16lstm_encode, i_val)
            vgg16gru_metric.update(gt, vgg16gru_encode, i_val)
            segnet_metric.update(gt, segnet_encode, i_val)

    vgg16lstm_acc_all, vgg16lstm_dsc_cls = vgg16lstm_metric.get_list()
    vgg16gru_acc_all, vgg16gru_dsc_cls = vgg16gru_metric.get_list()
    segnet_acc_all, segnet_dsc_cls = segnet_metric.get_list()
    # dsc_list = [vgg16lstm_dsc_cls.transpose(), vgg16gru_dsc_cls.transpose(), segnet_dsc_cls.transpose()]

    data0 = array2dataframe(vgg16lstm_dsc_cls)
    data0['Method'] = 'VGG16-LSTM'
    data1 = array2dataframe(vgg16gru_dsc_cls)
    data1['Method'] = 'VGG16-GRU'
    data2 = array2dataframe(segnet_dsc_cls)
    data2['Method'] = 'SegNet'
    data = pd.concat([data0, data1, data2])
def recover_pack():
    train_loader, test_loader = get_loader()

    pack = dotdict({
        'net': get_model(),
        'train_loader': train_loader,
        'test_loader': test_loader,
        'trainer': get_trainer(),
        'criterion': get_criterion(),
        'optimizer': None,
        'lr_scheduler': None
    })

    adjust_learning_rate(cfg.base.epoch, pack)
    return pack
Exemple #26
0
def train():
    transform = transforms.Compose([
        transforms.Resize((356, 356)),
        transforms.RandomCrop((299, 299)),
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])

    train_loader = get_loader(
        root_folder= "flickt8k/images",
        annotation_file = "flickt8k/captions.txt",
        transform= transform
    )

    torch.backends.cudnn.benchmark = True 
    device = torch.device("cuda" if torch.cuda.is_available()  else "cpu")
    # hyper 
    embed_size = 256  
    hidden_size= 256 
    vocab_size = len(dataset.vocab)
    num_layers = 1 
    learning_rate = 3e-4 
    num_epochs=100 

    model = CNNtoRNN(embed_size, hidden_size, vocab_size, num_layers).to(device)
    criterion = nn.CrossEntropyLoss(ignore_index=dataset.voca.stoi["<PAD>"])
    optimizer = optim.Adam(model.parameters(), lr=learning_rate)

    model.train() 
    for epoch in range(num_epochs):
        if save_model:
            checkpoint = {
                "state_dict": model.state_dict(), 
                "optimzier": optimizer.state_dict(),
                "step": step
            }

        for idx, (imgs, captions ) in enumerate(train_loader):
            imgs = imgs.to(device)
            captions = captions.to(device)

            outputs = model(imgs, captions[:-1]) # not sending end tpoken
            loss = criterion(outputs.reshape(-1, output.shape[2]), captions.reshape(-1, output.shape[2]))
            optimizer.zero_grad()
            loss.backward(loss)
            optimizer.step()
Exemple #27
0
 def train_extractor(self, batch_size=128):
     self.data_loader = get_loader(self.dataset,
                                   self.data_dir,
                                   batch_size=batch_size,
                                   mode='train',
                                   shuffle=True)
     self.model = get_extractor_model(dataset=self.dataset,
                                      method=self.method)
     if self.method == 'NT':
         self._train_nt()
     elif self.method == 'AT':
         self._train_at()
     elif self.method == 'AE':
         self._train_ae()
     torch.save(self.model.state_dict(), self.default_path)
     print('Saved extractor checkpoints into {}...'.format(
         self.default_path))
Exemple #28
0
def main():
    torch.random.manual_seed(0)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

    parser = argparse.ArgumentParser()
    parser.add_argument('--root', type=str, default='data')
    parser.add_argument('--batch-size', type=int, default=128)
    parser.add_argument('--lr', type=float, default=2e-4)
    parser.add_argument('--workers', type=int, default=0)
    parser.add_argument('--epochs', type=int, default=20)
    config = parser.parse_args()
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    # networks
    net_g = Generator().to(device)
    net_d = Discriminator().to(device)

    print(net_g)
    print(net_d)

    # optimizer
    optimizer_g = optim.Adam(net_g.parameters(),
                             lr=config.lr,
                             betas=(0.5, 0.999))
    optimizer_d = optim.Adam(net_d.parameters(),
                             lr=config.lr,
                             betas=(0.5, 0.999))

    print(optimizer_d)
    print(optimizer_g)

    # data loader
    dataloader = get_loader(config.root, config.batch_size, config.workers)

    trainer = Trainer(net_g, net_d, optimizer_g, optimizer_d, dataloader,
                      device)
    plotter = PlotHelper('samples/loss.html')
    for epoch in range(config.epochs):
        loss_g, loss_d = trainer.train()

        print('Train epoch: {}/{},'.format(epoch + 1, config.epochs),
              'loss g: {:.6f}, loss d: {:.6f}.'.format(loss_g, loss_d))

        trainer.save_sample('samples/sample_{:02d}.jpg'.format(epoch + 1))
        plotter.append(loss_g, loss_d, epoch + 1)
Exemple #29
0
def validate(args):

    # Setup Dataloader
    data_loader = get_loader(args.dataset)
    data_path = get_data_path(args.dataset)
    loader = data_loader(data_path,
                         split=args.split,
                         is_transform=True,
                         label_scale=1)
    n_classes = loader.n_classes
    valloader = data.DataLoader(loader, batch_size=args.batch_size)

    # Setup Model
    model = Enet(n_classes)
    model.load_state_dict(torch.load(args.model_path))
    model.eval()

    if torch.cuda.is_available():
        model.cuda(0)

    gts, preds = [], []
    t1 = time.time()
    for i, (images, labels) in enumerate(valloader):
        if torch.cuda.is_available():
            images = Variable(images.cuda(0))
            labels = Variable(labels.cuda(0))
        else:
            images = Variable(images)
            labels = Variable(labels)
        outputs = model(images)
        pred = outputs.data.max(1)[1].cpu().numpy()
        gt = labels.data.cpu().numpy()

        for gt_, pred_ in zip(gt, pred):
            gts.append(gt_)
            preds.append(pred_)
    t2 = time.time()
    print(t2 - t1)
    score, class_iou = scores(gts, preds, n_class=n_classes)

    for k, v in score.items():
        print k, v

    for i in range(n_classes):
        print i, class_iou[i]
def eval(**kwargs):
    opt = Config()
    for k, v in kwargs.items(): #设置参数
        setattr(opt, k, v)   
    # 数据
    dataloader,datas = get_loader(opt) 
    word2ix,ix2word = datas.word2ix, datas.ix2word
    sos = datas.sos
    eos = datas.eos
    unknown = datas.unknown
    voc_length = len(word2ix)
    #定义模型
    encoder = EncoderRNN(opt, voc_length)
    decoder = AttentionDecoderRNN(opt, voc_length)
    #加载模型
    if opt.model_ckpt == None:
        raise ValueError('model_ckpt is None.')
    #Load all tensors onto the CPU, using a function
    checkpoint = torch.load(opt.model_ckpt, map_location=lambda storage, location: storage)
    encoder.load_state_dict(checkpoint['en'])
    decoder.load_state_dict(checkpoint['de'])
    with torch.no_grad():
        #切换模式
        encoder = encoder.to(opt.device)
        decoder = decoder.to(opt.device)
        encoder.eval()
        decoder.eval()
        #定义seracher
        searcher = GreedySearchDecoder(encoder, decoder)
        while(1):
            input_sentence = input('> ')
            if input_sentence == 'q' or input_sentence == 'quit': break
            cop = re.compile("[^\u4e00-\u9fa5^a-z^A-Z^0-9]") #分词处理正则
            input_seq = jieba.lcut(cop.sub("",input_sentence)) #分词序列
            input_seq = input_seq[:opt.max_input_length] + ['</EOS>']
            input_seq = [word2ix.get(word, unknown) for word in input_seq]
            tokens = generate(input_seq, searcher, sos, eos, opt)
            output_words = ''.join([ix2word[token.item()] for token in tokens]).replace("</EOS>","")
            print('BOT: ', output_words)
Exemple #31
0
def eval_model(data_transforms, model, PATH_TO_IMAGES, TRAIN_DATASET):
    """
    Evaluates model on the source dataset after training
    Gives predictions for test fold and calculates AUCs using previously trained model

    Args:
        data_transforms: torchvision transforms to preprocess raw images; same as validation transforms
        model: densenet-121 from torchvision previously fine tuned to training data
        PATH_TO_IMAGES: path at which test images can be found
        TRAIN_DATASET: the name of the dataset in which the model was trained (nih, chexpert or mimic)
    Returns:
        pred_df: dataframe containing individual predictions and ground truth for each test image
        auc_df: dataframe containing aggregate AUCs by train/test tuples
    """

    # calc preds in batches of 32, can reduce if your GPU has less RAM
    BATCH_SIZE = 32

    # set model to eval mode; required for proper predictions given use of batchnorm
    model.train(False)

    CXR = get_loader(TEST_DATASET)
    # create dataloader
    dataset = CXR.CXRDataset(
        path_to_images=PATH_TO_IMAGES,
        fold="test",
        transform=data_transforms['val'])
    dataloader = torch.utils.data.DataLoader(
        dataset, BATCH_SIZE, shuffle=False, num_workers=8)
    size = len(dataset)

    # create empty dfs
    pred_df = pd.DataFrame(columns=["Image Index"])
    true_df = pd.DataFrame(columns=["Image Index"])

    # iterate over dataloader
    for i, data in enumerate(dataloader):

        inputs, labels, _ = data
        inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())

        true_labels = labels.cpu().data.numpy()
        batch_size = true_labels.shape

        outputs = model(inputs)
        probs = outputs.cpu().data.numpy()

        # get predictions and true values for each item in batch
        for j in range(0, batch_size[0]):
            thisrow = {}
            truerow = {}
            thisrow["Image Index"] = dataset.df.index[BATCH_SIZE * i + j]
            truerow["Image Index"] = dataset.df.index[BATCH_SIZE * i + j]

            # iterate over each entry in prediction vector; each corresponds to
            # individual label
            for k in range(len(dataset.PRED_LABEL)):
                thisrow["prob_" + dataset.PRED_LABEL[k]] = probs[j, k]
                truerow[dataset.PRED_LABEL[k]] = true_labels[j, k]

            pred_df = pred_df.append(thisrow, ignore_index=True)
            true_df = true_df.append(truerow, ignore_index=True)

        if(i % 10 == 0):
            print(str(i * BATCH_SIZE))

    auc_df = pd.DataFrame(columns=["label", "auc"])

    # calc AUCs
    for column in true_df:

        if column not in get_labels(TRAIN_DATASET):
            continue

        actual = true_df[column]
        pred = pred_df["prob_" + column]
        thisrow = {}
        thisrow['label'] = column
        thisrow['auc'] = np.nan
        try:
            thisrow['auc'] = sklm.roc_auc_score(
                actual.as_matrix().astype(int), pred.as_matrix())
        except BaseException:
            print("can't calculate auc for " + str(column))
        auc_df = auc_df.append(thisrow, ignore_index=True)

    pred_df.to_csv(TRAIN_DATASET + "/results/preds_" +
                   TRAIN_DATASET + ".csv", index=False)
    auc_df.to_csv(TRAIN_DATASET + "/results/aucs_" +
                  TRAIN_DATASET + ".csv", index=False)
    return pred_df, auc_df
Exemple #32
0
def make_pred_multilabel(data_transforms, model, PATH_TO_IMAGES, TRAIN_DATASET, TEST_DATASET):
    """
    Gives predictions for test fold using previously trained model and any of the three test datasets

    Args:
        data_transforms: torchvision transforms to preprocess raw images; same as validation transforms
        model: densenet-121 from torchvision previously fine tuned to training data
        PATH_TO_IMAGES: path at which test images can be found
        TRAIN_DATASET: the name of the dataset in which the model was trained (nih, chexpert or mimic)
        TEST_DATASET: the name of the dataset to evaluate the model performance (nih, chexpert or mimic)
    Returns:
        pred_df: dataframe containing individual predictions and ground truth for each test image
    """

    # calc preds in batches of 32, can reduce if your GPU has less RAM
    BATCH_SIZE = 32

    # set model to eval mode; required for proper predictions given use of batchnorm
    model.train(False)

    CXR = get_loader(TEST_DATASET)

    # create dataloader
    dataset = CXR.CXRDataset(
        path_to_images=PATH_TO_IMAGES,
        fold="test",
        transform=data_transforms['val'])

    dataloader = torch.utils.data.DataLoader(
        dataset, BATCH_SIZE, shuffle=False, num_workers=8)
    size = len(dataset)

    # create empty dfs
    pred_df = pd.DataFrame(columns=["Image Index"])
    true_df = pd.DataFrame(columns=["Image Index"])

    TRAIN_LABELS = get_labels(TRAIN_DATASET)

    # iterate over dataloader
    for i, data in enumerate(dataloader):
        inputs, labels, _ = data
        inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())

        batch_size = labels.cpu().data.numpy().shape

        outputs = model(inputs)
        probs = outputs.cpu().data.numpy()

        # get predictions and true values for each item in batch
        for j in range(0, batch_size[0]):
            thisrow = {}
            thisrow["Image Index"] = dataset.df.index[BATCH_SIZE * i + j]

            # iterate over each entry in prediction vector; each corresponds to
            # individual label
            for k in range(len(TRAIN_LABELS)):
                thisrow["prob_" + TRAIN_LABELS[k]] = probs[j, k]

            pred_df = pred_df.append(thisrow, ignore_index=True)

        if(i % 10 == 0):
            print(str(i * BATCH_SIZE))

    pred_df.to_csv(TEST_DATASET + "/results/preds_" +
                   TRAIN_DATASET + ".csv", index=False)

    return pred_df