FiftyOne初体验
FiftyOne Quickstart
本文讲述FiftyOne的简单演示,FiftyOne轻量化的特性可以帮助你更好的构造数据集和计算机视觉模型。
以下为本文将要讲述的概念:
- 加载数据集到FiftyOne
- 在notebook中使用Fifty
- 使用view和the App探索数据集的各个方面
- 评估模型的预测
- 查找数据集中的标签错误
安装FiftyOne
pip install fiftyone
加载数据集
首先从导入FiftyOne库:
import fiftyone as fo
如果上面的导入命令报cv2错误,那就是在当前环境中需要安装OpenCV库。
FiftyOne提供了一系列在数据、模型上丰富的资源,可以帮你启动并运行你的项目。在本文中,我们将从FiftyOne Dataset Zoo数据集加载一个小的检测数据集。
下面的命令从网络下载数据集并且将其加载到FiftyOne Dataset,然后我们将使用它来探索FiftyOne的各种功能:
import fiftyone.zoo as foz
dataset = foz.load_zoo_dataset("quickstart")
Dataset already downloaded
Loading 'quickstart'
100% |█████████████████| 200/200 [2.9s elapsed, 0s remaining, 61.9 samples/s]
Dataset 'quickstart' created
print(dataset)
Name: quickstart
Media type: image
Num samples: 200
Persistent: False
Tags: ['validation']
Sample fields:
id: fiftyone.core.fields.ObjectIdField
filepath: fiftyone.core.fields.StringField
tags: fiftyone.core.fields.ListField(fiftyone.core.fields.StringField)
metadata: fiftyone.core.fields.EmbeddedDocumentField(fiftyone.core.metadata.Metadata)
ground_truth: fiftyone.core.fields.EmbeddedDocumentField(fiftyone.core.labels.Detections)
uniqueness: fiftyone.core.fields.FloatField
predictions: fiftyone.core.fields.EmbeddedDocumentField(fiftyone.core.labels.Detections)
接下来让我们启动FiftyOne App以便于我们可以可视化的形式探索数据集。你可以马上就能看到,因为在notebook环境中,以我们选的数据集的the App的嵌入实例已经渲染到当前cell的输出中。
下面创建的Session对象是在你当前Python内核和FiftyOne App直接双向连接的,我们之后将会看到。
session = fo.launch_app(dataset, auto=False)
Session launched. Run `session.show()` to open the App in a cell output.
如果希望在当前浏览器新打开标签来操纵FiftyOne App,可以使用修改上面的
auto参数并且使用下面的语句在新标签页打开。
session.open_tab()

自动快照
Notebook之所以有很棒的原因,其中一点就是可以与其它人员分享你的工作。FiftyOne被设计用来帮助你编写在视觉数据上的notebook,捕获你的工作,使用称之为automatic screenshotting的特性。
无论何时当你在notebook cell中打开一个新的App实例,例如,通过更新你的Session对象,任何之前的App实例将被自动地被替换为静态快照(static screenshot)。事实上,这就是你下面将要看到的;当我们创建一个notebook时,就会打开App的快照。
下面cell提出一个新命令session.show(),这会在当前cell的输出中打开一个新App实例。当你运行cell时,你会注意到之前App实例已经被当前App实例状态的快照替换掉。你可以通过鼠标在任何地方悬停并点击重新启动之前的App实例。
下面的代码运行之后,在网格上的图片上双击来展开样例。
# session.show()

数据视图
FiftyOne真正强大之处就是使用dataset views。
将数据集视作为所有数据的根视图。创建DatasetView允许你研究数据集的特定指定子集或字段。
Dataset views可以被Python和FiftyOne App创建和更改。在App中通过Session的属性Session.view激活视图是总是有效的。这意味着如果你更新App的视图,它的状态将被Session.view捕获。亦或是,你可以使用Python创建一个视图并通过设置Session.view属性来打开。
让我们通过App来创建数据集的视图。我们将通过对uniqueness字段排序数据集来首先展示最独特(unique)的图像。
为了这么做,我们将在View Bar点击+ add stage并且以uniqueness字段添加SortBy,并且将reverse设为True。

