Example #1
0
    def __init__(self, local_channels=128, num_classes=None):
        super(Model, self).__init__()
        self.base1 = resnet50(pretrained=True)
        self.base2 = xception(num_classes=num_classes)
        planes = 2048

        bn_relu = []
        bn_relu += [nn.BatchNorm2d(planes)]
        bn_relu += [nn.ReLU(inplace=True)]
        bn_relu = nn.Sequential(*bn_relu)
        bn_relu.apply(weights_init_kaiming)
        self.bn_relu = bn_relu

        local_layer = []
        local_layer += [
            nn.MaxPool2d(kernel_size=(1, 7), stride=(1, 7), padding=0)
        ]
        local_layer += [nn.Conv2d(planes, local_channels, 1)]
        local_layer = nn.Sequential(*local_layer)
        local_layer.apply(weights_init_kaiming)
        self.local_layer = local_layer

        global_layer = []
        global_layer += [nn.AvgPool2d(kernel_size=(7, 7), stride=(7, 7))]
        global_layer = nn.Sequential(*global_layer)
        self.global_layer = global_layer

        if num_classes is not None:
            classifier = []
            classifier += [nn.Linear(planes, num_classes)]
            classifier = nn.Sequential(*classifier)
            classifier.apply(weights_init_classifier)
            self.classifier = classifier
Example #2
0
    def __init__(self,
                 num_class=1,
                 input_channel=3,
                 output_stride=32,
                 layer=101):
        super(resnet_backbone, self).__init__()
        if layer == 101:
            self.resnet = resnet101(pretrained=True,
                                    output_stride=output_stride)
        elif layer == 152:
            self.resnet = resnet152(pretrained=True,
                                    output_stride=output_stride)
        elif layer == 50:
            self.resnet = resnet50(pretrained=True,
                                   output_stride=output_stride)
        else:
            raise ValueError("only support ResNet101 and ResNet152 now")

        if input_channel == 1:
            self.conv1 = nn.Conv2d(1,
                                   64,
                                   kernel_size=7,
                                   stride=2,
                                   padding=3,
                                   bias=False)
        elif input_channel == 3:
            self.conv1 = self.resnet.conv1
        else:
            raise ValueError("input channel should be 3 or 1")
Example #3
0
    def __init__(self, topN=4, n_class=200, use_gpu=False):
        super(attention_net, self).__init__()
        self.n_class = n_class
        self.use_gpu = use_gpu
        self.pretrained_model = resnet.resnet50(pretrained=False)
        self.pretrained_model.avgpool = nn.AdaptiveAvgPool2d(1)
        self.pretrained_model.fc = nn.Linear(512 * 4, self.n_class)
        self.proposal_net = ProposalNet()
        self.topN = topN
        self.concat_net = nn.Linear(2048 * (CAT_NUM + 1 + 1), self.n_class)
        self.partcls_net = nn.Linear(512 * 4, self.n_class)

        self.pad_side = 224
        _, edge_anchors_small, _ = generate_default_anchor_maps(
            setting='small')
        self.edge_anchors_small = (edge_anchors_small + 224).astype(np.int)
        _, edge_anchors_large, _ = generate_default_anchor_maps(
            setting='large')
        self.edge_anchors_large = (edge_anchors_large + 224).astype(np.int)
Example #4
0
train_loader_t = DataLoader(dataset=train_dataset_t,
                            batch_size=int(batch_size / K),
                            shuffle=True,
                            num_workers=4,
                            drop_last=True)

test_dataset_t = imgdataset(dataset_dir=tar_dir_test,
                            txt_path=tar_annfile_test,
                            transformer='test')
test_loader_t = DataLoader(dataset=test_dataset_t,
                           batch_size=4,
                           shuffle=False,
                           num_workers=0)

###########   MODEL   ###########
model, param = resnet50(pretrained=imageNet_pretrain, num_classes=702)
model.cuda()
model = nn.DataParallel(model)  #, device_ids=[0,1])

losses = Losses(K=K,
                batch_size=batch_size,
                bank_size=len(train_dataset_t),
                ann_file=tar_annfile,
                cam_num=num_cam)
losses = losses.cuda()
optimizer = torch.optim.SGD(param,
                            lr=base_lr,
                            momentum=0.9,
                            weight_decay=5e-4,
                            nesterov=True)
Example #5
0
train_loader = DataLoader(dataset=train_dataset,
                          batch_size=1,
                          shuffle=False,
                          num_workers=4)

img_dir = dataset_path + 'Market-1501/'
test_dataset = imgdataset(dataset_dir=img_dir,
                          txt_path=ann_file_test,
                          transformer='test')
