find modelnet10 from modelnet40
all_label = np.concatenate(all_label, axis=0)
############################# 提出ModelNet10 的10个类别
all_label_MN10 = []
all_data_MN10 = []
class_10 = [1, 2, 8, 12, 14, 22, 23, 30, 33, 35]
for i in range(len(all_label)):
if all_label[i] in class_10:
if all_label[i] == 1:
all_label[i] = 0
elif all_label[i] == 2:
all_label[i] = 1
elif all_label[i] == 8:
all_label[i] = 2
elif all_label[i] == 12:
all_label[i] = 3
elif all_label[i] == 14:
all_label[i] = 4
elif all_label[i] == 22:
all_label[i] = 5
elif all_label[i] == 23:
all_label[i] = 6
elif all_label[i] == 30:
all_label[i] = 7
elif all_label[i] == 33:
all_label[i] = 8
elif all_label[i] == 35:
all_label[i] = 9
all_data_MN10.append(all_data[i])
all_label_MN10.append(all_label[i])
all_data_MN10 = np.array(all_data_MN10)
all_label_MN10 = np.array(all_label_MN10)
dou du shu ju suo yin
tmp_list = []
for k in range(total_num):
if self.label[k] != self.target_label:
tmp_list.append(k)
np.random.shuffle(tmp_list)
self.poison_set = frozenset(tmp_list[:self.poison_num])
读取训练、测试数据集
MESH_EXTENSIONS = [
'.obj',
]
def find_classes(dir):
classes = [d for d in os.listdir(dir) if os.path.isdir(os.path.join(dir, d))]
classes.sort()
class_to_idx = {classes[i]: i for i in range(len(classes))}
return classes, class_to_idx
def is_mesh_file(filename):
return any(filename.endswith(extension) for extension in MESH_EXTENSIONS)
def make_dataset_by_class(dir, class_to_idx, phase):
meshes = []
dir = os.path.expanduser(dir)
for target in sorted(os.listdir(dir)):
d = os.path.join(dir, target)
if not os.path.isdir(d):
continue
for root, _, fnames in sorted(os.walk(d)):
for fname in sorted(fnames):
if is_mesh_file(fname) and (root.count(phase)==1):
path = os.path.join(root, fname)
item = (path, class_to_idx[target])
meshes.append(item)
return meshes
dirr = 'datasets/shrec'
phase = 'train'
classes, class_to_idx = find_classes(dirr)
paths = make_dataset_by_class(dirr, class_to_idx, phase)
根据旋转向量得到旋转矩阵。
input: 绕x,y,z轴的旋转角度
output: 对应的旋转矩阵
def rotation_xyz(angles):
cos_x, sin_x = np.cos(angles[0]), np.sin(angles[0])
cos_y, sin_y = np.cos(angles[1]), np.sin(angles[1])
cos_z, sin_z = np.cos(angles[2]), np.sin(angles[2])
r_x = np.array([[1, 0, 0],
[0, cos_x, -sin_x],
[0, sin_x, cos_x]])
r_y = np.array([[cos_y, 0, sin_y],
[0, 1, 0],
[-sin_y, 0, cos_y]])
r_z = np.array([[cos_z, sin_z, 0],
[-sin_z, cos_z, 0],
[0, 0, 1]])
rotation_matrix = np.dot(np.dot(r_z, r_y), r_x)
return rotation_matrix
rotation_matrix = rotation_xyz(angles) # 得到旋转矩阵
output_pl = np.matmul(input_pl, rotation_matrix) # 对点云施加旋转
输出点云obj文件
输入:点云坐标,输出文件名
输出:点云对应的obj文件
def output_obj(one_pl, name):
one_pl = np.squeeze(one_pl)
vs = []
for vert_iter in range(len(one_pl)):
vs.append(one_pl[vert_iter])
name = name + '.obj'
path = 'distur_datasets'
if not os.path.exists(path):
os.makedirs(path)
filename = path + '\\' + name
with open(filename, 'w') as file_object:
for i in range(len(vs)):
file_object.write("v {} {} {}\n".format(vs[i][0], vs[i][1], vs[i][2]))
输出meshobj文件
输入:mesh点坐标,面,输出文件名
输出:点云对应的obj文件
def output_obj(vs, faces, name):
name = name + '.obj'
path = 'distur_datasets'
if not os.path.exists(path):
os.makedirs(path)
filename = path + '\\' + name
with open(filename, 'w') as file_object:
for i in range(len(vs)):
file_object.write("v {} {} {}\n".format(vs[i][0], vs[i][1], vs[i][2]))
for i in range(len(faces)):
file_object.write("f {} {} {}\n".format(faces[i][0] +1 , faces[i][1] +1 , faces[i][2] +1 ))
使用pyvista库可视化点云
输入:需要可视化的点云,需要特殊显示的点的坐标
import pyvista as pv
def show_pl(pointclouds_pl_adv, special_index):
pointclouds_pl_adv=pointclouds_pl_adv.squeeze()
p = pv.Plotter()
camera = pv.Camera()
camera.position = (18,4,-20)
camera.focal_point = (0,0,0)
#p.add_mesh(pv.PolyData(pointclouds_pl_map), color="blue", point_size=10, render_points_as_spheres=True)
for i in range(len(pointclouds_pl_adv)):
if i in special_index:
p.add_mesh(pv.PolyData(pointclouds_pl_adv[i]), color=[1, 0, 0], point_size=np.float(11) , render_points_as_spheres=True) #5
else:
p.add_mesh(pv.PolyData(pointclouds_pl_adv[i]), color=[0, 0, 0], point_size=np.float(11), render_points_as_spheres=True)
p.add_background_image('D:\\Desktop\\w.jpg') # 使用白色图片作为背景
p.camera = camera
p.show()