mindarmour.reliability
MindArmour的可靠性方法。
- class mindarmour.reliability.FaultInjector(model, fi_type=None, fi_mode=None, fi_size=None)[源代码]
故障注入模块模拟深度神经网络的各种故障场景,并评估模型的性能和可靠性。
详情请查看 实现模型故障注入评估模型容错性。
- 参数:
model (Model) - 需要评估模型。
fi_type (list) - 故障注入的类型,包括
'bitflips_random'
(随机翻转)、'bitflips_designated'
(翻转关键位)、'random'
、'zeros'
、'nan'
、'inf'
、'anti_activation'
、'precision_loss'
等。fi_mode (list) - 故障注入的模式。可选值:
'single_layer'
、'all_layer'
。fi_size (list) - 故障注入的次数,表示需要注入多少值。
样例:
>>> from mindspore import Model >>> import mindspore.ops.operations as P >>> from mindarmour.reliability.model_fault_injection.fault_injection import FaultInjector >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self._softmax = P.Softmax() ... self._Dense = nn.Dense(10,10) ... self._squeeze = P.Squeeze(1) ... def construct(self, inputs): ... out = self._softmax(inputs) ... out = self._Dense(out) ... return self._squeeze(out) >>> def dataset_generator(): ... batch_size = 16 ... batches = 1 ... data = np.random.randn(batches * batch_size,1,10).astype(np.float32) ... label = np.random.randint(0,10, batches * batch_size).astype(np.int32) ... for i in range(batches): ... yield data[i*batch_size:(i+1)*batch_size], label[i*batch_size:(i+1)*batch_size] >>> net = Net() >>> model = Model(net) >>> ds_eval = ds.GeneratorDataset(dataset_generator, ['image', 'label']) >>> fi_type = ['bitflips_random', 'bitflips_designated', 'random', 'zeros', ... 'nan', 'inf', 'anti_activation', 'precision_loss'] >>> fi_mode = ['single_layer', 'all_layer'] >>> fi_size = [1] >>> fi = FaultInjector(model, ds_eval, fi_type, fi_mode, fi_size) >>> fi.kick_off() >>> fi.metrics()
- class mindarmour.reliability.ConceptDriftCheckTimeSeries(window_size=100, rolling_window=10, step=10, threshold_index=1.5, need_label=False)[源代码]
概念漂移检查时间序列(ConceptDriftCheckTimeSeries)用于样本序列分布变化检测。
有关详细信息,请查看 实现时序数据概念漂移检测应用。
- 参数:
window_size (int) - 概念窗口的大小,不小于10。如果给定输入数据, window_size 取值范围为[10, 1/3*len( data )]。 如果数据是周期性的,通常 window_size 等于2-5个周期。例如,对于月/周数据,30/7天的数据量是一个周期。默认值:
1.0
。rolling_window (int) - 平滑窗口大小,在[1, window_size ]中。默认值:
10
。step (int) - 滑动窗口的跳跃长度,取值范围为[1, window_size ]。默认值:
10
。threshold_index (float) - 阈值索引,\((-\infty, +\infty)\) 。默认值:
1.5
。need_label (bool) - 如果为
True
,则需要概念漂移标签。默认值:False
。
样例:
>>> from mindarmour import ConceptDriftCheckTimeSeries >>> concept = ConceptDriftCheckTimeSeries(window_size=100, rolling_window=10, ... step=10, threshold_index=1.5, need_label=False) >>> data_example = 5*np.random.rand(1000) >>> data_example[200: 800] = 20*np.random.rand(600) >>> score, threshold, concept_drift_location = concept.concept_check(data_example)
- class mindarmour.reliability.OodDetector(model, ds_train)[源代码]
分布外检测器的抽象类。
- 参数:
model (Model) - 训练模型。
ds_train (numpy.ndarray) - 训练数据集。
- class mindarmour.reliability.OodDetectorFeatureCluster(model, ds_train, n_cluster, layer)[源代码]
训练OOD检测器。提取训练数据特征,得到聚类中心。测试数据特征与聚类中心之间的距离确定图像是否为分布外(OOD)图像。
有关详细信息,请查看 实现图像数据概念漂移检测应用。
- 参数:
model (Model) - 训练模型。
ds_train (numpy.ndarray) - 训练数据集。
n_cluster (int) - 聚类数量。取值属于[2,100]。 通常,n_cluster 等于训练数据集的类号。如果OOD检测器在测试数据集中性能较差,我们可以适当增加 n_cluster 的值。
layer (str) - 特征层的名称。layer (str)由’name[:Tensor]’表示,其中’name’由用户在训练模型时给出。 请查看有关如何在’README.md’中命名模型层的更多详细信息。
样例:
>>> from mindspore import Model >>> from mindspore.ops import TensorSummary >>> import mindspore.ops.operations as P >>> from mindarmour.reliability.concept_drift.concept_drift_check_images import OodDetectorFeatureCluster >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self._softmax = P.Softmax() ... self._Dense = nn.Dense(10,10) ... self._squeeze = P.Squeeze(1) ... self._summary = TensorSummary() ... def construct(self, inputs): ... out = self._softmax(inputs) ... out = self._Dense(out) ... self._summary('output', out) ... return self._squeeze(out) >>> net = Net() >>> model = Model(net) >>> batch_size = 16 >>> batches = 1 >>> ds_train = np.random.randn(batches * batch_size, 1, 10).astype(np.float32) >>> ds_eval = np.random.randn(batches * batch_size, 1, 10).astype(np.float32) >>> detector = OodDetectorFeatureCluster(model, ds_train, n_cluster=10, layer='output[:Tensor]') >>> num = int(len(ds_eval) / 2) >>> ood_label = np.concatenate((np.zeros(num), np.ones(num)), axis=0) >>> optimal_threshold = detector.get_optimal_threshold(ood_label, ds_eval)