def __getitem__(self, index): np.random.seed(index) normal_path, img_list, dirs = self._get_input_path(index) normal = util.exr_to_array(OpenEXR.InputFile(normal_path), 'normal') imgs = [] for i in img_list: img = util.exr_to_array(OpenEXR.InputFile(i), 'color') imgs.append(img) img = np.concatenate(imgs, 2) h, w, c = img.shape mask = pms_transforms.normal_to_mask(normal, 0.2) normal = normal * mask.repeat(3, 2) # As the image intensities of the dark materials are very small, we Scale up the magnitude of the synthetic images ratio = mask.sum().astype( np.float) / (mask.shape[0] * mask.shape[1] ) # ratio of object area in the whole image thres = 0.02 if img.mean( ) / ratio < thres: # if the mean value of the object region less than 0.02 # scale the mean value of the object region to thres (i.e., 0.02) img *= thres / (img.mean() / ratio) img = (img * 1.5).clip(0, 2) if self.args.int_aug: # and not no_int_aug: ints = pms_transforms.get_intensity(len(imgs)) img = np.dot(img, np.diag(ints.reshape(-1))) else: ints = np.ones(c) if self.args.test_resc: img, normal = pms_transforms.rescale( img, normal, [self.args.test_h, self.args.test_w]) mask = pms_transforms.rescale_single( mask, [self.args.test_h, self.args.test_w]) norm = np.sqrt((normal * normal).sum(2, keepdims=True)) normal = normal / (norm + 1e-10) item = {'normal': normal, 'img': img, 'mask': mask} proxys = pms_transforms.get_proxy_features(self.args, normal, dirs) for k in proxys: item[k] = proxys[k] for k in item.keys(): item[k] = pms_transforms.array_to_tensor(item[k]) item['dirs'] = torch.from_numpy(dirs).view(-1, 1, 1).float() item['ints'] = torch.from_numpy(ints).view(-1, 1, 1).float() item['obj'] = '_'.join(self.shape_list[index // self.repeat].split('/')) item['path'] = os.path.join(self.root, 'Images', self.shape_list[index // self.repeat]) return item
def __getitem__(self, index): obj = self.objs[index] select_idx = np.random.permutation(len( self.names))[:self.args.in_img_num] img_list = [ os.path.join(self.root, obj, 'Objects', self.names[i]) for i in select_idx ] if obj in ['cat']: dirs = np.genfromtxt(os.path.join(self.root, obj, 'refined_light.txt'), dtype='float', delimiter=',') else: dirs = np.genfromtxt(os.path.join(self.root, obj, 'refined_light.txt'), dtype='float') dirs = dirs.transpose()[select_idx] normal_path = os.path.join(self.root, obj, 'result.mat') normal = sio.loadmat(normal_path)['n'] normal[np.isnan(normal)] = 0 normal = np.array(np.flip(normal, 0)) imgs = [] for idx, img_name in enumerate(img_list): img = imread(img_name).astype(np.float32) / 255.0 hw = img.shape t_img = img.reshape(hw[0], hw[1], 1) img = np.repeat(t_img, 3, 2) imgs.append(img) img = np.concatenate(imgs, 2) mask = pms_transforms.normal_to_mask(normal) if self.args.test_resc: img, normal = pms_transforms.rescale( img, normal, [self.args.test_h, self.args.test_w]) mask = pms_transforms.rescale_single( mask, [self.args.test_h, self.args.test_w], 0) img = img * mask.repeat(img.shape[2], 2) item = {'normal': normal, 'img': img, 'mask': mask} downsample = 4 for k in item.keys(): item[k] = pms_transforms.imgsize_to_factor_of_k( item[k], downsample) for k in item.keys(): item[k] = pms_transforms.array_to_tensor(item[k]) item['dirs'] = torch.from_numpy(dirs).view(-1, 1, 1).float() item['ints'] = torch.ones(dirs.shape).view(-1, 1, 1).float() item['obj'] = obj item['path'] = os.path.join(self.root, obj) return item
def __getitem__(self, index): normal_path, img_list, dirs = self._get_input_path(index) normal = imread(normal_path).astype(np.float32) / 255.0 * 2 - 1 mask = pms_transforms.normal_to_mask(normal) normal = normal * mask.repeat(3, 2) # set background to [0, 0, 0] imgs = [] for i in img_list: img = imread(i).astype(np.float32) / 255.0 imgs.append(img) img = np.concatenate(imgs, 2) h, w, c = img.shape crop_h, crop_w = self.args.crop_h, self.args.crop_w if self.args.rescale and not (crop_h == h): sc_h = np.random.randint( crop_h, h) if self.args.rand_sc else self.args.scale_h sc_w = np.random.randint( crop_w, w) if self.args.rand_sc else self.args.scale_w img, normal = pms_transforms.rescale(img, normal, [sc_h, sc_w]) if self.args.crop: img, normal = pms_transforms.random_crop(img, normal, [crop_h, crop_w]) if self.args.color_aug: img = img * np.random.uniform(1, self.args.color_ratio) if self.args.int_aug: ints = pms_transforms.get_intensity(len(imgs)) img = np.dot(img, np.diag(ints.reshape(-1))) else: ints = np.ones(c) if self.args.noise_aug: img = pms_transforms.random_noise_aug(img, self.args.noise) mask = pms_transforms.normal_to_mask(normal) normal = pms_transforms.normalize_to_unit_len(normal, dim=2) normal = normal * mask.repeat(3, 2) # set background to [0, 0, 0] item = {'normal': normal, 'img': img, 'mask': mask} proxys = pms_transforms.get_proxy_features(self.args, normal, dirs) for k in proxys: item[k] = proxys[k] for k in item.keys(): item[k] = pms_transforms.array_to_tensor(item[k]) item['dirs'] = torch.from_numpy(dirs).view(-1, 1, 1).float() item['ints'] = torch.from_numpy(ints).view(-1, 1, 1).float() return item
def __getitem__(self, index): np.random.seed(index) obj = self.objs[index] select_idx = range(len(self.names)) img_list = [ os.path.join(self.root, obj, self.names[i]) for i in select_idx ] ints = [np.diag(1 / self.ints[obj][i]) for i in select_idx] dirs = self.l_dir[select_idx] normal_path = os.path.join(self.root, obj, 'Normal_gt.mat') normal = sio.loadmat(normal_path) normal = normal['Normal_gt'] imgs = [] for idx, img_name in enumerate(img_list): img = imread(img_name).astype(np.float32) / 255.0 if not self.args.int_aug: img = np.dot(img, ints[idx]) imgs.append(img) img = np.concatenate(imgs, 2) mask = self._get_mask(obj) if self.args.test_resc: img, normal = pms_transforms.rescale( img, normal, [self.args.test_h, self.args.test_w]) mask = pms_transforms.rescale_single( mask, [self.args.test_h, self.args.test_w], 0) img = img * mask.repeat(img.shape[2], 2) normal = pms_transforms.normalize_to_unit_len(normal, dim=2) normal = normal * mask.repeat(3, 2) item = {'normal': normal, 'img': img, 'mask': mask} downsample = 4 for k in item.keys(): item[k] = pms_transforms.imgsize_to_factor_of_k( item[k], downsample) proxys = pms_transforms.get_proxy_features(self.args, normal, dirs) for k in proxys: item[k] = proxys[k] for k in item.keys(): item[k] = pms_transforms.array_to_tensor(item[k]) item['dirs'] = torch.from_numpy(dirs).view(-1, 1, 1).float() item['ints'] = torch.from_numpy(self.ints[obj][select_idx]).view( -1, 1, 1).float() item['obj'] = obj item['path'] = os.path.join(self.root, obj) return item
def __getitem__(self, index): obj = self.objs[index // self.args.repeat] item = { 'normal': self.normals[obj], 'img': self.imgs[obj], 'mask': self.masks[obj] } h, w, c = self.imgs[obj].shape proxys = pms_transforms.get_zero_proxy(self.args, h, w, c // 3) for k in proxys: item[k] = proxys[k] for k in item.keys(): item[k] = pms_transforms.array_to_tensor(item[k]) item['dirs'] = torch.from_numpy(self.img_lights[obj]).view(-1, 1, 1).float() item['ints'] = torch.from_numpy(self.img_intens[obj]).view(-1, 1, 1).float() item['obj'] = obj item['path'] = os.path.join(self.root, obj) return item