コード例 #1
0
ファイル: deep_ensemble.py プロジェクト: yw981/OD-test
    def get_base_config(self, dataset):
        print("Preparing training D1 for %s" %
              (dataset.parent_dataset.__class__.__name__))

        all_loader = DataLoader(dataset,
                                batch_size=self.args.batch_size,
                                num_workers=self.args.workers,
                                pin_memory=True)

        # Set up the criterion
        criterion = nn.NLLLoss().cuda()

        # Set up the model
        model_class = Global.get_ref_classifier(
            dataset.name)[self.default_model]
        self.add_identifier = model_class.__name__

        # We must create 5 instances of this class.
        from models import get_ref_model_path
        all_models = []
        for mid in range(5):
            model = model_class()
            model = DeepEnsembleWrapper(model)
            model = model.to(self.args.device)
            h_path = get_ref_model_path(self.args,
                                        model_class.__name__,
                                        dataset.name,
                                        suffix_str='DE.%d' % mid)
            best_h_path = path.join(h_path, 'model.best.pth')
            if not path.isfile(best_h_path):
                raise NotImplementedError(
                    "Please use setup_model to pretrain the networks first! Can't find %s"
                    % best_h_path)
            else:
                print(colored('Loading H1 model from %s' % best_h_path, 'red'))
                model.load_state_dict(torch.load(best_h_path))
                model.eval()
            all_models.append(model)
        master_model = DeepEnsembleMasterWrapper(all_models)

        # Set up the config
        config = IterativeTrainerConfig()

        config.name = '%s-CLS' % (self.args.D1)
        config.phases = {
            'all': {
                'dataset': all_loader,
                'backward': False
            },
        }
        config.criterion = criterion
        config.classification = True
        config.cast_float_label = False
        config.stochastic_gradient = True
        config.model = master_model
        config.optim = None
        config.autoencoder_target = False
        config.visualize = False
        config.logger = Logger()
        return config
コード例 #2
0
ファイル: ae_setup.py プロジェクト: yw981/OD-test
def get_ae_config(args, model, dataset, BCE_Loss):
    print("Preparing training D1 for %s"%(dataset.name))

    # 80%, 20% for local train+test
    train_ds, valid_ds = dataset.split_dataset(0.8)

    if dataset.name in Global.mirror_augment:
        print(colored("Mirror augmenting %s"%dataset.name, 'green'))
        new_train_ds = train_ds + MirroredDataset(train_ds)
        train_ds = new_train_ds

    # Initialize the multi-threaded loaders.
    train_loader = DataLoader(train_ds, batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True)
    valid_loader = DataLoader(valid_ds, batch_size=args.batch_size, num_workers=args.workers, pin_memory=True)
    all_loader   = DataLoader(dataset,  batch_size=args.batch_size, num_workers=args.workers, pin_memory=True)

    # Set up the model
    model = model.to(args.device)

    # Set up the criterion
    criterion = None
    if BCE_Loss:
        criterion = nn.BCEWithLogitsLoss().to(args.device)
    else:
        criterion = nn.MSELoss().to(args.device)
        model.default_sigmoid = True

    # Set up the config
    config = IterativeTrainerConfig()

    config.name = 'autoencoder_%s_%s'%(dataset.name, model.preferred_name())

    config.train_loader = train_loader
    config.valid_loader = valid_loader
    config.phases = {
                    'train':   {'dataset' : train_loader,  'backward': True},
                    'test':    {'dataset' : valid_loader,  'backward': False},
                    'all':     {'dataset' : all_loader,    'backward': False},                        
                    }
    config.criterion = criterion
    config.classification = False
    config.cast_float_label = False
    config.autoencoder_target = True
    config.stochastic_gradient = True
    config.visualize = not args.no_visualize
    config.sigmoid_viz = BCE_Loss
    config.model = model
    config.logger = Logger()

    config.optim = optim.Adam(model.parameters(), lr=1e-3)
    config.scheduler = optim.lr_scheduler.ReduceLROnPlateau(config.optim, patience=10, threshold=1e-3, min_lr=1e-6, factor=0.1, verbose=True)
    config.max_epoch = 120
    
    if hasattr(model, 'train_config'):
        model_train_config = model.train_config()
        for key, value in model_train_config.iteritems():
            print('Overriding config.%s'%key)
            config.__setattr__(key, value)

    return config
