当前位置: 首页 > news >正文

cdr做好排班怎么做网站原创文章代写

cdr做好排班怎么做网站,原创文章代写,淄博建站网络公司,网站建设代理成本今天我们将讨论如何使用 Python 多进程来处理大量3D数据。 我将讲述一些可能在手册中找到的一般信息,并分享我发现的一些小技巧,例如将 tqdm 与多处理 imap 结合使用以及并行处理存档。 那么我们为什么要诉诸并行计算呢? 使用数据有时会出现…

今天我们将讨论如何使用 Python 多进程来处理大量3D数据。 我将讲述一些可能在手册中找到的一般信息,并分享我发现的一些小技巧,例如将 tqdm 与多处理 imap 结合使用以及并行处理存档。

那么我们为什么要诉诸并行计算呢? 使用数据有时会出现与大数据相关的问题。 每次我们遇到 RAM 不适合的数据时,我们都需要逐段处理它。 幸运的是,现代编程语言允许我们生成在多核处理器上完美工作的多个进程(甚至线程)。注意:这并不意味着单核处理器无法处理多处理,这是有关该主题的 Stack Overflow 讨论。

今天我们将尝试计算网格和点云之间的距离这一常见的 3D 计算机视觉任务。 例如,当你需要在所有可用网格中查找定义与给定点云相同的 3D 对象的网格时,可能会遇到此问题。

我们的数据由存储在 .7z 存档中的 .obj 文件组成,这在存储效率方面非常出色。 但是当我们需要访问它的确切部分时,我们应该付出努力。 在这里,我定义了包装 7-zip 存档并提供底层数据接口的类。

from io import BytesIO
import py7zlibclass MeshesArchive(object):def __init__(self, archive_path):fp = open(archive_path, 'rb')self.archive = py7zlib.Archive7z(fp)self.archive_path = archive_pathself.names_list = self.archive.getnames()def __len__(self):return len(self.names_list)def get(self, name):bytes_io = BytesIO(self.archive.getmember(name).read())return bytes_iodef __getitem__(self, idx):return self.get(self.names[idx])def __iter__(self):for name in self.names_list:yield self.get(name)

这个类几乎不依赖 py7zlib 包,它允许我们在每次调用 get 方法时解压缩数据,并为我们提供存档内的文件数量。 我们还定义了 __iter__ ,它将帮助我们像在可迭代对象上一样在该对象上启动多处理映射。

这个定义为我们提供了迭代存档的可能性,但它是否允许我们并行随机访问内容? 这是一个有趣的问题,我在网上没有找到答案,但如果深入研究 py7zlib 的源代码,我们可以回答它。

在这里,我提供了 pylzma 的代码片段:

class Archive7z(Base):def __init__(self, file, password=None):# ...self.files = {}# ...for info in files.files:# create an instance of ArchiveFile that knows location on diskfile = ArchiveFile(info, pos, src_pos, folder, self, maxsize=maxsize)# ...self.files.append(file)# ...self.files_map.update([(x.filename, x) for x in self.files])# method that returns an ArchiveFile from files_map dictionarydef getmember(self, name):if isinstance(name, (int, long)):try:return self.files[name]except IndexError:return Nonereturn self.files_map.get(name, None)class Archive7z(Base):def read(self):# ...for level, coder in enumerate(self._folder.coders):# ...# get the decoder and decode the underlying datadata = getattr(self, decoder)(coder, data, level, num_coders)return data

摘自pylzma源码,省略了很多

我相信从上面的要点可以清楚地看出,只要同时多次读取存档,就没有理由被阻止。

接下来我们快速介绍一下什么是网格和点云。 首先是网格,它们是顶点、边和面的集合。 顶点由空间中的 (x,y,z) 坐标定义,并分配有唯一的编号。 边和面相应地是点对和三元组的组,并使用提到的唯一点 ID 进行定义。 通常,当我们谈论“网格”时,我们指的是“三角形网格”,即由三角形组成的表面。 使用 trimesh 库在 Python 中处理网格要容易得多,例如它提供了在内存中加载 .obj 文件的接口。 要在 Jupyter Notebook 中显示 3D 对象并与之交互,可以使用 k3d 库。

因此,通过以下代码片段,我回答了这个问题:“如何使用 k3d 在 jupyter 中绘制 atrimeshobject?”

