コード例 #1
0
ファイル: vgg.py プロジェクト: paper-submissions/mixsize
def vgg(**config):
    dataset = config.pop('dataset', 'imagenet')
    depth = config.pop('depth', 16)
    bn = config.pop('bn', True)

    if dataset == 'imagenet':
        config.setdefault('num_classes', 1000)
        if depth == 11:
            if bn is False:
                return vgg11(pretrained=False, **config)
            else:
                return vgg11_bn(pretrained=False, **config)
        if depth == 13:
            if bn is False:
                return vgg13(pretrained=False, **config)
            else:
                return vgg13_bn(pretrained=False, **config)
        if depth == 16:
            if bn is False:
                return vgg16(pretrained=False, **config)
            else:
                return vgg16_bn(pretrained=False, **config)
        if depth == 19:
            if bn is False:
                return vgg19(pretrained=False, **config)
            else:
                return vgg19_bn(pretrained=False, **config)
    elif dataset == 'cifar10':
        config.setdefault('num_classes', 10)
    elif dataset == 'cifar100':
        config.setdefault('num_classes', 100)
    config.setdefault('batch_norm', bn)
    return VGG(model_name[depth], **config)
コード例 #2
0
    def __init__(self, dataset, arch='resnet18'):
        super().__init__()
        self.num_cam = dataset.num_cam
        self.img_shape, self.reducedgrid_shape = dataset.img_shape, dataset.reducedgrid_shape
        imgcoord2worldgrid_matrices = self.get_imgcoord2worldgrid_matrices(
            dataset.base.intrinsic_matrices, dataset.base.extrinsic_matrices,
            dataset.base.worldgrid2worldcoord_mat)
        self.coord_map = self.create_coord_map(self.reducedgrid_shape + [1])
        # img
        self.upsample_shape = list(
            map(lambda x: int(x / dataset.img_reduce), self.img_shape))
        img_reduce = np.array(self.img_shape) / np.array(self.upsample_shape)
        img_zoom_mat = np.diag(np.append(img_reduce, [1]))
        # map
        map_zoom_mat = np.diag(
            np.append(np.ones([2]) / dataset.grid_reduce, [1]))
        # projection matrices: img feat -> map feat
        self.proj_mats = [
            torch.from_numpy(
                map_zoom_mat @ imgcoord2worldgrid_matrices[cam] @ img_zoom_mat)
            for cam in range(self.num_cam)
        ]

        if arch == 'vgg11':
            base = vgg11().features
            base[-1] = nn.Sequential()
            base[-4] = nn.Sequential()
            split = 10
            self.base_pt1 = base[:split].to('cuda:1')
            self.base_pt2 = base[split:].to('cuda:0')
            out_channel = 512
        elif arch == 'resnet18':
            base = nn.Sequential(*list(
                resnet18(replace_stride_with_dilation=[False, True, True
                                                       ]).children())[:-2])
            split = 7
            self.base_pt1 = base[:split].to('cuda:1')
            self.base_pt2 = base[split:].to('cuda:0')
            out_channel = 512
        else:
            raise Exception('architecture currently support [vgg11, resnet18]')
        # 2.5cm -> 0.5m: 20x
        self.img_classifier = nn.Sequential(nn.Conv2d(out_channel, 64, 1),
                                            nn.ReLU(),
                                            nn.Conv2d(64, 2, 1,
                                                      bias=False)).to('cuda:0')
        self.map_classifier = nn.Sequential(
            nn.Conv2d(self.num_cam + 2, 512, 3, padding=1),
            nn.ReLU(),
            # nn.Conv2d(512, 512, 5, 1, 2), nn.ReLU(),
            nn.Conv2d(512, 512, 3, padding=2, dilation=2),
            nn.ReLU(),
            nn.Conv2d(512, 1, 3, padding=4, dilation=4,
                      bias=False)).to('cuda:0')
        pass
コード例 #3
0
def vgg_11(batch_norm=True, pretrained=False, fixed_feature=True):
    """ VGG 11-layer model from torchvision's vgg model.

	:param batch_norm: train model with batch normalization
	:param pretrained: if true, return a model pretrained on ImageNet
	:param fixed_feature: if true and pretrained is true, model features are fixed while training.
	"""
    if batch_norm:
        from torchvision.models.vgg import vgg11_bn
        model = vgg11_bn(pretrained)
    else:
        from torchvision.models.vgg import vgg11
        model = vgg11(pretrained)

    ff = True if pretrained and fixed_feature else False
    return _VGG(model, model.features, ff)