コード例 #3
0
    def get_H_config(self, dataset, will_train=True):
        print("Preparing training D1+D2 (H)")
        print("Mixture size: %s"%colored('%d'%len(dataset), 'green'))

        # 80%, 20% for local train+test
        train_ds, valid_ds = dataset.split_dataset(0.8)

        if self.args.D1 in Global.mirror_augment:
            print(colored("Mirror augmenting %s"%self.args.D1, 'green'))
            new_train_ds = train_ds + MirroredDataset(train_ds)
            train_ds = new_train_ds

        # Initialize the multi-threaded loaders.
        train_loader = DataLoader(train_ds, batch_size=self.args.batch_size, shuffle=True, num_workers=self.args.workers, pin_memory=True)
        valid_loader = DataLoader(valid_ds, batch_size=self.args.batch_size, shuffle=True, num_workers=self.args.workers, pin_memory=True)

        # To make the threshold learning, actually threshold learning
        # the margin must be set to 0.
        criterion = SVMLoss(margin=0.0).to(self.args.device)

        # Set up the model
        model = MCDropoutModelWrapper(self.base_model).to(self.args.device)

        old_valid_loader = valid_loader

        # By definition, this approach is uncacheable :(

        # Set up the config
        config = IterativeTrainerConfig()

        base_model_name = self.base_model.__class__.__name__
        if hasattr(self.base_model, 'preferred_name'):
            base_model_name = self.base_model.preferred_name()

        config.name = '_%s[%s](%s-%s)'%(self.__class__.__name__, base_model_name, self.args.D1, self.args.D2)
        config.train_loader = train_loader
        config.valid_loader = valid_loader
        config.phases = {
                        'train':   {'dataset' : train_loader,  'backward': True},
                        'test':    {'dataset' : valid_loader,  'backward': False},
                        'testU':   {'dataset' : old_valid_loader, 'backward': False},                                                
                        }
        config.criterion = criterion
        config.classification = True
        config.cast_float_label = True
        config.stochastic_gradient = True
        config.visualize = not self.args.no_visualize  
        config.model = model
        config.optim = optim.Adagrad(model.H.parameters(), lr=1e-1, weight_decay=0)
        config.scheduler = optim.lr_scheduler.ReduceLROnPlateau(config.optim, patience=10, threshold=1e-1, min_lr=1e-8, factor=0.1, verbose=True)
        h_path = path.join(self.args.experiment_path, '%s' % (self.__class__.__name__),
                           '%d' % (self.default_model),
                           '%s-%s.pth' % (self.args.D1, self.args.D2))
        h_parent = path.dirname(h_path)
        config.logger = Logger(h_parent)
        config.max_epoch = 100

        return config
コード例 #4
0
ファイル: pixelcnn.py プロジェクト: yw981/OD-test
    def get_base_config(self, dataset):
        print("Preparing training D1 for %s" %
              (dataset.parent_dataset.__class__.__name__))

        all_loader = DataLoader(dataset,
                                batch_size=self.args.batch_size,
                                num_workers=self.args.workers,
                                pin_memory=True)

        # Set up the model
        model = Global.get_ref_pixelcnn(dataset.name)[self.default_model]().to(
            self.args.device)
        self.add_identifier = model.__class__.__name__

        # Load the snapshot
        from models import get_ref_model_path
        h_path = get_ref_model_path(self.args,
                                    model.__class__.__name__,
                                    dataset.name,
                                    suffix_str=model.netid)
        best_h_path = path.join(h_path, 'model.best.pth')
        if not path.isfile(best_h_path):
            raise NotImplementedError(
                "Please use setup_model to pretrain the networks first! Can't find %s"
                % best_h_path)
        else:
            print(colored('Loading H1 model from %s' % best_h_path, 'red'))
            model.load_state_dict(torch.load(best_h_path))
            model.eval()

        # Set up the criterion
        criterion = PCNN_Loss(one_d=(model.input_channels == 1)).to(
            self.args.device)

        # Set up the config
        config = IterativeTrainerConfig()

        config.name = '%s-pcnn' % (self.args.D1)
        config.phases = {
            'all': {
                'dataset': all_loader,
                'backward': False
            },
        }
        config.criterion = criterion
        config.classification = False
        config.cast_float_label = False
        config.autoencoder_target = True
        config.stochastic_gradient = True
        config.model = model
        config.optim = None
        config.visualize = False
        config.logger = Logger()
        return config
コード例 #5
0
    def get_base_config(self, dataset):
        print("Preparing training D1 for %s" % (dataset.name))

        # Initialize the multi-threaded loaders.
        all_loader = DataLoader(dataset, batch_size=self.args.batch_size, num_workers=self.args.workers,
                                pin_memory=True)

        # Set up the model
        model = Global.get_ref_ali(dataset.name)[0]().to(self.args.device)

        # Set up the criterion
        criterion = None
        if self.default_model == 0:
            criterion = nn.BCEWithLogitsLoss().to(self.args.device)
        else:
            criterion = nn.MSELoss().to(self.args.device)
            model.default_sigmoid = True

        # Set up the config
        config = IterativeTrainerConfig()

        config.name = '%s-ALIAE1' % (self.args.D1)
        config.phases = {
            'all': {'dataset': all_loader, 'backward': False},
        }
        config.criterion = criterion
        config.classification = False
        config.cast_float_label = False
        config.autoencoder_target = True
        config.stochastic_gradient = True
        config.visualize = not self.args.no_visualize
        config.sigmoid_viz = self.default_model == 0
        config.model = model
        config.optim = None

        h_path = path.join(self.args.experiment_path, '%s' % (self.__class__.__name__),
                           '%d' % (self.default_model),
                           '%s-%s.pth' % (self.args.D1, self.args.D2))
        h_parent = path.dirname(h_path)

        config.logger = Logger(h_parent)

        return config
