def __init__(self, garment_class, split=None, gender='female', smooth_level=0, smpl=None): super(MultiStyleShape, self).__init__() self.garment_class = garment_class self.smooth_level = smooth_level self.split, self.gender = split, gender self.smpl = smpl assert(gender in ['neutral', 'male', 'female']) assert(split in ['train', 'test', None, 'train_train', 'train_test', 'test_train', 'test_test']) self.one_style_shape_datasets = self.get_single_datasets() self.ds = ConcatDataset(self.one_style_shape_datasets) if smooth_level == 1 and global_var.SMOOTH_STORED: print("Using Smoothing in the dataset") return if self.smooth_level != 0 and self.smooth_level != -1: print("Using Smoothing in the dataset") print(self.smooth_level, Ltype) with open(os.path.join(global_var.DATA_DIR, global_var.GAR_INFO_FILE), 'rb') as f: class_info = pickle.load(f) num_v = len(class_info[garment_class]['vert_indices']) self.smoothing = DiffusionSmoothing( np.zeros((num_v, 3)), class_info[garment_class]['f']) self.smpl = TorchSMPL4Garment(gender=gender) else: self.smoothing = None self.smpl = None
def save_smooth(): """Helper function to save smooth garment displacements.""" garment_class = 'short-pant' gender = 'male' smooth_level = 1 OUT_DIR = global_var.SMOOTH_DATA_DIR data_dir = os.path.join(global_var.DATA_DIR, '{}_{}'.format(garment_class, gender)) with open(os.path.join(data_dir, "test.txt"), "r") as f: train_pivots = [l.strip().split('_') for l in f.readlines()] with open(os.path.join(global_var.DATA_DIR, global_var.GAR_INFO_FILE), 'rb') as f: class_info = pickle.load(f) num_v = len(class_info[garment_class]['vert_indices']) smoothing = DiffusionSmoothing( np.zeros((num_v, 3)), class_info[garment_class]['f']) smpl = TorchSMPL4Garment(gender=gender) for shape_idx, style_idx in train_pivots: beta = torch.from_numpy(np.load(os.path.join( data_dir, 'shape/beta_{}.npy'.format(shape_idx))).astype(np.float32)[:10]) gamma = torch.from_numpy(np.load(os.path.join( data_dir, 'style/gamma_{}.npy'.format(shape_idx))).astype(np.float32)) outdir = os.path.join(OUT_DIR, "{}_{}".format(garment_class, gender), "pose/{}_{}".format(shape_idx, style_idx)) if not os.path.exists(outdir): os.makedirs(outdir) seq_idx = 0 while True: seq_path = os.path.join(data_dir, 'pose/{}_{}/poses_{:03d}.npz'.format(shape_idx, style_idx, seq_idx)) if not os.path.exists(seq_path): break data = np.load(seq_path) verts_d_path = os.path.join(data_dir, 'pose/{}_{}/unposed_{:03d}.npy'.format(shape_idx, style_idx, seq_idx)) if not os.path.exists(verts_d_path): print("{} doesn't exist.".format(verts_d_path)) seq_idx += 1 continue outpath = os.path.join(outdir, "smooth_unposed_{:03d}.npy".format(seq_idx)) if os.path.exists(outpath): print("{} exists.".format(outpath)) seq_idx += 1 continue print(verts_d_path) thetas = torch.from_numpy(data['thetas'].astype(np.float32)) verts_d = torch.from_numpy(np.load(verts_d_path).astype(np.float32)) smooth_verts_d = [] for theta, vert_d in zip(thetas, verts_d): svert_d = smooth_it(smoothing, smooth_level, smpl, theta, beta, vert_d, garment_class) smooth_verts_d.append(svert_d.numpy()) smooth_verts_d = np.stack(smooth_verts_d) np.save(outpath, smooth_verts_d) seq_idx += 1
betas[7, 1] = -1 vcanonical = smpl(np.zeros_like(betas[0]), apose) for gc in ['skirt']: gc_gender_dir = osp.join(ROOT, '{}_{}'.format(gc, gender)) shape_dir = osp.join(gc_gender_dir, 'shape') save_dir = osp.join(gc_gender_dir, 'style_shape') style_dir = osp.join(gc_gender_dir, 'style') if not osp.exists(shape_dir): os.makedirs(shape_dir) if not osp.exists(save_dir): os.makedirs(save_dir) if not osp.exists(style_dir): os.makedirs(style_dir) part_faces = part_body_faces(gc) ds = DS(smpl.base.np_v_template, part_faces) # gamma gammas = [] gammas.append([0., 0., 0., 0.]) # for x1 in np.linspace(-1, 1, 11): # if x1 == 0: # continue # gammas.append([x1, 0, 0, 0]) for x1 in np.linspace(-1, 1, 7): for x2 in np.linspace(-1, 1, 3): if x1 == 0 and x2 == 0: continue gammas.append([x1, x2, 0, 0]) gammas = np.array(gammas, dtype=np.float32) np.save(osp.join(style_dir, 'gammas.npy'), gammas)
def __init__(self, garment_class, shape_idx, style_idx, split, gender='female', smooth_level=0): super(OneStyleShape, self).__init__() self.garment_class = garment_class self.split, self.gender = split, gender self.style_idx, self.shape_idx = style_idx, shape_idx self.smooth_level = smooth_level data_dir = os.path.join(global_var.DATA_DIR, '{}_{}'.format(garment_class, gender)) beta = np.load(os.path.join(data_dir, 'shape/beta_{}.npy'.format(shape_idx))) gamma = np.load(os.path.join(data_dir, 'style/gamma_{}.npy'.format(style_idx))) # I had a hard time figuring out the bug in the following line: # gamma = np.load(os.path.join(data_dir, 'style/gamma_{}.npy'.format(shape_idx))) thetas = [] pose_order = [] verts_d = [] smooth_verts_d = [] seq_idx = 0 while True: seq_path = os.path.join(data_dir, 'pose/{}_{}/poses_{:03d}.npz'.format(shape_idx, style_idx, seq_idx)) if not os.path.exists(seq_path): break data = np.load(seq_path) verts_d_path = os.path.join(data_dir, 'pose/{}_{}/unposed_{:03d}.npy'.format(shape_idx, style_idx, seq_idx)) if not os.path.exists(verts_d_path): print("{} doesn't exist. This is not an error. " "It's just that this sequence was not simulated well.".format(verts_d_path)) seq_idx += 1 continue thetas.append(data['thetas']) pose_order.append(data['pose_order']) verts_d.append(np.load(verts_d_path)) if smooth_level == 1 and global_var.SMOOTH_STORED: smooth_verts_d_path = os.path.join( global_var.SMOOTH_DATA_DIR, '{}_{}'.format(garment_class, gender), 'pose/{}_{}/smooth_unposed_{:03d}.npy'.format(shape_idx, style_idx, seq_idx)) if not os.path.exists(smooth_verts_d_path): print("{} doesn't exist.".format(smooth_verts_d_path)) exit(-1) smooth_verts_d.append(np.load(smooth_verts_d_path)) seq_idx += 1 # print("Using just one sequence file") # break thetas = np.concatenate(thetas, axis=0) pose_order = np.concatenate(pose_order, axis=0) verts_d = np.concatenate(verts_d, axis=0) if smooth_level == 1 and global_var.SMOOTH_STORED: smooth_verts_d = np.concatenate(smooth_verts_d, axis=0) if split is not None: assert(split in ['test', 'train']) # SMPL has 1782 poses. We set aside 350 poses as test set and remaining in train set. So if a frame has a # pose from these 1782 poses, it's easy to classify them as train or test. # But during simulation of these poses, we add some intermediate poses for simulation stability. # To classify these intermediate poses in test and train split, we follow this policy: # - For train pivots, intermediate poses go into train set because there are significant amount of # intermediate poses and we can't afford to give them away during training. # - For test pivots, we add intermediate poses to test set. Assuming that intermediate poses are randomly # distributed, it's fair to assume that any intermediate test pose will be unseen from training. split_file_path = os.path.join(global_var.DATA_DIR, global_var.POSE_SPLIT_FILE) if seq_idx > 1: # train pivot test_orig_idx = np.load(split_file_path)['test'] test_idx = np.in1d(pose_order, test_orig_idx) chosen_idx = np.where(test_idx)[0] if split == 'test' else np.where(~test_idx)[0] else: # test pivot train_orig_idx = np.load(split_file_path)['train'] train_idx = np.in1d(pose_order, train_orig_idx) chosen_idx = np.where(train_idx)[0] if split == 'train' else np.where(~train_idx)[0] thetas = thetas[chosen_idx] verts_d = verts_d[chosen_idx] if smooth_level == 1 and global_var.SMOOTH_STORED: smooth_verts_d = smooth_verts_d[chosen_idx] self.verts_d = torch.from_numpy(verts_d.astype(np.float32)) self.thetas = torch.from_numpy(thetas.astype(np.float32)) self.beta = torch.from_numpy(beta[:10].astype(np.float32)) self.gamma = torch.from_numpy(gamma.astype(np.float32)) if smooth_level == 1 and global_var.SMOOTH_STORED: self.smooth_verts_d = torch.from_numpy(smooth_verts_d.astype(np.float32)) return if self.smooth_level != 0 and self.smooth_level != -1: with open(os.path.join(global_var.DATA_DIR, global_var.GAR_INFO_FILE), 'rb') as f: class_info = pickle.load(f) num_v = len(class_info[garment_class]['vert_indices']) self.smoothing = DiffusionSmoothing( np.zeros((num_v, 3)), class_info[garment_class]['f']) self.smpl = TorchSMPL4Garment(gender=gender) else: self.smoothing = None self.smpl = None
class_info = pickle.load(f, encoding='latin-1') pant_ind = class_info['pant']['vert_indices'] pant_bnd = np.load(osp.join(ROOT, 'pant_upper_boundary.npy')) skirt_bnd = np.load(osp.join(ROOT, 'skirt_upper_boundary.npy')) pant_bnd_loc = np.mean(canonical_body[pant_ind][pant_bnd], 0) all_v = [] for people_name, v in zip(people_names, verts): skirt_bnd_loc = np.mean(v[skirt_bnd], 0) trans = (pant_bnd_loc - skirt_bnd_loc)[None] trans_v = v + trans all_v.append(trans_v) all_v = np.array(all_v).astype(np.float32) np.save(os.path.join(SAVE_DIR, '{}.npy'.format(garment_class)), all_v) np.save(os.path.join(SAVE_DIR, '{}_trans.npy'.format(garment_class)), all_v) # smoothing ds = DS(all_v[0], faces) smooth_step = 100 smooth_vs = [] for unsmooth_v in tqdm(all_v): smooth_v = unsmooth_v.copy() for _ in range(smooth_step): smooth_v = ds.smooth(smooth_v, smoothness=0.03) smooth_vs.append(smooth_v) smooth_vs = np.array(smooth_vs, dtype=np.float32) np.save(osp.join(SAVE_DIR, '{}_smooth.npy'.format(garment_class)), smooth_vs)
gc_gender_dir = osp.join(ROOT, '{}_{}'.format(gc, gender)) shape_dir = osp.join(gc_gender_dir, 'shape') save_dir = osp.join(gc_gender_dir, 'style_shape') style_dir = osp.join(gc_gender_dir, 'style') if not osp.exists(shape_dir): os.makedirs(shape_dir) if not osp.exists(save_dir): os.makedirs(save_dir) if not osp.exists(style_dir): os.makedirs(style_dir) # shape np.save(osp.join(shape_dir, 'betas.npy'), betas) part_faces = part_body_faces(gc) start_body_list = [] ds = DS(smpl.base.np_v_template, part_faces) for i, beta in enumerate(betas): np.save(osp.join(shape_dir, 'beta_{:03d}.npy'.format(i)), beta) vbeta = smpl(beta, apose) # smoothing sm_body_list = [] sm_body = np.copy(vbeta) for sm_i in range(SM_FNUM): sm_body = ds.smooth(sm_body, smoothness=0.2) sm_body = ds.smooth(sm_body, smoothness=0.2) sm_body_list.append(sm_body) sm_body_list = np.array(sm_body_list)[::-1] vstart = sm_body_list[0]