Ejemplo n.º 1
0
def alexnet(pretrained=False, **kwargs):  # 224*224
    if pretrained:
        model = AlexNet(**kwargs)
        pretrained_state_dict = torch.load(
            './Authority/alexnet-owt-4df8aa71.pth')
        now_state_dict = model.state_dict()  # 返回model模块的字典
        pretrained_state_dict.pop('classifier.6.weight')
        pretrained_state_dict.pop('classifier.6.bias')
        now_state_dict.update(pretrained_state_dict)
        model.load_state_dict(now_state_dict)
        return model
    return AlexNet(**kwargs)
Ejemplo n.º 2
0
def pytorch_cos():
    model = AlexNet(num_classes=2)
    optimizer = optim.SGD(params=model.parameters(), lr=0.0001)

    epoch = 100
    len_loader = 100

    scheduler = lr_scheduler.CosineAnnealingWarmRestarts(optimizer,
                                                         T_0=2,
                                                         T_mult=2,
                                                         eta_min=1e-6,
                                                         last_epoch=-1)
    plt.figure()
    x = []
    y = []
    for e in range(epoch):
        for i in range(len_loader):
            step = e + i / len_loader
            scheduler.step(step)
            lr = scheduler.get_last_lr()[0]

            x.append(step)
            y.append(lr)

    plt.plot(x, y)
    plt.xticks(np.arange(0, epoch + 1, 4))
    plt.show()
def test_resnet50_UniFLOPs():
    model = AlexNet()
    input = torch.randn(1,3,224,224)

    params = calParameters(model)
    flops = calGuideline(model, input)
    print("flops: ", flops, "params: ", params)
Ejemplo n.º 4
0
def compute_time():
    """
    计算100次,取平均值
    :return:
    """
    model_alexnet = AlexNet(num_classes=1000)
    model_googlenet = googlenet.GoogLeNet(num_classes=1000)
    model_alexnet.eval()
    model_googlenet.eval()

    total_time_alexnet = 0.0
    total_time_googlenet = 0.0

    epoch = 100
    for i in range(epoch):
        data = torch.randn((1, 3, 224, 224))
        start = time.time()
        outputs = model_alexnet.forward(data)
        end = time.time()
        total_time_alexnet += (end - start)

        start = time.time()
        outputs = model_googlenet.forward(data)
        end = time.time()
        total_time_googlenet += (end - start)

    print('[alexnet] time: {:.4f}'.format(total_time_alexnet / epoch))
    print('[googlenet] time: {:.4f}'.format(total_time_googlenet / epoch))
    print('time_googlenet / time_alexnet: {:.3f}'.format(total_time_googlenet /
                                                         total_time_alexnet))
Ejemplo n.º 5
0
def train_pipe(args, part='parameters'):
    torch.manual_seed(args.seed)
    deepspeed.runtime.utils.set_random_seed(args.seed)

    #
    # Build the model
    #

    # VGG also works :-)
    #net = vgg19(num_classes=10)
    net = AlexNet(num_classes=10)
    net = PipelineModule(layers=join_layers(net),
                         loss_fn=torch.nn.CrossEntropyLoss(),
                         num_stages=args.pipeline_parallel_size,
                         partition_method=part,
                         activation_checkpoint_interval=0)

    trainset = cifar_trainset(args.local_rank)

    engine, _, _, _ = deepspeed.initialize(
        args=args,
        model=net,
        model_parameters=[p for p in net.parameters() if p.requires_grad],
        training_data=trainset)

    for step in range(args.steps):
        loss = engine.train_batch()