コード例 #6
0
    def get_base_config(self, dataset):
        print("Preparing training D1 for %s" % (dataset.name))

        all_loader = DataLoader(dataset,
                                batch_size=self.args.batch_size,
                                num_workers=self.args.workers,
                                pin_memory=True)

        # Set up the criterion
        criterion = nn.NLLLoss().to(self.args.device)

        # Set up the model
        import global_vars as Global
        model = Global.get_ref_classifier(
            dataset.name)[self.default_model]().to(self.args.device)
        self.add_identifier = model.__class__.__name__
        if hasattr(model, 'preferred_name'):
            self.add_identifier = model.preferred_name()

        # Set up the config
        config = IterativeTrainerConfig()

        config.name = '%s-CLS' % (self.args.D1)
        config.phases = {
            'all': {
                'dataset': all_loader,
                'backward': False
            },
        }
        config.criterion = criterion
        config.classification = True
        config.cast_float_label = False
        config.stochastic_gradient = True
        config.model = model
        config.optim = None
        config.autoencoder_target = False
        config.visualize = False
        config.logger = Logger()
        return config
コード例 #7
0
def get_classifier_config(args, model, dataset, mid=0):
    print("Preparing training D1 for %s" % (dataset.name))

    # 80%, 20% for local train+test
    train_ds, valid_ds = dataset.split_dataset(0.8)

    if dataset.name in Global.mirror_augment:
        print(colored("Mirror augmenting %s" % dataset.name, 'green'))
        new_train_ds = train_ds + MirroredDataset(train_ds)
        train_ds = new_train_ds

    # Initialize the multi-threaded loaders.
    train_loader = DataLoader(train_ds,
                              batch_size=args.batch_size / 2,
                              shuffle=True,
                              num_workers=args.workers,
                              pin_memory=True)
    valid_loader = DataLoader(valid_ds,
                              batch_size=args.batch_size,
                              num_workers=args.workers,
                              pin_memory=True)
    all_loader = DataLoader(dataset,
                            batch_size=args.batch_size,
                            num_workers=args.workers,
                            pin_memory=True)

    import methods.deep_ensemble as DE
    # Set up the model
    model = DE.DeepEnsembleWrapper(model).to(args.device)

    # Set up the criterion
    criterion = DE.DeepEnsembleLoss(ensemble_network=model).to(args.device)

    # Set up the config
    config = IterativeTrainerConfig()

    base_model_name = model.__class__.__name__
    if hasattr(model, 'preferred_name'):
        base_model_name = model.preferred_name()

    config.name = 'DeepEnsemble_%s_%s(%d)' % (dataset.name, base_model_name,
                                              mid)

    config.train_loader = train_loader
    config.valid_loader = valid_loader
    config.phases = {
        'train': {
            'dataset': train_loader,
            'backward': True
        },
        'test': {
            'dataset': valid_loader,
            'backward': False
        },
        'all': {
            'dataset': all_loader,
            'backward': False
        },
    }
    config.criterion = criterion
    config.classification = True
    config.stochastic_gradient = True
    config.visualize = not args.no_visualize
    config.model = model
    config.logger = Logger()

    config.optim = optim.Adam(model.parameters(), lr=1e-3)
    config.scheduler = optim.lr_scheduler.ReduceLROnPlateau(config.optim,
                                                            patience=10,
                                                            threshold=1e-2,
                                                            min_lr=1e-6,
                                                            factor=0.1,
                                                            verbose=True)
    config.max_epoch = 120

    if hasattr(model.model, 'train_config'):
        model_train_config = model.model.train_config()
        for key, value in model_train_config.iteritems():
            print('Overriding config.%s' % key)
            config.__setattr__(key, value)

    return config
