Beispiel #1
0
    def testWideResNet(self):
        resolutions = [
            [3, 32, 32],
        ]
        depths = [
            28,
            40,
        ]
        widths = [
            10,
            20,
        ]
        normalizations = [True, False]

        classes = 10
        batch_size = 100
        for resolution in resolutions:
            for depth in depths:
                for width in widths:
                    for normalization in normalizations:
                        model = models.WideResNet(classes,
                                                  resolution,
                                                  clamp=True,
                                                  depth=depth,
                                                  width=width,
                                                  normalization=normalization)
                        output = model(
                            torch.autograd.Variable(
                                torch.zeros([batch_size] + resolution)))
                        self.assertEqual(output.size()[0], batch_size)
                        self.assertEqual(output.size()[1], classes)
def test(in_dataset, out_dataset, wide, epsilon, temperature):

    testsetout = torchvision.datasets.ImageFolder(os.path.expanduser(
        "./data/{}".format(out_dataset)),
                                                  transform=transform)
    testloaderOut = torch.utils.data.DataLoader(testsetout,
                                                batch_size=100,
                                                shuffle=False,
                                                num_workers=2)

    if in_dataset == "cifar100":
        testset = torchvision.datasets.CIFAR100(root='./data',
                                                train=False,
                                                download=True,
                                                transform=transform)
        testloaderIn = torch.utils.data.DataLoader(testset,
                                                   batch_size=100,
                                                   shuffle=False,
                                                   num_workers=2)

    elif in_dataset == "cifar10":
        testset = torchvision.datasets.CIFAR10(root='./data',
                                               train=False,
                                               download=True,
                                               transform=transform)
        testloaderIn = torch.utils.data.DataLoader(testset,
                                                   batch_size=100,
                                                   shuffle=False,
                                                   num_workers=2)

    for fold in range(1, 6):
        print(f"Processing fold {fold}")

        nclasses = int(in_dataset[5:])
        if wide:
            net = models.WideResNet(int(nclasses * 4 / 5))
            ck = torch.load(
                f"./checkpoints/{in_dataset}_fold_{fold}_wide_checkpoint/model_best.pth.tar"
            )
        else:
            net = models.DenseNet(int(nclasses * 4 / 5))
            ck = torch.load(
                f"./checkpoints/{in_dataset}_fold_{fold}_dense_checkpoint/model_best.pth.tar"
            )

        net.load_state_dict(ck['state_dict'])

        net.cuda()
        net.eval()

        d.testData(net, criterion, testloaderIn, testloaderOut, in_dataset,
                   out_dataset, epsilon, temperature, fold)

    m.test(in_dataset, out_dataset, plot=True)
Beispiel #3
0
 def set_model(self):
     if args.wide:
         self.g = models.WideResNet().cuda()
         self.c1 = models.Classifier(self.g.nChannels,
                                     self.args.num_classes).cuda()
         self.c2 = models.Classifier(self.g.nChannels,
                                     self.args.num_classes).cuda()
     else:
         self.g = models.DenseNet().cuda()
         self.c1 = models.Classifier(self.g.in_planes,
                                     self.args.num_classes).cuda()
         self.c2 = models.Classifier(self.g.in_planes,
                                     self.args.num_classes).cuda()
 def set_model(self):
     if args.wide:
         self.model = models.WideResNet(int(args.num_classes * 4 / 5),
                                        dropRate=0.3).cuda()
     else:
         self.model = models.DenseNet(int(args.num_classes * 4 / 5)).cuda()
Beispiel #5
0
                cfg = [300, 100]
                for i in range(len(cfg)):
                    cfg[i] = round(cfg[i] * (1 - args.pruning_ratio))
                temp_cfg = cfg
            pass

    # generate the model
    if args.arch == 'VGG':
        model = models.VGG(num_classes, cfg=cfg)
    elif args.arch == 'LeNet_300_100':
        model = models.LeNet_300_100(bias_flag=True, cfg=cfg)
    elif args.arch == 'ResNet':
        model = models.ResNet(int(args.depth_wide), num_classes, cfg=cfg)
    elif args.arch == 'WideResNet':
        model = models.WideResNet(args.depth_wide[0],
                                  num_classes,
                                  widen_factor=args.depth_wide[1],
                                  cfg=cfg)
    else:
        pass

    if args.cuda:
        model.cuda()

    # pretrain
    best_acc = 0.0
    best_epoch = 0
    if args.pretrained:
        pretrained_model = torch.load(args.pretrained)
        best_epoch = 0
        if args.model_type == 'original':
            best_acc = pretrained_model['acc']