import os import glob import random import yaml import torch from torch.utils import data import numpy as np import json from PIL import Image import h5py import torch.distributed as dist import open3d as o3d o3d.utility.set_verbosity_level(o3d.utility.VerbosityLevel.Error) import pickle as p import time import cv2 from torchvision import transforms import copy from datasets.taxonomy import category_map_from_synthetic as category_ids class Object_Occ(data.Dataset): def __init__(self, dataset_folder, split, categories=['03001627', "future_chair", 'ABO_chair'], transform=None, sampling=True, num_samples=4096, return_surface=True, surface_sampling=True, surface_size=2048, replica=16): self.pc_size = surface_size self.transform = transform self.num_samples = num_samples self.sampling = sampling self.split = split self.dataset_folder = dataset_folder self.return_surface = return_surface self.surface_sampling = surface_sampling self.dataset_folder = dataset_folder self.point_folder = os.path.join(self.dataset_folder, 'occ_data') self.mesh_folder = os.path.join(self.dataset_folder, 'other_data') if categories is None: categories = os.listdir(self.point_folder) categories = [c for c in categories if os.path.isdir(os.path.join(self.point_folder, c)) and c.startswith('0')] categories.sort() print(categories) self.models = [] for c_idx, c in enumerate(categories): subpath = os.path.join(self.point_folder, c) print(subpath) assert os.path.isdir(subpath) split_file = os.path.join(subpath, split + '.lst') with open(split_file, 'r') as f: models_c = f.readlines() models_c = [item.rstrip('\n') for item in models_c] for m in models_c[:]: if len(m)<=1: continue if m.endswith('.npz'): model_id = m[:-4] else: model_id = m self.models.append({ 'category': c, 'model': model_id }) self.replica = replica def __getitem__(self, idx): if self.replica >= 1: idx = idx % len(self.models) else: random_segment = random.randint(0, int(1 / self.replica) - 1) idx = int(random_segment * self.replica * len(self.models) + idx) category = self.models[idx]['category'] model = self.models[idx]['model'] point_path = os.path.join(self.point_folder, category, model + '.npz') # print(point_path) try: start_t = time.time() with np.load(point_path) as data: vol_points = data['vol_points'] vol_label = data['vol_label'] near_points = data['near_points'] near_label = data['near_label'] end_t = time.time() # print("loading time %f"%(end_t-start_t)) except Exception as e: print(e) print(point_path) with open(point_path.replace('.npz', '.npy'), 'rb') as f: scale = np.load(f).item() # scale=1.0 if self.return_surface: pc_path = os.path.join(self.mesh_folder, category, '4_pointcloud', model + '.npz') with np.load(pc_path) as data: try: surface = data['points'].astype(np.float32) except: print(pc_path,"has problems") raise AttributeError surface = surface * scale if self.surface_sampling: ind = np.random.default_rng().choice(surface.shape[0], self.pc_size, replace=False) surface = surface[ind] surface = torch.from_numpy(surface) if self.sampling: '''need to conduct label balancing''' vol_ind=np.random.default_rng().choice(vol_points.shape[0], self.num_samples, replace=(vol_points.shape[0]= 1: idx = idx % len(self.models) else: random_segment = random.randint(0, int(1 / self.replica) - 1) idx = int(random_segment * self.replica * len(self.models) + idx) category = self.models[idx]['category'] model = self.models[idx]['model'] #image_filenames = self.model_images_names[model] image_filenames = self.models[idx]["image_filenames"] if self.split=="train": n_frames = np.random.randint(min(2,len(image_filenames)), min(len(image_filenames) + 1, self.max_img_length + 1)) img_indexes = np.random.choice(len(image_filenames), n_frames, replace=(n_frames > len(image_filenames))).tolist() else: if self.eval_multiview: '''use all images''' n_frames=len(image_filenames) img_indexes=[i for i in range(n_frames)] else: n_frames = min(len(image_filenames),self.max_img_length) img_indexes=np.linspace(start=0,stop=len(image_filenames)-1,num=n_frames).astype(np.int32) partial_filenames = self.models[idx]['partial_filenames'] par_index = np.random.choice(len(partial_filenames), 1)[0] partial_name = partial_filenames[par_index] vol_points,vol_label,near_points,near_label=None,None,None,None points,labels=None,None point_path = os.path.join(self.point_folder, category, model + '.npz') if self.ret_sample: vol_points,vol_label,near_points,near_label=self.load_samples(point_path) points,labels = self.process_samples(vol_points, vol_label, near_points,near_label) with open(point_path.replace('.npz', '.npy'), 'rb') as f: scale = np.load(f).item() surface=None pc_path = os.path.join(self.mesh_folder, category, '4_pointcloud', model + '.npz') if self.return_surface: surface=self.load_surface(pc_path,scale) partial_path = os.path.join(self.mesh_folder, category, "5_partial_points", model, partial_name) if self.par_point_aug is not None and random.random()