コード例 #8
0
ファイル: deep_ensemble.py プロジェクト: yw981/OD-test
    def get_H_config(self, dataset, will_train=True):
        print("Preparing training D1+D2 (H)")
        print("Mixture size: %s" % colored('%d' % len(dataset), 'green'))

        # 80%, 20% for local train+test
        train_ds, valid_ds = dataset.split_dataset(0.8)

        if self.args.D1 in Global.mirror_augment:
            print(colored("Mirror augmenting %s" % self.args.D1, 'green'))
            new_train_ds = train_ds + MirroredDataset(train_ds)
            train_ds = new_train_ds

        # Initialize the multi-threaded loaders.
        train_loader = DataLoader(train_ds,
                                  batch_size=self.args.batch_size,
                                  shuffle=True,
                                  num_workers=self.args.workers,
                                  pin_memory=True)
        valid_loader = DataLoader(valid_ds,
                                  batch_size=self.args.batch_size,
                                  shuffle=True,
                                  num_workers=self.args.workers,
                                  pin_memory=True)

        # To make the threshold learning, actually threshold learning
        # the margin must be set to 0.
        criterion = SVMLoss(margin=0.0).to(self.args.device)

        # Set up the model
        model = DeepEnsembleModelWrapper(self.base_model).to(self.args.device)

        old_valid_loader = valid_loader
        if will_train:
            # cache the subnetwork for faster optimization.
            from methods import get_cached
            from torch.utils.data.dataset import TensorDataset

            trainX, trainY = get_cached(model, train_loader, self.args.device)
            validX, validY = get_cached(model, valid_loader, self.args.device)

            new_train_ds = TensorDataset(trainX, trainY)
            new_valid_ds = TensorDataset(validX, validY)

            # Initialize the new multi-threaded loaders.
            train_loader = DataLoader(new_train_ds,
                                      batch_size=2048,
                                      shuffle=True,
                                      num_workers=0,
                                      pin_memory=False)
            valid_loader = DataLoader(new_valid_ds,
                                      batch_size=2048,
                                      shuffle=True,
                                      num_workers=0,
                                      pin_memory=False)

            # Set model to direct evaluation (for cached data)
            model.set_eval_direct(True)

        # Set up the config
        config = IterativeTrainerConfig()

        base_model_name = self.base_model.preferred_name()

        config.name = '_%s[%s](%s->%s)' % (self.__class__.__name__,
                                           base_model_name, self.args.D1,
                                           self.args.D2)
        config.train_loader = train_loader
        config.valid_loader = valid_loader
        config.phases = {
            'train': {
                'dataset': train_loader,
                'backward': True
            },
            'test': {
                'dataset': valid_loader,
                'backward': False
            },
            'testU': {
                'dataset': old_valid_loader,
                'backward': False
            },
        }
        config.criterion = criterion
        config.classification = True
        config.cast_float_label = True
        config.stochastic_gradient = True
        config.visualize = not self.args.no_visualize
        config.model = model
        config.optim = optim.Adagrad(model.H.parameters(),
                                     lr=1e-1,
                                     weight_decay=0)
        config.scheduler = optim.lr_scheduler.ReduceLROnPlateau(config.optim,
                                                                patience=10,
                                                                threshold=1e-1,
                                                                min_lr=1e-8,
                                                                factor=0.1,
                                                                verbose=True)
        config.logger = Logger()
        config.max_epoch = 100

        return config
