def create_loader(args_data, loader_type): kwargs = {'num_workers': args_data.num_loader_workers} if loader_type == LoaderMode.TRAIN: loader = torch.utils.data.DataLoader(NyuHandPoseMultiViewDataset( args_data.nyu_data_basepath, train=True, cropSize=args_data.in_crop_size, doJitterCom=args_data.do_jitter_com, sigmaCom=args_data.sigma_com, doAddWhiteNoise=args_data.do_add_white_noise, sigmaNoise=args_data.sigma_noise, transform=transforms.ToTensor(), useCache=args_data.use_pickled_cache, cacheDir=args_data.nyu_data_basepath_pickled, annoType=args_data.anno_type, neededCamIdsReal=args_data.needed_cam_ids_train_real, neededCamIdsSynth=args_data.needed_cam_ids_train_synth, randomSeed=args_data.seed, cropSize3D=args_data.crop_size_3d_tuple, args_data=args_data), batch_size=args_data.batch_size, **kwargs) elif loader_type == LoaderMode.TEST: needed_cam_ids_synth = args_data.needed_cam_ids_test if args_data.do_test_on_synth else [] loader = torch.utils.data.DataLoader(NyuHandPoseMultiViewDataset( args_data.nyu_data_basepath, train=False, cropSize=args_data.in_crop_size, doJitterCom=args_data.do_jitter_com_test, sigmaCom=args_data.sigma_com, doAddWhiteNoise=args_data.do_add_white_noise_test, sigmaNoise=args_data.sigma_noise, transform=transforms.ToTensor(), useCache=args_data.use_pickled_cache, cacheDir=args_data.nyu_data_basepath_pickled, annoType=args_data.anno_type, neededCamIdsReal=args_data.needed_cam_ids_test, neededCamIdsSynth=needed_cam_ids_synth, randomSeed=args_data.seed, cropSize3D=args_data.crop_size_3d_tuple, args_data=args_data), batch_size=args_data.batch_size, **kwargs) print("Using {} samples for test".format(len(loader.sampler))) else: raise UserWarning("LoaderMode unknown.") return loader
def create_sequential_nyu_trainloader(args_data): kwargs = {'num_workers': args_data.num_loader_workers} loader = torch.utils.data.DataLoader(NyuHandPoseMultiViewDataset( args_data.nyu_data_basepath, train=True, cropSize=args_data.in_crop_size, doJitterCom=args_data.do_jitter_com, sigmaCom=args_data.sigma_com, doAddWhiteNoise=args_data.do_add_white_noise, sigmaNoise=args_data.sigma_noise, transform=transforms.ToTensor(), useCache=args_data.use_pickled_cache, cacheDir=args_data.nyu_data_basepath_pickled, annoType=args_data.anno_type, neededCamIdsReal=args_data.needed_cam_ids_train_real, neededCamIdsSynth=args_data.needed_cam_ids_train_synth, randomSeed=args_data.seed, cropSize3D=args_data.crop_size_3d_tuple, args_data=args_data), batch_size=args_data.batch_size, **kwargs) return loader
def create_dataloader(loader_type, args_data, do_use_gpu): """ Create a data loader according to the parameters """ kwargs = { 'num_workers': args_data.num_loader_workers, 'pin_memory': True } if do_use_gpu else {} if args_data.dataset_type == DatasetType.NYU: if loader_type == LoaderMode.TRAIN: # Set up sample IDs to sample from ids_train = np.arange(args_data.id_start_train, args_data.id_end_train + 1) ids_train_permuted = args_data.rng.permutation(ids_train) ids_train_labeled = ids_train_permuted[:args_data. num_labeled_samples] ids_train_unlabeled = ids_train_permuted[args_data. num_labeled_samples:] # Ensure a minimum sampling probability for labeled samples ratio_labeled = len(ids_train_labeled) / float(len(ids_train)) prob_labeled = max(args_data.min_sampling_prob_labeled, ratio_labeled) prob_unlabeled = 1.0 - prob_labeled # Set up distribution/weights to sample from (considering un-/labeled samples) scale_weights = float( len(ids_train)) # value to which weights will sum up sample_weight_labeled = prob_labeled * scale_weights / float( len(ids_train_labeled)) sample_weight_unlabeled = prob_unlabeled * scale_weights \ / float(len(ids_train_unlabeled)) \ if len(ids_train_unlabeled) > 0 else 0.0 sampling_weights = np.zeros((args_data.num_all_samples_train)) sampling_weights[ids_train_labeled] = sample_weight_labeled sampling_weights[ids_train_unlabeled] = sample_weight_unlabeled num_samples_used_for_train = np.count_nonzero(sampling_weights) loader = torch.utils.data.DataLoader( NyuHandPoseMultiViewDataset( args_data.nyu_data_basepath, train=True, cropSize=args_data.in_crop_size, doJitterCom=args_data.do_jitter_com, sigmaCom=args_data.sigma_com, doAddWhiteNoise=args_data.do_add_white_noise, sigmaNoise=args_data.sigma_noise, doLoadRealSamples=args_data.do_use_real_samples, unlabeledSampleIds=ids_train_unlabeled, transform=transforms.ToTensor(), useCache=args_data.use_pickled_cache, cacheDir=args_data.nyu_data_basepath_pickled, annoType=args_data.anno_type, neededCamIds=args_data.needed_cam_ids_train, randomSeed=args_data.seed, cropSize3D=args_data.crop_size_3d_tuple, args_data=args_data), batch_size=args_data.batch_size, sampler=smpl.WeightedRandomSampler( sampling_weights, num_samples=num_samples_used_for_train, replacement=True), **kwargs) print("Using {} samples for training".format( num_samples_used_for_train)) if sample_weight_labeled > 0.: print(" {} labeled".format(len(ids_train_labeled))) if sample_weight_unlabeled > 0.: print(" {} unlabeled".format(len(ids_train_unlabeled))) elif loader_type == LoaderMode.VAL: num_samples_val = min( int( round(args_data.max_val_train_ratio * args_data.num_labeled_samples)), args_data.max_num_samples_val) ids_val = np.arange(args_data.id_start_val, args_data.id_end_val + 1) ids_val = args_data.rng.permutation(ids_val) ids_val = ids_val[:num_samples_val] loader = torch.utils.data.DataLoader( NyuHandPoseMultiViewDataset( args_data.nyu_data_basepath, train=False, cropSize=args_data.in_crop_size, doJitterCom=args_data.do_jitter_com_test, sigmaCom=args_data.sigma_com, doAddWhiteNoise=args_data.do_add_white_noise_test, sigmaNoise=args_data.sigma_noise, doLoadRealSamples=args_data.do_use_real_samples, transform=transforms.ToTensor(), useCache=args_data.use_pickled_cache, cacheDir=args_data.nyu_data_basepath_pickled, annoType=args_data.anno_type, neededCamIds=args_data.needed_cam_ids_test, randomSeed=args_data.seed, cropSize3D=args_data.crop_size_3d_tuple, args_data=args_data), batch_size=args_data.batch_size, sampler=smpl.SubsetRandomSampler(ids_val), **kwargs) print("Using {} samples for validation".format(len(ids_val))) elif loader_type == LoaderMode.TEST: ids_test = np.arange(args_data.id_start_test, args_data.id_end_test + 1) loader = torch.utils.data.DataLoader( NyuHandPoseMultiViewDataset( args_data.nyu_data_basepath, train=False, cropSize=args_data.in_crop_size, doJitterCom=args_data.do_jitter_com_test, sigmaCom=args_data.sigma_com, doAddWhiteNoise=args_data.do_add_white_noise_test, sigmaNoise=args_data.sigma_noise, doLoadRealSamples=args_data.do_use_real_samples, transform=transforms.ToTensor(), useCache=args_data.use_pickled_cache, cacheDir=args_data.nyu_data_basepath_pickled, annoType=args_data.anno_type, neededCamIds=args_data.needed_cam_ids_test, randomSeed=args_data.seed, cropSize3D=args_data.crop_size_3d_tuple, args_data=args_data), batch_size=args_data.batch_size, sampler=smpl.SubsetRandomSampler(ids_test), **kwargs) print("Using {} samples for test".format(len(ids_test))) else: raise UserWarning("LoaderMode unknown.") elif args_data.dataset_type == DatasetType.ICG: args_data = set_loader_type_specific_settings_icg( args_data, loader_type) dataset = IcgHandPoseMultiViewDataset(args_data) if loader_type == LoaderMode.TRAIN: num_samples = len(dataset) num_samples_labeled_all = dataset.get_num_samples_labeled() num_samples_unlabeled = dataset.get_num_samples_unlabeled() num_samples_labeled_used = min(num_samples_labeled_all, args_data.num_labeled_samples) num_samples_used = num_samples_labeled_used + num_samples_unlabeled # Set up sample IDs to sample from ids_train = np.arange(num_samples) ids_train_labeled_all = ids_train[:num_samples_labeled_all] ids_train_labeled_perm = args_data.rng.permutation( ids_train_labeled_all) ids_train_labeled = ids_train_labeled_perm[: num_samples_labeled_used] ids_train_unlabeled = ids_train[num_samples_labeled_all:] # Ensure a minimum sampling probability for labeled samples ratio_labeled = len(ids_train_labeled) / float(num_samples_used) prob_labeled = max(args_data.min_sampling_prob_labeled, ratio_labeled) prob_unlabeled = 1.0 - prob_labeled # Set up distribution/weights to sample from (considering un-/labeled samples) scale_weights = float( num_samples_used) # value to which weights will sum up sample_weight_labeled = prob_labeled * scale_weights / float( len(ids_train_labeled)) sample_weight_unlabeled = prob_unlabeled * scale_weights \ / float(len(ids_train_unlabeled)) \ if len(ids_train_unlabeled) > 0 else 0.0 sampling_weights = np.zeros(len(ids_train)) sampling_weights[ids_train_labeled] = sample_weight_labeled sampling_weights[ids_train_unlabeled] = sample_weight_unlabeled num_samples_used_for_train = np.count_nonzero(sampling_weights) loader = torch.utils.data.DataLoader( dataset, batch_size=args_data.batch_size, sampler=smpl.WeightedRandomSampler( sampling_weights, num_samples=num_samples_used_for_train, replacement=True), **kwargs) print("Using {} samples for training".format( num_samples_used_for_train)) if sample_weight_labeled > 0.: print(" {} labeled".format(len(ids_train_labeled))) if sample_weight_unlabeled > 0.: print(" {} unlabeled".format(len(ids_train_unlabeled))) elif loader_type == LoaderMode.VAL: # Prepare val. sample IDs ids_val = np.arange(len(dataset)) if args_data.do_val_only_with_labeled: num_samples_labeled_all = dataset.get_num_samples_labeled() ids_val = np.arange(num_samples_labeled_all) # Use subset? max_num_samples_val = int( round(args_data.max_val_train_ratio * args_data.num_labeled_samples)) num_samples_val = min(max_num_samples_val, len(ids_val)) ids_val = args_data.rng.permutation(ids_val) ids_val = ids_val[:num_samples_val] loader = torch.utils.data.DataLoader( dataset, batch_size=args_data.batch_size, sampler=smpl.SubsetRandomSampler(ids_val), **kwargs) print("Using {} samples for validation".format(len(ids_val))) print( " {} labeled (might be incorrect if a subset is used (e.g., wrt. train set size))" .format(dataset.get_num_samples_labeled())) if not args_data.do_val_only_with_labeled: print( " {} unlabeled (might be incorrect if a subset is used (e.g., wrt. train set size))" .format(dataset.get_num_samples_unlabeled())) elif loader_type == LoaderMode.TEST: # Prepare test sample IDs ids_test = np.arange(len(dataset)) if args_data.do_test_only_with_labeled: num_samples_labeled_all = dataset.get_num_samples_labeled() ids_test = np.arange(num_samples_labeled_all) loader = torch.utils.data.DataLoader( dataset, batch_size=args_data.batch_size, sampler=smpl.SubsetRandomSampler(ids_test), **kwargs) print("Using {} samples for test".format(len(ids_test))) print(" {} labeled".format(dataset.get_num_samples_labeled())) if not args_data.do_test_only_with_labeled: print(" {} unlabeled".format( dataset.get_num_samples_unlabeled())) else: raise UserWarning("DatasetType unknown.") return loader
def create_loader_preview(loader_type, args_data, do_use_gpu, num_loader_workers, min_samp_prob_label, batch_size, cam_ids_real, cam_ids_synth, seed, id_range=[], ids_train_permuted=[]): # Sanity check if not loader_type == LoaderMode.TRAIN: print("Required loader-type {} not implemented.".format(loader_type)) raise UserWarning("requested loader generation currently only implemented for TRAINING data loaders") kwargs = {'num_workers': num_loader_workers, 'pin_memory': True} if do_use_gpu else {} if args_data.dataset_type == DatasetType.NYU: if len(id_range) == 0: id_range = [args_data.id_start_train, args_data.id_end_train+1] # Set up sample IDs to sample from ids_train = np.arange(*id_range) if len(ids_train_permuted) == 0: ids_train_permuted = args_data.rng.permutation(ids_train) ids_train_labeled = ids_train_permuted[:args_data.num_labeled_samples] ids_train_unlabeled = ids_train_permuted[args_data.num_labeled_samples:] # Ensure a minimum sampling probability for labeled samples ratio_labeled = len(ids_train_labeled) / float(len(ids_train)) prob_labeled = max(min_samp_prob_label, ratio_labeled) prob_unlabeled = 1.0 - prob_labeled # Set up distribution/weights to sample from (considering un-/labeled samples) scale_weights = float(len(ids_train)) # value to which weights will sum up sample_weight_labeled = prob_labeled * scale_weights / float(len(ids_train_labeled)) sample_weight_unlabeled = prob_unlabeled * scale_weights \ / float(len(ids_train_unlabeled)) \ if len(ids_train_unlabeled) > 0 else 0.0 sampling_weights = np.zeros((args_data.num_all_samples_train)) sampling_weights[ids_train_labeled] = sample_weight_labeled sampling_weights[ids_train_unlabeled] = sample_weight_unlabeled num_samples_used_for_train = np.count_nonzero(sampling_weights) loader = torch.utils.data.DataLoader( NyuHandPoseMultiViewDataset(args_data.nyu_data_basepath, train=True, cropSize=args_data.in_crop_size, doJitterCom=args_data.do_jitter_com, sigmaCom=args_data.sigma_com, doAddWhiteNoise=args_data.do_add_white_noise, sigmaNoise=args_data.sigma_noise, unlabeledSampleIds=ids_train_unlabeled, transform=transforms.ToTensor(), useCache=args_data.use_pickled_cache, cacheDir=args_data.nyu_data_basepath_pickled, annoType=args_data.anno_type, neededCamIdsReal=cam_ids_real, neededCamIdsSynth=cam_ids_synth, randomSeed=seed, cropSize3D=args_data.crop_size_3d_tuple, args_data=args_data), batch_size=batch_size, sampler=smpl.WeightedRandomSampler(sampling_weights, num_samples=num_samples_used_for_train, replacement=True), **kwargs) print("Using {} samples for training".format(num_samples_used_for_train)) if sample_weight_labeled > 0.: print(" {} labeled".format(len(ids_train_labeled))) if sample_weight_unlabeled > 0.: print(" {} unlabeled".format(len(ids_train_unlabeled))) return loader
def create_dataloader(loader_type, args): """ Create a data loader according to the parameters """ kwargs = {'num_workers': args.num_loader_workers, 'pin_memory': True} if args.cuda else {} if args.dataset_type == DatasetType.NYU: if loader_type == LoaderMode.VAL: print("===> Creating validation data loader...") needed_cam_ids = np.append(args.output_cam_ids_test, args.needed_cam_ids_test) needed_cam_ids_synth = [] num_samples_val = min(int(round(args.max_val_train_ratio * args.num_labeled_samples)), args.max_num_samples_val) ids_val = np.arange(args.id_start_val, args.id_end_val+1) ids_val = args.rng.permutation(ids_val) ids_val = ids_val[:num_samples_val] loader = torch.utils.data.DataLoader( NyuHandPoseMultiViewDataset(args.nyu_data_basepath, train=False, cropSize=args.in_crop_size, doJitterCom=args.do_jitter_com_test, sigmaCom=args.sigma_com, doAddWhiteNoise=args.do_add_white_noise_test, sigmaNoise=args.sigma_noise, transform=transforms.ToTensor(), useCache=args.use_pickled_cache, cacheDir=args.nyu_data_basepath_pickled, annoType=args.anno_type, neededCamIdsReal=needed_cam_ids, neededCamIdsSynth=needed_cam_ids_synth, randomSeed=args.seed, cropSize3D=args.crop_size_3d_tuple, args_data=args), batch_size=args.batch_size, sampler=smpl.SubsetRandomSampler(ids_val), **kwargs) print("Using {} samples for validation".format(len(ids_val))) elif loader_type == LoaderMode.TEST: print("===> Creating test data loader...") needed_cam_ids_synth = [] loader = torch.utils.data.DataLoader( NyuHandPoseMultiViewDataset(args.nyu_data_basepath, train=False, cropSize=args.in_crop_size, doJitterCom=args.do_jitter_com_test, sigmaCom=args.sigma_com, doAddWhiteNoise=args.do_add_white_noise_test, sigmaNoise=args.sigma_noise, transform=transforms.ToTensor(), useCache=args.use_pickled_cache, cacheDir=args.nyu_data_basepath_pickled, annoType=args.anno_type, neededCamIdsReal=args.needed_cam_ids_test, neededCamIdsSynth=needed_cam_ids_synth, randomSeed=args.seed, cropSize3D=args.crop_size_3d_tuple, args_data=args), batch_size=args.batch_size, **kwargs) print("Using {} samples for test".format(len(loader.sampler))) else: raise UserWarning("LoaderMode implemented.") else: raise UserWarning("DatasetType not implemented.") return loader