Ejemplo n.º 1
0
def main():
    """
    -------------------
    Input:
    Output:
    -------------------
    """
    root = "GE Gorakhpur"
    image_type = "blocks"

    prefix = common.get_s3_paths(root, image_type)
    suffix = ".npz"
    n = 3
    dev = 0

    files = utils.get_files(prefix, suffix)

    for f in files[0:n]:
        train = utils.get_train_dev_set(files=[f], n=1, dev=dev)
        train = utils.get_X_Y(train)

        hypers = [1]
        for c in hypers:
            log_reg = utils.fit_log_reg(
                np.transpose(np.array(train[4:-2])),
                np.transpose(np.array(train[-1].data)),
                c,
            )

    utils.save_model(log_reg, "log_reg_{}.sav".format(str(c)))
Ejemplo n.º 2
0
def main():
    """
    -------------------
    Input:
    Output:
    -------------------
    """
    root = "GE Gorakhpur"
    image_type = "blocks"

    prefix = common.get_s3_paths(root, image_type)
    suffix = ".npz"

    n = 3
    dev = 0
    run = 1
    n_estimators = 50
    files = utils.get_files(prefix, suffix)

    for f in files[0:n]:
        train = utils.get_train_dev_set(files=[f], n=1, dev=dev)
        train = utils.get_X_Y(train)
        rf_reg = utils.fit_random_forest(
            np.transpose(np.array(train[4:-2])),
            np.transpose(np.array(train[-1].data)),
            n_estimators,
        )

    utils.save_model(rf_reg, "rf_reg_{}.sav".format(str(run)))
Ejemplo n.º 3
0
def main(opt):
    train_data, valid_data = get_train_valid_split_data_names(opt.img_folder, opt.ano_folder, valid_size=1/8)

    # データの読み込み
    print("load data")
    train_dataset = Phase1Dataset(train_data, load_size=(640, 640), augment=True, limit=opt.limit)
    print("train data length : %d" % (len(train_dataset)))
    valid_dataset = Phase1Dataset(valid_data, load_size=(640, 640), augment=False, limit=opt.limit)
    print("valid data length : %d" % (len(valid_dataset)))
    # DataLoaderの作成
    train_loader = torch.utils.data.DataLoader(
        train_dataset,
        batch_size=opt.batch_size,
        shuffle=True,
        num_workers=opt.num_workers,
        pin_memory=True,
        drop_last=True
    )

    valid_loader = torch.utils.data.DataLoader(
        valid_dataset,
        batch_size=1,
        shuffle=False,
        num_workers=opt.num_workers,
        pin_memory=True,
        drop_last=True
    )

    # GPUの設定(PyTorchでは明示的に指定する必要がある)
    device = torch.device('cuda' if opt.gpus > 0 else 'cpu')

    # モデルの作成
    heads = {'hm': 1}
    model = get_pose_net(18, heads, 256).to(device)
    if opt.load_model != '':
        model, optimizer, start_epoch = load_model(
            model, opt.load_model, optimizer)

    # 最適化手法を定義
    if opt.optimizer == "SGD":
        optimizer = torch.optim.SGD(model.parameters(), lr=opt.lr)#, momentum=m, dampening=d, weight_decay=w, nesterov=n)
    elif opt.optimizer == "Adam":
        optimizer = torch.optim.Adam(model.parameters(), opt.lr)
    elif opt.optimizer == "RAdam":
        optimizer = optim.RAdam(model.parameters(), lr=opt.lr)
    
    # 損失関数を定義
    criterion = HMLoss()
    # 学習率のスケジューリングを定義
    scheduler = CosineAnnealingWarmRestarts(optimizer, T_0=10, T_mult=1, eta_min=0.00001)

    start_epoch = 0
    best_validation_loss = 1e10
    # 保存用フォルダの作成
    os.makedirs(os.path.join(opt.save_dir, opt.task, 'visualized'), exist_ok=True)

    # 学習 TODO エポック終了時点ごとにテスト用データで評価とモデル保存
    for epoch in range(start_epoch + 1, opt.num_epochs + 1):
        print("learning rate : %f" % scheduler.get_last_lr()[0])
        train(train_loader, model, optimizer, criterion, device, opt.num_epochs, epoch)
        if opt.optimizer == "SGD":
            scheduler.step()

        # 最新モデルの保存
        save_model(os.path.join(opt.save_dir, opt.task, 'model_last.pth'),
                   epoch, model, optimizer, scheduler)

        # テスト用データで評価
        validation_loss, accumulate_datas = valid(valid_loader, model, criterion, device)
        # ベストスコア更新でモデルの保存
        if validation_loss < best_validation_loss:
            best_validation_loss = validation_loss
            save_model(os.path.join(opt.save_dir, opt.task, 'model_best.pth'),
                       epoch, model, optimizer, scheduler)
            print("saved best model")
            visualization(os.path.join(opt.save_dir, opt.task, 'visualized'),
                        accumulate_datas)