コード例 #9
0
    def get_H_config(self,
                     train_ds,
                     valid_ds,
                     will_train=True,
                     epsilon=0.0012,
                     temperature=1000):
        print("Preparing training D1+D2 (H)")

        # Initialize the multi-threaded loaders.
        train_loader = DataLoader(train_ds,
                                  batch_size=self.args.batch_size,
                                  shuffle=True,
                                  num_workers=self.args.workers,
                                  pin_memory=True)
        valid_loader = DataLoader(valid_ds,
                                  batch_size=self.args.batch_size,
                                  shuffle=True,
                                  num_workers=self.args.workers,
                                  pin_memory=True)

        # Set up the criterion
        # To make the threshold learning, actually threshold learning
        # the margin must be set to 0.
        criterion = SVMLoss(margin=0.0).to(self.args.device)

        # Set up the model
        model = ODINModelWrapper(self.base_model,
                                 epsilon=epsilon,
                                 temperature=temperature).to(self.args.device)

        old_valid_loader = valid_loader
        if will_train:
            # cache the subnetwork for faster optimization.
            from methods import get_cached
            from torch.utils.data.dataset import TensorDataset

            trainX, trainY = get_cached(model, train_loader, self.args.device)
            validX, validY = get_cached(model, valid_loader, self.args.device)

            new_train_ds = TensorDataset(trainX, trainY)
            x_center = trainX[trainY == 0].mean()
            y_center = trainX[trainY == 1].mean()
            init_value = (x_center + y_center) / 2
            if model.H.threshold.device.type == "cpu":
                model.H.threshold.data = init_value.view((1, ))
            else:
                model.H.threshold.data = init_value.cuda().view((1, ))
            #model.H.threshold.fill_(init_value)
            print("Initializing threshold to %.2f" % (init_value.item()))

            new_valid_ds = TensorDataset(validX, validY)

            # Initialize the new multi-threaded loaders.
            train_loader = DataLoader(new_train_ds,
                                      batch_size=2048,
                                      shuffle=True,
                                      num_workers=0,
                                      pin_memory=False)
            valid_loader = DataLoader(new_valid_ds,
                                      batch_size=2048,
                                      shuffle=True,
                                      num_workers=0,
                                      pin_memory=False)

            # Set model to direct evaluation (for cached data)
            model.set_eval_direct(True)

        # Set up the config
        config = IterativeTrainerConfig()

        base_model_name = self.base_model.__class__.__name__
        if hasattr(self.base_model, 'preferred_name'):
            base_model_name = self.base_model.preferred_name()

        config.name = '_%s[%s](%s-%s)' % (self.__class__.__name__,
                                          base_model_name, self.args.D1,
                                          self.args.D2)
        config.train_loader = train_loader
        config.valid_loader = valid_loader
        config.phases = {
            'train': {
                'dataset': train_loader,
                'backward': True
            },
            'test': {
                'dataset': valid_loader,
                'backward': False
            },
            'testU': {
                'dataset': old_valid_loader,
                'backward': False
            },
        }
        config.criterion = criterion
        config.classification = True
        config.cast_float_label = True
        config.stochastic_gradient = True
        config.visualize = not self.args.no_visualize
        config.model = model
        config.optim = optim.Adagrad(model.H.parameters(),
                                     lr=1e-2,
                                     weight_decay=0)
        config.scheduler = optim.lr_scheduler.ReduceLROnPlateau(config.optim,
                                                                patience=5,
                                                                threshold=1e-1,
                                                                min_lr=1e-8,
                                                                factor=0.1,
                                                                verbose=True)
        h_path = path.join(self.args.experiment_path,
                           '%s' % (self.__class__.__name__),
                           '%d' % (self.default_model),
                           '%s-%s.pth' % (self.args.D1, self.args.D2))
        h_parent = path.dirname(h_path)
        config.logger = Logger(h_parent)
        config.max_epoch = 30

        return config
コード例 #10
0
    def get_H_config(self,
                     train_ds,
                     valid_ds,
                     will_train=True,
                     epsilon=0.0012):
        print("Preparing training D1+D2 (H)")

        # Initialize the multi-threaded loaders.
        train_loader = DataLoader(train_ds,
                                  batch_size=self.args.batch_size,
                                  shuffle=True,
                                  num_workers=self.args.workers,
                                  pin_memory=True)
        valid_loader = DataLoader(valid_ds,
                                  batch_size=self.args.batch_size,
                                  shuffle=True,
                                  num_workers=self.args.workers,
                                  pin_memory=True)

        # Set up the criterion
        criterion = nn.BCEWithLogitsLoss().cuda()
        # Set up the model
        model = MahaODModelWrapper(self.base_model,
                                   epsilon=epsilon,
                                   num_class=2,
                                   num_layers=1).to(self.args.device)

        old_valid_loader = valid_loader
        if will_train:
            # cache the subnetwork for faster optimization.
            from methods import get_cached
            from torch.utils.data.dataset import TensorDataset

            trainX, trainY = get_cached(model, train_loader, self.args.device)
            validX, validY = get_cached(model, valid_loader, self.args.device)

            new_train_ds = TensorDataset(trainX, trainY)
            new_valid_ds = TensorDataset(validX, validY)

            # Initialize the new multi-threaded loaders.
            train_loader = DataLoader(new_train_ds,
                                      batch_size=2048,
                                      shuffle=True,
                                      num_workers=0,
                                      pin_memory=False)
            valid_loader = DataLoader(new_valid_ds,
                                      batch_size=2048,
                                      shuffle=True,
                                      num_workers=0,
                                      pin_memory=False)

            # Set model to direct evaluation (for cached data)
            model.set_eval_direct(True)

        # Set up the config
        config = IterativeTrainerConfig()

        base_model_name = self.base_model.__class__.__name__
        if hasattr(self.base_model, 'preferred_name'):
            base_model_name = self.base_model.preferred_name()

        config.name = '_%s[%s](%s-%s)' % (self.__class__.__name__,
                                          base_model_name, self.args.D1,
                                          self.args.D2)
        config.train_loader = train_loader
        config.valid_loader = valid_loader
        config.phases = {
            'train': {
                'dataset': train_loader,
                'backward': True
            },
            'test': {
                'dataset': valid_loader,
                'backward': False
            },
            'testU': {
                'dataset': old_valid_loader,
                'backward': False
            },
        }
        config.criterion = criterion
        config.classification = True
        config.cast_float_label = True
        config.stochastic_gradient = True
        config.visualize = not self.args.no_visualize
        config.model = model
        config.optim = optim.Adam(model.H.parameters(), lr=1e-1)
        config.scheduler = optim.lr_scheduler.ReduceLROnPlateau(config.optim,
                                                                patience=5,
                                                                threshold=1e-1,
                                                                min_lr=1e-6,
                                                                factor=0.1,
                                                                verbose=True)
        h_path = path.join(self.args.experiment_path,
                           '%s' % (self.__class__.__name__),
                           '%d' % (self.default_model),
                           '%s-%s.pth' % (self.args.D1, self.args.D2))
        h_parent = path.dirname(h_path)
        config.logger = Logger(h_parent)
        config.max_epoch = 100
        return config