我们可以在Python中访问视图,例如,打印最独特的样例:
print(session.view.first())
,
'iscrowd': ,
}),
'tags': BaseList([]),
'label': 'airplane',
'bounding_box': BaseList([
0.05365625,
0.34533957845433255,
0.769828125,
0.45049180327868854,
]),
'mask': None,
'confidence': None,
'index': None,
}>,
]),
}>,
'uniqueness': 1.0,
'predictions': ,
,
,
]),
}>,
}>
Python中复杂的视图
有些时候你也许会对复杂的视图complex views感兴趣,比如通过指定一系列的条件或者过滤操作出的视图。
你可以在FiftyOne通过chaining view stages链到一起来定义你想要的数据视图。
例如,我们创建一个视图,这个视图仅包含25个数据集中最独特的图片,并且在这些样例上预测的自信度(confidence)>0.5。
记住一点,因为我们运作在notebook,任何时候我们改变了我们的Session对象,一个新App实例就会在cell输出。
from fiftyone import ViewField as F
session.view = (
dataset
.sort_by("uniqueness", reverse=True)
.limit(25)
.filter_labels("predictions", F("confidence") > 0.5)
)

调试模型
一个FiftyOne主要的使用案例就是能够轻松地可视化和探索模型的预测来找到需要被处理的例子来提升性能。
quickstart数据集已经在predictions字段中进行预测了,对于不同任务分类、检测、分割和关键点等,你也可以轻松地添加你模型的预测。亦或是,如果你还没有训练自己的模型,你可以仅用几行代码来下载FiftyOne Model Zoo预训练模型在你的数据集上生成预测来进行检查。
一旦你在你的数据集上开始预测,你可以使用FiftyOne强大的evaluation framework来进行评估。例如,使用我们数据集内建方法evaluate_detections()来计算我们的预测COCO风格的mAP(mean average precision):
# Computes the mAP of the predictions in the `predictions` field
# w.r.t. the ground truth labels in the `ground_truth` field
results = dataset.evaluate_detections(
"predictions",
gt_field="ground_truth",
compute_mAP=True,
)
print("\nmAP: %.4f" % results.mAP())
Evaluating detections...
100% |█████████████████| 200/200 [3.6s elapsed, 0s remaining, 43.9 samples/s]
Performing IoU sweep...
100% |█████████████████| 200/200 [5.8s elapsed, 0s remaining, 25.2 samples/s]
mAP: 0.3957
接下来,让我们评估自信度大于0.75的预测:
# 创建仅预测自信度大于0.75的数据视图
high_conf_view = dataset.filter_labels("predictions", F("confidence") > 0.75)
# Evaluate the predictions in the `predictions` field w.r.t. the ground truth
# labels in the `ground_truth` field
results = high_conf_view.evaluate_detections(
"predictions",
gt_field="ground_truth",
eval_key="eval",
)
Evaluating detections...
100% |█████████████████| 199/199 [2.9s elapsed, 0s remaining, 50.2 samples/s]
返回的results对象提供了少量的方法来生成我们模型的各种性能报告。
例如,让我们打印一个分类报告输出前10类最普通的目标类别:
# Get the 10 most common classes in the dataset
counts = dataset.count_values("ground_truth.detections.label")
classes = sorted(counts, key=counts.get, reverse=True)[:10]
# Print a classification report for the top-10 classes
results.print_report(classes=classes)
precision recall f1-score support
person 0.85 0.72 0.78 412
kite 0.84 0.68 0.75 91
car 0.74 0.51 0.60 61
bird 0.91 0.48 0.63 64
carrot 0.58 0.40 0.48 47
boat 0.62 0.35 0.45 37
surfboard 0.63 0.40 0.49 30
traffic light 0.88 0.62 0.73 24
airplane 0.90 0.79 0.84 24
giraffe 0.81 0.91 0.86 23
micro avg 0.81 0.64 0.71 813
macro avg 0.78 0.59 0.66 813
weighted avg 0.81 0.64 0.71 813
这些聚合的指标并没有给出一个模型性能的全貌。在实际操作中,一个模型的限制因素通常是数据质量问题,这才是你需要看到并处理的问题。FiftyOne就是被设计出使你更轻松地处理这个问题。
注意到上一个tutorial和blog posts有更多的调试模型的不同类型的例子。
查找错误标注
FiftyOne的另一个核心功能就是加载并且探索数据集和标注来轻松获得数据分布和标注质量。
另外在App的自导分析中(self-guided analysis),Faster-RCNN模型。但是,尝试Model Zoo里的其它模型将会产生更多的mistakennes分数信息。
分享notebook
为了使得次notebook已经准备为别人分享,你将需要将当前激活的App通过调用tutorial和recipes来学习更多的具体使用方法和最佳实践案例。
而且FiftyOne也是开源的,如果你认为有哪里错误的地方,你也可以查看GitHub仓库并且leave a issure!