App下載

如何使用Python Multiprocessing庫處理3D數(shù)據(jù)?使用Python Multiprocessing庫處理3D數(shù)據(jù)方法!

小丸子的西瓜夢 2021-09-30 10:03:54 瀏覽數(shù) (2602)
反饋

今天我們將介紹處理大量數(shù)據(jù)時非常方便的工具。我不會只告訴您可能在手冊中找到的一般信息,而是分享一些我發(fā)現(xiàn)的小技巧,例如tqdm與 multiprocessingimap一起使用、并行處理檔案、繪制和處理 3D 數(shù)據(jù)以及如何搜索如果您有點云,則用于對象網(wǎng)格中的類似對象。

那么我們?yōu)槭裁匆笾诓⑿杏嬎隳??如今,如果您處理任何類型的?shù)據(jù),您可能會面臨與“大數(shù)據(jù)”相關的問題。每次我們有不適合 RAM 的數(shù)據(jù)時,我們都需要一塊一塊地處理它。幸運的是,現(xiàn)代編程語言允許我們生成在多核處理器上完美運行的多個進程(甚至線程)。(注意:這并不意味著單核處理器不能處理多處理。  這是關于該主題的堆棧溢出線程。)

今天,我們將嘗試處理經(jīng)常發(fā)生的計算網(wǎng)格和點云之間距離的 3D 計算機視覺任務。例如,當您需要在所有可用網(wǎng)格中找到定義與給定點云相同的 3D 對象的網(wǎng)格時,您可能會遇到此問題。

我們的數(shù)據(jù)由.obj存儲在.7z存檔中的文件組成,這在存儲效率方面非常出色。但是當我們需要訪問它的確切部分時,我們應該努力。在這里,我定義了包裝 7-zip 存檔并提供底層數(shù)據(jù)接口的類。

from io import BytesIO
import py7zlib

class MeshesArchive(object):
    def __init__(self, archive_path):
        fp = open(archive_path, 'rb')
        self.archive = py7zlib.Archive7z(fp)
        self.archive_path = archive_path
        self.names_list = self.archive.getnames()
        self.cur_id = 0
        
    def __len__(self):
        return len(self.names_list)
    
    def get(self, name):
        bytes_io = BytesIO(self.archive.getmember(name).read())
        return bytes_io

    def __getitem__(self, idx):
        return self.get(self.names[idx])
      
    def __iter__(self):
        return self

    def __next__(self):
      if self.cur_id >= len(self.names_list):
          raise StopIteration
      name = self.names_list[self.cur_id]
      self.cur_id += 1
      return self.get(name)

這個類幾乎不依賴py7zlib包,它允許我們在每次調(diào)用get方法時解壓縮數(shù)據(jù),并為我們提供存檔中的文件數(shù)。我們還定義了__iter__這將幫助我們map像在可迭代對象上一樣在該對象上啟動多處理。 

您可能知道,可以創(chuàng)建一個 Python 類,從中可以實例化可迭代對象。該類應滿足以下條件:覆蓋__getitem__返回self和__next__返回后續(xù)元素。我們絕對遵循這個規(guī)則。 

上面的定義為我們提供了遍歷存檔的可能性,但 它是否允許我們 對內(nèi)容進行并行隨機訪問?這是一個有趣的問題,我在網(wǎng)上沒有找到答案,但我們可以研究源代碼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 disk
      file = 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 dictionary
  def getmember(self, name):
      if isinstance(name, (int, long)):
          try:
              return self.files[name]
          except IndexError:
              return None

      return 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 data
      data = getattr(self, decoder)(coder, data, level, num_coders)

    return data

在代碼中,您可以看到在從存檔中讀取下一個對象期間調(diào)用的方法。我相信從上面可以清楚地看出,只要同時多次讀取存檔,就沒有理由阻止存檔。

接下來,我們快速介紹一下什么是網(wǎng)格和點云。 

首先,網(wǎng)格是頂點、邊和面的集合。頂點由空間中的(x,y,z) 坐標定義并分配有唯一編號。邊和面是相應的點對和三元組的組,并用提到的唯一點 id 定義。通常,當我們談論“網(wǎng)格”時,我們指的是“三角形網(wǎng)格”,即由三角形組成的表面。使用trimesh庫在 Python 中使用網(wǎng)格要容易得多。例如,它提供了.obj在內(nèi)存中加載文件的接口。要在jupyter notebook一個3D 對象中顯示和交互,可以使用k3d庫。

所以,用下面的代碼片段我回答這個問題:“你怎么繪制trimesh的對象jupyter有k3d?”

import trimesh
import k3d

with 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()

Stanford Bunny mesh displayed by k3d

k3d 顯示的斯坦福兔子網(wǎng)格 
其次,點云是表示空間中對象的 3D 點數(shù)組。許多 3D 掃描儀生成點云作為掃描對象的表示。出于演示目的,我們可以讀取相同的網(wǎng)格并將其頂點顯示為點云。
import trimesh
import k3d

with 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()

Point cloud drawn by k3d

