このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。
固定小数点の量子化された制限ボルツマン マシン アルゴリズムを使用したイメージのノイズ除去
この例では、組み込み効率の良い完全な固定小数点設計を得るのに役立つ 2 つのワークフローを取り上げます。以下の例では、次のような操作方法を説明します。
データ型最適化で複数のシミュレーション シナリオを使用する。
データ型最適化に設計範囲から派生した範囲を使用する。
数値的な動作のベンチマークとして、Model Verification ライブラリのブロックを使用してシナリオごとに異なるベンチマークを使用する。
固定小数点データ型をサポートしない算術演算を効率的なルックアップ テーブルに置き換える。
最適な固定小数点データ型を使用するためのモデルの変換
この例のモデルでは、イメージのノイズの除去に制限ボルツマン マシン (RBM) アルゴリズムを使用しています。イメージ データと RBM アルゴリズムの重みを読み込みます。元のイメージと歪んだイメージが変数 imgOriginal
および imgDistorted
に格納されます。それぞれの行列の各行は MNIST データセットのテスト イメージです。
load RBMData;
最初のテスト イメージのセットを開いて表示します。
singleImgDistorted = imgDistorted(1,:); singleImgOriginal = imgOriginal(1,:); imgSize = length(singleImgOriginal); subplot(1,2,1) imshow(reshape(singleImgOriginal,[28,28])') title('Original Image'); subplot(1,2,2) imshow(reshape(singleImgDistorted,[28,28])') title('Distorted Image')
モデルを開きます。このモデルでは、歪んだテスト イメージを読み込み、RBM アルゴリズムを使用してイメージのノイズを除去してから、ノイズ除去後のイメージをノイズが追加されていない元のイメージと比較します。シミュレーション速度を向上させるため、このモデルではビデオ表示はオフになっています。ビデオ表示をオンにするには、変数 DISPLAY_VIEWER
を 1 に設定します。
model = 'ex_rbmDenoiser01';
open_system(model);
DISPLAY_VIEWER = 0;
固定小数点データ型を使用するようにモデルを変換する際は、モデルを動作範囲全体にわたって実行しながら範囲を収集することが重要です。そのためには、複数のシミュレーション シナリオを定義します。この例では、5 つのシミュレーション シナリオのそれぞれで、ノイズを除去して元のイメージと比較する新しいテスト イメージのセットを定義します。
IMGN = 5; si = Simulink.SimulationInput.empty(0, IMGN); for indx = 1:IMGN si(indx) = Simulink.SimulationInput(model); si(indx) = si(indx).setVariable('singleImgDistorted', imgDistorted(indx,:)); si(indx) = si(indx).setVariable('singleImgOriginal', imgOriginal(indx,:)); end
各シミュレーション シナリオで、元のイメージとノイズ除去後のイメージの平均二乗誤差が 0.02 未満であることを検証します。
si(1) = si(1).setBlockParameter([model '/CompareToOriginal/check'], 'max', '0.02'); si(2) = si(2).setBlockParameter([model '/CompareToOriginal/check'], 'max', '0.02'); si(3) = si(3).setBlockParameter([model '/CompareToOriginal/check'], 'max', '0.02'); si(4) = si(4).setBlockParameter([model '/CompareToOriginal/check'], 'max', '0.02'); si(5) = si(5).setBlockParameter([model '/CompareToOriginal/check'], 'max', '0.03');
最適化で使用するオプションを定義します。この例では、変換後のモデルの語長を 8 ~ 16 ビットに制限します。最適化アルゴリズムで実行する反復回数を制限することもできます。
options = fxpOptimizationOptions(... 'AllowableWordLengths', [8 16], ... 'MaxIterations', 50, ... 'Patience', 50);
シミュレーション シナリオを使用したシミュレーション範囲の収集に加え、モデルの派生範囲も収集するには、UseDerivedRangeAnalysis
オプションを true
に設定します。多くの場合、派生範囲の解析では、システムのダイナミック レンジについてシミュレーションを通じて収集される範囲よりも保守的な推定が返されます。
options.AdvancedOptions.UseDerivedRangeAnalysis = true;
最適化で使用するシミュレーション シナリオを指定します。
options.AdvancedOptions.SimulationScenarios = si;
関数 fxpopt
を使用して、fxpOptimizationOptions
オブジェクト options
で指定したオプションに従って RBM Denoiser サブシステムのデータ型を最適化します。
result = fxpopt(model, [model '/RBM Denoiser'], options);
+ Starting data type optimization... + Checking for unsupported constructs. + Preprocessing + Modeling the optimization problem - Constructing decision variables + Running the optimization solver - Evaluating new solution: cost 344, does not meet the behavioral constraints. - Evaluating new solution: cost 656, meets the behavioral constraints. - Updated best found solution, cost: 656 - Evaluating new solution: cost 640, meets the behavioral constraints. - Updated best found solution, cost: 640 - Evaluating new solution: cost 624, does not meet the behavioral constraints. - Evaluating new solution: cost 632, meets the behavioral constraints. - Updated best found solution, cost: 632 - Evaluating new solution: cost 608, meets the behavioral constraints. - Updated best found solution, cost: 608 - Evaluating new solution: cost 600, meets the behavioral constraints. - Updated best found solution, cost: 600 - Evaluating new solution: cost 584, does not meet the behavioral constraints. - Evaluating new solution: cost 592, meets the behavioral constraints. - Updated best found solution, cost: 592 - Evaluating new solution: cost 568, meets the behavioral constraints. - Updated best found solution, cost: 568 - Evaluating new solution: cost 560, meets the behavioral constraints. - Updated best found solution, cost: 560 - Evaluating new solution: cost 544, meets the behavioral constraints. - Updated best found solution, cost: 544 - Evaluating new solution: cost 504, meets the behavioral constraints. - Updated best found solution, cost: 504 - Evaluating new solution: cost 440, meets the behavioral constraints. - Updated best found solution, cost: 440 - Evaluating new solution: cost 432, meets the behavioral constraints. - Updated best found solution, cost: 432 - Evaluating new solution: cost 424, meets the behavioral constraints. - Updated best found solution, cost: 424 - Evaluating new solution: cost 408, meets the behavioral constraints. - Updated best found solution, cost: 408 - Evaluating new solution: cost 400, meets the behavioral constraints. - Updated best found solution, cost: 400 - Evaluating new solution: cost 392, meets the behavioral constraints. - Updated best found solution, cost: 392 - Evaluating new solution: cost 376, meets the behavioral constraints. - Updated best found solution, cost: 376 - Evaluating new solution: cost 360, does not meet the behavioral constraints. - Evaluating new solution: cost 360, does not meet the behavioral constraints. - Evaluating new solution: cost 392, meets the behavioral constraints. - Evaluating new solution: cost 416, meets the behavioral constraints. - Evaluating new solution: cost 424, meets the behavioral constraints. - Evaluating new solution: cost 480, meets the behavioral constraints. - Evaluating new solution: cost 472, meets the behavioral constraints. - Evaluating new solution: cost 512, meets the behavioral constraints. + Optimization has finished. - Neighborhood search complete. - Maximum number of iterations completed. + Fixed-point implementation that satisfies the behavioral constraints found. The best found solution is applied on the model. - Total cost: 376 - Use the explore method of the result to explore the implementation.
倍精度のベースライン モデルの動作を固定小数点データ型を使用するモデルと比較するために、最適化された固定小数点モデルを新しい名前で保存します。
modelAfterFxpopt = 'ex_rbmDenoiser02';
save_system(model, modelAfterFxpopt);
ロジスティック回帰のルックアップ テーブルへの置き換え
LogisticExpression サブシステムには、固定小数点データ型をサポートしない演算が含まれています。それらのサブシステムを元の動作を厳密に近似するルックアップ テーブルに置き換えます。
functionToApproximate = [modelAfterFxpopt '/RBM Denoiser/Logistic/LogisticExpression'];
problem = FunctionApproximation.Problem(functionToApproximate);
problem.Options.AbsTol = 2^-6;
problem.Options.RelTol = 2^-7;
solution = solve(problem);
replaceWithApproximate(solution);
| ID | Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification | Error(Max,Current) | | 0 | 32 | 0 | 2 | 8 | 8 | EvenSpacing | 1.562500e-02, 5.000000e-01 | | 1 | 160 | 0 | 18 | 8 | 8 | EvenSpacing | 1.562500e-02, 1.562500e-01 | | 2 | 312 | 0 | 37 | 8 | 8 | EvenSpacing | 1.562500e-02, 9.375000e-02 | | 3 | 704 | 0 | 86 | 8 | 8 | EvenSpacing | 1.562500e-02, 3.125000e-02 | | 4 | 2064 | 1 | 256 | 8 | 8 | EvenSpacing | 1.562500e-02, 0.000000e+00 | | 5 | 128 | 0 | 14 | 8 | 8 | EvenSpacing | 1.562500e-02, 3.593750e-01 | | 6 | 120 | 0 | 13 | 8 | 8 | EvenSpacing | 1.562500e-02, 4.218750e-01 | | 7 | 248 | 0 | 29 | 8 | 8 | EvenSpacing | 1.562500e-02, 1.718750e-01 | | 8 | 224 | 0 | 26 | 8 | 8 | EvenSpacing | 1.562500e-02, 1.875000e-01 | | 9 | 528 | 0 | 64 | 8 | 8 | EvenSpacing | 1.562500e-02, 3.125000e-02 | | 10 | 432 | 0 | 52 | 8 | 8 | EvenSpacing | 1.562500e-02, 6.250000e-02 | | 11 | 1040 | 1 | 128 | 8 | 8 | EvenSpacing | 1.562500e-02, 1.562500e-02 | | 12 | 96 | 0 | 10 | 8 | 8 | EvenSpacing | 1.562500e-02, 3.125000e-01 | | 13 | 88 | 0 | 9 | 8 | 8 | EvenSpacing | 1.562500e-02, 5.625000e-01 | | 14 | 168 | 0 | 19 | 8 | 8 | EvenSpacing | 1.562500e-02, 3.125000e-01 | | 15 | 128 | 1 | 8 | 8 | 8 | ExplicitValues | 1.562500e-02, 1.562500e-02 | | 16 | 128 | 1 | 8 | 8 | 8 | ExplicitValues | 1.562500e-02, 1.562500e-02 | | 17 | 2064 | 1 | 256 | 8 | 8 | EvenPow2Spacing | 1.562500e-02, 0.000000e+00 | | 18 | 1040 | 1 | 128 | 8 | 8 | EvenPow2Spacing | 1.562500e-02, 1.562500e-02 | Best Solution | ID | Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification | Error(Max,Current) | | 15 | 128 | 1 | 8 | 8 | 8 | ExplicitValues | 1.562500e-02, 1.562500e-02 |
両方の LogisticExpression サブシステムで同じアルゴリズムを実装しているため、2 つ目の LogisticExpression サブシステムは前の手順で作成した同じルックアップ テーブルに置き換えることができます。
lutBlockPath = functionToApproximate; subsystemToReplace = [modelAfterFxpopt '/RBM Denoiser/Logistic1/LogisticExpression']; pos = get_param(subsystemToReplace, 'Position'); delete_block(subsystemToReplace); add_block(lutBlockPath, subsystemToReplace,'Position',pos); set_param(subsystemToReplace, 'Commented', 'off');
元のモデルの動作と組み込み効率の良いバージョンの比較
ルックアップ テーブルによる近似を使用した固定小数点モデルのシミュレーション動作を、元の倍精度のベースライン バージョンと比較します。同じシミュレーション シナリオを更新後のモデル用に定義します。
siFA = Simulink.SimulationInput.empty(0, IMGN); for indx = 1:IMGN siFA(indx) = Simulink.SimulationInput(modelAfterFxpopt); siFA(indx) = siFA(indx).setVariable('singleImgDistorted', imgDistorted(indx,:)); siFA(indx) = siFA(indx).setVariable('singleImgOriginal', imgOriginal(indx,:)); end siFA(1) = siFA(1).setBlockParameter([modelAfterFxpopt '/CompareToOriginal/check'], 'max', '0.02'); siFA(2) = siFA(2).setBlockParameter([modelAfterFxpopt '/CompareToOriginal/check'], 'max', '0.02'); siFA(3) = siFA(3).setBlockParameter([modelAfterFxpopt '/CompareToOriginal/check'], 'max', '0.02'); siFA(4) = siFA(4).setBlockParameter([modelAfterFxpopt '/CompareToOriginal/check'], 'max', '0.02'); siFA(5) = siFA(5).setBlockParameter([modelAfterFxpopt '/CompareToOriginal/check'], 'max', '0.03');
ルックアップ テーブル置換を含むモデルをシミュレートし、シミュレーション動作を観察します。元のイメージとノイズ除去後のイメージの平均二乗誤差が 0.02 を超えると、モデルからエラーがスローされます。
simOutAfterFA = sim(siFA);
assert(all(arrayfun(@(x)(isempty(x.ErrorMessage)), simOutAfterFA)), 'Final model does not meet the behavioral constraints');
[01-Oct-2020 10:50:29] Running simulations... [01-Oct-2020 10:50:30] Completed 1 of 5 simulation runs [01-Oct-2020 10:50:31] Completed 2 of 5 simulation runs [01-Oct-2020 10:50:32] Completed 3 of 5 simulation runs [01-Oct-2020 10:50:32] Completed 4 of 5 simulation runs [01-Oct-2020 10:50:33] Completed 5 of 5 simulation runs
モデルの保存
サポートされないサブシステムをルックアップ テーブルによる近似に置き換えたら、モデルを保存します。
modelAfterFunctionApproximation = 'ex_rbmDenoiser03';
save_system(modelAfterFxpopt, modelAfterFunctionApproximation);