def main(): parser = argparse.ArgumentParser() parser.add_argument('--EXP_NAME', type=str, default='segment') parser.add_argument('--EXP_DIR', type=str, default='/home/yangle/result/TrainNet/') parser.add_argument('--DATASET_PATH', type=str, default='/home/yangle/BasicDataset/dataset/MSRA10K/') parser.add_argument('--SAVE_DIR', type=str, default='/home/yangle/result/mask/MSRA10K/') parser.add_argument('--LEARNING_RATE', type=float, default=1e-4) parser.add_argument('--WEIGHT_DECAY', type=float, default=0.0001) args = parser.parse_args() if not os.path.exists(args.SAVE_DIR): os.makedirs(args.SAVE_DIR) normalize = transforms.Normalize(mean=saliency.mean, std=saliency.std) test_joint_transformer = transforms.Compose( [joint_transforms.JointResize(224)]) test_dset = saliency.TestImage(args.DATASET_PATH, 'val', joint_transform=None, transform=transforms.Compose([ transforms.ToTensor(), normalize, ])) test_loader = torch.utils.data.DataLoader(test_dset, batch_size=1, shuffle=False) model = tiramisu.FCDenseNet57(in_channels=3, n_classes=2) # model = model.cuda() model = torch.nn.DataParallel(model).cuda() optimizer = optim.RMSprop(model.parameters(), lr=args.LEARNING_RATE, weight_decay=args.WEIGHT_DECAY) exper = experiment.Experiment(args.EXP_NAME, args.EXP_DIR) # exper.resume(model, optimizer) base_path = args.EXP_DIR + args.EXP_NAME + '/weights/' weights_fpath = base_path + 'segment-weights-132-0.109-4.278-0.120-4.493.pth' optim_path = base_path + 'segment-optim-132.pth' exper.resume(model, optimizer, weights_fpath, optim_path) # count = 1 for count, (img, name) in enumerate(test_loader): # for img, name in test_loader: data = Variable(img.cuda(), volatile=True) output = model(data) pred = utils.get_predictions(output) pred = pred[0] img_name = name[0] # img_name = str(name) # img_name = img_name.replace('tif', 'png') save_path = args.SAVE_DIR + img_name torchvision.utils.save_image(pred, save_path) print(count)
def ini_seg_model(fpath=None): if fpath is None: fpath = '/home/yangle/code/CorFilter/ext_fea/tiramisu12/weights/latest_weights.pth' seg_model = tiramisu.FCDenseNet57(n_classes=2) seg_model = seg_model.cuda() seg_model.eval() print('load the segmentation model') state = torch.load(fpath) seg_model.load_state_dict(state['state_dict']) return seg_model
def main(): parser = argparse.ArgumentParser() parser.add_argument('--DATASET_PATH', type=str, default='/home/zhangdong/database/DUTS/') parser.add_argument('--SAVE_DIR', type=str, default='/home/yangle/DAVIS/result/DUTS/') args = parser.parse_args() normalize = transforms.Normalize(mean=saliency.mean, std=saliency.std) # test_joint_transforms = transforms.Compose([joint_transforms.JointResize(224)]) test_dset = saliency.TestImage(args.DATASET_PATH, 'val', joint_transform=None, transform=transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), normalize ])) model = tiramisu.FCDenseNet57(n_classes=2) # model = model.cuda() model = torch.nn.DataParallel(model).cuda() weight_path = '/home/yangle/DAVIS/result/TrainNet/tiramisu/weights/latest_weights.pth' state = torch.load(weight_path) model.load_state_dict(state['state_dict']) model = model.module test_loader = torch.utils.data.DataLoader(test_dset, batch_size=1, shuffle=False) count = 1 for data, name in test_loader: data = Variable(data.cuda(), volatile=True) _, _, hei, wid = data.size() output = model(data) pred = utils.get_predictions(output) pred = pred[0] # transforms_size = torchvision.transforms.Resize((hei, wid)) # mask = transforms_size([pred]) name = name[0] img_name = str(name) save_path = args.SAVE_DIR + img_name torchvision.utils.save_image(pred, save_path) print(count) count += 1
def main(): parser = argparse.ArgumentParser() parser.add_argument('--DATASET_PATH', type=str, default='/home/yangle/dataset/DUTS/') parser.add_argument('--RESULTS_PATH', type=str, default='/home/yangle/result/TrainNet/results/') parser.add_argument('--WEIGHTS_PATH', type=str, default='/home/yangle/result/TrainNet/models/') parser.add_argument('--EXPERIMENT', type=str, default='/home/yangle/result/TrainNet/') parser.add_argument('--N_EPOCHS', type=int, default=300) parser.add_argument('--MAX_PATIENCE', type=int, default=30) parser.add_argument('--batch_size', type=int, default=32) parser.add_argument('--seed', type=int, default=0) parser.add_argument('--N_CLASSES', type=int, default=2) parser.add_argument('--LEARNING_RATE', type=float, default=1e-4) parser.add_argument('--LR_DECAY', type=float, default=0.995) parser.add_argument('--DECAY_LR_EVERY_N_EPOCHS', type=int, default=1) parser.add_argument('--WEIGHT_DECAY', type=float, default=0.0001) parser.add_argument('--CUDNN', type=bool, default=True) args = parser.parse_args() torch.cuda.manual_seed(args.seed) cudnn.benchmark = args.CUDNN normalize = transforms.Normalize(mean=saliency.mean, std=saliency.std) train_joint_transformer = transforms.Compose([ joint_transforms.JointResize(224), joint_transforms.JointRandomHorizontalFlip() ]) train_dset = saliency.Saliency( args.DATASET_PATH, 'train', joint_transform=train_joint_transformer, transform=transforms.Compose([transforms.ToTensor(), normalize, ])) train_loader = torch.utils.data.DataLoader( train_dset, batch_size=args.batch_size, shuffle=False) val_joint_transformer = transforms.Compose([joint_transforms.JointResize(224)]) val_dset = saliency.Saliency( args.DATASET_PATH, 'val', joint_transform=val_joint_transformer, transform=transforms.Compose([transforms.ToTensor(), normalize, ])) val_loader = torch.utils.data.DataLoader( val_dset, batch_size=8, shuffle=False) print("TrainImages: %d" % len(train_loader.dataset.imgs)) print("ValImages: %d" % len(val_loader.dataset.imgs)) # print("TestImages: %d" % len(test_loader.dataset.imgs)) example_inputs, example_targets = next(iter(train_loader)) print("InputsBatchSize: ", example_inputs.size()) print("TargetsBatchSize: ", example_targets.size()) print("\nInput (size, max, min) ---") #input i = example_inputs[0] print(i.size()) print(i.max()) print(i.min()) print("Target (size, max, min) ---") #target t = example_targets[0] print(t.size()) print(t.max()) print(t.min()) ###################################### # load weights from pretrained model # ###################################### model_pre = tiramisu_pre.FCDenseNet57(in_channels=3, n_classes=2) model_pre = torch.nn.DataParallel(model_pre).cuda() fpath = '/home/yangle/result/TrainNet/segment/weights/segment-weights-132-0.109-4.278-0.120-4.493.pth' state = torch.load(fpath) pretrained_dict = state['state_dict'] model = tiramisu.FCDenseNet57(in_channels=3, n_classes=2) model = torch.nn.DataParallel(model).cuda() model_dict = model.state_dict() # 1. filter out unnecessary keys pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict} # 2. overwrite entries in the existing state dict model_dict.update(pretrained_dict) # 3. load the new state dict model.load_state_dict(model_dict) # convert model trained with multiple GPUs into model within single GPU model = model.module # not train existing layers # for k in pretrained_dict: count = 0 para_optim = [] for k in model.children(): # for k in model.module.children(): count += 1 if count > 6: for param in k.parameters(): para_optim.append(param) else: for param in k.parameters(): param.requires_grad = False # print(k) print('para_optim') print(len(para_optim)) optimizer = optim.RMSprop(para_optim, lr=args.LEARNING_RATE, weight_decay=args.WEIGHT_DECAY, eps=1e-12) criterion = nn.NLLLoss2d().cuda() exp_dir = args.EXPERIMENT + 'GRU_test' if os.path.exists(exp_dir): shutil.rmtree(exp_dir) exp = experiment.Experiment('GRU_test', args.EXPERIMENT) exp.init() START_EPOCH = exp.epoch END_EPOCH = START_EPOCH + args.N_EPOCHS for epoch in range(START_EPOCH, END_EPOCH): since = time.time() ### Train ### trn_loss, trn_err = utils.train(model, train_loader, optimizer, criterion, epoch) print('Epoch {:d}: Train - Loss: {:.4f}\tErr: {:.4f}'.format(epoch, trn_loss, trn_err)) time_elapsed = time.time() - since print('Train Time {:.0f}m {:.0f}s'.format( time_elapsed // 60, time_elapsed % 60)) ### Test ### val_loss, val_err = utils.test(model, val_loader, criterion, epoch) print('Val - Loss: {:.4f}, Error: {:.4f}'.format(val_loss, val_err)) time_elapsed = time.time() - since print('Total Time {:.0f}m {:.0f}s\n'.format( time_elapsed // 60, time_elapsed % 60)) ### Save Metrics ### exp.save_history('train', trn_loss, trn_err) exp.save_history('val', val_loss, val_err) ### Checkpoint ### exp.save_weights(model, trn_loss, val_loss, trn_err, val_err) exp.save_optimizer(optimizer, val_loss) ## Early Stopping ## if (epoch - exp.best_val_loss_epoch) > args.MAX_PATIENCE: print(("Early stopping at epoch %d since no " +"better loss found since epoch %.3").format(epoch, exp.best_val_loss)) break # Adjust Lr ###--old method utils.adjust_learning_rate(args.LEARNING_RATE, args.LR_DECAY, optimizer, epoch, args.DECAY_LR_EVERY_N_EPOCHS) exp.epoch += 1
import torch import torch.optim as optim import tiramisu_preTrain as tiramisu_ori import tiramisu model_ori = tiramisu_ori.FCDenseNet57(in_channels=3, n_classes=2) model_ori = torch.nn.DataParallel(model_ori).cuda() fpath = '/weights.pth' state = torch.load(fpath) pretrained_dict = state['state_dict'] model = tiramisu.FCDenseNet57(in_channels=3, n_classes=2) model = torch.nn.DataParallel(model).cuda() model_dict = model.state_dict() # 1. filter out unnecessary keys pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict} # 2. overwrite entries in the existing state dict model_dict.update(pretrained_dict) # 3. load the new state dict model.load_state_dict(model_dict) # origional model is trained with multiple GPUs, convert it to single GPU model model = model.module # not train existing layers count = 0 para_optim = [] for k in model.children(): count += 1
def main(): parser = argparse.ArgumentParser() parser.add_argument('--DATASET_PATH', type=str, default='/disk2/zhangni/davis/dataset/Objectness/') parser.add_argument('--EXPERIMENT', type=str, default='/disk2/zhangni/davis/result/TrainNet/') parser.add_argument('--N_EPOCHS', type=int, default=200) parser.add_argument('--MAX_PATIENCE', type=int, default=20) parser.add_argument('--batch_size', type=int, default=16) parser.add_argument('--seed', type=int, default=0) parser.add_argument('--N_CLASSES', type=int, default=10) parser.add_argument('--LEARNING_RATE', type=float, default=1e-2) parser.add_argument('--LR_DECAY', type=float, default=0.995) parser.add_argument('--DECAY_LR_EVERY_N_EPOCHS', type=int, default=1) parser.add_argument('--WEIGHT_DECAY', type=float, default=0.0001) parser.add_argument('--CUDNN', type=bool, default=True) args = parser.parse_args() torch.cuda.manual_seed(args.seed) cudnn.benchmark = args.CUDNN normalize = transforms.Normalize(mean=saliency.mean, std=saliency.std) train_joint_transformer = transforms.Compose([ joint_transforms.JointResize((224)), joint_transforms.JointRandomHorizontalFlip() ]) train_dset = saliency.Saliency(args.DATASET_PATH, 'train', joint_transform=train_joint_transformer, transform=transforms.Compose([ transforms.ToTensor(), normalize, ])) train_loader = torch.utils.data.DataLoader(train_dset, batch_size=args.batch_size, shuffle=True) test_joint_transforms = transforms.Compose( [joint_transforms.JointResize(224)]) val_dset = saliency.Saliency(args.DATASET_PATH, 'val', joint_transform=test_joint_transforms, transform=transforms.Compose( [transforms.ToTensor(), normalize])) val_loader = torch.utils.data.DataLoader(val_dset, batch_size=args.batch_size, shuffle=False) model = tiramisu.FCDenseNet57(n_classes=args.N_CLASSES) #model = model.cuda() model = torch.nn.DataParallel(model).cuda() print(' + Number of params: {}'.format( sum([p.data.nelement() for p in model.parameters()]))) model.apply(utils.weights_init) optimizer = optim.SGD(model.parameters(), lr=args.LEARNING_RATE, momentum=0.9, weight_decay=0.0005) criterion = nn.NLLLoss2d().cuda() exp_dir = args.EXPERIMENT + 'Objectness' if os.path.exists(exp_dir): shutil.rmtree(exp_dir) exp = experiment.Experiment('Objectness', args.EXPERIMENT) exp.init() START_EPOCH = exp.epoch END_EPOCH = START_EPOCH + args.N_EPOCHS for epoch in range(1, END_EPOCH): since = time.time() ### Train ### trn_loss, trn_err = utils.train(model, train_loader, optimizer, criterion, epoch) print('Epoch {:d}: Train - Loss: {:.4f}\tErr: {:.4f}'.format( epoch, trn_loss, trn_err)) time_elapsed = time.time() - since print('Train Time {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60)) ### Test ### val_loss, val_err = utils.test(model, val_loader, criterion, epoch) print('Val - Loss: {:.4f}, Error: {:.4f}'.format(val_loss, val_err)) time_elapsed = time.time() - since print('Total Time {:.0f}m {:.0f}s\n'.format(time_elapsed // 60, time_elapsed % 60)) ### Save Metrics ### exp.save_history('train', trn_loss, trn_err) exp.save_history('val', val_loss, val_err) ### Checkpoint ### exp.save_weights(model, trn_loss, val_loss, trn_err, val_err) exp.save_optimizer(optimizer, val_loss) ## Early Stopping ## if (epoch - exp.best_val_loss_epoch) > args.MAX_PATIENCE: print(("Early stopping at epoch %d since no " + "better loss found since epoch %.3").format( epoch, exp.best_val_loss)) break # Adjust Lr ###--old method if epoch % 4 == 0: utils.adjust_learning_rate(args.LEARNING_RATE, args.LR_DECAY, optimizer, epoch, args.DECAY_LR_EVERY_N_EPOCHS) exp.epoch += 1
def main(): torch.cuda.manual_seed(seed) cudnn.benchmark = CUDNN model = tiramisu.FCDenseNet57(n_classes=N_CLASSES) #model = model.cuda() model = torch.nn.DataParallel(model).cuda() print(' + Number of params: {}'.format( sum([p.data.nelement() for p in model.parameters()]))) model.apply(utils.weights_init) optimizer = optim.SGD(model.parameters(), lr=LEARNING_RATE, momentum=0.9, weight_decay=0.0005) criterion = nn.NLLLoss2d().cuda() exp_dir = EXPERIMENT + 'Objectness' if os.path.exists(exp_dir): shutil.rmtree(exp_dir) exp = experiment.Experiment('Objectness', EXPERIMENT) exp.init() START_EPOCH = exp.epoch END_EPOCH = START_EPOCH + N_EPOCHS for epoch in range(1, END_EPOCH): since = time.time() # # ### Collect data ### # # # delete existing folder and old data cont_rp = data_root_path + 'traincont/' if os.path.exists(cont_rp): shutil.rmtree(cont_rp) utils.collect_data(data_root_path, 'train') cont_rp = data_root_path + 'valcont/' if os.path.exists(cont_rp): shutil.rmtree(cont_rp) utils.collect_data(data_root_path, 'val') # data loader train_loader, val_loader = utils.data_loader(data_root_path) ### Train ### trn_loss, trn_err = utils.train(model, train_loader, optimizer, criterion, epoch) print('Epoch {:d}: Train - Loss: {:.4f}\tErr: {:.4f}'.format( epoch, trn_loss, trn_err)) time_elapsed = time.time() - since print('Train Time {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60)) ### Test ### val_loss, val_err = utils.test(model, val_loader, criterion, epoch) print('Val - Loss: {:.4f}, Error: {:.4f}'.format(val_loss, val_err)) time_elapsed = time.time() - since print('Total Time {:.0f}m {:.0f}s\n'.format(time_elapsed // 60, time_elapsed % 60)) ### Save Metrics ### exp.save_history('train', trn_loss, trn_err) exp.save_history('val', val_loss, val_err) ### Checkpoint ### exp.save_weights(model, trn_loss, val_loss, trn_err, val_err) exp.save_optimizer(optimizer, val_loss) ## Early Stopping ## if (epoch - exp.best_val_loss_epoch) > MAX_PATIENCE: print(("Early stopping at epoch %d since no " + "better loss found since epoch %.3").format( epoch, exp.best_val_loss)) break # Adjust Lr ###--old method if epoch % 4 == 0: utils.adjust_learning_rate(LEARNING_RATE, LR_DECAY, optimizer, epoch, DECAY_LR_EVERY_N_EPOCHS) exp.epoch += 1
def main(): parser = argparse.ArgumentParser() parser.add_argument('--DATASET_PATH', type=str, default='/home/zhangdong/database/DUTS/') parser.add_argument('--EXPERIMENT', type=str, default='/home/yangle/DAVIS/result/TrainNet/') parser.add_argument('--N_EPOCHS', type=int, default=200) parser.add_argument('--MAX_PATIENCE', type=int, default=30) parser.add_argument('--batch_size', type=int, default=24) parser.add_argument('--seed', type=int, default=0) parser.add_argument('--N_CLASSES', type=int, default=2) parser.add_argument('--LEARNING_RATE', type=float, default=5e-4) parser.add_argument('--LR_DECAY', type=float, default=0.995) parser.add_argument('--DECAY_LR_EVERY_N_EPOCHS', type=int, default=1) parser.add_argument('--WEIGHT_DECAY', type=float, default=0.0001) parser.add_argument('--CUDNN', type=bool, default=True) args = parser.parse_args() torch.cuda.manual_seed(args.seed) cudnn.benchmark = args.CUDNN normalize = transforms.Normalize(mean=saliency.mean, std=saliency.std) train_joint_transformer = transforms.Compose([ joint_transforms.JointResize(224), joint_transforms.JointRandomHorizontalFlip() ]) train_dset = saliency.Saliency(args.DATASET_PATH, 'train', joint_transform=train_joint_transformer, transform=transforms.Compose([ transforms.ToTensor(), normalize, ])) train_loader = torch.utils.data.DataLoader(train_dset, batch_size=args.batch_size, shuffle=True) test_joint_transforms = transforms.Compose( [joint_transforms.JointResize(224)]) val_dset = saliency.Saliency(args.DATASET_PATH, 'val', joint_transform=test_joint_transforms, transform=transforms.Compose( [transforms.ToTensor(), normalize])) val_loader = torch.utils.data.DataLoader(val_dset, batch_size=args.batch_size, shuffle=False) print("TrainImages: %d" % len(train_loader.dataset.imgs)) print("ValImages: %d" % len(val_loader.dataset.imgs)) # print("TestImages: %d" % len(test_loader.dataset.imgs)) example_inputs, example_targets = next(iter(train_loader)) print("InputsBatchSize: ", example_inputs.size()) print("TargetsBatchSize: ", example_targets.size()) print("\nInput (size, max, min) ---") #input i = example_inputs[0] print(i.size()) print(i.max()) print(i.min()) print("Target (size, max, min) ---") #target t = example_targets[0] print(t.size()) print(t.max()) print(t.min()) model = tiramisu.FCDenseNet57(n_classes=args.N_CLASSES) # model = model.cuda() model = torch.nn.DataParallel(model).cuda() print(' + Number of params: {}'.format( sum([p.data.nelement() for p in model.parameters()]))) model.apply(utils.weights_init) optimizer = optim.RMSprop(model.parameters(), lr=args.LEARNING_RATE, weight_decay=args.WEIGHT_DECAY, eps=1e-12) criterion = nn.NLLLoss2d().cuda() exp_dir = args.EXPERIMENT + 'tiramisu' if os.path.exists(exp_dir): shutil.rmtree(exp_dir) exp = experiment.Experiment('tiramisu', args.EXPERIMENT) exp.init() START_EPOCH = exp.epoch END_EPOCH = START_EPOCH + args.N_EPOCHS for epoch in range(START_EPOCH, END_EPOCH): since = time.time() ### Train ### trn_loss, trn_err = utils.train(model, train_loader, optimizer, criterion, epoch) print('Epoch {:d}: Train - Loss: {:.4f}\tErr: {:.4f}'.format( epoch, trn_loss, trn_err)) time_elapsed = time.time() - since print('Train Time {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60)) ### Test ### val_loss, val_err = utils.test(model, val_loader, criterion, epoch) print('Val - Loss: {:.4f}, Error: {:.4f}'.format(val_loss, val_err)) time_elapsed = time.time() - since print('Total Time {:.0f}m {:.0f}s\n'.format(time_elapsed // 60, time_elapsed % 60)) ### Save Metrics ### exp.save_history('train', trn_loss, trn_err) exp.save_history('val', val_loss, val_err) ### Checkpoint ### exp.save_weights(model, trn_loss, val_loss, trn_err, val_err) exp.save_optimizer(optimizer, val_loss) ## Early Stopping ## if (epoch - exp.best_val_loss_epoch) > args.MAX_PATIENCE: print(("Early stopping at epoch %d since no " + "better loss found since epoch %.3").format( epoch, exp.best_val_loss)) break # lr_sche.step(val_loss) #### print learning rate #### # for param_group in optimizer.param_groups: # print(param_group['lr']) # Adjust Lr ###--old method utils.adjust_learning_rate(args.LEARNING_RATE, args.LR_DECAY, optimizer, epoch, args.DECAY_LR_EVERY_N_EPOCHS) exp.epoch += 1