You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CB系统SA攻击算法IoM替换Biohash后重构函数失效求助

问题:替换Biohash为IoM函数后重构函数无法生成目标矩阵

我运行一套用于测量CB系统相似度攻击的代码(针对BTP模板的原像攻击),原Biohash示例可正常运行,但将Biohash替换为IoM函数后,重构函数无法生成所需矩阵。相关代码如下:

自定义IoM相关函数

function [randum] = random_IoM_edit(orig_length,Ndimension,Nprojection)
    randum = randn(orig_length,Ndimension,Nprojection);
end

function [transformed_templates] = IoM_edit(opts)
    persons=opts.nopersons;
    for i=1:persons
        training_vector=opts.data(i,:);
     maxout_code_training_ind = [];
     for counter = 1:opts.Nprojection
                tmp_training= training_vector* opts.model(:,:,counter);
                [m_trainig ind_training] = max(tmp_training);
                maxout_code_training_ind = [maxout_code_training_ind, ind_training];
     end
     transformed_templates(i,:)=maxout_code_training_ind;
    end
end

function [distance] = fitness_iom_edit(x, hashcode,opts)
     [transformed_data] = IoM_edit(opts);
    distcc=[];
    for a=1:size(hashcode,1)
        distcc=[distcc  1-matching_IoM(hashcode(a,:),transformed_data)];
    end
    distance=mean(distcc);
end

主MAT文件代码

%clear all;
close all;
load('data\lfw\LFW_10Samples_insightface.mat')
load('data\lfw\LFW_label_10Samples_insightface.mat')
labels=ceil(0.1:0.1:158);

addpath('matlab_tools');
addpath_recurse("btp")
    opts.data= LFW_10Samples_insightface
    opts.Ndimension= 50;
    opts.Nprojection = 300;
    opts.nopersons=size(LFW_10Samples_insightface,1);
    opts.dX=size(LFW_10Samples_insightface,2);
    opts.model = random_IoM_edit(opts.dX,opts.Ndimension,opts.Nprojection);
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% facenet iom generate dataset
[transformed_data] = IoM_edit(opts);
    
    %scores = 1- pdist2(transformed_data,transformed_data,'Hamming');
    %hamming_gen_score = scores(labels'==labels);
    %hamming_gen_score = hamming_gen_score(find(hamming_gen_score~=1));
    %hamming_imp_score = scores(labels'~=labels);
    
    %[EER_HASH, mTSR, mFAR, mFRR, mGAR] =computeperformance(hamming_gen_score, hamming_imp_score, 0.001);  % isnightface 3.43 % 4.40 %
    reconstruct_x=zeros(158,512);
    
    %% reconstruct the first one
    for i=1:158
        disp(['reconstructing ',num2str(i)])
        to_retrieve_hash=transformed_data((i-1)*10+1,:); % first of the template are used to reconstruct
        %rng default % For reproducibility
        f_fitness = @(x)fitness_iom_edit(x,to_retrieve_hash,opts); % fitness function
         f_constr = []; 
         
        reconstruct_x(i,:) = reconstruct(f_fitness,f_constr,opts);  
    end
    
    save(['data/iomhashing_reconstructnoconstraint_',num2str(dimensions),'.mat'],'reconstruct_x');
    save(['data/iomhashing_eer_',num2str(dimensions),'.mat'],'EER_HASH');

问题根源与修正方案

1. 核心问题:Fitness函数未使用待优化的x

fitness_iom_edit中直接调用IoM_edit(opts),但opts.data仍然是原始的LFW数据集,完全没有用到输入的x(重构目标样本)。这导致每次计算的都是原始数据的IoM哈希,优化过程完全无法收敛到目标哈希对应的样本。

修正后的fitness_iom_edit函数:

function [distance] = fitness_iom_edit(x, hashcode,opts)
    % 复制opts并替换为当前待优化的单条样本x
    opts_local = opts;
    opts_local.data = x(:)'; % 确保转为行向量,与原始数据格式一致
    opts_local.nopersons = 1; % 仅处理当前这一条样本
    
    % 计算x对应的IoM哈希
    [transformed_data] = IoM_edit(opts_local);

    distcc = [];
    for a=1:size(hashcode,1)
        % 计算哈希匹配率(若matching_IoM未实现,直接用此逻辑替代)
        match_ratio = mean(hashcode(a,:) == transformed_data);
        distcc = [distcc, 1 - match_ratio];
    end
    distance = mean(distcc);
end

2. IoM_edit函数内存未预分配

原函数中通过循环拼接数组maxout_code_training_ind = [maxout_code_training_ind, ind_training],效率极低且可能导致内存异常。

修正后的IoM_edit函数:

function [transformed_templates] = IoM_edit(opts)
    persons = opts.nopersons;
    % 预分配哈希矩阵内存,避免动态拼接
    transformed_templates = zeros(persons, opts.Nprojection);
    
    for i=1:persons
        training_vector = opts.data(i,:);
        maxout_code_training_ind = zeros(1, opts.Nprojection);
        
        for counter = 1:opts.Nprojection
            tmp_training = training_vector * opts.model(:,:,counter);
            [~, ind_training] = max(tmp_training); % 无需保存最大值,仅取索引
            maxout_code_training_ind(counter) = ind_training;
        end
        transformed_templates(i,:) = maxout_code_training_ind;
    end
end

3. 未定义dimensions变量导致保存失败

主文件最后保存时使用了num2str(dimensions),但代码中从未定义该变量,会直接报错。将其替换为已定义的opts.Ndimension:

save(['data/iomhashing_reconstructnoconstraint_',num2str(opts.Ndimension),'.mat'],'reconstruct_x');
save(['data/iomhashing_eer_',num2str(opts.Ndimension),'.mat'],'EER_HASH');

4. 补充matching_IoM函数实现(若缺失)

如果原代码中没有实现matching_IoM,添加以下函数计算IoM哈希的匹配比例:

function [match_ratio] = matching_IoM(hash1, hash2)
    % 计算两个IoM哈希的匹配比例:相同位置索引相等的占比
    if size(hash2, 1) > 1
        % hash2为多条样本时,逐行计算与hash1的匹配率
        match_ratio = mean(hash1 == hash2, 2);
    else
        match_ratio = mean(hash1 == hash2);
    end
end

内容的提问来源于stack exchange,提问作者GeGe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 09:22:08