コード例 #4
0
ファイル: integration_test.py プロジェクト: ElementAI/baal
def test_integration():
    transform_pipeline = Compose([Resize((64, 64)), ToTensor()])
    cifar10_train = DummyDataset(transform_pipeline)
    cifar10_test = DummyDataset(transform_pipeline)

    al_dataset = ActiveLearningDataset(cifar10_train, pool_specifics={'transform': transform_pipeline})
    al_dataset.label_randomly(10)

    use_cuda = False
    model = vgg.vgg11(pretrained=False,
                      num_classes=10)

    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9, weight_decay=0.0005)

    # We can now use BaaL to create the active learning loop.

    model = ModelWrapper(model, criterion)
    # We create an ActiveLearningLoop that will automatically label the most uncertain samples.
    # In this case, we use the widely used BALD heuristic.

    active_loop = ActiveLearningLoop(al_dataset,
                                     model.predict_on_dataset,
                                     heuristic=heuristics.BALD(),
                                     query_size=10,
                                     batch_size=10,
                                     iterations=2,
                                     use_cuda=use_cuda,
                                     workers=4)

    # We're all set!
    num_steps = 10
    for step in range(num_steps):
        old_param = list(map(lambda x: x.clone(), model.model.parameters()))
        model.train_on_dataset(al_dataset, optimizer=optimizer, batch_size=10,
                               epoch=1, use_cuda=use_cuda, workers=2)
        model.test_on_dataset(cifar10_test, batch_size=10, use_cuda=use_cuda,
                              workers=2)

        if not active_loop.step():
            break
        new_param = list(map(lambda x: x.clone(), model.model.parameters()))
        assert any([not np.allclose(i.detach(), j.detach())
                    for i, j in zip(old_param, new_param)])
    assert step == 4  # 10 + (4 * 10) = 50, so it stops at iterations 4
コード例 #5
0
 def test_anneal_lr(self):
     net = vgg11()
     opt = torch.optim.SGD(net.parameters(), lr = .1)
     anneal_lr(opt, 10)
     for o in opt.param_groups:
         self.assertEqual(o["lr"], 0.01)
コード例 #6
0
ファイル: record.py プロジェクト: SunnerLi/Bok
def recordVGG(info):
    global SKIP
    import torchvision.models.vgg as vggGen

    if not (SKIP and 'vgg11' in info['name_list']):
        INFO("proceeding for VGG11...")
        net = vggGen.vgg11(pretrained=True).cuda()
        sum = __summary(net, [3, 224, 224], verbose=True)
        __writeInfoJSON(sum, 'vgg11')
    else:
        INFO("Skip VGG11")

    if not (SKIP and 'vgg13' in info['name_list']):
        INFO("proceeding for VGG13...")
        net = vggGen.vgg13(pretrained=True).cuda()
        sum = __summary(net, [3, 224, 224], verbose=True)
        __writeInfoJSON(sum, 'vgg13')
    else:
        INFO("Skip VGG13")

    if not (SKIP and 'vgg16' in info['name_list']):
        INFO("proceeding for VGG16...")
        net = vggGen.vgg16(pretrained=True).cuda()
        sum = __summary(net, [3, 224, 224], verbose=True)
        __writeInfoJSON(sum, 'vgg16')
    else:
        INFO("Skip VGG16")

    if not (SKIP and 'vgg19' in info['name_list']):
        INFO("proceeding for VGG19...")
        net = vggGen.vgg19(pretrained=True).cuda()
        sum = __summary(net, [3, 224, 224], verbose=True)
        __writeInfoJSON(sum, 'vgg19')
    else:
        INFO("Skip VGG19")

    if not (SKIP and 'vgg11_bn' in info['name_list']):
        INFO("proceeding for VGG11_bn...")
        net = vggGen.vgg11_bn(pretrained=True).cuda()
        sum = __summary(net, [3, 224, 224], verbose=True)
        __writeInfoJSON(sum, 'vgg11_bn')
    else:
        INFO("Skip VGG11_bn")

    if not (SKIP and 'vgg13_bn' in info['name_list']):
        INFO("proceeding for VGG13_bn...")
        net = vggGen.vgg13_bn(pretrained=True).cuda()
        sum = __summary(net, [3, 224, 224], verbose=True)
        __writeInfoJSON(sum, 'vgg13_bn')
    else:
        INFO("Skip VGG13_bn")

    if not (SKIP and 'vgg16_bn' in info['name_list']):
        INFO("proceeding for VGG16_bn...")
        net = vggGen.vgg16_bn(pretrained=True).cuda()
        sum = __summary(net, [3, 224, 224], verbose=True)
        __writeInfoJSON(sum, 'vgg16_bn')
    else:
        INFO("Skip VGG16_bn")

    if not (SKIP and 'vgg19_bn' in info['name_list']):
        INFO("proceeding for VGG19_bn...")
        net = vggGen.vgg19_bn(pretrained=True).cuda()
        sum = __summary(net, [3, 224, 224], verbose=True)
        __writeInfoJSON(sum, 'vgg19_bn')
    else:
        INFO("Skip VGG19_bn")