test_loader = DataLoader(dataset=test_dataset,
                         batch_size=1,
                         shuffle=False,
                         num_workers=4)

###########   TEST   ###########
model, _ = resnet50(pretrained=snapshot, num_classes=702)
model.cuda()
model.eval()

print('extract feature for training set')
train_feas = extract_fea_camtrans(model, train_loader)
_, cam_ids, frames = get_info(ann_file_train)

print('generate spatial-temporal distribution')
dist = cdist(train_feas, train_feas)
dist = np.power(dist, 2)
#dist = re_ranking(original_dist=dist)
labels = cluster(dist)
num_ids = len(set(labels))
print('cluster id num:', num_ids)
distribution = get_st_distribution(cam_ids,
Example #6
0
def main(
    architecture,
    batch_size,
    length_scale,
    centroid_size,
    learning_rate,
    l_gradient_penalty,
    gamma,
    weight_decay,
    final_model,
    output_dir,
):
    writer = SummaryWriter(log_dir=f"runs/{output_dir}")

    ds = all_datasets["CIFAR10"]()
    input_size, num_classes, dataset, test_dataset = ds

    # Split up training set
    idx = list(range(len(dataset)))
    random.shuffle(idx)

    if final_model:
        train_dataset = dataset
        val_dataset = test_dataset
    else:
        val_size = int(len(dataset) * 0.8)
        train_dataset = torch.utils.data.Subset(dataset, idx[:val_size])
        val_dataset = torch.utils.data.Subset(dataset, idx[val_size:])

        val_dataset.transform = (test_dataset.transform
                                 )  # Test time preprocessing for validation

    if architecture == "WRN":
        model_output_size = 640
        epochs = 200
        milestones = [60, 120, 160]
        feature_extractor = WideResNet()
    elif architecture == "ResNet18":
        model_output_size = 512
        epochs = 200
        milestones = [60, 120, 160]
        feature_extractor = resnet18()
    elif architecture == "ResNet50":
        model_output_size = 2048
        epochs = 200
        milestones = [60, 120, 160]
        feature_extractor = resnet50()
    elif architecture == "ResNet110":
        model_output_size = 2048
        epochs = 200
        milestones = [60, 120, 160]
        feature_extractor = resnet110()
    elif architecture == "DenseNet121":
        model_output_size = 1024
        epochs = 200
        milestones = [60, 120, 160]
        feature_extractor = densenet121()

        # Adapted resnet from:
        # https://github.com/kuangliu/pytorch-cifar/blob/master/models/resnet.py
        feature_extractor.conv1 = torch.nn.Conv2d(3,
                                                  64,
                                                  kernel_size=3,
                                                  stride=1,
                                                  padding=1,
                                                  bias=False)
        feature_extractor.maxpool = torch.nn.Identity()
        feature_extractor.fc = torch.nn.Identity()

    if centroid_size is None:
        centroid_size = model_output_size

    model = ResNet_DUQ(
        feature_extractor,
        num_classes,
        centroid_size,
        model_output_size,
        length_scale,
        gamma,
    )
    model = model.cuda()

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

    scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer,
                                                     milestones=milestones,
                                                     gamma=0.2)

    def calc_gradients_input(x, y_pred):
        gradients = torch.autograd.grad(
            outputs=y_pred,
            inputs=x,
            grad_outputs=torch.ones_like(y_pred),
            create_graph=True,
        )[0]

        gradients = gradients.flatten(start_dim=1)

        return gradients

    def calc_gradient_penalty(x, y_pred):
        gradients = calc_gradients_input(x, y_pred)

        # L2 norm
        grad_norm = gradients.norm(2, dim=1)

        # Two sided penalty
        gradient_penalty = ((grad_norm - 1)**2).mean()

        return gradient_penalty

    def step(engine, batch):
        model.train()

        optimizer.zero_grad()

        x, y = batch
        x, y = x.cuda(), y.cuda()

        x.requires_grad_(True)

        y_pred = model(x)

        y = F.one_hot(y, num_classes).float()

        loss = F.binary_cross_entropy(y_pred, y, reduction="mean")

        if l_gradient_penalty > 0:
            gp = calc_gradient_penalty(x, y_pred)
            loss += l_gradient_penalty * gp

        loss.backward()
        optimizer.step()

        x.requires_grad_(False)

        with torch.no_grad():
            model.eval()
            model.update_embeddings(x, y)

        return loss.item()

    def eval_step(engine, batch):
        model.eval()

        x, y = batch
        x, y = x.cuda(), y.cuda()

        x.requires_grad_(True)

        y_pred = model(x)

        return {"x": x, "y": y, "y_pred": y_pred}

    trainer = Engine(step)
    evaluator = Engine(eval_step)

    metric = Average()
    metric.attach(trainer, "loss")

    metric = Accuracy(output_transform=lambda out: (out["y_pred"], out["y"]))
    metric.attach(evaluator, "accuracy")

    def bce_output_transform(out):
        return (out["y_pred"], F.one_hot(out["y"], num_classes).float())

    metric = Loss(F.binary_cross_entropy,
                  output_transform=bce_output_transform)
    metric.attach(evaluator, "bce")

    metric = Loss(calc_gradient_penalty,
                  output_transform=lambda out: (out["x"], out["y_pred"]))
    metric.attach(evaluator, "gradient_penalty")

    pbar = ProgressBar(dynamic_ncols=True)
    pbar.attach(trainer)

    kwargs = {"num_workers": 4, "pin_memory": True}

    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=batch_size,
                                               shuffle=True,
                                               drop_last=True,
                                               **kwargs)

    val_loader = torch.utils.data.DataLoader(val_dataset,
                                             batch_size=batch_size,
                                             shuffle=False,
                                             **kwargs)

    test_loader = torch.utils.data.DataLoader(test_dataset,
                                              batch_size=batch_size,
                                              shuffle=False,
                                              **kwargs)

    @trainer.on(Events.EPOCH_COMPLETED)
    def log_results(trainer):
        metrics = trainer.state.metrics
        loss = metrics["loss"]

        print(f"Train - Epoch: {trainer.state.epoch} Loss: {loss:.2f}")

        writer.add_scalar("Loss/train", loss, trainer.state.epoch)

        if trainer.state.epoch > (epochs - 5):
            accuracy, auroc = get_cifar_svhn_ood(model)
            print(f"Test Accuracy: {accuracy}, AUROC: {auroc}")
            writer.add_scalar("OoD/test_accuracy", accuracy,
                              trainer.state.epoch)
            writer.add_scalar("OoD/roc_auc", auroc, trainer.state.epoch)

            accuracy, auroc = get_auroc_classification(val_dataset, model)
            print(f"AUROC - uncertainty: {auroc}")
            writer.add_scalar("OoD/val_accuracy", accuracy,
                              trainer.state.epoch)
            writer.add_scalar("OoD/roc_auc_classification", auroc,
                              trainer.state.epoch)

        evaluator.run(val_loader)
        metrics = evaluator.state.metrics
        acc = metrics["accuracy"]
        bce = metrics["bce"]
        GP = metrics["gradient_penalty"]
        loss = bce + l_gradient_penalty * GP

        print((f"Valid - Epoch: {trainer.state.epoch} "
               f"Acc: {acc:.4f} "
               f"Loss: {loss:.2f} "
               f"BCE: {bce:.2f} "
               f"GP: {GP:.2f} "))

        writer.add_scalar("Loss/valid", loss, trainer.state.epoch)
        writer.add_scalar("BCE/valid", bce, trainer.state.epoch)
        writer.add_scalar("GP/valid", GP, trainer.state.epoch)
        writer.add_scalar("Accuracy/valid", acc, trainer.state.epoch)

        scheduler.step()

    trainer.run(train_loader, max_epochs=epochs)
    evaluator.run(test_loader)
    acc = evaluator.state.metrics["accuracy"]

    print(f"Test - Accuracy {acc:.4f}")

    torch.save(model.state_dict(), f"runs/{output_dir}/model.pt")
    writer.close()
Example #7
0
tar_annfile_test = 'list_{}/list_{}_test.txt'.format(opt.tar, opt.tar)

train_dataset_t = imgdataset_camtrans(dataset_dir=tar_dir,
                                      txt_path=tar_annfile,
                                      transformer='train',
                                      num_cam=num_cam,
                                      K=K)
train_loader_t = DataLoader(dataset=train_dataset_t,
                            batch_size=int(batch_size / K),
                            shuffle=True,
                            num_workers=4,
                            drop_last=True)

###########   MODEL   ###########
imageNet_pretrain = 'resnet50-19c8e357.pth'
model, param = resnet50(pretrained=imageNet_pretrain,
                        num_classes=numpids + len(train_dataset2.pids))
model.cuda()
model = CamDataParallel(model)  #, device_ids=[0,1])

losses = Losses(K=K,
                batch_size=batch_size,
                bank_size=len(train_dataset_t),
                ann_file=tar_annfile,
                cam_num=num_cam)
losses = losses.cuda()
optimizer = torch.optim.SGD(param,
                            lr=base_lr,
                            momentum=0.9,
                            weight_decay=5e-4,
                            nesterov=True)