k3d繪制的
點云
如上所述,3D 掃描儀為我們提供了一個點云。假設我們有一個網(wǎng)格數(shù)據(jù)庫,我們想在我們的數(shù)據(jù)庫中找到一個與掃描對象對齊的網(wǎng)格,也就是點云。
為了解決這個問題,我們可以提出一種簡單的方法。我們將從我們的檔案中搜索給定點云的點與每個網(wǎng)格之間的最大距離。
如果1e-4某些網(wǎng)格的距離更小,我們將認為該網(wǎng)格與點云對齊。

最后,我們來到了多處理部分。請記住,我們的存檔中有大量文件可能無法放在一起放在內(nèi)存中,因為我們更喜歡并行處理它們。

為了實現(xiàn)這一點,我們將使用 multiprocessing Pool,它使用mapimap/imap_unordered方法處理用戶定義函數(shù)的多次調(diào)用。

mapimap影響我們的區(qū)別在于,map在將其發(fā)送到工作進程之前將其轉(zhuǎn)換為列表。如果存檔太大而無法寫入 RAM,則不應將其解壓縮到 Python 列表中。換句話說,兩者的執(zhí)行速度是相似的。

[加載網(wǎng)格:pool.map w/o manager] 4 個進程池耗時:37.213207403818764 秒
[加載網(wǎng)格:pool.imap_unordered w/o manager] 4 個進程池耗時:37.219303369522095 秒

在上面,您可以看到從適合內(nèi)存的網(wǎng)格檔案中簡單讀取的結果。

更進一步imap:讓我們討論如何實現(xiàn)找到靠近點云的網(wǎng)格的目標。這是數(shù)據(jù)。我們有來自斯坦福模型的 5 個不同的網(wǎng)格。我們將通過向斯坦福兔子網(wǎng)格的頂點添加噪聲來模擬 3D 掃描。

import numpy as np
from numpy.random import default_rng

def normalize_pc(points):
    points = points - points.mean(axis=0)[None, :]
    dists = np.linalg.norm(points, axis=1)
    scaled_points = points / dists.max()
    return scaled_points


def 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 cloud
    rng = default_rng()
    noise = rng.normal(0.0, STD, scaled_bunny.shape)
    distorted_bunny = scaled_bunny + noise
    return distorted_bunny

當然,我們之前在下面將點云和網(wǎng)格頂點歸一化,以在 3D 立方體中縮放它們。

要計算點云和網(wǎng)格之間的距離,我們將使用igl。為了完成,我們需要編寫一個函數(shù)來調(diào)用每個進程及其依賴項。讓我們用以下代碼段來總結。

import itertools
import time
import numpy as np
from numpy.random import default_rng
import trimesh
import igl
from tqdm import tqdm
from multiprocessing import Pool
def load_mesh(obj_file):
    mesh = trimesh.load(obj_file, 'obj')
    return mesh
def 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_dist
def read_meshes_get_distances_pool_imap(archive_path, point_cloud, num_proc, num_iterations):
    # do the meshes processing within a pool
    elapsed_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 result  
 if __name__ == "__main__":
    bunny_path = "./data/meshes/stanford-bunny.obj"
    archive_path = "./data/meshes.7z"
    num_proc = 4
    num_iterations = 3
    point_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是一個中心函數(shù),其中完成以下操作:

  • MeshesArchive并multiprocessing.Pool初始化
  • tqdm 用于觀察池進度,并手動完成整個池的分析
  • 執(zhí)行結果的輸出

請注意我們?nèi)绾蝹鬟f參數(shù)以imap從archive和point_cloud使用zip(archive, itertools.repeat(point_cloud)). 這允許我們將點云數(shù)組粘貼到存檔的每個條目上,避免轉(zhuǎn)換archive為列表。

執(zhí)行結果如下:

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 <0時,5.18it /秒] 
[方法網(wǎng)眼:pool.imap W / O管理器] 4個過程的池經(jīng)過時間:1.0080536206563313秒
armadillo.obj 0.16176825266293382 
beast.obj 0.28608649819198073 
cow.obj 0.41653845909820164
現(xiàn)貨.obj 0.22739556571296735 
stanford-bunny.obj 2.3699851136074263e-05

我們可以注意到斯坦福兔子是最接近給定點云的網(wǎng)格。還可以看出,我們沒有使用大量數(shù)據(jù),但我們已經(jīng)證明,即使我們在存檔中有大量網(wǎng)格,該解決方案也能奏效。

多處理使數(shù)據(jù)科學家不僅在 3D 計算機視覺中而且在機器學習的其他領域中都取得了出色的表現(xiàn)。理解并行執(zhí)行比在循環(huán)內(nèi)執(zhí)行快得多是非常重要的。差異變得顯著,尤其是在正確編寫算法時。大量數(shù)據(jù)揭示了如果沒有關于如何使用有限資源的創(chuàng)造性方法就無法解決的問題。幸運的是,Python 語言及其廣泛的庫集幫助我們數(shù)據(jù)科學家解決了這些問題。


0 人點贊