Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
gpuy authored Jan 25, 2023
1 parent cd2f57c commit 4b83d98
Showing 1 changed file with 2 additions and 121 deletions.
123 changes: 2 additions & 121 deletions utils/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,124 +188,5 @@ def __call__(self, pcloud, labels):
)

return pc[ind, :], None if labels is None else labels[ind]


class InstanceCutMix(Transformation):
def __init__(self, phase="train"):
"""Instance cutmix coded only for SemanticKITTI"""
super().__init__(inplace=True)

raise ValueError("Include latest verion")

self.phase = phase
self.rootdir = "/root/local_storage/semantic_kitti_instance_" + self.phase
self.bank = {1: [], 2: [], 5: [], 6: [], 7: []}
for key in self.bank.keys():
self.bank[key] = glob(os.path.join(self.rootdir, f"{key}", "*.bin"))
self.loaded = self.test_loaded()
# v2
self.rot = Compose(
(
FlipXY(inplace=True),
Rotation(inplace=True),
Scale(dims=(0, 1, 2), range=0.1, inplace=True),
)
)
self.nb_to_add = 40
self.vox = Voxelize(dims=(0, 1, 2), voxel_size=1.0, random=True)
""" v1
self.rot = Rotation(inplace=False)
self.max_size = 100 # Unused
self.nb_to_add = 20
self.vox = Voxelize(dims=(0, 1, 2), voxel_size=.1, random=True)
"""

def test_loaded(self):
if self.phase == "train":
if len(self.bank[1]) != 5083:
print(len(self.bank[1]), 5083)
return False
if len(self.bank[2]) != 3092:
print(len(self.bank[2]), 3092)
return False
if len(self.bank[5]) != 8084:
print(len(self.bank[5]), 8084)
return False
if len(self.bank[6]) != 1551:
print(len(self.bank[6]), 1551)
return False
if len(self.bank[7]) != 560:
print(len(self.bank[7]), 560)
return False
elif self.phase == "trainval":
if len(self.bank[1]) != 8213:
print(len(self.bank[1]), 8213)
return False
if len(self.bank[2]) != 4169:
print(len(self.bank[2]), 4169)
return False
if len(self.bank[5]) != 12190:
print(len(self.bank[5]), 12190)
return False
if len(self.bank[6]) != 2943:
print(len(self.bank[6]), 2943)
return False
if len(self.bank[7]) != 701:
print(len(self.bank[7]), 701)
return False
return True

def add_in_bank(self, pc, class_label, instance_label):
for id_class in self.bank.keys():
where_class = class_label == id_class
all_instances = np.unique(instance_label[where_class])
for id_instance in all_instances:
# Segment instance
where_ins = instance_label == id_instance
if where_ins.sum() <= 5:
continue
pc_to_add = pc[where_ins, :]
# Center instance
pc_to_add[:, :2] -= pc_to_add[:, :2].mean(0, keepdims=True)
pc_to_add[:, 2] -= pc_to_add[:, 2].min(0, keepdims=True)
#
pathfile = os.path.join(
self.rootdir, f"{id_class}", f"{len(self.bank[id_class]):07d}.bin"
)
os.makedirs(os.path.join(self.rootdir, f"{id_class}"), exist_ok=True)
pc_to_add.tofile(pathfile)
self.bank[id_class].append(pathfile)

def add_in_pc(self, pc, class_label):
new_pc = [pc]
new_label = [class_label]
# Find location where to add new object (on a surface)
pc_vox, class_label_vox = self.vox(pc, class_label)

# v2
where_surface = np.where((class_label_vox >= 8) & (class_label_vox <= 10))[0]

""" v1
where_surface = np.where( ( (class_label_vox>=8) & (class_label_vox<=11) ) | (class_label_vox==16) )[0]
"""

where_surface = where_surface[torch.randperm(len(where_surface))]
id_tot = 0
for id_class in self.bank.keys():
which_one = torch.randint(len(self.bank[id_class]), (self.nb_to_add,))
for ii in range(self.nb_to_add):
p = pc_vox[where_surface[id_tot]]
object = self.bank[id_class][which_one[ii]]
object = np.fromfile(object, dtype=np.float32).reshape((-1, 4))
object, _ = self.rot(object, 1)
object[:, :3] += p[:3][None]
new_pc.append(object)
new_label.append(np.ones((object.shape[0],), dtype=np.int) * id_class)
id_tot += 1
return np.concatenate(new_pc, 0), np.concatenate(new_label, 0)

def __call__(self, pc, class_label, instance_label):
if not self.loaded:
self.add_in_bank(pc, class_label, instance_label)
return np.zeros((2, 4)), None
return self.add_in_pc(pc, class_label)


0 comments on commit 4b83d98

Please sign in to comment.