Ejemplo n.º 4
0
def main() -> None:
    """
    config is made up of
    dictionary {data loader, optimizer, criterion, scheduler, tokenizer, args, model}
    """
    args, performance_check_objects = system_setting()
    config = model_setting(args)
    global_steps = [0]

    if args.train_ == 'True':
        logger.info('Start Training')

        for epoch in range(args.epochs):
            start_time = time.time()

            config['model'].train()
            train_loss, train_mem_acc, pp = train(config, global_steps)

            config['model'].eval()
            valid_loss, valid_mem_acc, = valid(config)

            end_time = time.time()
            epoch_mins, epoch_secs = epoch_time(start_time, end_time)

            performance = {
                'tl': train_loss,
                'vl': valid_loss,
                'tma': train_mem_acc,
                'tla': 0,
                'vma': valid_mem_acc,
                'vla': 0,
                'ep': epoch,
                'epm': epoch_mins,
                'eps': epoch_secs
            }

            performance_check_objects['early_stop_check'], performance_check_objects['best_valid_loss'] = \
                save_model(config, performance, performance_check_objects, pp)

    correct_list = []
    if args.test_ == 'True':
        logger.info("Start Test")

        config['model'].load_state_dict(torch.load(
            args.path_to_saved_model))  #, strict=False)
        #config['model'].load_state_dict(torch.load(args.path_to_saved_model)['State_dict'])
        config['model'].eval()

        test_loss, test_mem_acc, f1 = test(config, correct_list)
        print(
            f'\n\t==Test loss: {test_loss:.4f} | Test memory acc: {test_mem_acc:.4f} | Test logic acc: {0.1:.4f} | Test F1 score: {f1:.4f}==\n'
        )

    if args.eval == 'True':
        logger.info("Start Inference")

        config['memory_model'].load_state_dict(
            torch.load('./output/memory_our_9805.pt'), strict=False)
        config['logic_model'].load_state_dict(
            torch.load('./output/logic_8640.pt'), strict=False)

        print("=========================================================")
        print(f"Memory model size: ")
        print_size_of_model(config['memory_model'])
        print(f"Logic model size: ")
        print_size_of_model(config['logic_model'])
        print("=========================================================")

        config['memory_model'].eval()
        config['logic_model'].eval()

        inference(config)