コード例 #11
0
def get_pcnn_config(args, model, home_path, dataset):
    print("Preparing training D1 for %s" % (dataset.name))

    sample_im, _ = dataset[0]
    obs = sample_im.size()
    obs = [int(d) for d in obs]

    # 80%, 20% for local train+test
    train_ds, valid_ds = dataset.split_dataset(0.8)

    if dataset.name in Global.mirror_augment:
        print(colored("Mirror augmenting %s" % dataset.name, 'green'))
        new_train_ds = train_ds + MirroredDataset(train_ds)
        train_ds = new_train_ds

    # Initialize the multi-threaded loaders.
    train_loader = DataLoader(train_ds,
                              batch_size=args.batch_size,
                              shuffle=True,
                              num_workers=args.workers,
                              pin_memory=True)
    valid_loader = DataLoader(valid_ds,
                              batch_size=args.batch_size,
                              num_workers=args.workers,
                              pin_memory=True)
    all_loader = DataLoader(dataset,
                            batch_size=args.batch_size,
                            num_workers=args.workers,
                            pin_memory=True)

    # Set up the model
    model = model.to(args.device)

    # Set up the criterion
    criterion = pcnn_utils.PCNN_Loss(one_d=(model.input_channels == 1))

    # Set up the config
    config = IterativeTrainerConfig()

    config.name = 'PCNN_%s_%s' % (dataset.name, model.preferred_name())

    config.train_loader = train_loader
    config.valid_loader = valid_loader
    config.phases = {
        'train': {
            'dataset': train_loader,
            'backward': True
        },
        'test': {
            'dataset': valid_loader,
            'backward': False
        },
        'all': {
            'dataset': all_loader,
            'backward': False
        },
    }
    config.criterion = criterion
    config.classification = False
    config.cast_float_label = False
    config.autoencoder_target = True
    config.stochastic_gradient = True
    config.visualize = not args.no_visualize
    config.model = model
    config.logger = Logger(home_path)
    config.sampler = lambda x: sample(x.model, 32, obs)

    config.optim = optim.Adam(model.parameters(), lr=1e-3)
    config.scheduler = optim.lr_scheduler.ReduceLROnPlateau(config.optim,
                                                            patience=10,
                                                            threshold=1e-2,
                                                            min_lr=1e-5,
                                                            factor=0.1,
                                                            verbose=True)
    config.max_epoch = 60

    if hasattr(model, 'train_config'):
        model_train_config = model.train_config()
        for key, value in model_train_config.items():
            print('Overriding config.%s' % key)
            config.__setattr__(key, value)

    return config