Ejemplo n.º 6
0
 def __init__(self):
     super().__init__()
     self.features = AlexNet().features
     self.classifier = nn.Sequential(nn.Dropout(),
                                     nn.Conv2d(256, 4096, kernel_size=6),
                                     nn.ReLU(), nn.Dropout(),
                                     nn.Conv2d(4096, 4096, kernel_size=1),
                                     nn.ReLU(),
                                     nn.Conv2d(4096, 21, kernel_size=1))
    def __init__(self):
        super(Feature_AlexNet, self).__init__()
        self.model = AlexNet()
        self.model.load_state_dict(model_zoo.load_url(model_urls['alexnet']))

        self.fc1 = nn.Linear(256 * 6 * 6, 4096)
        self.bn1_fc = nn.BatchNorm1d(4096)
        self.fc2 = nn.Linear(4096, 2048)
        self.bn2_fc = nn.BatchNorm1d(2048)

        self.relu = nn.ReLU(inplace=True)
Ejemplo n.º 8
0
def get_model(device=None):
    # 加载CNN模型
    model = AlexNet(num_classes=2)
    model.load_state_dict(
        torch.load('./models/best_linear_svm_alexnet_car.pth'))
    model.eval()

    # 取消梯度追踪
    for param in model.parameters():
        param.requires_grad = False
    if device:
        model = model.to(device)

    return model
Ejemplo n.º 9
0
def compute_param():
    model_alexnet = AlexNet(num_classes=1000)
    model_googlenet = googlenet.GoogLeNet(num_classes=1000)
    model_alexnet.eval()
    model_googlenet.eval()

    num_alexnet = util.num_model(model_alexnet)
    num_googlenet = util.num_model(model_googlenet)

    print('[alexnet] param num: {}'.format(num_alexnet))
    print('[googlenet] param num: {}'.format(num_googlenet))

    print('num_alexnet / num_googlenet: {:.2f}'.format(num_alexnet /
                                                       num_googlenet))
