4. BatchNorm和NonLinearity 实现

[C++ 基于Eigen库实现CRN前向推理]

第三部分:TransposedConv2d实现 (含dilation)

1. 基于Eigen实现BatchNorm2d
1.1 Layer_BatchNorm2d.h
//
// Created by Koer on 2022/10/31.
//

#ifndef CRN_LAYER_BATCHNORM2D_H
#define CRN_LAYER_BATCHNORM2D_H


#include "Eigen"
#include "mat.h"
#include "Eigen/CXX11/Tensor"

class Layer_BatchNorm2d {
   
public:
    Layer_BatchNorm2d();

    Layer_BatchNorm2d(int64_t bn_ch);

    void LoadState(MATFile *pmFile, const std::string &state_preffix);

    void LoadTestState();

    Eigen::Tensor<float_t, 4> forward(Eigen::Tensor<float_t, 4> &input);

private:
    int64_t channels;
    Eigen::Tensor<float_t, 2> weights;
    Eigen::Tensor<float_t, 2> bias;
    Eigen::Tensor<float_t, 2> running_mean;
    Eigen::Tensor<float_t, 2> running_var;
    int32_t num_batches_tracked;


};


#endif //CRN_LAYER_BATCHNORM2D_H

1.2 Layer_BatchNorm2d.cpp
//
// Created by Koer on 2022/10/31.
//
#include "iostream"
#include "../include/Layer_BatchNorm2d.h"

Layer_BatchNorm2d::Layer_BatchNorm2d() {
   
    this->channels = 1;
}

Layer_BatchNorm2d::Layer_BatchNorm2d(int64_t bn_ch) {
   
    this->channels = bn_ch;
}

void Layer_BatchNorm2d::LoadState(MATFile *pmFile, const std::string &state_preffix) {
   
    std::string weight_name = state_preffix + "_weight";
    std::string bias_name = state_preffix + "_bias";
    std::string rm_name = state_preffix + "_running_mean";
    std::string rv_name = state_preffix + "_running_var";
    std::string nbt_name = state_preffix + "_num_batches_tracked";

    mxArray *pa = matGetVariable(pmFile, weight_name.c_str());
    auto *values = (float_t *) mxGetData(pa);
    long long dim1 = mxGetM(pa);
    long long dim2 = mxGetN(pa);
    this->weights.resize(dim1, dim2);
    int idx = 0;
    for (int i = 0; i < dim2; i++) {
   
        for (int j = 0; j < dim1; j++) {
   
            this->weights(j, i) =<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值