Ejemplo n.º 5
0
def train(opt):
    # set device to cpu/gpu
    if opt.use_gpu:
        device = torch.device("cuda", opt.gpu_id)
    else:
        device = torch.device("cpu")

    # Data transformations for data augmentation
    transform_train = transforms.Compose([
        transforms.RandomCrop(32, padding=4),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.RandomErasing(),
    ])
    transform_val = transforms.Compose([
        transforms.ToTensor(),
    ])

    # get CIFAR10/CIFAR100 train/val set
    if opt.dataset == "CIFAR10":
        alp_lambda = 0.5
        margin = 0.03
        lambda_loss = [2, 0.001]
        train_set = CIFAR10(root="./data",
                            train=True,
                            download=True,
                            transform=transform_train)
        val_set = CIFAR10(root="./data",
                          train=True,
                          download=True,
                          transform=transform_val)
    else:
        alp_lambda = 0.5
        margin = 0.03
        lambda_loss = [2, 0.001]
        train_set = CIFAR100(root="./data",
                             train=True,
                             download=True,
                             transform=transform_train)
        val_set = CIFAR100(root="./data",
                           train=True,
                           download=True,
                           transform=transform_val)
    num_classes = np.unique(train_set.targets).shape[0]

    # set stratified train/val split
    idx = list(range(len(train_set.targets)))
    train_idx, val_idx, _, _ = train_test_split(idx,
                                                train_set.targets,
                                                test_size=opt.val_split,
                                                random_state=42)

    # get train/val samplers
    train_sampler = SubsetRandomSampler(train_idx)
    val_sampler = SubsetRandomSampler(val_idx)

    # get train/val dataloaders
    train_loader = DataLoader(train_set,
                              sampler=train_sampler,
                              batch_size=opt.batch_size,
                              num_workers=opt.num_workers)
    val_loader = DataLoader(val_set,
                            sampler=val_sampler,
                            batch_size=opt.batch_size,
                            num_workers=opt.num_workers)

    data_loaders = {"train": train_loader, "val": val_loader}

    print(
        "Dataset -- {}, Metric -- {}, Train Mode -- {}, Backbone -- {}".format(
            opt.dataset, opt.metric, opt.train_mode, opt.backbone))
    print("Train iteration batch size: {}".format(opt.batch_size))
    print("Train iterations per epoch: {}".format(len(train_loader)))

    # get backbone model
    if opt.backbone == "resnet18":
        model = resnet18(pretrained=False)
    else:
        model = resnet34(pretrained=False)

    # set metric loss function
    model.fc = Softmax(model.fc.in_features, num_classes)

    model.to(device)
    if opt.use_gpu:
        model = DataParallel(model).to(device)

    criterion = CrossEntropyLoss()
    mse_criterion = MSELoss()

    # set optimizer and LR scheduler
    if opt.optimizer == "sgd":
        optimizer = SGD([{
            "params": model.parameters()
        }],
                        lr=opt.lr,
                        weight_decay=opt.weight_decay,
                        momentum=0.9)
    else:
        optimizer = Adam([{
            "params": model.parameters()
        }],
                         lr=opt.lr,
                         weight_decay=opt.weight_decay)
    if opt.scheduler == "decay":
        scheduler = lr_scheduler.StepLR(optimizer,
                                        step_size=opt.lr_step,
                                        gamma=opt.lr_decay)
    else:
        scheduler = lr_scheduler.ReduceLROnPlateau(optimizer,
                                                   factor=0.1,
                                                   patience=10)

    # train/val loop
    for epoch in range(opt.epoch):
        for phase in ["train", "val"]:
            total_examples, total_correct, total_loss = 0, 0, 0

            if phase == "train":
                model.train()
            else:
                model.eval()

            start_time = time.time()
            for ii, data in enumerate(data_loaders[phase]):
                # load data batch to device
                images, labels = data
                images = images.to(device)
                labels = labels.to(device).long()

                # perform adversarial attack update to images
                if opt.train_mode == "at" or opt.train_mode == "alp":
                    adv_images = pgd(model, images, labels, 8. / 255, 2. / 255,
                                     7)
                else:
                    pass

                # at train mode
                if opt.train_mode == "at":
                    # get feature embedding from resnet
                    features, _ = model(images, labels)
                    adv_features, adv_predictions = model(adv_images, labels)

                    # get triplet loss (margin 0.03 CIFAR10/100, 0.01 TinyImageNet from paper)
                    tpl_loss, _, mask = batch_all_triplet_loss(
                        labels, features, margin, adv_embeddings=adv_features)

                    # get adv anchor and clean pos/neg feature norm loss
                    norm = features.mm(features.t()).diag()
                    adv_norm = adv_features.mm(adv_features.t()).diag()
                    norm_loss = adv_norm[mask.nonzero()[0]] + norm[
                        mask.nonzero()[1]] + norm[mask.nonzero()[2]]
                    norm_loss = torch.sum(norm_loss) / \
                        (mask.nonzero()[0].shape[0] + mask.nonzero()
                         [1].shape[0] + mask.nonzero()[2].shape[0])

                    # get cross-entropy loss (only adv considering anchor examples)
                    adv_anchor_predictions = adv_predictions[np.unique(
                        mask.nonzero()[0])]
                    anchor_labels = labels[np.unique(mask.nonzero()[0])]
                    ce_loss = criterion(adv_anchor_predictions, anchor_labels)

                    # combine cross-entropy loss, triplet loss and feature norm loss using lambda weights
                    loss = ce_loss + lambda_loss[0] * \
                        tpl_loss + lambda_loss[1] * norm_loss
                    optimizer.zero_grad()

                    # for result accumulation
                    predictions = adv_anchor_predictions
                    labels = anchor_labels

                # alp train mode
                elif opt.train_mode == "alp":
                    # get feature embedding from resnet
                    features, predictions = model(images, labels)
                    adv_features, adv_predictions = model(adv_images, labels)

                    # get triplet loss (margin 0.03 CIFAR10/100, 0.01 TinyImageNet from paper)
                    tpl_loss, _, mask = batch_all_triplet_loss(
                        labels, features, margin, adv_embeddings=adv_features)

                    # get adv anchor and clean pos/neg feature norm loss
                    norm = features.mm(features.t()).diag()
                    adv_norm = adv_features.mm(adv_features.t()).diag()
                    norm_loss = adv_norm[mask.nonzero()[0]] + norm[
                        mask.nonzero()[1]] + norm[mask.nonzero()[2]]
                    norm_loss = torch.sum(norm_loss) / \
                        (mask.nonzero()[0].shape[0] + mask.nonzero()
                         [1].shape[0] + mask.nonzero()[2].shape[0])

                    # get cross-entropy loss (only considering adv anchor examples)
                    anchor_predictions = predictions[np.unique(
                        mask.nonzero()[0])]
                    adv_anchor_predictions = adv_predictions[np.unique(
                        mask.nonzero()[0])]
                    anchor_labels = labels[np.unique(mask.nonzero()[0])]
                    ce_loss = criterion(adv_anchor_predictions, anchor_labels)

                    # get alp loss
                    alp_loss = mse_criterion(adv_anchor_predictions,
                                             anchor_predictions)

                    # combine cross-entropy loss, triplet loss and feature norm loss using lambda weights
                    loss = ce_loss + lambda_loss[0] * \
                        tpl_loss + lambda_loss[1] * norm_loss
                    # combine loss with alp loss
                    loss = loss + alp_lambda * alp_loss
                    optimizer.zero_grad()

                    # for result accumulation
                    predictions = adv_anchor_predictions
                    labels = anchor_labels

                # clean train mode
                else:
                    # get feature embedding from resnet
                    features, predictions = model(images, labels)

                    # get triplet loss (margin 0.03 CIFAR10/100, 0.01 TinyImageNet from paper)
                    tpl_loss, _, mask = batch_all_triplet_loss(
                        labels, features, margin)

                    # get feature norm loss
                    norm = features.mm(features.t()).diag()
                    norm_loss = norm[mask.nonzero()[0]] + \
                        norm[mask.nonzero()[1]] + norm[mask.nonzero()[2]]
                    norm_loss = torch.sum(norm_loss) / \
                        (mask.nonzero()[0].shape[0] + mask.nonzero()
                         [1].shape[0] + mask.nonzero()[2].shape[0])

                    # get cross-entropy loss (only considering anchor examples)
                    anchor_predictions = predictions[np.unique(
                        mask.nonzero()[0])]
                    anchor_labels = labels[np.unique(mask.nonzero()[0])]
                    ce_loss = criterion(anchor_predictions, anchor_labels)

                    # combine cross-entropy loss, triplet loss and feature norm loss using lambda weights
                    loss = ce_loss + lambda_loss[0] * \
                        tpl_loss + lambda_loss[1] * norm_loss
                    optimizer.zero_grad()

                    # for result accumulation
                    predictions = anchor_predictions
                    labels = anchor_labels

                # only take step if in train phase
                if phase == "train":
                    loss.backward()
                    optimizer.step()

                # accumulate train or val results
                predictions = torch.argmax(predictions, 1)
                total_examples += predictions.size(0)
                total_correct += predictions.eq(labels).sum().item()
                total_loss += loss.item()

                # print accumulated train/val results at end of epoch
                if ii == len(data_loaders[phase]) - 1:
                    end_time = time.time()
                    acc = total_correct / total_examples
                    loss = total_loss / len(data_loaders[phase])
                    print(
                        "{}: Epoch -- {} Loss -- {:.6f} Acc -- {:.6f} Time -- {:.6f}sec"
                        .format(phase, epoch, loss, acc,
                                end_time - start_time))

                    if phase == "train":
                        loss = total_loss / len(data_loaders[phase])
                        scheduler.step(loss)
                    else:
                        print("")

    # save model after training for opt.epoch
    save_model(model, opt.dataset, opt.metric, opt.train_mode, opt.backbone)