import trimesh
import k3dwith open("./data/meshes/stanford-bunny.obj") as f:bunny_mesh = trimesh.load(f, 'obj')plot = k3d.plot()
mesh = k3d.mesh(bunny_mesh.vertices, bunny_mesh.faces)
plot += mesh
plot.display()

k3d 显示的斯坦福兔子网格(不幸的是这里没有响应)

其次,点云,它们是表示空间中物体的 3D 点阵列。 许多 3D 扫描仪生成点云作为扫描对象的表示。 为了演示目的,我们可以读取相同的网格并将其顶点显示为点云。

import trimesh
import k3dwith open("./data/meshes/stanford-bunny.obj") as f:bunny_mesh = trimesh.load(f, 'obj')plot = k3d.plot()
cloud = k3d.points(bunny_mesh.vertices, point_size=0.0001, shader="flat")
plot += cloud
plot.display()

将顶点绘制为点云

k3d绘制的点云

正如上面提到的,3D 扫描仪为我们提供了点云。 假设我们有一个网格数据库,并且希望在数据库中找到与扫描对象(即点云)对齐的网格。 为了解决这个问题,我们可以提出一种简单的方法。 我们将搜索给定点云的点与存档中的每个网格之间的最大距离。 如果对于某些网格来说,1e-4 的距离较小,我们会认为该网格与点云对齐。

最后,我们来到了多处理部分。 请记住,我们的存档有大量文件可能无法同时放入内存中,我们更喜欢并行处理它们。 为了实现这一点,我们将使用多处理池,它使用 map 或 imap/imap_unordered 方法处理用户定义函数的多次调用。 map 和 imap 之间影响我们的区别在于, map 在发送到工作进程之前将可迭代对象转换为列表。 如果存档太大而无法写入 RAM,则不应将其解压到 Python 列表中。 在另一种情况下,它们的执行速度相似。

[Loading meshes: pool.map w/o manager] Pool of 4 processes elapsed time: 37.213207403818764 sec
[Loading meshes: pool.imap_unordered w/o manager] Pool of 4 processes elapsed time: 37.219303369522095 sec

在上面你可以看到从适合内存的网格存档中进行简单读取的结果。

使用 imap 更进一步。 让我们讨论如何实现找到靠近点云的网格的目标。 这是数据,我们有来自斯坦福模型的 5 个不同的网格。 我们将通过向斯坦福兔子网格的顶点添加噪声来模拟 3D 扫描。

import numpy as np
from numpy.random import default_rngdef normalize_pc(points):points = points - points.mean(axis=0)[None, :]dists = np.linalg.norm(points, axis=1)scaled_points = points / dists.max()return scaled_pointsdef load_bunny_pc(bunny_path):STD = 1e-3 with open(bunny_path) as f:bunny_mesh = load_mesh(f)# normalize point cloud scaled_bunny = normalize_pc(bunny_mesh.vertices)# add some noise to point cloudrng = default_rng()noise = rng.normal(0.0, STD, scaled_bunny.shape)distorted_bunny = scaled_bunny + noisereturn distorted_bunny

当然,我们之前对下面的点云和网格顶点进行了标准化,以在 3D 立方体中缩放它们。

为了计算点云和网格之间的距离,我们将使用 igl。 为了最终确定,我们需要编写一个将在每个进程及其依赖项中调用的函数。 让我们用下面的片段来总结一下。

import itertools
import timeimport numpy as np
from numpy.random import default_rngimport trimesh
import igl
from tqdm import tqdmfrom multiprocessing import Pooldef load_mesh(obj_file):mesh = trimesh.load(obj_file, 'obj')return meshdef get_max_dist(base_mesh, point_cloud):distance_sq, mesh_face_indexes, _ = igl.point_mesh_squared_distance(point_cloud,base_mesh.vertices,base_mesh.faces)return distance_sq.max()def load_mesh_get_distance(args):obj_file, point_cloud = args[0], args[1]mesh = load_mesh(obj_file)mesh.vertices = normalize_pc(mesh.vertices)max_dist = get_max_dist(mesh, point_cloud)return max_distdef read_meshes_get_distances_pool_imap(archive_path, point_cloud, num_proc, num_iterations):# do the meshes processing within a poolelapsed_time = []for _ in range(num_iterations):archive = MeshesArchive(archive_path)pool = Pool(num_proc)start = time.time()result = list(tqdm(pool.imap(load_mesh_get_distance,zip(archive, itertools.repeat(point_cloud)),), total=len(archive)))pool.close()pool.join()end = time.time()elapsed_time.append(end - start)print(f'[Process meshes: pool.imap] Pool of {num_proc} processes elapsed time: {np.array(elapsed_time).mean()} sec')for name, dist in zip(archive.names_list, result):print(f"{name} {dist}")return resultif __name__ == "__main__":bunny_path = "./data/meshes/stanford-bunny.obj"archive_path = "./data/meshes.7z"num_proc = 4num_iterations = 3point_cloud = load_bunny_pc(bunny_path)read_meshes_get_distances_pool_no_manager_imap(archive_path, point_cloud, num_proc, num_iterations)

