def main(self): # initialize random seed torch.manual_seed(self.config.SEED) torch.cuda.manual_seed_all(self.config.SEED) np.random.seed(self.config.SEED) random.seed(self.config.SEED) # build the model and initialize model = EdgeConnect(self.config) model.load() # model training if self.config.MODE == 1: self.config.print() print('\nstart training...\n') model.train() # model test elif self.config.MODE == 2: self.config.print() print('\nstart testing...\n') model.test() # eval mode else: print('\nstart eval...\n') model.eval()
def main(mode=None): r"""starts the model Args: mode (int): 1: train, 2: test, 3: eval, reads from config file if not specified """ config = load_config(mode) # cuda visble devices os.environ['CUDA_VISIBLE_DEVICES'] = ','.join(str(e) for e in config.GPU) # init device if torch.cuda.is_available(): config.DEVICE = torch.device("cuda") torch.backends.cudnn.benchmark = True # cudnn auto-tuner else: config.DEVICE = torch.device("cpu") config.DEVICE = torch.device("cpu") # set cv2 running threads to 1 (prevents deadlocks with pytorch dataloader) cv2.setNumThreads(0) # initialize random seed # initialize random seed torch.manual_seed(config.SEED) torch.cuda.manual_seed_all(config.SEED) np.random.seed(config.SEED) random.seed(config.SEED) # build the model and initialize model = EdgeConnect(config) model.load() # model training if config.MODE == 1: config.print() print('\nstart training...\n') model.train() # model test elif config.MODE == 2: print('\nstart testing...\n') model.test() # eval mode else: print('\nstart eval...\n') model.eval()
def main(mode=None): # ---------------------------------- # starts the model # mode(int): 1 train, 2 test, 3 eval # ---------------------------------- config = load_config(mode) # cuda visible devices os.environ['CUDA_VISIBLE_DEVICES'] = ','.join(str(_) for _ in config.GPU) # initialize device if torch.cuda.is_available(): config.DEVICE = torch.device('cuda') torch.backends.cudnn.benchmark = True # cudnn auto-tuner else: config.DEVICE = torch.device('cpu') # set cv2 running threads to 1 (prevents deadlocks with PyTorch dataloader) cv2.setNumThreads(0) # initialize random seed torch.manual_seed(config.SEED) torch.cuda.manual_seed_all(config.SEED) np.random.seed(config.SEED) random.seed(config.SEED) # build model and initialize model = EdgeConnect(config) model.load() # model training if config.MODE == 1: config.print() print('Start training...') model.train() # model test elif config.MODE == 2: print('Start testing...') model.test() # model eval else: print('Start eval...') model.eval()
def main(mode=None): r"""starts the model """ config = load_config(mode) # cuda visble devices os.environ['CUDA_VISIBLE_DEVICES'] = ','.join(str(e) for e in config.GPU) # init device if torch.cuda.is_available(): config.DEVICE = torch.device("cuda") torch.backends.cudnn.benchmark = True # cudnn auto-tuner else: config.DEVICE = torch.device("cpu") # set cv2 running threads to 1 (prevents deadlocks with pytorch dataloader) cv2.setNumThreads(0) # initialize random seed torch.manual_seed(config.SEED) torch.cuda.manual_seed_all(config.SEED) np.random.seed(config.SEED) random.seed(config.SEED) # build the model and initialize model = EdgeConnect(config) model.load() # model test print('\nstart testing...\n') model.test()
def main(mode=None): if not os.path.exists(a_path): os.makedirs(a_path) copyfile(sys.argv[0], a_path + '/' + 'config_copy') config = load_config() if config.GAN: config.MMD = False else: config.MMD = True # cuda visble devices os.environ['CUDA_VISIBLE_DEVICES'] = ','.join(str(e) for e in config.GPU) # init device if torch.cuda.is_available(): config.DEVICE = torch.device("cuda") torch.backends.cudnn.benchmark = True # cudnn auto-tuner else: config.DEVICE = torch.device("cpu") # set cv2 running threads to 1 (prevents deadlocks with pytorch dataloader) cv2.setNumThreads(0) # initialize random seed torch.manual_seed(config.SEED) torch.cuda.manual_seed_all(config.SEED) np.random.seed(config.SEED) random.seed(config.SEED) # build the model and initialize model = EdgeConnect(config) model.load() THIS_DIR = abspath(dirname(__file__)) TMP_DIR = join(THIS_DIR, a_path) if not isdir(TMP_DIR): os.makedirs(TMP_DIR) # model training if config.MODE == 1: if not a_Unsup: print('\nstart training...\n') model.train() else: print('\nstart training... on Unsupervised mode...\n') model.train(unsup=True) elif config.MODE == 2: if not a_Unsup: print('\nstart testing out...\n') model.real_test_out() else: print('\nstart testing out... on Unsupervised mode...\n') model.real_test_out(unsup=True) # eval mode elif config.MODE == 3: if not a_Unsup: print('\npre-training...\n') model.train(semi=False) else: print('\npre-training on Unsupervised mode...\n') model.train(unsup=True, semi=False) else: print('\nstart eval...\n') model.eval()
def randomTune(config): # 'LR': 0.0001, # learning rate # 'D2G_LR': 0.1, # discriminator/generator learning rate ratio # 'BETA1': 0.0, # adam optimizer beta1 # 'BETA2': 0.9, # adam optimizer beta2 # 'L1_LOSS_WEIGHT': 1, # l1 loss weight # 'FM_LOSS_WEIGHT': 10, # feature-matching loss weight config.MAX_STEPS = 1500 config.EVAL_INTERVAL = 80 config.MAX_EPOCHES = 1 # config.MAX_STEPS = 3 experiments = 50 for i in range(experiments): # sample from a Uniform distribution on a log-scale # config.LR = 10 ** np.random.uniform(-3, -5) # Sample learning rate candidates in the range (0.001 to 0.00001) # config.D2G_LR = 10 ** np.random.uniform(-2, # 0) # Sample regularization candidates in the range (0.01 to 0.0001) # config.LR = 0.0001 # config.D2G_LR =0.1 # # config.LR=0.0001 # config.PATH = './checkpoints/places2_tune_%d_%f%f_' % (i, config.LR, config.D2G_LR) # logdir= config.PATH+('/log_%s_%s' % (config.LR , config.D2G_LR)) create_dir(config.PATH) if TRAIN_LOSS: # if config.MODEL == 1: # config.L1_LOSS_WEIGHT = 10 ** np.random.uniform(-1, # 1) # Sample regularization candidates in the range (1 to 200) # config.FM_LOSS_WEIGHT = 10 ** np.random.uniform(-1, # 1.5) # Sample regularization candidates in the range (1 to 200) # config.ADV_LOSS_WEIGHT = 10 ** np.random.uniform(-1, # 1) # Sample regularization candidates in the range (1 to 200) if config.MODEL != 1: # config.L1_LOSS_WEIGHT = 10 ** np.random.uniform(-1, # 1) # Sample regularization candidates in the range (1 to 200) # config.FM_LOSS_WEIGHT = 10 ** np.random.uniform(-1, # 1.5) # Sample regularization candidates in the range (1 to 200) config.STYLE_LOSS_WEIGHT = np.random.uniform( 10, 400 ) # Sample regularization candidates in the range (1 to 200) # config.CONTENT_LOSS_WEIGHT = 2 * 10 ** np.random.uniform(0, # 2) # Sample regularization candidates in the range (1 to 200) # config.INPAINT_ADV_LOSS_WEIGHT = 10 ** np.random.uniform(-1, # 1) # Sample regularization candidates in the range (1 to 200) model = EdgeConnect(config) model.load() # config.print() print('\nEx %d: learning_rate:%f D_Learning_rate: %f:' % (i, config.LR, config.D2G_LR)) if TRAIN_LOSS: if config.MODEL == 1: print('Ex %d: L1:%f FM: %f ADV: %f:' % (i, config.L1_LOSS_WEIGHT, config.FM_LOSS_WEIGHT, config.ADV_LOSS_WEIGHT)) if config.MODEL != 1: print('Ex %d: L1:%f FM: %f STYLE: %f CONTENT: %f ADV: %f:' % (i, config.L1_LOSS_WEIGHT, config.FM_LOSS_WEIGHT, config.STYLE_LOSS_WEIGHT, config.CONTENT_LOSS_WEIGHT, config.INPAINT_ADV_LOSS_WEIGHT)) model.train() os._exit(0)
def main(mode=None, config=None): r"""starts the model Args: mode (int): 1: train, 2: test, 3: eval, reads from config file if not specified 4: demo_patch, """ if mode == 4: config = load_config_demo(mode, config=config) else: config = load_config(mode) # init environment if (config.DEVICE == 1 or config.DEVICE is None) and torch.cuda.is_available(): os.environ['CUDA_VISIBLE_DEVICES'] = ','.join( str(e) for e in config.GPU) config.DEVICE = torch.device("cuda") torch.backends.cudnn.benchmark = True # cudnn auto-tuner else: config.DEVICE = torch.device("cpu") # print(torch.cuda.is_available()) print('DEVICE is:', config.DEVICE) # set cv2 running threads to 1 (prevents deadlocks with pytorch dataloader) cv2.setNumThreads(0) # initialize random seed torch.manual_seed(config.SEED) torch.cuda.manual_seed_all(config.SEED) np.random.seed(config.SEED) random.seed(config.SEED) # enable the cudnn auto-tuner for hardware. torch.backends.cudnn.benchmark = True # build the model and initialize model = EdgeConnect(config) model.load() # model training if config.MODE == 1: config.print() print('\nstart training...\n') model.train() # model test elif config.MODE == 2: print('\nstart testing...\n') # import time # start = time.time() with torch.no_grad(): model.test() # print(time.time() - start) # eval mode elif config.MODE == 3: print('\nstart eval...\n') with torch.no_grad(): model.eval() elif config.MODE == 4: if config.DEBUG: config.print() print('model prepared.') return model
def main(mode=None): r"""starts the model Args: mode (int): 1: train, 2: test, 3: eval, reads from config file if not specified """ # image size change path = "./examples/places2/origin/" file_list = os.listdir(path) for file in file_list: # origin imager if str(file) == '.DS_Store': continue L_image = Image.open(path + str(file)) print(str(file), L_image) width = L_image.size[0] - L_image.size[0] % 4 height = L_image.size[1] - L_image.size[1] % 4 L_image = L_image.resize((width, height)) out = L_image.convert("RGB") print(L_image.size) # convert image if (str(file).endswith('mask.png')): out.save("./examples/places2/masks/" + str(file).replace('_mask', '')) else: out.save("./examples/places2/images/" + str(file)) config = load_config(mode) # cuda visble devices os.environ['CUDA_VISIBLE_DEVICES'] = ','.join(str(e) for e in config.GPU) # init device if torch.cuda.is_available(): config.DEVICE = torch.device("cuda") torch.backends.cudnn.benchmark = True # cudnn auto-tuner else: config.DEVICE = torch.device("cpu") # set cv2 running threads to 1 (prevents deadlocks with pytorch dataloader) cv2.setNumThreads(0) # initialize random seed torch.manual_seed(config.SEED) torch.cuda.manual_seed_all(config.SEED) np.random.seed(config.SEED) random.seed(config.SEED) # build the model and initialize model = EdgeConnect(config) model.load() # model training if config.MODE == 1: config.print() print('\nstart training...\n') model.train() # model test elif config.MODE == 2: print('\nstart testing...\n') model.test() # eval mode else: print('\nstart eval...\n') model.eval()