Ejemplo n.º 6
0
def train(opt):
    # set device to cpu/gpu
    if opt.use_gpu:
        device = torch.device("cuda", opt.gpu_id)
    else:
        device = torch.device("cpu")

    # Data transformations for data augmentation
    transform_train = transforms.Compose([
        transforms.RandomCrop(32, padding=4),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.RandomErasing(),
    ])
    transform_val = transforms.Compose([
        transforms.ToTensor(),
    ])

    # get CIFAR10/CIFAR100 train/val set
    if opt.dataset == "CIFAR10":
        alp_lambda = 0.5
        train_set = CIFAR10(root="./data",
                            train=True,
                            download=True,
                            transform=transform_train)
        val_set = CIFAR10(root="./data",
                          train=True,
                          download=True,
                          transform=transform_val)
    else:
        alp_lambda = 0.5
        train_set = CIFAR100(root="./data",
                             train=True,
                             download=True,
                             transform=transform_train)
        val_set = CIFAR100(root="./data",
                           train=True,
                           download=True,
                           transform=transform_val)
    num_classes = np.unique(train_set.targets).shape[0]

    # set stratified train/val split
    idx = list(range(len(train_set.targets)))
    train_idx, val_idx, _, _ = train_test_split(idx,
                                                train_set.targets,
                                                test_size=opt.val_split,
                                                random_state=42)

    # get train/val samplers
    train_sampler = SubsetRandomSampler(train_idx)
    val_sampler = SubsetRandomSampler(val_idx)

    # get train/val dataloaders
    train_loader = DataLoader(train_set,
                              sampler=train_sampler,
                              batch_size=opt.batch_size,
                              num_workers=opt.num_workers)
    val_loader = DataLoader(val_set,
                            sampler=val_sampler,
                            batch_size=opt.batch_size,
                            num_workers=opt.num_workers)

    data_loaders = {"train": train_loader, "val": val_loader}

    print(
        "Dataset -- {}, Metric -- {}, Train Mode -- {}, Backbone -- {}".format(
            opt.dataset, opt.metric, opt.train_mode, opt.backbone))
    print("Train iteration batch size: {}".format(opt.batch_size))
    print("Train iterations per epoch: {}".format(len(train_loader)))

    # get backbone model
    if opt.backbone == "resnet18":
        model = resnet18(pretrained=False)
    else:
        model = resnet34(pretrained=False)

    model.fc = Softmax(model.fc.in_features, num_classes)

    model.to(device)
    if opt.use_gpu:
        model = DataParallel(model).to(device)

    criterion = CrossEntropyLoss()
    mse_criterion = MSELoss()

    # set optimizer and LR scheduler
    if opt.optimizer == "sgd":
        optimizer = SGD([{
            "params": model.parameters()
        }],
                        lr=opt.lr,
                        weight_decay=opt.weight_decay,
                        momentum=0.9)
    else:
        optimizer = Adam([{
            "params": model.parameters()
        }],
                         lr=opt.lr,
                         weight_decay=opt.weight_decay)
    if opt.scheduler == "decay":
        scheduler = lr_scheduler.StepLR(optimizer,
                                        step_size=opt.lr_step,
                                        gamma=opt.lr_decay)
    else:
        scheduler = lr_scheduler.ReduceLROnPlateau(optimizer,
                                                   factor=0.1,
                                                   patience=10)

    # train/val loop
    best_acc = 0
    for epoch in range(opt.epoch):
        for phase in ["train", "val"]:
            total_examples, total_correct, total_loss = 0, 0, 0

            if phase == "train":
                model.train()
            else:
                model.eval()

            start_time = time.time()
            for ii, data in enumerate(data_loaders[phase]):
                # load data batch to device
                images, labels = data
                images = images.to(device)
                labels = labels.to(device).long()

                # perform adversarial attack update to images
                if opt.train_mode == "at" or opt.train_mode == "alp":
                    adv_images = pgd(model, images, labels, 8. / 255, 2. / 255,
                                     7)
                else:
                    pass

                # at train mode prediction
                if opt.train_mode == "at":
                    # logits for adversarial images
                    _, adv_predictions = model(adv_images, labels)

                    # get loss
                    loss = criterion(adv_predictions, labels)
                    optimizer.zero_grad()

                    # for result accumulation
                    predictions = adv_predictions

                # alp train mode prediction
                elif opt.train_mode == "alp":
                    # logits for clean and adversarial images
                    _, predictions = model(images, labels)
                    _, adv_predictions = model(adv_images, labels)

                    # get ce loss
                    ce_loss = criterion(adv_predictions, labels)

                    # get alp loss
                    alp_loss = mse_criterion(adv_predictions, predictions)

                    # get overall loss
                    loss = ce_loss + alp_lambda * alp_loss
                    optimizer.zero_grad()

                    # for result accumulation
                    predictions = adv_predictions

                # clean train mode prediction
                else:
                    # logits for clean images
                    _, predictions = model(images, labels)

                    # get loss
                    loss = criterion(predictions, labels)
                    optimizer.zero_grad()

                # only take step if in train phase
                if phase == "train":
                    loss.backward()
                    optimizer.step()

                # accumulate train or val results
                predictions = torch.argmax(predictions, 1)
                total_examples += predictions.size(0)
                total_correct += predictions.eq(labels).sum().item()
                total_loss += loss.item()

                # print accumulated train/val results at end of epoch
                if ii == len(data_loaders[phase]) - 1:
                    end_time = time.time()
                    acc = total_correct / total_examples
                    loss = total_loss / len(data_loaders[phase])
                    print(
                        "{}: Epoch -- {} Loss -- {:.6f} Acc -- {:.6f} Time -- {:.6f}sec"
                        .format(phase, epoch, loss, acc,
                                end_time - start_time))

                    if phase == "train":
                        loss = total_loss / len(data_loaders[phase])
                        scheduler.step(loss)
                    else:
                        if acc > best_acc:
                            print("Accuracy improved. Saving model")
                            best_acc = acc
                            save_model(model, opt.dataset, opt.metric,
                                       opt.train_mode, opt.backbone)
                        print("")

    # save model after training for opt.epoch
    if opt.test_bb:
        save_model(model, opt.dataset, "bb", "", opt.backbone)
    '''