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
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
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
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
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