コード例 #12
0
    def get_H_config(self, dataset, will_train=True):
        print("Preparing training D1+D2 (H)")
        print("Mixture size: %s" % colored('%d' % len(dataset), 'green'))

        # 80%, 20% for local train+test
        train_ds, valid_ds = dataset.split_dataset(0.8)

        if self.args.D1 in Global.mirror_augment:
            print(colored("Mirror augmenting %s" % self.args.D1, 'green'))
            new_train_ds = train_ds + MirroredDataset(train_ds)
            train_ds = new_train_ds

        # Initialize the multi-threaded loaders.
        train_loader = DataLoader(train_ds,
                                  batch_size=self.args.batch_size,
                                  shuffle=True,
                                  num_workers=self.args.workers,
                                  pin_memory=True)
        valid_loader = DataLoader(valid_ds,
                                  batch_size=self.args.batch_size,
                                  shuffle=True,
                                  num_workers=self.args.workers,
                                  pin_memory=True)

        # Set up the criterion
        # margin must be non-zero.
        criterion = SVMLoss(margin=1.0).cuda()

        # Set up the model
        model = OTModelWrapper(self.base_model, self.mav,
                               self.weib_models).to(self.args.device)

        old_valid_loader = valid_loader
        if will_train:
            # cache the subnetwork for faster optimization.
            from methods import get_cached
            from torch.utils.data.dataset import TensorDataset

            trainX, trainY = get_cached(model, train_loader, self.args.device)
            validX, validY = get_cached(model, valid_loader, self.args.device)

            trainX_notnan = trainX[torch.logical_not(
                torch.isnan(trainX)[:, 0]).nonzero().squeeze(1)]
            trainY_notnan = trainY[torch.logical_not(
                torch.isnan(trainX)[:, 0]).nonzero().squeeze(1)]
            validX_notnan = validX[torch.logical_not(
                torch.isnan(validX)[:, 0]).nonzero().squeeze(1)]
            validY_notnan = validY[torch.logical_not(
                torch.isnan(validX)[:, 0]).nonzero().squeeze(1)]
            new_train_ds = TensorDataset(trainX_notnan, trainY_notnan)
            new_valid_ds = TensorDataset(validX_notnan, validY_notnan)

            # Initialize the new multi-threaded loaders.
            train_loader = DataLoader(new_train_ds,
                                      batch_size=2048,
                                      shuffle=True,
                                      num_workers=0,
                                      pin_memory=False)
            valid_loader = DataLoader(new_valid_ds,
                                      batch_size=2048,
                                      shuffle=True,
                                      num_workers=0,
                                      pin_memory=False)

            # Set model to direct evaluation (for cached data)
            model.set_eval_direct(True)

        # Set up the config
        config = IterativeTrainerConfig()

        base_model_name = self.base_model.__class__.__name__
        if hasattr(self.base_model, 'preferred_name'):
            base_model_name = self.base_model.preferred_name()

        config.name = '_%s[%s](%s->%s)' % (self.__class__.__name__,
                                           base_model_name, self.args.D1,
                                           self.args.D2)
        config.train_loader = train_loader
        config.valid_loader = valid_loader
        config.phases = {
            'train': {
                'dataset': train_loader,
                'backward': True
            },
            'test': {
                'dataset': valid_loader,
                'backward': False
            },
            'testU': {
                'dataset': old_valid_loader,
                'backward': False
            },
        }
        config.criterion = criterion
        config.classification = True
        config.cast_float_label = True
        config.stochastic_gradient = True
        config.visualize = not self.args.no_visualize
        config.model = model
        config.optim = optim.SGD(model.H.parameters(),
                                 lr=1e-2,
                                 weight_decay=0.0)  #1.0/len(train_ds))
        config.scheduler = optim.lr_scheduler.ReduceLROnPlateau(config.optim,
                                                                patience=10,
                                                                threshold=1e-1,
                                                                min_lr=1e-8,
                                                                factor=0.1,
                                                                verbose=True)
        h_path = path.join(self.args.experiment_path,
                           '%s' % (self.__class__.__name__),
                           '%d' % (self.default_model),
                           '%s-%s.pth' % (self.args.D1, self.args.D2))
        h_parent = path.dirname(h_path)
        config.logger = Logger(h_parent)
        config.max_epoch = 100

        return config
コード例 #13
0
def get_classifier_config(args, model, dataset, balanced=False):
    print("Preparing training D1 for %s" % (dataset.name))

    # 80%, 20% for local train+test
    train_ds, valid_ds = dataset.split_dataset(0.8)

    if dataset.name in Global.mirror_augment:
        print(colored("Mirror augmenting %s" % dataset.name, 'green'))
        new_train_ds = train_ds + MirroredDataset(train_ds)
        train_ds = new_train_ds

    # Initialize the multi-threaded loaders.
    if balanced:
        y_train = []
        for x, y in train_ds:
            y_train.append(y.numpy())
        y_train = np.array(y_train)
        class_sample_count = np.array(
            [len(np.where(y_train == t)[0]) for t in np.unique(y_train)])
        print(class_sample_count)
        weight = 1. / class_sample_count
        samples_weight = np.array([weight[t] for t in y_train])

        samples_weight = torch.from_numpy(samples_weight)
        sampler = WeightedRandomSampler(
            samples_weight.type('torch.DoubleTensor'), len(samples_weight))
        train_loader = DataLoader(train_ds,
                                  batch_size=args.batch_size,
                                  num_workers=args.workers,
                                  pin_memory=True,
                                  sampler=sampler)

        y_val = []
        for x, y in valid_ds:
            y_val.append(y.numpy())
        y_val = np.array(y_val)
        class_sample_count = np.array(
            [len(np.where(y_val == t)[0]) for t in np.unique(y_val)])
        print(class_sample_count)
        weight = 1. / class_sample_count
        samples_weight = np.array([weight[t] for t in y_val])

        samples_weight = torch.from_numpy(samples_weight)
        sampler = WeightedRandomSampler(
            samples_weight.type('torch.DoubleTensor'), len(samples_weight))
        valid_loader = DataLoader(valid_ds,
                                  batch_size=args.batch_size,
                                  num_workers=args.workers,
                                  pin_memory=True,
                                  sampler=sampler)

    else:
        train_loader = DataLoader(train_ds,
                                  batch_size=args.batch_size,
                                  shuffle=True,
                                  num_workers=args.workers,
                                  pin_memory=True)

        valid_loader = DataLoader(valid_ds,
                                  batch_size=args.batch_size,
                                  num_workers=args.workers,
                                  pin_memory=True)
    all_loader = DataLoader(dataset,
                            batch_size=args.batch_size,
                            num_workers=args.workers,
                            pin_memory=True)

    # Set up the criterion
    criterion = nn.NLLLoss().to(args.device)

    # Set up the model
    model = model.to(args.device)

    # Set up the config
    config = IterativeTrainerConfig()

    config.name = 'classifier_%s_%s' % (dataset.name, model.__class__.__name__)

    config.train_loader = train_loader
    config.valid_loader = valid_loader
    config.phases = {
        'train': {
            'dataset': train_loader,
            'backward': True
        },
        'test': {
            'dataset': valid_loader,
            'backward': False
        },
        'all': {
            'dataset': all_loader,
            'backward': False
        },
    }
    config.criterion = criterion
    config.classification = True
    config.stochastic_gradient = True
    config.visualize = not args.no_visualize
    config.model = model
    home_path = Models.get_ref_model_path(args,
                                          config.model.__class__.__name__,
                                          dataset.name,
                                          model_setup=True,
                                          suffix_str='base0')
    config.logger = Logger(home_path)

    config.optim = optim.Adam(model.parameters(), lr=1e-3)
    config.scheduler = optim.lr_scheduler.ReduceLROnPlateau(config.optim,
                                                            patience=10,
                                                            threshold=1e-2,
                                                            min_lr=1e-6,
                                                            factor=0.1,
                                                            verbose=True)
    config.max_epoch = 120

    if hasattr(model, 'train_config'):
        model_train_config = model.train_config()
        for key, value in model_train_config.items():
            print('Overriding config.%s' % key)
            config.__setattr__(key, value)

    return config
