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):
        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
Beispiel #3
0
    def __getitem__(self, index):
        normal_path, img_list, dirs, reflectance_path,  mask_path = self._getInputPath(index)
        normal = sio.loadmat(normal_path)['normal'].astype(np.float32)
        reflectance = imread(reflectance_path).astype(np.float32) / 255.0
        if(reflectance.shape[2] == 4):
                reflectance = reflectance[:,:,:3]
        imgs   =  []
        for i in img_list:
            img = imread(i).astype(np.float32) / 255.0
            if(img.shape[2] == 4):
                img = img[:,:,:3]
            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.randomCrop(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.getIntensity(len(imgs))
            img  = np.dot(img, np.diag(ints.reshape(-1)))
        else:
            ints = np.ones(c)

        # if self.args.noise_aug:
        #     img = pms_transforms.randomNoiseAug(img, self.args.noise)

        mask = sio.loadmat(mask_path)['mask'].astype(np.float32)
        norm   = np.sqrt((normal * normal).sum(2, keepdims=True))
        normal = normal / (norm + 1e-10) # Rescale normal to unit length

        item = {'normal': normal, 'img': img, 'reflectance': reflectance}
        for k in item.keys(): 
            item[k] = pms_transforms.arrayToTensor(item[k])
        #item['dirs'] = torch.from_numpy(dirs).view(-1, 1, 1).float()
        item['lights'] = torch.from_numpy(dirs).view(-1, 1, 1).float()
        item['ints'] = torch.from_numpy(ints).view(-1, 1, 1).float()
        item['mask'] = torch.from_numpy(mask).unsqueeze(0)
        
        # normal : torch.Size([3, 128, 128])
        # img : torch.Size([6, 128, 128])
        # mask : torch.Size([1, 128, 128])
        # dirs : torch.Size([6, 1, 1])
        # ints : torch.Size([6, 1, 1])

        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
Beispiel #5
0
    def __getitem__(self, index):
        np.random.seed(index)
        obj = self.objs[index]
        f = os.path.join(self.root, 'diligent_test') + '/' + str(
            self.test_set) + '/' + obj + '.txt'
        idx_read = np.loadtxt(f, dtype=int, delimiter='\\')
        select_idx = list(idx_read - 1)
        # 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_gt']

        imgs = []
        for idx, img_name in enumerate(img_list):
            img = imread(img_name).astype(np.float32) / 255.0
            # if self.args.in_light and not self.args.int_aug:
            if not self.args.int_aug:
                img = np.dot(img, ints[idx])
            imgs.append(img)
        img = np.concatenate(imgs, 2)

        mask = self._getMask(obj)
        if self.args.test_resc:
            img, normal = pms_transforms.rescale(
                img, normal, [self.args.test_h, self.args.test_w])
            mask = pms_transforms.rescaleSingle(
                mask, [self.args.test_h, self.args.test_w])

        img = img * mask.repeat(img.shape[2], 2)

        norm = np.sqrt((normal * normal).sum(2, keepdims=True))
        normal = normal / (norm + 1e-10)

        item = {'normal': normal, 'img': img, 'mask': mask}

        downsample = 4
        for k in item.keys():
            item[k] = pms_transforms.imgSizeToFactorOfK(item[k], downsample)

        for k in item.keys():
            item[k] = pms_transforms.arrayToTensor(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
Beispiel #6
0
    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
Beispiel #7
0
    def __getitem__(self, index):
        normal_path, img_list, lights = self._getInputPath(index)
        normal = imread(normal_path).astype(np.float32) / 255.0 * 2 - 1
        imgs = []
        shadows = []
        for i in img_list:
            img = imread(i).astype(np.float32) / 255.0
            if self.args.shadow:
                s = imread(i.replace('/l_', '/s_')).astype(np.float32) / 255.0
                shadows.append(s[:, :, 0:1])
            imgs.append(img)

        img = np.concatenate(imgs, 2)
        shadows = np.concatenate(shadows, 2)
        img = np.concatenate([img, shadows], 2)

        h, w, c = img.shape
        crop_h, crop_w = self.args.crop_h, self.args.crop_w
        if self.args.rescale:
            sc_h = np.random.randint(crop_h, h)
            sc_w = np.random.randint(crop_w, w)
            img, normal = pms_transforms.rescale(img, normal, [sc_h, sc_w])

        if self.args.crop:
            img, normal = pms_transforms.randomCrop(img, normal,
                                                    [crop_h, crop_w])
        if self.args.shadow:
            shadow = np.empty_like(img[:, :, 96:])
            shadow[:] = img[:, :, 96:]
        if self.args.color_aug:
            img = (img * np.random.uniform(1, 3)).clip(0, 2)

        if self.args.noise_aug:
            img = pms_transforms.randomNoiseAug(img, self.args.noise)

        mask = pms_transforms.normalToMask(normal)
        normal = normal * mask.repeat(3, 2)
        item = {'N': normal, 'img': img, 'mask': mask}
        for k in item.keys():
            item[k] = pms_transforms.arrayToTensor(item[k])

        if self.args.in_light:
            item['light'] = torch.from_numpy(lights).view(-1, 1, 1).float()
        if self.args.shadow:
            item['shadow'] = shadow

        return item
Beispiel #8
0
    def __getitem__(self, index):
        normal_path, img_list, dirs = self._getInputPath(index)
        normal = imread(normal_path).astype(np.float32) / 255.0 * 2 - 1
        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.randomCrop(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.getIntensity(len(imgs))
            img = np.dot(img, np.diag(ints.reshape(-1)))
        else:
            ints = np.ones(c)

        if self.args.noise_aug:
            img = pms_transforms.randomNoiseAug(img, self.args.noise)

        mask = pms_transforms.normalToMask(normal)
        normal = normal * mask.repeat(3, 2)
        norm = np.sqrt((normal * normal).sum(2, keepdims=True))
        normal = normal / (norm + 1e-10)  # Rescale normal to unit length

        item = {'normal': normal, 'img': img, 'mask': mask}
        for k in item.keys():
            item[k] = pms_transforms.arrayToTensor(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
Beispiel #9
0
    def __getitem__(self, index):
        normal_path, img_list, lights = self._getInputPath(index)
        normal = imread(normal_path).astype(np.float32) / 255.0 * 2 - 1
        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:
            sc_h = np.random.randint(crop_h, h)
            sc_w = np.random.randint(crop_w, w)
            img, normal = pms_transforms.rescale(img, normal, [sc_h, sc_w])

        if self.args.crop:
            img, normal = pms_transforms.randomCrop(img, normal,
                                                    [crop_h, crop_w])

        if self.args.color_aug and not self.args.normalize:
            img = (img * np.random.uniform(1, 3)).clip(0, 2)

        if self.args.normalize:
            imgs = np.split(img, img.shape[2] // 3, 2)
            imgs = pms_transforms.normalize(imgs)
            img = np.concatenate(imgs, 2)

        if self.args.noise_aug:
            img = pms_transforms.randomNoiseAug(img, self.args.noise)

        mask = pms_transforms.normalToMask(normal)
        normal = normal * mask.repeat(3, 2)
        norm = np.sqrt((normal * normal).sum(2, keepdims=True))
        normal = normal / (norm + 1e-10)

        item = {'N': normal, 'img': img, 'mask': mask}
        for k in item.keys():
            item[k] = pms_transforms.arrayToTensor(item[k])

        if self.args.in_light:
            item['light'] = torch.from_numpy(lights).view(-1, 1, 1).float()

        return item
Beispiel #10
0
    def __getitem__(self, index):
        np.random.seed(index)
        obj = self.objs[index]

        # if self.args.partial_data:
        #     select_idx = range(48, 96)
        # else:
        #     select_idx = range(48)

        select_idx = range(len(self.names))
        #print('\n', self.args.partial_data)
        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_gt']
        # print('normal:', type(normal), normal.shape)
        # normal: <class 'numpy.ndarray'> (172, 172, 3)

        imgs = []
        for idx, img_name in enumerate(img_list):
            img = imread(img_name).astype(np.float32) / 255.0
            if self.args.in_light and not self.args.int_aug:
                img = np.dot(img, ints[idx])
            imgs.append(img)
        img = np.concatenate(imgs, 2)

        mask = self._getMask(obj)
        if self.args.test_resc:
            img, normal = pms_transforms.rescale(
                img, normal, [self.args.test_h, self.args.test_w])
            mask = pms_transforms.rescaleSingle(
                mask, [self.args.test_h, self.args.test_w])

        img = img * mask.repeat(img.shape[2], 2)

        norm = np.sqrt((normal * normal).sum(2, keepdims=True))
        normal = normal / (norm + 1e-10)

        item = {'normal': normal, 'img': img, 'mask': mask}

        downsample = 4
        for k in item.keys():
            item[k] = pms_transforms.imgSizeToFactorOfK(item[k], downsample)

        for k in item.keys():
            item[k] = pms_transforms.arrayToTensor(item[k])

        item['dirs'] = torch.from_numpy(dirs).view(-1, 1, 1).float()
        #print('\nIn Dataset:', item['img'].shape,type(item['img']))
        # In Dataset: torch.Size([144, 244, 400]) Tensor

        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