【2022工业图像异常检测文献】CFLOW-AD: 通过条件归一化流实现实时无监督定位异常检测

CFLOW-AD: Real-Time Unsupervised Anomaly Detection with Localization via Conditional Normalizing Flows

1、Background

虽然最近提出针对此类数据设置的模型在准确性指标上取得了很高的成绩,但它们的复杂性限制了实时处理的能力。

CFLOW-AD由一个经过判别式预训练的编码器组成,后面跟着一个多尺度生成解码器,后者明确估计编码特征的似然性。

该方法得到了一个计算和内存高效的模型: CFLOW-AD在相同的输入设置下,比之前的最先进模型快10倍,小10倍。

2、Method

在这里插入图片描述

在这里插入图片描述

pseudo-code

# 输入: 训练数据 Dtrain, 测试数据 Dtest
# 输出: 异常检测和定位的结果

# Step 1: 特征提取
def feature_extraction(image):
    # 使用预训练的CNN作为编码器
    encoder = pretrained_cnn()
    feature_maps = encoder(image)
    return feature_maps

# Step 2: 多尺度特征金字塔池化
def pyramid_pooling(feature_maps):
    pooled_features = []
    for scale in scales:
        pooled_feature = pool(feature_maps[scale])
        pooled_features.append(pooled_feature)
    return pooled_features

# Step 3: 条件归一化流
def conditional_normalizing_flows(pooled_features, conditions):
    decoders_outputs = []
    for k, pooled_feature in enumerate(pooled_features):
        decoder = train_cflow_decoder(pooled_feature.shape)
        likelihood = decoder(pooled_feature, conditions[k])
        decoders_outputs.append(likelihood)
    return decoders_outputs

# Step 4: 多尺度融合
def multi_scale_fusion(decoders_outputs):
    anomaly_map = sum(upsample(decoder_output) for decoder_output in decoders_outputs)
    return anomaly_map

# Step 5: 阈值处理和异常定位
def thresholding(anomaly_map, threshold):
    binary_map = anomaly_map > threshold
    return binary_map

# 训练阶段
def train(model, Dtrain):
    for image in Dtrain:
        features = feature_extraction(image)
        conditions = get_conditions(features)
        model = update_model(features, conditions)
    return model

# 测试阶段
def test(model, Dtest):
    anomalies = []
    for image in Dtest:
        features = feature_extraction(image)
        conditions = get_conditions(features)
        decoders_outputs = conditional_normalizing_flows(features, conditions)
        anomaly_map = multi_scale_fusion(decoders_outputs)
        threshold = determine_threshold(anomaly_map)
        binary_map = thresholding(anomaly_map, threshold)
        anomalies.append(binary_map)
    return anomalies

# 主函数
def main(Dtrain, Dtest):
    model = train(Dtrain)
    test_results = test(model, Dtest)
    return test_results

# 运行模型
Dtrain = load_training_data()
Dtest = load_testing_data()
results = main(Dtrain, Dtest)

3、Experiments

在这里插入图片描述

4、Conclusion

提出了一种结合多尺度特征提取和条件归一化流的异常检测CFLOW-AD模型

<think>嗯,用户想要用C语言实现自适应阈值的方法或代码示例。首先,我需要回忆一下自适应阈值的基本概念。自适应阈值处理是根据图像的局部区域动态计算阈值,适用于光照不均的情况。常见的算法有局部均值法和局部高斯加权平均法。 接下来,我需要确定如何用C语言实现这些方法。可能需要遍历图像的每个像素,对每个像素周围的邻域进行计算。比如,对于局部均值法,计算周围像素的平均值,然后减去一个常数C。对于高斯加权,则使用高斯核进行加权平均。需要注意的是,处理边缘像素时可能需要边界填充,比如镜像填充或者复制边缘值。 然后,代码结构方面,可能需要定义函数,参数包括输入输出图像指针、图像尺寸、邻域大小、常数C和方法选择。函数内部需要嵌套循环遍历每个像素,再在邻域内计算平均值或高斯加权值。对于高斯加权,可能需要预先生成高斯核,这可以通过高斯函数计算权重,然后归一化。 用户可能还需要示例代码,所以得写一个简单的实现,比如局部均值法的示例,并说明如何调用。同时,要提醒用户注意性能问题,比如大尺寸图像的处理可能需要优化,比如使用积分图来加速均值计算。 另外,可能还要提到其他方法,比如OpenCV中的adaptiveThreshold函数,以及引用相关的参考文献,比如引用中的方法,比如CFLOW-AD中的处理方式,但这里可能需要确认引用内容是否相关。比如引用3提到正态分布和梯度的问题,可能与自适应阈值的关系不大,但可以提到训练方法中的优化思路,比如交替训练参数,但可能不适用于此处。 最后,生成相关问题,比如自适应阈值的应用场景、与全局阈值的区别、性能优化方法,以及与其他算法结合使用的情况,比如在异常检测中的应用,如引用3提到的AUC结果,可能涉及图像处理中的异常检测。</think>### 自适应阈值C语言实现方法 自适应阈值算法根据图像局部区域动态计算阈值,适用于光照不均的图像处理。核心公式为: $$ T(x,y) = \mu_{neighborhood} - C $$ 其中$\mu_{neighborhood}$是像素点$(x,y)$邻域均值,$C$为调节常数。 #### 代码实现步骤 1. **定义参数** ```c #define WINDOW_SIZE 15 // 邻域窗口尺寸 #define C 5 // 调节常数 ``` 2. **边界处理函数** 采用镜像填充处理图像边界: ```c int mirror_padding(int pos, int max) { if (pos < 0) return -pos; if (pos >= max) return 2*max - pos -1; return pos; } ``` 3. **核心算法函数** ```c void adaptiveThreshold(uint8_t* src, uint8_t* dst, int width, int height) { int half = WINDOW_SIZE / 2; for (int y=0; y<height; y++) { for (int x=0; x<width; x++) { int sum = 0, count = 0; // 遍历邻域窗口 for (int dy=-half; dy<=half; dy++) { for (int dx=-half; dx<=half; dx++) { int nx = mirror_padding(x+dx, width); int ny = mirror_padding(y+dy, height); sum += src[ny*width + nx]; count++; } } uint8_t threshold = (sum / count) - C; dst[y*width + x] = (src[y*width + x] > threshold) ? 255 : 0; } } } ``` #### 优化建议 1. **积分图加速**:使用积分图可将时间复杂度从$O(Nk^2)$降为$O(N)$,适用于大尺寸图像[^3] 2. **高斯加权**:采用高斯核替代均值计算,公式为: $$ T(x,y) = G_{\sigma} * I(x,y) - C $$ 3. **多线程优化**:对图像进行分块并行处理
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值