コード例 #7
0
ファイル: models.py プロジェクト: magamba/hp_arrangements
def load_model(model_name, classes=1000, pretrained=True, in_channels=3):
    """Load the specified VGG architecture for ImageNet
  
    Args:
      model_name: VGG architecture type
      classes: number of predicted classes
      pretrained: load pretrained network on ImageNet
  """
    if pretrained:
        assert classes == 1000, "Pretrained models are provided only for Imagenet."

    kwargs = {'num_classes': classes}

    if model_name == 'vgg11':
        net = VGG.vgg11(pretrained=pretrained, **kwargs)
        if in_channels != 3:
            input_layer = nn.Conv2d(in_channels, 64, kernel_size=3, padding=1)
            nn.init.kaiming_normal_(input_layer.weight,
                                    mode='fan_out',
                                    nonlinearity='relu')
            input_layer.bias.data.zero_()
            net.features[0] = input_layer
    elif model_name == 'vgg13':
        net = VGG.vgg13(pretrained=pretrained, **kwargs)
        if in_channels != 3:
            input_layer = nn.Conv2d(in_channels, 64, kernel_size=3, padding=1)
            nn.init.kaiming_normal_(input_layer.weight,
                                    mode='fan_out',
                                    nonlinearity='relu')
            input_layer.bias.data.zero_()
            net.features[0] = input_layer
    elif model_name == 'vgg16':
        net = VGG.vgg16(pretrained=pretrained, **kwargs)
        if in_channels != 3:
            input_layer = nn.Conv2d(in_channels, 64, kernel_size=3, padding=1)
            nn.init.kaiming_normal_(input_layer.weight,
                                    mode='fan_out',
                                    nonlinearity='relu')
            input_layer.bias.data.zero_()
            net.features[0] = input_layer
    elif model_name == 'vgg19':
        net = VGG.vgg19(pretrained=pretrained, **kwargs)
        if in_channels != 3:
            input_layer = nn.Conv2d(in_channels, 64, kernel_size=3, padding=1)
            nn.init.kaiming_normal_(input_layer.weight,
                                    mode='fan_out',
                                    nonlinearity='relu')
            input_layer.bias.data.zero_()
            net.features[0] = input_layer
    elif model_name == 'vgg11bn':
        net = VGG.vgg11_bn(pretrained=pretrained, **kwargs)
        if in_channels != 3:
            input_layer = nn.Conv2d(in_channels, 64, kernel_size=3, padding=1)
            nn.init.kaiming_normal_(input_layer.weight,
                                    mode='fan_out',
                                    nonlinearity='relu')
            input_layer.bias.data.zero_()
            net.features[0] = input_layer
    elif model_name == 'vgg13bn':
        net = VGG.vgg13_bn(pretrained=pretrained, **kwargs)
        if in_channels != 3:
            input_layer = nn.Conv2d(in_channels, 64, kernel_size=3, padding=1)
            nn.init.kaiming_normal_(input_layer.weight,
                                    mode='fan_out',
                                    nonlinearity='relu')
            input_layer.bias.data.zero_()
            net.features[0] = input_layer
    elif model_name == 'vgg16bn':
        net = VGG.vgg16_bn(pretrained=pretrained, **kwargs)
        if in_channels != 3:
            input_layer = nn.Conv2d(in_channels, 64, kernel_size=3, padding=1)
            nn.init.kaiming_normal_(input_layer.weight,
                                    mode='fan_out',
                                    nonlinearity='relu')
            input_layer.bias.data.zero_()
            net.features[0] = input_layer
    elif model_name == 'vgg19bn':
        net = VGG.vgg19_bn(pretrained=pretrained, **kwargs)
        if in_channels != 3:
            input_layer = nn.Conv2d(in_channels, 64, kernel_size=3, padding=1)
            nn.init.kaiming_normal_(input_layer.weight,
                                    mode='fan_out',
                                    nonlinearity='relu')
            input_layer.bias.data.zero_()
            net.features[0] = input_layer
    elif model_name == 'vgg19_orig':
        net = VGG.vgg19(pretrained=False, **kwargs)
        if in_channels != 3:
            input_layer = nn.Conv2d(in_channels, 64, kernel_size=3, padding=1)
            net.features[0] = input_layer
        init_weights_vgg_orig(net)
    elif model_name == 'alexnet':
        net = AlexNet(pretrained=pretrained, **kwargs)
        if in_channels != 3:
            input_layer = nn.Conv2d(in_channels,
                                    64,
                                    kernel_size=11,
                                    stride=4,
                                    padding=2)
            nn.init.kaiming_normal_(input_layer.weight,
                                    mode='fan_out',
                                    nonlinearity='relu')
            input_layer.bias.data.zero_()
            net.features[0] = input_layer
    elif model_name == 'lenet':
        kwargs['in_channels'] = in_channels
        net = lenet(**kwargs)
    else:
        raise ValueError("Unsupported model architecture.")
    return net
