ncnn源碼分析-003-net


1.結構信息

net是ncnn的核心部分,起着組織整個框架結構的作用,捋順net的結構,基本上對ncnn的代碼框架也就有一個大概的了解了。首先看一下net的類結構信息。

class Net
{
public:
    int usewinograd_convolution; //是否使用winograd進行卷積
    int use_sgemm_convolution; //是否使用矩形乘法的形式進行卷積
    int use_int8_inference; //是否使用int8進行推斷
    int use_vulkan_compute; //是否使用gpu
    
    int load_param(FILE *fp); //從參數文件中讀取網絡結構
    int load_model(FILE *fp); //從模型文件中讀取模型參數
    Extractor create_extractor(); //net中的另一個類Extractor
protected:
    std::vector<Blob> blobs;//網絡的所有blob,但是不包含blob的具體數據(nchw維數據)
    std::vector<Layer*> layers;//網絡的所有層指針
    
    int forward_layer(int layer_index, std::vector<Mat> &blob_mats, Options &opt);
    int find_blob_index_by_name(const char* name); //通過blob名字查找blobs里的index
    int find_layer_index_by_name(const char *name); //通過layer名字確定layer的索引
}

class Extractor
{
public:
    int Extractor::input(const char *blob_name, const Mat &in);
    int Extractor::input(int blob_index, VkMat &feat, VkCompute &cmd);
    int Extractor::extract(const char *blob_name, Mat &feat);
    int Extractor::extract(blob_index, const Mat &feat);//次函數直接forward_layer()

private:
    const Net *net;
    std::vector<Mat> blob_mats; // 該結構體是blob的真正數據存放
    Option opt;
}
2.forward_layer
  • forwar_layer有兩個主要輸入參數,分別是layer_index和blob_mats
    layer_index:要提取的blob的生產者
    blob_mats:整個網絡中所有blob的真正數據
    首先根據layer_index找到對應layer,然后提取該layer的bottom_index和top_index,再根據bottom_index找到對應的blob,最后找到該blob的生產者,也就是上一層,進入遞歸調用。直到找到網絡的第一層。
  • 入棧過程工作是從后往前找:layer_index——>bottom_blob——>bottom.producer(layer_index)不斷找當前層的前一層,終止條件是bottom_blob維度不等於0
  • 出棧是從前往后一次執行layer的類成員函數layer->forward(bottom_blob, top_blob,opt):第一層的的輸入是數據,得到輸出,再作為下一層的輸入,依次出棧,bottom_blob_index和top_blob_index是在入棧時候確定好的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM