luminoth
计算机视觉是一种使用人工智能自动进行图像识别的方法,即使用计算机来识别照片,视频或其他图像类型中的内容。 最新版本的Luminoth ,一个开源的计算机视觉工具包构建的Python和使用Tensorflow和十四行诗,提供了几个改进了其前身(V 0.1):
- Single Shot MultiBox Detector (SSD)模型的实现,它比已经包含的Faster R-CNN快得多(尽管准确性不高)。 SSD可以在大多数现代GPU上实时进行对象检测,以支持视频流的处理。
- 对Faster R-CNN模型进行了一些调整,并采用了新的基本配置,使其在使用COCO和Pascal VOC视觉对象检测数据集进行训练时可以获得与现有实现相当的结果。
- 分别在Pascal和COCO数据集上训练的SSD和Faster R-CNN模型的检查点,具有最新的结果。 这使得图像中的对象检测非常简单,因为即使在仅使用命令行界面(CLI)的情况下,库也会自动下载这些检查点。
- 常规的可用性改进,例如,更干净的CLI用于大多数命令,对预测视频的支持以及对所包含的Web前端的重新设计,以使其更易于使用模型。
让我们通过逐步构建自己的计算机视觉图像检测器来探索这些功能中的每一个。
安装和测试Luminoth
首先,安装Luminoth。 在您的虚拟环境中,运行:
$ pip install luminoth
如果您有可用的GPU并想使用它,请首先运行pip install tensorflow-gpu
,然后运行上面的安装命令。
Luminoth的新检查点功能为开箱即用的Faster R-CNN和SSD提供了预训练的模型。 这意味着您只需几个命令就可以下载并使用经过全面训练的对象检测模型。 让我们首先使用Luminoth的CLI工具lumi
刷新检查点存储库:
$ lumi checkpoint refresh
Retrieving remote index... done.
2 new remote checkpoints added.
$ lumi checkpoint list
================================================================================
| id | name | alias | source | status |
================================================================================
| 48ed2350f5b2 | Faster R-CNN w/COCO | accurate | remote | NOT_DOWNLOADED |
| e3256ffb7e29 | SSD w/Pascal VOC | fast | local | NOT_DOWNLOADED |
================================================================================
输出显示所有可用的预训练检查点。 每个检查点都使用id
字段(在此示例中为48ed2350f5b2
和e3256ffb7e29
)以及可能的alias
(例如, accurate
和fast
)标识。 您可以使用lumi checkpoint detail <checkpoint_id_or_alias>
命令检查其他信息。 我们将尝试使用Faster R-CNN检查点,因此我们将下载它(使用别名而不是ID),然后使用lumi predict
命令:
$ lumi checkpoint download accurate
Downloading checkpoint... [####################################] 100%
Importing checkpoint... done.
Checkpoint imported successfully.
$ lumi predict image.png
Found 1 files to predict.
Neither checkpoint not config specified, assuming `accurate`.
Predicting image.jpg... done.
{
"file": "image.jpg",
"objects": [
{"bbox": [294, 231, 468, 536], "label": "person", "prob": 0.9997},
{"bbox": [494, 289, 578, 439], "label": "person", "prob": 0.9971},
{"bbox": [727, 303, 800, 465], "label": "person", "prob": 0.997},
{"bbox": [555, 315, 652, 560], "label": "person", "prob": 0.9965},
{"bbox": [569, 425, 636, 600], "label": "bicycle", "prob": 0.9934},
{"bbox": [326, 410, 426, 582], "label": "bicycle", "prob": 0.9933},
{"bbox": [744, 380, 784, 482], "label": "bicycle", "prob": 0.9334},
{"bbox": [506, 360, 565, 480], "label": "bicycle", "prob": 0.8724},
{"bbox": [848, 319, 858, 342], "label": "person", "prob": 0.8142},
{"bbox": [534, 298, 633, 473], "label": "person", "prob": 0.4089}
]
}
lumi predict
命令默认使用别名accurate
的检查点,但是我们可以使用--checkpoint=<alias_or_id>
选项来指定。 在现代CPU上运行约30秒后,输出如下:
您还可以将JSON输出写入文件(通过--output
或-f
选项),并使Luminoth存储带有绘制边框的图像(通过--save-media-to
或-d
选项)。
现在实时
除非您要在未来几年内阅读这篇文章(过去的问候!),否则您可能会注意到Faster R-CNN花费了相当长的时间来检测图像中的对象。 这是因为此模型比预测效率更注重预测精度,因此将其用于诸如视频的实时处理之类的操作是不可行的(尤其是如果您没有现代硬件的话)。 即使在相当快速的GPU上,Faster R-CNN每秒也不会处理超过2至5张图像。
输入单发MultiBox检测器。 该模型的速度精度较低(随着您要检测的类更多而提高):在上面使用的相同硬件上,每秒可传输约60张图像,因此通常适合在视频流或视频上运行。
让我们尝试一下。 再次运行lumi predict
,但这一次使用fast
检查点。 另外,这次我们不会事先下载; CLI将注意到该命令并在远程存储库中查找该命令。
$ lumi predict video.mp4 --checkpoint=fast --save-media-to=.
Found 1 files to predict.
Predicting video.mp4 [####################################] 100% fps: 45.9
快很多! 该命令将通过逐帧运行SSD来生成视频,因此没有理想的时间预测模型(至少目前如此)。 在实践中,这意味着您可能会在盒子中看到一些抖动,以及一些预测的出现和消失,但这些都是后处理无法解决的。
训练自己的模型
假设您想检测车窗外的汽车,而您对COCO中的80个课程不感兴趣。 训练模型以检测较少的类可能会提高检测质量,所以让我们这样做。 但是请注意,在CPU上进行训练可能需要花费相当长的时间,因此请确保使用GPU或云服务,例如Google的ML Engine(与Luminoth集成 )。
Luminoth包含用于从标准格式(例如COCO和Pascal使用的格式)准备和构建自定义数据集的工具。 您还可以构建自己的数据集转换器以支持自己的格式,但这超出了本文的主题。 现在,我们将使用lumi dataset
CLI工具来构建仅包含从COCO和Pascal(2007和2012)中提取的汽车的数据集。
首先从Pascal 2007 , Pascal 2012和COCO下载数据集,并将它们存储在工作目录中创建的datasets/
目录中(特别是: datasets/pascal/2007/
, datasets/pascal/2012/
和datasets/coco/
)。 然后运行以下命令,将所有数据合并到单个.tfrecords
文件中,以供.tfrecords
使用:
$ lumi dataset transform \
--type pascal \
--data-dir datasets/pascal/VOCdevkit/VOC2007/ \
--output-dir datasets/pascal/tf/2007/ \
--split train --split val --split test \
--only-classes=car
$ lumi dataset transform \
--type pascal \
--data-dir datasets/pascal/VOCdevkit/VOC2012/ \
--output-dir datasets/pascal/tf/2012/ \
--split train --split val \
--only-classes=car
$ lumi dataset transform \
--type coco \
--data-dir datasets/coco/ \
--output-dir datasets/coco/tf/ \
--split train --split val \
--only-classes=car
$ lumi dataset merge \
datasets/pascal/tf/2007/classes-car/train.tfrecords \
datasets/pascal/tf/2012/classes-car/train.tfrecords \
datasets/coco/tf/classes-car/train.tfrecords \
datasets/tf/train.tfrecords
$ lumi dataset merge \
datasets/pascal/tf/2007/classes-car/val.tfrecords \
datasets/pascal/tf/2012/classes-car/val.tfrecords \
datasets/coco/tf/classes-car/val.tfrecords \
datasets/tf/val.tfrecords
现在我们准备开始培训。 要使用Luminoth训练模型,您必须创建一个配置文件,该文件指定一些必需的信息(例如运行名称,数据集位置和要使用的模型,以及与模型有关的超参数组合)。 由于Luminoth提供了基本的配置文件,所以像这样就足够了:
train:
run_name: ssd-cars
# Directory in which model checkpoints & summaries (for Tensorboard) will be saved.
job_dir: jobs/
# Specify the learning rate schedule to use. These defaults should be good enough.
learning_rate:
decay_method: piecewise_constant
boundaries: [1000000, 1200000]
values: [0.0003, 0.0001, 0.00001]
dataset:
type: object_detection
# Directory from which to read the dataset.
dir: datasets/tf/
model:
type: ssd
network:
# Total number of classes to predict. One, in this case.
num_classes: 1
将其作为config.yml
存储在您的工作目录( datasets/
所在的目录)中。 如您所见,我们将训练SSD模型。 运行以下命令:
$ lumi train -c config.yml
INFO:tensorflow:Starting training for SSD
INFO:tensorflow:Constructing op to load 32 variables from pretrained checkpoint
INFO:tensorflow:ImageVisHook was created with mode = "debug"
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Saving checkpoints for 1 into jobs/ssd-cars/model.ckpt.
INFO:tensorflow:step: 1, file: b'000004.jpg', train_loss: 20.626895904541016, in 0.07s
INFO:tensorflow:step: 2, file: b'000082.jpg', train_loss: 12.471542358398438, in 0.07s
INFO:tensorflow:step: 3, file: b'000074.jpg', train_loss: 7.3356428146362305, in 0.06s
INFO:tensorflow:step: 4, file: b'000137.jpg', train_loss: 8.618950843811035, in 0.07s
(ad infinitum)
许多小时后,该模型应该会产生一些合理的结果(当超过100万个步骤时,可以将其停止)。 您可以通过运行以下命令使用内置的Web界面立即对其进行测试:
$ lumi server web -c config.yml
Neither checkpoint not config specified, assuming 'accurate'.
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
由于Luminoth基于Tensorflow构建,因此如果您想查看训练进度,也可以通过在配置中指定的job_dir
上运行Tensorboard来利用Tensorboard 。
学到更多
在本概述中,我们使用了Luminoth通过预先训练的模型来检测图像和视频中的对象,甚至使用了一些命令来训练自己的对象。 我们只使用CLI工具,甚至没有使用Python API,您可以从中使用经过训练的模型作为大型系统的一部分。
如果您想了解更多信息,请查阅文档 ,其中包含更多使用Luminoth的示例。
翻译自: https://opensource.com/article/18/5/getting-started-luminoth
luminoth