def main(): global args args = parser.parse_args() os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu # Data loading code if args.dataset == 'office': traindir = './office_list/' + args.traindata + '_list.txt' targetdir = './office_list/' + 'labeled_' + args.valdata + '.txt' valdir = './office_list/' + 'unlabeled_' + args.valdata + '.txt' data_transforms = { 'train': caffe_t.transform_train(resize_size=256, crop_size=224), 'val': caffe_t.transform_train(resize_size=256, crop_size=224), } data_transforms = caffe_t.transform_test(data_transforms=data_transforms, resize_size=256, crop_size=224) source_dataset = ImageList(open(traindir).readlines(), open(targetdir).readlines(), domain="train", transform=data_transforms["train"]) source_loader = torch.utils.data.DataLoader(source_dataset, batch_size=args.batch_size, shuffle=True, num_workers=args.workers) target_dataset = ImageList(open(valdir).readlines(), domain="val", transform=data_transforms["val"]) target_loader = torch.utils.data.DataLoader(target_dataset, batch_size=args.batch_size, shuffle=True, num_workers=args.workers) val_dataset = ImageList(open(valdir).readlines(), domain="val", transform=data_transforms["val9"]) val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.workers) net = TCL.TCL_Net(args).cuda() TCL.train_val(source_loader, target_loader, val_loader, net, args)
def main(): global args, best_loss args = parser.parse_args() os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu # create model model = Model().cuda() # optionally resume from a checkpoint if args.resume: if os.path.isfile(args.resume): print("=> loading checkpoint '{}'".format(args.resume)) checkpoint = torch.load(args.resume) args.start_epoch = checkpoint['epoch'] best_loss = checkpoint['best_loss'] model.load_state_dict(checkpoint['state_dict']) print("=> loaded checkpoint '{}' (epoch {})".format( args.resume, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}'".format(args.resume)) cudnn.benchmark = True ckpts = 'ckpts' if not os.path.exists(ckpts): os.makedirs(ckpts) # Data loading code args.arch = 'deepmedic' train_dir = args.data valid_dir = args.data train_list = 'train_list.txt' valid_list = 'valid_list.txt' # The loader will get 1000 patches from 50 subjects for each sub epoch train_loader = PEDataLoader(ImageList(train_list, root=train_dir, split='train', sample_size=20), batch_size=50, shuffle=True, num_batches=20, num_workers=args.workers, pin_memory=False) valid_loader = PEDataLoader(ImageList(valid_list, root=valid_dir, split='valid', sample_size=20), batch_size=50, shuffle=True, num_batches=20, num_workers=args.workers, pin_memory=False) criterion = nn.CrossEntropyLoss().cuda() optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, weight_decay=args.weight_decay) if args.evaluate: loss = validate(valid_loader, model, criterion) print('Validation loss', loss) return logging.info( '-------------- New training session, LR = %f ----------------' % (args.lr, )) for epoch in range(args.start_epoch, args.epochs): adjust_learning_rate(optimizer, epoch) # train for one epoch train_loss = train(train_loader, model, criterion, optimizer, epoch) # evaluate on validation set valid_loss = validate(valid_loader, model, criterion) # remember best prec@1 and save checkpoint is_best = valid_loss < best_loss best_loss = min(valid_loss, best_loss) file_name = os.path.join(ckpts, 'model_epoch_%d.tar' % (epoch + 1, )) save_checkpoint( { 'epoch': epoch + 1, 'arch': args.arch, 'state_dict': model.state_dict(), 'best_loss': best_loss, }, is_best, filename=file_name) msg = 'Epoch: {0:02d} Train loss {1:.4f} Valid loss {2:.4f}'.format( epoch + 1, train_loss, valid_loss) logging.info(msg)
def main(): if not torch.cuda.is_available(): logging.info('No GPU device available') sys.exit(1) np.random.seed(args.seed) cudnn.benchmark = True torch.manual_seed(args.seed) cudnn.enabled = True torch.cuda.manual_seed(args.seed) logging.info("args = %s", args) logging.info("unparsed_args = %s", unparsed) num_gpus = torch.cuda.device_count() genotype = eval("genotypes.%s" % args.arch) print('---------Genotype---------') logging.info(genotype) print('--------------------------') model = Network(args.init_channels, CLASSES, args.layers, args.auxiliary, genotype, args.use_dropout) if num_gpus > 1: model = nn.DataParallel(model) model = model.cuda() else: model = model.cuda() logging.info("param size = %fMB", utils.count_parameters_in_MB(model)) if args.load_path is not None: pretrained_dict = torch.load(os.path.expanduser(args.load_path), map_location=lambda storage, loc: storage) model_dict = model.state_dict() # 获取模型的参数字典 model_dict.update(pretrained_dict['state_dict'] ) # pretrained_dict与model_dict相同key的value的维度必须相同 model.load_state_dict(model_dict) # 更新模型权重 criterion = nn.CrossEntropyLoss() criterion = criterion.cuda() criterion_smooth = CrossEntropyLabelSmooth(CLASSES, args.label_smooth) criterion_smooth = criterion_smooth.cuda() optimizer = torch.optim.SGD(model.parameters(), args.learning_rate, momentum=args.momentum, weight_decay=args.weight_decay) dataset_train = ImageList(root=args.root_path, fileList=args.root_path + '/' + args.trainFile) collate_fn = None if args.prefetcher and args.mixup > 0: collate_fn = FastCollateMixup(args.mixup, args.smoothing, args.num_classes) num_train = len(dataset_train) train_queue = create_loader( dataset_train, # here call the transform implicitly input_size=144, batch_size=args.batch_size, is_training=True, use_prefetcher=args.prefetcher, rand_erase_prob=args.reprob, rand_erase_mode=args.remode, color_jitter=args.color_jitter, interpolation= 'random', # FIXME cleanly resolve this? data_config['interpolation'], # mean=data_config['mean'], # std=data_config['std'], num_workers=args.workers, collate_fn=collate_fn, auto_augment=args.aa, use_aug=True) # scheduler = torch.optim.lr_scheduler.StepLR(optimizer, args.decay_period, gamma=args.gamma) scheduler = torch.optim.lr_scheduler.CosineAnnealingLR( optimizer, float(args.epochs)) best_LFWACC = 0 lr = args.learning_rate writer = SummaryWriter(os.path.join(args.save, 'tensorboard'), comment='pcdarts-for-LFW') for epoch in range(args.epochs): if args.lr_scheduler == 'cosine': scheduler.step() current_lr = scheduler.get_lr()[0] elif args.lr_scheduler == 'linear': current_lr = adjust_lr(optimizer, epoch) else: print('Wrong lr type, exit') sys.exit(1) logging.info('Epoch: %d lr %e', epoch, current_lr) if epoch < 5 and args.load_path is None: # and args.batch_size > 256: for param_group in optimizer.param_groups: param_group['lr'] = lr * (epoch + 1) / 5.0 current_lr = lr * (epoch + 1) / 5.0 logging.info('Warming-up Epoch: %d, LR: %e', epoch, current_lr) if num_gpus > 1: model.module.drop_path_prob = args.drop_path_prob * epoch / args.epochs else: model.drop_path_prob = args.drop_path_prob * epoch / args.epochs epoch_start = time.time() train_acc, train_obj = train(train_queue, model, criterion_smooth, optimizer) logging.info('Train_acc: %f', train_acc) writer.add_scalar('lr', current_lr, epoch) writer.add_scalar('train_acc', train_acc, epoch) writer.add_scalar('train_loss', train_obj, epoch) if (epoch >= 50 and (epoch + 1) % 2 == 0) or args.load_path is not None: # valid_acc, valid_obj = infer(valid_queue, model, criterion) # logging.info('valid_acc %f', valid_acc) LFWACC, std, thd = lfw_eval(args, model) logging.info('lfw_eval LFW_ACC:%f LFW_std:%f LFW_thd:%f', LFWACC, std, thd) writer.add_scalar('LFW_ACC', LFWACC, epoch) writer.add_scalar('LFW_std', std, epoch) writer.add_scalar('LFW_thd', thd, epoch) is_best = False if LFWACC >= best_LFWACC: best_LFWACC = LFWACC is_best = True logging.info('lfw_eval BEST_LFW_ACC:%f', best_LFWACC) utils.save_checkpoint( { 'epoch': epoch + 1, 'state_dict': model.state_dict(), 'best_LFWACC': best_LFWACC, 'optimizer': optimizer.state_dict(), }, is_best, args.save) writer.close()
def main(): global args args = parser.parse_args() os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu # create model net = TCL.TCL_Net(args).cuda() # Data loading code if args.noisetype == 'corruption': if args.dataset == 'officehome': traindir = './officehome_corrupted_list/'+args.traindata+'_corrupted_'+args.noiselevel+'.txt' valdir = './officehome_list/'+args.valdata+'.txt' elif args.dataset == 'office': traindir = './office_corrupted_list/'+args.traindata+'_list_corrupted_'+args.noiselevel+'.txt' valdir = './office_list/'+args.valdata+'_list.txt' elif args.noisetype == 'noise': if args.dataset == 'officehome': traindir = './officehome_list/'+args.traindata+'_noisy_'+args.noiselevel+'.txt' valdir = './officehome_list/'+args.valdata+'.txt' elif args.dataset == 'office': traindir = './office_list/'+args.traindata+'_list_noisy_'+args.noiselevel+'.txt' valdir = './office_list/'+args.valdata+'_list.txt' elif args.noisetype == 'both': if args.dataset == 'officehome': traindir = './officehome_noisycorrupted_list/'+args.traindata+'_noisycorrupted_'+args.noiselevel+'.txt' valdir = './officehome_list/'+args.valdata+'.txt' elif args.dataset == 'office': traindir = './office_noisycorrupted_list/'+args.traindata+'_list_noisycorrupted_'+args.noiselevel+'.txt' valdir = './office_list/'+args.valdata+'_list.txt' #set data_transforms data_transforms = { 'train': caffe_t.transform_train(resize_size=256, crop_size=224), 'val': caffe_t.transform_train(resize_size=256, crop_size=224), } data_transforms = caffe_t.transform_test(data_transforms=data_transforms, resize_size=256, crop_size=224) source_loader = torch.utils.data.DataLoader( ImageList(open(traindir).readlines(), transform = data_transforms["train"]), batch_size=args.batch_size, shuffle=True, num_workers=args.workers) target_loader = torch.utils.data.DataLoader( ImageList(open(valdir).readlines(), transform = data_transforms["val"]), batch_size=args.batch_size, shuffle=True, num_workers=args.workers) val_loader = torch.utils.data.DataLoader( ImageList(open(valdir).readlines(), transform = data_transforms["val9"]), batch_size=args.batch_size, shuffle=False, num_workers=args.workers) TCL.train_val(source_loader, target_loader, val_loader, net, args)
def main(): if not torch.cuda.is_available(): logging.info('no gpu device available') sys.exit(1) np.random.seed(args.seed) torch.cuda.set_device(args.gpu) cudnn.benchmark = True torch.manual_seed(args.seed) cudnn.enabled = True torch.cuda.manual_seed(args.seed) logging.info('gpu device = %d' % args.gpu) logging.info("args = %s", args) criterion = nn.CrossEntropyLoss() criterion = criterion.cuda() model = Network(args.init_channels, CIFAR_CLASSES, args.layers, criterion, args.dropout_type) model = model.cuda() logging.info("param size = %fMB", utils.count_parameters_in_MB(model)) optimizer = torch.optim.SGD( model.parameters(), args.learning_rate, momentum=args.momentum, weight_decay=args.weight_decay) # train_transform, valid_transform = utils._data_transforms_cifar10(args) # if args.set=='cifar100': # train_data = dset.CIFAR100(root=args.data, train=True, download=False, transform=train_transform) # else: # train_data = dset.CIFAR10(root=args.data, train=True, download=False, transform=train_transform) dataset_train = ImageList(root=args.data, fileList=args.data + '/' + args.trainFile) collate_fn = None if args.prefetcher and args.mixup > 0: collate_fn = FastCollateMixup(args.mixup, args.smoothing, args.num_classes) num_train = len(dataset_train) # indices = list(range(num_train)) # randint(args.data_portion * num_train) indices = np.linspace(0, num_train-1, args.data_portion*num_train, dtype=np.int) random.shuffle(indices) num_train = len(indices) # patch = int(np.floor(args.data_portion * num_train)) split = int(np.floor(args.train_portion * num_train)) train_queue = create_loader( dataset_train, # here call the transform implicitly input_size=144, batch_size=args.batch_size, is_training=True, use_prefetcher=args.prefetcher, rand_erase_prob=args.reprob, rand_erase_mode=args.remode, color_jitter=args.color_jitter, interpolation='random', # FIXME cleanly resolve this? data_config['interpolation'], # mean=data_config['mean'], # std=data_config['std'], num_workers=args.workers, sampler=torch.utils.data.sampler.SubsetRandomSampler(indices[:split]), collate_fn=collate_fn, auto_augment=args.aa ) valid_queue = create_loader( dataset_train, # here call the transform implicitly input_size=144, batch_size=args.batch_size, is_training=True, use_prefetcher=args.prefetcher, rand_erase_prob=args.reprob, rand_erase_mode=args.remode, color_jitter=args.color_jitter, interpolation='random', # FIXME cleanly resolve this? data_config['interpolation'], # mean=data_config['mean'], # std=data_config['std'], num_workers=args.workers, sampler=torch.utils.data.sampler.SubsetRandomSampler(indices[split:num_train]), collate_fn=collate_fn, auto_augment=args.aa ) scheduler = torch.optim.lr_scheduler.CosineAnnealingLR( optimizer, float(args.epochs), eta_min=args.learning_rate_min) architect = Architect(model, args) writer = SummaryWriter(os.path.join(args.save, 'tensorboard')) for epoch in range(args.epochs): scheduler.step() lr = scheduler.get_lr()[0] logging.info('epoch %d lr %e', epoch, lr) genotype = model.genotype() logging.info('genotype = %s', genotype) # training train_acc, train_obj = train(train_queue, valid_queue, model, architect, criterion, optimizer, lr, epoch, args.arch_epoch, writer) logging.info('train_acc %f', train_acc) writer.add_scalar('lr', lr, epoch) writer.add_scalar('train_acc', train_acc, epoch) writer.add_scalar('train_loss', train_obj, epoch) # validation # if (epoch+1) >= args.valid_epoch and (epoch+1) % 5 == 0: # LFWACC, std, thd = lfw_eval(args, model) # logging.info('lfw_eval LFW_ACC:%f LFW_std:%f LFW_thd:%f', LFWACC,std,thd) # writer.add_scalar('LFW_ACC', LFWACC, epoch) # writer.add_scalar('LFW_std', std, epoch) # writer.add_scalar('LFW_thd', thd, epoch) # utils.save(model, os.path.join(args.save, 'weights.pt')) writer.close()
#set dataset batch_size = {"train":24, "test":4} if config["dset_name"] == "Office": task_name_list = ["amazon", "webcam", "dslr", "c"] train_file_list = [os.path.join(project_path, "data", "office", task_name_list[i], "train_5.txt") for i in xrange(config["num_tasks"])] test_file_list = [os.path.join(project_path, "data", "office", task_name_list[i], "test_5.txt") for i in xrange(config["num_tasks"])] dset_classes = range(10) elif config["dset_name"] == "Office-Home": task_name_list = ["Art", "Product", "Clipart", "Real_World"] train_file_list = [os.path.join(project_path, "data", "office-home", task_name_list[i], "train_10.txt") for i in xrange(config["num_tasks"])] test_file_list = [os.path.join(project_path, "data", "office-home", task_name_list[i], "test_10.txt") for i in xrange(config["num_tasks"])] dset_classes = range(65) dsets = {"train": [ImageList(open(train_file_list[i]).readlines(), transform=data_transforms["train"]) for i in xrange(config["num_tasks"])], "test":[ImageList(open(test_file_list[i]).readlines(), transform=data_transforms["val9"]) for i in xrange(config["num_tasks"])]} dset_loaders = {"train":[], "test":[]} for train_dset in dsets["train"]: dset_loaders["train"].append(torch.utils.data.DataLoader(train_dset, batch_size=batch_size["train"], shuffle=True, num_workers=4)) for test_dset in dsets["test"]: dset_loaders["test"].append(torch.utils.data.DataLoader(test_dset, batch_size=batch_size["test"], shuffle=True, num_workers=4)) config["loaders"] = dset_loaders #construct model and initialize config["model"] = model_multi_task.HomoMultiTaskModel(config["num_tasks"], "vgg16no_fc", len(dset_classes), config["gpus"], config["file_out"], trade_off=1, optim_param={"init_lr":0.00003, "gamma":0.3, "power":0.75, "stepsize":3000}) #start train print "start train" config["file_out"].write("start train\n") config["file_out"].flush()
def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('-o', '--output', type=str, required=True) parser.add_argument('--num-epochs', type=int, default=50) parser.add_argument('--batch-size', type=int, default=64) parser.add_argument('--learning-rate', type=float, default=1e-3) return parser.parse_args() args = parse_args() output_path = Path(args.output)/datetime.now().strftime('%Y%m%d') output_path.mkdir(parents=True, exist_ok=True) img_celebs = Path.home()/'GDL_code/data/celeb/img_align_celeba' tfms = [make_rgb, ResizeFixed(128), to_byte_tensor, to_float_tensor] il = ImageList.from_files(img_celebs, tfms=tfms) dl = torch.utils.data.DataLoader(il, batch_size=args.batch_size, shuffle=True) from torch.autograd import Variable model = VAEForFaces() opt = Adam(model.parameters(), lr=args.learning_rate) r_loss_func = nn.MSELoss(reduction='sum') def loss_func(pred, x, mu, logvar): r_loss = r_loss_func(pred, x) kl_divergence = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp()) return r_loss + kl_divergence for epoch in range(10):
task_name_list = ["Art", "Product", "Clipart", "Real_World"] train_file_list = [ os.path.join(project_path, "data", "office-home", task_name_list[i], "train_10.txt") for i in range(config["num_tasks"]) ] test_file_list = [ os.path.join(project_path, "data", "office-home", task_name_list[i], "test_10.txt") for i in range(config["num_tasks"]) ] dset_classes = range(65) dsets = { "train": [ ImageList(open(train_file_list[i]).readlines(), transform=data_transforms["train"]) for i in range(config["num_tasks"]) ], "test": [ ImageList(open(test_file_list[i]).readlines(), transform=data_transforms["val9"]) for i in range(config["num_tasks"]) ] } dset_loaders = {"train": [], "test": []} for train_dset in dsets["train"]: dset_loaders["train"].append( torch.utils.data.DataLoader(train_dset, batch_size=batch_size["train"], shuffle=True, num_workers=4))