コード例 #8
0
ファイル: example_deep.py プロジェクト: delve-team/delve
    test_data = CIFAR10(root="./tmp",
                        train=False,
                        download=True,
                        transform=Compose([ToTensor()]))

    train_loader = DataLoader(train_data,
                              batch_size=1024,
                              shuffle=True,
                              pin_memory=True)
    test_loader = DataLoader(test_data,
                             batch_size=1024,
                             shuffle=False,
                             pin_memory=True)

    # instantiate model
    model = vgg11(num_classes=10).to(device)

    # instantiate optimizer and loss
    optimizer = Adam(params=model.parameters())
    criterion = CrossEntropyLoss().to(device)

    # initialize delve
    tracker = SaturationTracker("experiment",
                                save_to="plotcsv",
                                stats=["lsat"],
                                modules=model,
                                device=device)

    # begin training
    for epoch in range(10):
        # only record saturation for uneven epochs
コード例 #9
0
    plt.show()
    train_monitors[1].plot(res)
    plt.show()
    train_monitors[5].plot(res)
    plt.show()
    train_monitors[6].plot(res)
    plt.show()
    # You can still have access to test_errors etc by doing "te, _ = Test_Error(testpred)(net)"
    # Note that if you ask again, it will not recompute the net output
    # To force recomputation, do "te, _ = Test_Error(testpred)(net, recompute=True)"

if example_3:
    from torchvision.models.vgg import vgg11
    trainset, valset, testset, trainloader, valloader, testloader, classes = load_plant_seed(
        size=(224, 224), validation_size=10)
    net = vgg11(pretrained=True)
    # for param in net.parameters():
    #     param.requires_grad = False
    #net.fc3 = nn.Linear(4096, len(classes))
    net.classifier[6] = nn.Linear(4096, len(classes))
    freeze(net, -1)
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
    res = {}
    testpred = Test_Prediction(valloader)
    train_monitors = [
        Train_Error(10),
        Running_Loss(10),
        Test_Error(testpred, 50),
        Save_Net(100)
    ]  #,
コード例 #10
0
from torch.utils.data import DataLoader

from torchvision import transforms
from torchvision.datasets import CIFAR10
from torchvision.models.resnet import resnet18
from torchvision.models.vgg import vgg11
from tqdm import tqdm

if __name__ == '__main__':
    transform = transforms.Compose((transforms.ToTensor(), transforms.Normalize(0.5, 0.5, 0.5)))
    train_dataset = CIFAR10('data', train=True, transform=transform, download=False)
    test_dataset = CIFAR10('data', train=False, transform=transform, download=False)

    max_epochs = 100
    batch_size = 64
    net = vgg11(pretrained=False, num_classes=10).cuda()
    optimizer = torch.optim.Adam(net.parameters(), lr=1e-5)
    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, max_epochs, eta_min=1e-10)
    loss_f = CrossEntropyLoss()

    train_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=3)
    test_dataloader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=3)

    for epoch in range(max_epochs):
        train_acc = 0
        test_acc = 0

        net.train()
        for idx, data in enumerate(tqdm(train_dataloader)):
            optimizer.zero_grad()
            inputs = data[0].cuda()
コード例 #11
0
def main():
    vgg = vgg11()
    dataset = Cifar10Dataset()
    kubenet = KubeVGG(vgg, dataset)
    return kubenet.start()
コード例 #12
0
 def __init__(self):
     super().__init__()
     vgg = vgg11()
     self.features = vgg.features  # We only need the convolutional part of vgg11