コード例 #14
0
ファイル: binary_classifier.py プロジェクト: yw981/OD-test
    def get_H_config(self, dataset, will_train=True):
        print("Preparing training D1+D2 (H)")
        print("Mixture size: %s" % colored('%d' % len(dataset), 'green'))
        import global_vars as Global

        # 80%, 20% for local train+test
        train_ds, valid_ds = dataset.split_dataset(0.8)

        if self.args.D1 in Global.mirror_augment:
            print(colored("Mirror augmenting %s" % self.args.D1, 'green'))
            new_train_ds = train_ds + MirroredDataset(train_ds)
            train_ds = new_train_ds

        # Initialize the multi-threaded loaders.
        train_loader = DataLoader(train_ds,
                                  batch_size=self.args.batch_size,
                                  shuffle=True,
                                  num_workers=self.args.workers,
                                  pin_memory=True,
                                  drop_last=True)
        valid_loader = DataLoader(valid_ds,
                                  batch_size=self.args.batch_size,
                                  num_workers=self.args.workers,
                                  pin_memory=True)
        all_loader = DataLoader(dataset,
                                batch_size=self.args.batch_size,
                                num_workers=self.args.workers,
                                pin_memory=True)

        # Set up the criterion
        criterion = nn.BCEWithLogitsLoss().cuda()

        # Set up the model
        model = Global.get_ref_classifier(
            self.args.D1)[self.default_model]().to(self.args.device)
        self.add_identifier = model.__class__.__name__
        if hasattr(model, 'preferred_name'):
            self.add_identifier = model.preferred_name()
        model = BinaryModelWrapper(model).to(self.args.device)

        # Set up the config
        config = IterativeTrainerConfig()

        base_model_name = model.__class__.__name__
        if hasattr(model, 'preferred_name'):
            base_model_name = model.preferred_name()

        config.name = '_%s[%s](%s->%s)' % (self.__class__.__name__,
                                           base_model_name, self.args.D1,
                                           self.args.D2)
        config.train_loader = train_loader
        config.valid_loader = valid_loader
        config.phases = {
            'train': {
                'dataset': train_loader,
                'backward': True
            },
            'test': {
                'dataset': valid_loader,
                'backward': False
            },
            'testU': {
                'dataset': all_loader,
                'backward': False
            },
        }
        config.criterion = criterion
        config.classification = True
        config.cast_float_label = True
        config.stochastic_gradient = True
        config.visualize = not self.args.no_visualize
        config.model = model
        config.logger = Logger()
        config.optim = optim.Adam(model.parameters(), lr=1e-3)
        config.scheduler = optim.lr_scheduler.ReduceLROnPlateau(config.optim,
                                                                patience=5,
                                                                threshold=1e-2,
                                                                min_lr=1e-6,
                                                                factor=0.1,
                                                                verbose=True)
        config.max_epoch = 30

        if hasattr(model, 'train_config'):
            model_train_config = model.train_config()
            for key, value in model_train_config.iteritems():
                print('Overriding config.%s' % key)
                config.__setattr__(key, value)

        return config