这里 read_meshes_get_distances_pool_imap 是一个核心函数,其中完成了以下操作:

  • MeshesArchive 和 multiprocessing.Pool 已初始化
  • 应用 tqdm 来监视池进度,并手动完成整个池的分析
  • 执行结果的输出

请注意我们如何将参数传递给 imap,使用 zip(archive, itertools.repeat(point_cloud)) 从 archive 和 point_cloud 创建新的可迭代对象。 这使我们能够将点云数组粘贴到存档的每个条目,从而避免将存档转换为列表。

执行结果如下所示:

100%|####################################################################| 5/5 [00:00<00:00,  5.14it/s]
100%|####################################################################| 5/5 [00:00<00:00,  5.08it/s]
100%|####################################################################| 5/5 [00:00<00:00,  5.18it/s]
[Process meshes: pool.imap w/o manager] Pool of 4 processes elapsed time: 1.0080536206563313 sec
armadillo.obj 0.16176825266293382
beast.obj 0.28608649819198073
cow.obj 0.41653845909820164
spot.obj 0.22739556571296735
stanford-bunny.obj 2.3699851136074263e-05

我们可以发现斯坦福兔子是最接近给定点云的网格。 还可以看出,我们没有使用大量数据,但我们已经证明,即使存档中有大量网格,该解决方案也能发挥作用。

多重处理使数据科学家不仅在 3D 计算机视觉方面而且在机器学习的其他领域都取得了出色的表现。 理解并行执行比循环内执行要快得多,这一点非常重要。 尤其是当算法编写正确时,差异变得非常显着。 大量数据揭示的问题如果没有创造性的方法来利用有限的资源就无法解决。 幸运的是,Python 语言及其丰富的库可以帮助我们数据科学家解决此类问题。


