def forward_with_trimesh(self, batch): self.load_batch(batch, include_gt=False) with torch.no_grad(): pred_1 = self.net.depth_and_inpaint.net1.forward(self._input) pred_abs_depth = self.net.depth_and_inpaint.get_abs_depth( pred_1, self._input) proj = self.net.depth_and_inpaint.proj_depth(pred_abs_depth) pred_depth = self.net.depth_and_inpaint.base_class.postprocess( pred_1['depth'].detach()) silhou = self.net.base_class.postprocess(self._input.silhou).detach() pred_depth = pred_depth.cpu().numpy() pred_depth_minmax = pred_1['depth_minmax'].detach().cpu().numpy()[0, :] silhou = silhou.cpu().numpy()[0, 0, :, :] pack = {'depth': pred_depth, 'depth_minmax': pred_depth_minmax} rendered_sph = util_sph.render_spherical(pack, silhou)[None, None, ...] rendered_sph = torch.from_numpy(rendered_sph).float().cuda() rendered_sph = sph_pad(rendered_sph) with torch.no_grad(): out2 = self.net.depth_and_inpaint.net2(rendered_sph) pred_proj_sph = self.net.backproject_spherical(out2['spherical']) pred_proj_sph = torch.transpose(pred_proj_sph, 3, 4) pred_proj_sph = torch.flip(pred_proj_sph, [3]) proj = torch.transpose(proj, 3, 4) proj = torch.flip(proj, [3]) refine_input = torch.cat((pred_proj_sph, proj), dim=1) with torch.no_grad(): pred_voxel = self.net.refine_net(refine_input) pred_1['pred_sph_full'] = out2['spherical'] pred_1['pred_sph_partial'] = rendered_sph pred_1['pred_proj_depth'] = proj pred_1['pred_voxel'] = pred_voxel.flip([3]).transpose(3, 4) pred_1['pred_proj_sph_full'] = pred_proj_sph return pred_1
def depth2sphere(data): ''' :param data: a dictionary of an individual sample, coming from json file :return: sph_map, sph_centered_map ''' depth_path = data['depth_path'] #import pdb; pdb.set_trace() depth_img = skimage.io.imread(depth_path, as_gray=True) data['mask_img'] = np.where(depth_img > 0.001, np.ones_like(depth_img), np.zeros_like(depth_img)) # print("my depth minmax is: ", depth_minmax) depth_img = skimage.util.invert(depth_img) depth_img = depth_img[None, None, :, :] data['depth_img'] = depth_img sph_map, sph_centered_map = render_spherical(data, res=129) #sphere2vox(sph_centered_map) return sph_map, sph_centered_map
def depth2sphere(data): ''' :param data: a dictionary of an individual sample, coming from json file :return: sph_map, sph_centered_map ''' depth_path = data['mask'].replace('mask', 'depth').replace('.png', '1.png').replace('media', 'mnt/hamming') mask_path = data['mask'].replace('mask', 'bin_mask').replace('media', 'mnt/hamming') # import pdb; pdb.set_trace() if not os.path.exists(depth_path) or not os.path.exists(mask_path): return None # import pdb; pdb.set_trace() depth_img = skimage.io.imread(depth_path, as_gray=True) mask_img = skimage.io.imread(mask_path, as_gray=True) # print("my depth minmax is: ", depth_minmax) depth_img = skimage.util.invert(depth_img) depth_img = depth_img[None, None, :, :] data['depth_img'] = depth_img data['mask_img'] = mask_img sph_map, sph_centered_map = render_spherical(data, res=129) return sph_map, sph_centered_map
def depth2sphere(depth_address): mine = True if mine: box = True if box: depth = skimage.io.imread( depth_address, as_gray=True) #[np.newaxis, np.newaxis, ...] mask = skimage.io.imread(depth_address.replace( "depth", "bin_mask").replace("1.png", ".png"), as_gray=True) with open("/mnt/Extra/dsl/GenRe-ShapeHD/gen_sphr/data/0000.json", "r") as j_file: data = json.load(j_file)[0] else: depth_address = depth_address.replace("000001-depth-001", "000381-depth-021") depth = skimage.io.imread( depth_address, as_gray=True) # [np.newaxis, np.newaxis, ...] mask = skimage.io.imread(depth_address.replace( "depth", "bin_mask").replace("1.png", ".png"), as_gray=True) with open("/mnt/Extra/dsl/GenRe-ShapeHD/gen_sphr/data/0006.json", "r") as j_file: data = json.load(j_file)[996] # from scipy import ndimage as nd # depth = nd.zoom(depth, 2, mode="nearest") # mask = nd.zoom(mask, 2, mode="nearest") # plt.imshow(depth) # plt.show() depth_minmax = data["depth_minmax"] print("my depth minmax is: ", depth_minmax) depth = skimage.util.invert(depth) # depth = skimage.util.pad(depth, ([np.abs(np.subtract(*depth.shape))//2]*2, (0, 0)), 'constant', constant_values=0.) # mask = skimage.util.pad(mask, ([np.abs(np.subtract(*mask.shape))//2]*2, (0, 0)), 'constant', constant_values=0.) # plt.imshow(mask_img) # plt.show() # plt.imshow(depth) # plt.show() # plt.imshow(mask) # plt.show() # plt.imshow(depth) # plt.show() else: depth_minmax = np.load( 'data/02691156_fff513f407e00e85a9ced22d91ad7027_view019.npy') depth = skimage.io.imread('data/depth.png', as_gray=True) mask = skimage.io.imread('data/mask.png', as_gray=True) print("his depth minmax is: ", depth_minmax) # plt.imshow(mask) # plt.show() # plt.imshow(depth) # plt.show() depth = depth[None, None, :, :] # print(type(depth)) # print(depth.shape) # There is a better way for sure. I'm not an expert in python... Save and Load was the easiest way for me... # np.savez('/home/pvicente/software/git/GenRe-ShapeHD/tmpData/mine/tmpdata2.npz', depth=depth, # depth_minmax=depth_minmax) # data = np.load('/home/pvicente/software/git/GenRe-ShapeHD/tmpData/mine/tmpdata2.npz') data = {'depth_img': depth, 'depth_minmax': depth_minmax, "mask_img": mask} sph = render_spherical( data, # mask, res=129 if mine else 128 ) # , I change the code in Perlis to receive the res as well, The default is 64 plt.imshow(sph) # plt.show() plt.savefig('depth2sphere.png') sphere2vox(sph.astype(np.float32), res=128 if mine else 128)