Ejemplo n.º 10
0
def alexnet(num_classes, num_domains=None, pretrained=True):
    """AlexNet model architecture from the
    `"One weird trick..." <https://arxiv.org/abs/1404.5997>`_ paper.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = AlexNet()
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['alexnet']))
        print('Load pre trained model')
    num_ftrs = model.classifier[-1].in_features
    model.classifier[-1] = nn.Linear(num_ftrs, num_classes)
    nn.init.xavier_uniform_(model.classifier[-1].weight, .1)
    nn.init.constant_(model.classifier[-1].bias, 0.)
    return model
Ejemplo n.º 11
0
def get_model(name, input_size=None, output=None):
    name = name.lower()
    if name == 'lenet-300-100':
        model = LeNet_300_100(input_size, output)
    elif name == 'lenet-5':
        model = LeNet(input_size, output)
    elif 'vgg' in name:
        # if 'bn' in name:
        if name == 'vgg11':
            model = vgg11(pretrained=False, num_classes=output)
        elif name == 'vgg16':
            model = vgg16(pretrained=False, num_classes=output)
        else:
            assert False

        for n, m in model.named_modules():
            if hasattr(m, 'bias') and not isinstance(m, _BatchNorm):
                if m.bias is not None:
                    if m.bias.sum() == 0:
                        m.bias = None

    elif 'alexnet' in name:
        model = AlexNet(num_classes=output)

        for n, m in model.named_modules():
            if hasattr(m, 'bias') and not isinstance(m, _BatchNorm):
                if m.bias is not None:
                    if m.bias.sum() == 0:
                        m.bias = None
    elif 'resnet' in name:
        if name == 'resnet20':
            model = resnet20(num_classes=output)
        elif name == 'resnet32':
            model = resnet32(num_classes=output)
        else:
            assert False

        for n, m in model.named_modules():
            if hasattr(m, 'bias') and not isinstance(m, _BatchNorm):
                if m.bias is not None:
                    if m.bias.sum() == 0:
                        m.bias = None

    else:
        assert False

    return model
Ejemplo n.º 12
0
    def test_get_matching_parameter(self):
        # pick a model
        model = AlexNet()
        # define how to prune it
        schema = eval(open(currdir / "pruning_schema_alexnet.py").read())
        # prune the model according to the schema
        prune_model(model, schema, seed=0)

        correct = model.features[0].weight_orig
        retrieved = get_matching_parameter("features.0.weight", model)
        assert torch.equal(correct, retrieved)

        correct = model.features[0].weight_orig
        retrieved = get_matching_parameter("features.0.weight_orig", model)
        assert torch.equal(correct, retrieved)

        correct = model.features[3].bias
        retrieved = get_matching_parameter("features.3.bias_orig", model)
        assert torch.equal(correct, retrieved)

        with pytest.raises(KeyError):
            get_matching_parameter("blah", model)
Ejemplo n.º 13
0
def train_base(args):
    torch.manual_seed(args.seed)

    # VGG also works :-)
    #net = vgg19(num_classes=10)
    net = AlexNet(num_classes=10)

    trainset = cifar_trainset(args.local_rank)

    engine, _, dataloader, __ = deepspeed.initialize(
        args=args,
        model=net,
        model_parameters=[p for p in net.parameters() if p.requires_grad],
        training_data=trainset)

    dataloader = RepeatingLoader(dataloader)
    data_iter = iter(dataloader)

    rank = dist.get_rank()
    gas = engine.gradient_accumulation_steps()

    criterion = torch.nn.CrossEntropyLoss()

    total_steps = args.steps * engine.gradient_accumulation_steps()
    step = 0
    for micro_step in range(total_steps):
        batch = next(data_iter)
        inputs = batch[0].to(engine.device)
        labels = batch[1].to(engine.device)

        outputs = engine(inputs)
        loss = criterion(outputs, labels)
        engine.backward(loss)
        engine.step()

        if micro_step % engine.gradient_accumulation_steps() == 0:
            step += 1
            if rank == 0 and (step % 10 == 0):
                print(f'step: {step:3d} / {args.steps:3d} loss: {loss}')
Ejemplo n.º 14
0
                  step_size_down=20,
                  mode='triangular'):
        scheduler = CyclicLR(self.optimizer,
                             base_lr=base_lr,
                             max_lr=max_lr,
                             step_size_up=step_size_up,
                             step_size_down=step_size_down,
                             mode=mode)
        return scheduler

    def adjust(self, base_lr, type):
        pass


if __name__ == '__main__':
    net = AlexNet(num_classes=2)
    optimizer = SGD(net.parameters(), lr=0.0003)
    adj = AdjustLr(optimizer)
    sch1 = adj.LambdaLR_(milestone=5, gamma=0.92)
    epoches = 40
    plt.figure()
    x1 = list(range(epoches))
    y1 = list()
    lr = optimizer.param_groups[0]['lr']
    for epoch in range(epoches):
        optimizer.step()
        sch1.step(epoch)

        a = sch1.get_lr()
        print(epoch, a)
        y1.append(a)
Ejemplo n.º 15
0
def train():
    torch.multiprocessing.freeze_support()

    traindir = os.path.join('./200508_cat_classification/dogs-vs-cats',
                            'train')  #경로를 병합함 .
    testdir = os.path.join('./200508_cat_classification/dogs-vs-cats', 'test')

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

    train_loader = datautil.DataLoader(TrainImageFolder(
        traindir,
        transforms.Compose([
            transforms.RandomResizedCrop(224),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            normalize,
        ])),
                                       batch_size=4,
                                       shuffle=True,
                                       num_workers=4,
                                       pin_memory=True)

    test_loader = datautil.DataLoader(TestImageFolder(
        testdir,
        transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            normalize,
        ])),
                                      batch_size=1,
                                      shuffle=False,
                                      num_workers=1,
                                      pin_memory=False)

    net = AlexNet()
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    net = net.to(device)
    load_model(net, './alexnet.pth')

    if torch.cuda.device_count() > 1:
        print("Let's use", torch.cuda.device_count(), "GPUs!")

    net = nn.DataParallel(net)

    if torch.cuda.is_available():
        net.cuda()

    import torch.optim as optim

    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD(net.parameters(), lr=0.004)

    for epoch in range(3):
        running_loss = 0.0
        acc = 0.
        correct = 0
        for i, data in enumerate(train_loader, 0):
            inputs, labels = data
            inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())

            optimizer.zero_grad()
            outputs = net(inputs)
            #print(outputs)
            loss = criterion(outputs, labels)

            loss.backward()
            optimizer.step()

            running_loss += loss.data.item()

            prediction = torch.max(outputs.data, 1)[1]

            correct += prediction.eq(
                labels.data.view_as(prediction)).cpu().sum()

            if i % 2000 == 1999:
                total = (i + 1) * 4
                print(
                    f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 2000:.6f} acc : {correct} / {total}'
                )
                running_loss = 0.0

    print('Finished Training')

    save_model(net, './')
Ejemplo n.º 16
0
            nn.Dropout(),
            nn.Linear(256 * 6 * 6, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Linear(4096, num_classes),
        )

    def forward(self, x):
        x = self.features(x)
        x = x.view(x.size(0), 256 * 6 * 6)
        return self.classifier(x)


model = AlexNet(10).to(device)
h1 = hl.build_graph(model, torch.zeros(64, 3, 224, 224).to(device))
h1.save('images/alexnet.png', format='png')

# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)


def update_lr(optimizer, lr):
    """For updating learning rate."""

    for param_group in optimizer.param_groups:
        param_group['lr'] = lr

Ejemplo n.º 17
0
import torch
import torch.optim as optim
from torch.optim import lr_scheduler
from torchvision.models import AlexNet
import matplotlib.pyplot as plt

model = AlexNet(num_classes=2)
optimizer = optim.SGD(params=model.parameters(), lr=0.01)


def f_step():
    scheduler = lr_scheduler.StepLR(optimizer, step_size=2, gamma=0.98)
    x = list(range(100))
    y = []
    for epoch in range(100):
        scheduler.step()
        lr = scheduler.get_lr()[0]
        print(epoch, lr)
        y.append(lr)

    return x, y


def f_multistep():
    scheduler = lr_scheduler.MultiStepLR(optimizer, [30, 80], gamma=0.98)
    x = list(range(100))
    y = []
    for epoch in range(100):
        scheduler.step()
        lr = scheduler.get_lr()[0]
        print(epoch, lr)
Ejemplo n.º 18
0
def test_resnet50_ptflops():
   net = AlexNet()
   flops, params = get_model_complexity_info(net, (3, 224, 224), as_strings=True, print_per_layer_stat=True)
   print('Flops:  ' + flops)
   print('Params: ' + params)
Ejemplo n.º 19
0
def test_resnet50_thop():
    model = AlexNet()
    input = torch.randn(1,3,224,224)
    flops, params = profile(model, inputs=(input,))
    flops, params = clever_format([flops, params], "%.3f")
    print("flops: ", flops, "params: ", params)
Ejemplo n.º 20
0
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
Ejemplo n.º 21
0
 def __init__(self, NUM_CLASSES):
     super(AlexNet1, self).__init__()
     self.model_name = "AlexNet1"
     self.model = AlexNet(num_classes=NUM_CLASSES)
Ejemplo n.º 22
0
import sys
import torch

sys.path.append('/home/wanghongwei/WorkSpace/source/tools/pytorchviz/')
from torchvision.models import AlexNet
from torchviz import make_dot_from_trace, make_dot

model = AlexNet()
x = torch.randn(1, 3, 227, 227).requires_grad_(True)

with torch.onnx.set_training(model, False):
    trace, _ = torch.jit.get_trace_graph(model, args=(x, ))
make_dot_from_trace(trace)

# y = model(x)
# make_dot(y, params=dict(list(model.named_parameters()) + [('x', x)]))