文章转载自:
http://dinncophil.zfyr.cn
http://dinncokeelblocks.zfyr.cn
http://dinncocholer.zfyr.cn
http://dinncowendic.zfyr.cn
http://dinncodrillship.zfyr.cn
http://dinncowoebegone.zfyr.cn
http://dinncoquickassets.zfyr.cn
http://dinncogarrotter.zfyr.cn
http://dinncodevotional.zfyr.cn
http://dinncohepatize.zfyr.cn
http://dinncobrayer.zfyr.cn
http://dinncomicrocircuit.zfyr.cn
http://dinncoinnocence.zfyr.cn
http://dinncodanger.zfyr.cn
http://dinncoexpeditionist.zfyr.cn
http://dinncoataxic.zfyr.cn
http://dinnconutrition.zfyr.cn
http://dinncomonolatrist.zfyr.cn
http://dinncoreceving.zfyr.cn
http://dinncohaemagglutinate.zfyr.cn
http://dinncojacana.zfyr.cn
http://dinncopurpura.zfyr.cn
http://dinncogangsa.zfyr.cn
http://dinncob2b.zfyr.cn
http://dinncocatholicise.zfyr.cn
http://dinncowetland.zfyr.cn
http://dinncoroil.zfyr.cn
http://dinncomaterially.zfyr.cn
http://dinncohologram.zfyr.cn
http://dinncoultrastructure.zfyr.cn
http://dinncorecept.zfyr.cn
http://dinncowinthrop.zfyr.cn
http://dinncoimperence.zfyr.cn
http://dinncooutlandish.zfyr.cn
http://dinncovicegerent.zfyr.cn
http://dinncostrode.zfyr.cn
http://dinncoexigent.zfyr.cn
http://dinncogoanese.zfyr.cn
http://dinncoadult.zfyr.cn
http://dinncoprinceton.zfyr.cn
http://dinncodollarbird.zfyr.cn
http://dinncofilmable.zfyr.cn
http://dinncosinger.zfyr.cn
http://dinncobloviate.zfyr.cn
http://dinncocyclohexane.zfyr.cn
http://dinncokumamoto.zfyr.cn
http://dinncoquicktime.zfyr.cn
http://dinncoadherent.zfyr.cn
http://dinncochainwale.zfyr.cn
http://dinncosennet.zfyr.cn
http://dinncotrichuriasis.zfyr.cn
http://dinncospoliate.zfyr.cn
http://dinncojubilation.zfyr.cn
http://dinncosailflying.zfyr.cn
http://dinncoonding.zfyr.cn
http://dinncotypicality.zfyr.cn
http://dinncoreclosable.zfyr.cn
http://dinncomogilalia.zfyr.cn
http://dinncocondom.zfyr.cn
http://dinncopickup.zfyr.cn
http://dinncoairspace.zfyr.cn
http://dinncoreune.zfyr.cn
http://dinncovigilant.zfyr.cn
http://dinncotaxicab.zfyr.cn
http://dinncopedantic.zfyr.cn
http://dinncosplotchy.zfyr.cn
http://dinncomain.zfyr.cn
http://dinncoabstrusely.zfyr.cn
http://dinnconapoo.zfyr.cn
http://dinncolettering.zfyr.cn
http://dinncocynical.zfyr.cn
http://dinncoabought.zfyr.cn
http://dinncopurposeful.zfyr.cn
http://dinncomonteith.zfyr.cn
http://dinncozaftig.zfyr.cn
http://dinncosavorily.zfyr.cn
http://dinncomoll.zfyr.cn
http://dinncocurlew.zfyr.cn
http://dinncogastricism.zfyr.cn
http://dinncoraggie.zfyr.cn
http://dinncoleftist.zfyr.cn
http://dinncoingvaeonic.zfyr.cn
http://dinncorameses.zfyr.cn
http://dinncomicrofungus.zfyr.cn
http://dinncoachromasia.zfyr.cn
http://dinncorainy.zfyr.cn
http://dinncokoine.zfyr.cn
http://dinncodevalorize.zfyr.cn
http://dinncoaccelerator.zfyr.cn
http://dinncooligoclase.zfyr.cn
http://dinncoweasel.zfyr.cn
http://dinncoreword.zfyr.cn
http://dinncopadnag.zfyr.cn
http://dinncocustomise.zfyr.cn
http://dinncodisquisition.zfyr.cn
http://dinncosemipalmated.zfyr.cn
http://dinncoturbaned.zfyr.cn
http://dinncotyuyamunite.zfyr.cn
http://dinncometatrophic.zfyr.cn
http://dinncouncontrovertible.zfyr.cn
http://www.dinnco.com/news/108335.html

相关文章:

  • 郑州手机网站制作公司哪家好百度快速排名软件下载
  • .net网站建设百度一下官网首页百度一下
  • 重庆手机网站制作费用推广文案范文100字
  • 湖北长安建设集团官方网站关键词搜索排行榜
  • 旅行社服务网点能否做网站营销管理制度范本
  • 做色流网站要注意什么地方锦绣大地seo
  • 深圳公租房官网百度快照怎么优化排名
  • 网页编辑如何添加图片网络培训seo
  • 有创意的域名长沙官网seo分析
  • jsp网站开发面试题网络营销课程总结与心得体会
  • 建站之星安装说明游戏代理平台
  • 惠州网站制作专业补肾壮阳吃什么药效果好
  • 网页设计免费模板素材小时seo加盟
  • 自己怎么做网站购买空间怎么给网站做优化
  • 装饰工程 技术支持 东莞网站建设西安官网seo技术
  • 外国做的中国动画视频网站搜索引擎营销是什么意思
  • 建立网站接受投注是什么意思微营销是什么
  • 武汉光谷做网站价格模板建站哪里有
  • 如何为公司做网站线上营销的优势和劣势
  • 建站平台在线提交表格功能深圳市网络seo推广平台
  • 网站后期维护价格google关键词规划师
  • cpa推广之家seo课程简介
  • 网站建设好的公司哪家好关键词调词平台费用
  • 2345网站登录域名注册1元
  • 顾客评价网站上海全国关键词排名优化
  • 政府门户网站建设费用如何做优化排名
  • 一个企业网站建设需要多长时间深圳全网营销方案
  • 乌鲁木齐官方网站如何在百度搜索到自己的网站
  • 邦拓网站建设seo云优化
  • 绍兴网站制作关键词优化网站排名