从大型图像中进行快速采样
项目描述
阅读文档 |
|
Gitlab(主要) |
|
Github(镜像) |
|
Pypi |
本项目的官方网站是: https://gitlab.kitware.com/computer-vision/ndsampler
快速访问大型图像中的小区域。
通过将图像转换为高效的底层格式(当前底层格式包括云优化的GeoTIFF(cog)或numpy数组文件(npy))来平均随机访问。如果图像已经是COG格式,则不需要转换。
ndsampler模块是在考虑到检测、分割和分类任务的情况下构建的,但它并不限于这些用例。
基本思想是确保您的数据处于MS-coco格式,然后CocoSampler类将允许您采样正负区域。
对于分类任务,MS-COCO数据可能只是每个图像都有一个占满整个图像的注释。
本模块的愿望是允许您就地访问数据(即无需预处理),尽管这可能取决于您的数据访问效率。然而,可以通过牺牲磁盘空间来构建更快的缓存。目前,我们有一个“cog”和“npy”后端。需要帮助整合hdf5和其他医学/特定领域格式的后端。
安装
可以使用pip安装ndsampler软件包。
pip install ndsampler
请注意,ndsampler依赖于kwimage,其中opencv-python和opencv-python-headless之间存在已知兼容性问题。请确保安装其中一个(但不是两个)。
pip install opencv-python-headless
# OR
pip install opencv-python
最后,为了充分利用ndsampler的功能,必须安装GDAL(尽管大多数ndsampler功能可以在不安装GDAL的情况下工作)。Kitware有一个pypi索引,托管Linux系统的GDAL wheels,但其他系统需要找到安装gdal的方法(conda是一个安全的选择)。
pip install --find-links https://girder.github.io/large_image_wheels GDAL
功能
CocoDataset用于管理和操作注释图像数据集。
对N维时空数据(与常数窗口大小相关)进行摊销O(1)采样(例如图像和视频)。
分层或互斥类别管理。
随机负窗口采样。
基于覆盖的正采样。
动态toydata生成器。
同时安装kwcoco软件包和CLI工具。
使用方法
使用的主要模式是
使用kwcoco加载基于json的COCO数据集(或程序化地创建kwcoco.CocoDataset)。
将此数据集传递给一个ndsampler.CocoSampler对象,这将有效地包装包含您图像和注释的json结构,并允许您高效地从这些图像中采样补丁。
您可以手动指定图像+区域,也可以指定注释id,在这种情况下,它将加载与注释对应的区域。
示例
此示例展示了如何高效地从图像中加载子区域。
>>> # Imagine you have some images
>>> import kwimage
>>> image_paths = [
>>> kwimage.grab_test_image_fpath('astro'),
>>> kwimage.grab_test_image_fpath('carl'),
>>> kwimage.grab_test_image_fpath('airport'),
>>> ] # xdoc: +IGNORE_WANT
['~/.cache/kwimage/demodata/KXhKM72.png',
'~/.cache/kwimage/demodata/flTHWFD.png',
'~/.cache/kwimage/demodata/Airport.jpg']
>>> # And you want to randomly load subregions of them in O(1) time
>>> import ndsampler
>>> import kwcoco
>>> # First make a COCO dataset that refers to your images (and possibly annotations)
>>> dataset = {
>>> 'images': [{'id': i, 'file_name': fpath} for i, fpath in enumerate(image_paths)],
>>> 'annotations': [],
>>> 'categories': [],
>>> }
>>> coco_dset = kwcoco.CocoDataset(dataset)
>>> print(coco_dset)
<CocoDataset(tag=None, n_anns=0, n_imgs=3, n_cats=0)>
>>> # Now pass the dataset to a sampler and tell it where it can store temporary files
>>> workdir = ub.Path.appdir('ndsampler/demo').ensuredir()
>>> sampler = ndsampler.CocoSampler(coco_dset, workdir=workdir)
>>> # Now you can load arbirary samples by specifing a target dictionary
>>> # with an image_id (gid) center location (cx, cy) and width, height.
>>> target = {'gid': 0, 'cx': 200, 'cy': 200, 'width': 100, 'height': 100}
>>> sample = sampler.load_sample(target)
>>> # The sample contains the image data, any visible annotations, a reference
>>> # to the original target, and params of the transform used to sample this
>>> # patch
>>> print(sorted(sample.keys()))
['annots', 'im', 'params', 'tr']
>>> im = sample['im']
>>> print(im.shape)
(100, 100, 3)
>>> # The load sample function is at the core of what ndsampler does
>>> # There are other helper functions like load_positive / load_negative
>>> # which deal with annotations. See those for more details.
>>> # For random negative sampling see coco_regions.
关于COGs的说明
COGs(云优化地理标签)是ndsampler库中高效采样的骨干。
为了有效地进行深度学习,您需要能够有效地从图像中随机采样裁剪区域,因此当ndsampler.Sampler(更准确地说,属于基本Sampler对象的FramesSampler)处于“cog”模式时,它会缓存所有大于512x512的图像。
我注意到即使是“小”的1024x1024图像也显著提高了速度。我尚未有效利用概述功能,但将来我计划这样做,因为我希望ndsampler能够在空间和尺度上进行采样。
使用“npy”后端也可以获得这种加速,它支持真正的随机采样,但这是一个未压缩的格式,可能需要大量的磁盘空间。使用“None”后端意味着加载一个小窗口区域需要先加载整个图像(这可能对某些应用程序来说是可行的)。
使用COGs需要安装GDAL。但是,安装GDAL是一个麻烦的过程。
https://gist.github.com/cspanring/5680334
使用conda相对简单
conda install gdal
# Test that this works
python -c "from osgeo import gdal; print(gdal)"
也可以使用系统软件包
# References:
# https://gis.stackexchange.com/questions/28966/python-gdal-package-missing-header-file-when-installing-via-pip
# https://gist.github.com/cspanring/5680334
# Install GDAL system libs
sudo apt install libgdal-dev
GDAL_VERSION=`gdal-config --version`
echo "GDAL_VERSION = $GDAL_VERSION"
pip install --global-option=build_ext --global-option="-I/usr/include/gdal" GDAL==$GDAL_VERSION
# Test that this works
python -c "from osgeo import gdal; print(gdal)"
Kitware还有一个pypi索引,托管Linux系统的GDAL wheels
pip install --find-links https://girder.github.io/large_image_wheels GDAL
待办事项
当前仅支持基于图像的检测任务,但扩展到视频并不需要太多工作。代码最初是基于视频采样代码编写的,因此 ndimensions 已内置到代码的许多地方。然而,目前还没有测试用例来证明这个库可以与视频一起工作。因此,我们应该(a)从 irharn 迁移视频 toydata 代码来测试 ndcases,以及(b)修复代码以使其能够处理静止图像和视频,并解决出现的问题。
目前我们在 2D 图像中加载许多小物体方面做得很好。然而,我们在加载需要下采样的单个大物体图像方面做得不好(例如,加载整个 1024x1024 的图像并将其下采样到 224x224)。我们应该找到一种方法,使用后端 COG 文件中的金字塔概述来减轻这个问题。
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。