CoolFace
Datasetpublic

GSaha567/seq_level_training_data

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes52downloads
shard_000055.csv82424 linesDownload Raw Back to root
1text,length,is_long_context,metric_val,label_metric2"/*3 * Licensed to the Apache Software Foundation (ASF) under one4 * or more contributor license agreements.  See the NOTICE file5 * distributed with this work for additional information6 * regarding copyright ownership.  The ASF licenses this file7 * to you under the Apache License, Version 2.0 (the8 * License); you may not use this file except in compliance9 * with the License.  You may obtain a copy of the License at10 *11 *   http://www.apache.org/licenses/LICENSE-2.012 *13 * Unless required by applicable law or agreed to in writing,14 * software distributed under the License is distributed on an15 * AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY16 * KIND, either express or implied.  See the License for the17 * specific language governing permissions and limitations18 * under the License.19 */20 21/*22 * Copyright (c) 2017, Open AI Lab23 * Author: haitao@openailab.com24 * Author: chunyinglv@openailab.com25 */26#include <iostream>27#include <functional>28#include <unordered_map>29#include ""compiler.hpp""30#include <algorithm>31#include <google/protobuf/io/coded_stream.h>32#include <google/protobuf/io/zero_copy_stream_impl.h>33#include <google/protobuf/text_format.h>34#include <google/protobuf/message.h>35 36#include ""tengine_c_api.h""37#include ""data_type.hpp""38#include ""type_name.hpp""39#include ""exec_attr.hpp""40#include ""tengine_errno.hpp""41#include ""caffe_serializer.hpp""42#include ""operator_manager.hpp""43#include ""operator/conv_param.hpp""44#include ""operator/pool_param.hpp""45#include ""operator/fc_param.hpp""46#include ""operator/concat_param.hpp""47#include ""operator/batch_norm_param.hpp""48#include ""operator/scale_param.hpp""49#include ""operator/lrn_param.hpp""50#include ""operator/softmax_param.hpp""51#include ""operator/eltwise_param.hpp""52#include ""operator/slice_param.hpp""53#include ""operator/normalize_param.hpp""54#include ""operator/permute_param.hpp""55#include ""operator/flatten_param.hpp""56#include ""operator/priorbox_param.hpp""57#include ""operator/reshape_param.hpp""58#include ""operator/detection_output_param.hpp""59#include ""operator/rpn_param.hpp""60#include ""operator/roi_pooling_param.hpp""61#include ""operator/relu_param.hpp""62#include ""operator/reorg_param.hpp""63#include ""operator/region_param.hpp""64#include ""operator/deconv_param.hpp""65#include ""operator/resize_param.hpp""66#include ""operator/split_param.hpp""67#include ""operator/upsample_param.hpp""68#include ""operator/power_param.hpp""69#include ""operator/clip_param.hpp""70#include ""operator/tile_param.hpp""71#include ""operator/tile_param.hpp""72#include ""operator/shuffle_channel_param.hpp""73#include ""operator/crop_param.hpp""74#include ""operator/absval.hpp""75#include ""operator/interp.hpp""76#include ""operator/elu.hpp""77#include ""operator/threshold.hpp""78#include ""operator/mvn.hpp""79#include ""operator/embed.hpp""80#include ""operator/reduction.hpp""81#include ""operator/bias.hpp""82 83namespace TEngine {84 85using op_load_t = std::function<bool(StaticGraph*, StaticNode*, const te_caffe::LayerParameter&)>;86using blob_load_t = std::function<bool(StaticGraph*, StaticNode*, const te_caffe::LayerParameter&)>;87 88std::unordered_map<std::string, blob_load_t> blob_load_map;89 90// Check if NetParameter uses old style V0LayerParameter91static bool NetNeedsV0ToV1Upgrade(const te_caffe::NetParameter& caffe_net)92{93    for(int i = 0; i < caffe_net.layers_size(); ++i)94    {95        if(caffe_net.layers(i).has_layer())96            return true;97    }98    return false;99}100 101// Check if NetParameter uses old style V1LayerParameter102static bool NetNeedsV1ToV2Upgrade(const te_caffe::NetParameter& caffe_net)103{104    return (caffe_net.layers_size() > 0);105}106 107// Check if NetParameter uses old style input fields108static bool NetNeedsInputUpgrade(const te_caffe::NetParameter& caffe_net)109{110    return (caffe_net.input_size() > 0);111}112 113// Check if NetParameter uses old style data transformation fields114static bool NetNeedsDataUpgrade(const te_caffe::NetParameter& caffe_net)115{116    for(int i = 0; i < caffe_net.layers_size(); ++i)117    {118        if(caffe_net.layers(i).type() == te_caffe::V1LayerParameter_LayerType_DATA)119        {120            te_caffe::DataParameter layer_param = caffe_net.layers(i).data_param();121            if(layer_param.has_scale() || layer_param.has_mean_file() || layer_param.has_crop_size() ||122               layer_param.has_mirror())123                return true;124        }125        if(caffe_net.layers(i).type() == te_caffe::V1LayerParameter_LayerType_IMAGE_DATA)126        {127            te_caffe::ImageDataParameter layer_param = caffe_net.layers(i).image_data_param();128            if(layer_param.has_scale() || layer_param.has_mean_file() || layer_param.has_crop_size() ||129               layer_param.has_mirror())130                return true;131        }132        if(caffe_net.layers(i).type() == te_caffe::V1LayerParameter_LayerType_WINDOW_DATA)133        {134            te_caffe::WindowDataParameter layer_param = caffe_net.layers(i).window_data_param();135            if(layer_param.has_scale() || layer_param.has_mean_file() || layer_param.has_crop_size() ||136               layer_param.has_mirror())137                return true;138        }139    }140    return false;141}142 143static bool NetNeedsUpgrade(const char* fname, const te_caffe::NetParameter& caffe_net)144{145    if(NetNeedsV0ToV1Upgrade(caffe_net) || NetNeedsV1ToV2Upgrade(caffe_net) || NetNeedsInputUpgrade(caffe_net) ||146       NetNeedsDataUpgrade(caffe_net))147    {148        LOG_ERROR() << ""The input file specified is using deprecated params: "" << fname << ""\\n"";149        LOG_ERROR()150            << ""Please upgrade the input file by using caffe tools(upgrade_net_proto_text/upgrade_net_proto_binary).\\n"";151        return true;152    }153    return false;154}155 156bool CaffeSingle::LoadBinaryFile(const char* fname, te_caffe::NetParameter& caffe_net)157{158    std::ifstream is(fname, std::ios::in | std::ios::binary);159 160    if(!is.is_open())161    {162        LOG_ERROR() << ""cannot open file: "" << fname << ""\\n"";163        set_tengine_errno(ENOENT);164        return false;165    }166 167    google::protobuf::io::IstreamInputStream input_stream(&is);168    google::protobuf::io::CodedInputStream coded_input(&input_stream);169    // SetTotalBytesLimit(max_limit, warning_threshold)170    coded_input.SetTotalBytesLimit(1024 << 20, 512 << 20);171 172    bool ret = caffe_net.ParseFromCodedStream(&coded_input);173 174    is.close();175 176    if(!ret)177        LOG_ERROR() << ""parse file: "" << fname << "" failed\\n"";178 179    if(NetNeedsUpgrade(fname, caffe_net))180    {181        set_tengine_errno(EINVAL);182        return false;183    }184 185    return ret;186}187 188bool CaffeSingle::LoadTextFile(const char* fname, te_caffe::NetParameter& caffe_net)189{190    std::ifstream is(fname, std::ios::in);191 192    if(!is.is_open())193    {194        LOG_ERROR() << ""cannot open file: "" << fname << ""\\n"";195        set_tengine_errno(ENOENT);196        return false;197    }198 199    google::protobuf::io::IstreamInputStream input_stream(&is);200    bool ret = google::protobuf::TextFormat::Parse(&input_stream, &caffe_net);201 202    is.close();203 204    if(!ret)205        LOG_ERROR() << ""parse file: "" << fname << "" failed\\n"";206 207    if(NetNeedsUpgrade(fname, caffe_net))208    {209        set_tengine_errno(EINVAL);210        return false;211    }212 213    return ret;214}215 216bool CaffeSingle::LoadModel(const std::vector<std::string>& file_list, StaticGraph* graph)217{218    te_caffe::NetParameter caffe_net;219 220    if(file_list.size() != GetFileNum())221        return false;222 223    if(!LoadBinaryFile(file_list[0].c_str(), caffe_net))224        return false;225 226    SetGraphSource(graph, file_list[0]);227    SetGraphSourceFormat(graph, ""caffe"");228    SetGraphConstTensorFile(graph, file_list[0]);229    SetGraphLayout(graph, TENGINE_LAYOUT_NCHW);230    SetModelLayout(graph, TENGINE_LAYOUT_NCHW);231    SetModelFormat(graph, MODEL_FORMAT_CAFFE);232 233    return LoadGraph(caffe_net, graph);234}235 236bool CaffeSingle::LoadNode(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param,237                           name_map_t& tensor_name_map)238{239    for(int i = 0; i < layer_param.bottom_size(); i++)240    {241        const std::string& orig_name = layer_param.bottom(i);242 243        std::string& tensor_name = tensor_name_map[orig_name];244 245        StaticTensor* tensor = FindTensor(graph, tensor_name);246 247        AddNodeInputTensor(node, tensor);248    }249 250    for(int i = 0; i < layer_param.top_size(); i++)251    {252        const std::string& orig_name = layer_param.top(i);253        std::string tensor_name;254 255        if(tensor_name_map.count(orig_name))256            tensor_name = GetNodeName(node) + ""/"" + std::to_string(i);257        else258            tensor_name = orig_name;259 260        StaticTensor* tensor = CreateStaticTensor(graph, tensor_name);261 262        SetTensorDataType(tensor, DataType::GetTypeID(""float32""));263 264        AddNodeOutputTensor(node, tensor);265 266        // record the name mapping267 268        tensor_name_map[orig_name] = tensor_name;269    }270 271    return true;272}273 274bool CaffeSingle::LoadGraph(te_caffe::NetParameter& caffe_net, StaticGraph* graph)275{276    SetGraphIdentity(graph, ""caffe"", caffe_net.name(), ""0"");277 278    name_map_t tensor_name_map;279 280    int layer_num = caffe_net.layer_size();281    int i;282 283    std::vector<std::string> no_supported_op;284    for(i =0; i < layer_num; i++)285    {286        const te_caffe::LayerParameter& layer_param = caffe_net.layer(i);287        const std::string& caffe_op_name = layer_param.type();288 289        if(!FindOpLoadMethod(caffe_op_name))290        {291            auto it = find(no_supported_op.begin(), no_supported_op.end(), caffe_op_name);292            if(it == no_supported_op.end())293                no_supported_op.push_back(caffe_op_name);294        }295    }296    if(no_supported_op.size() != 0)297    {298        LOG_ERROR() << ""These ""<< no_supported_op.size() << ""ops are not supported:\\n"";299        LOG_ERROR() << ""{"";300 301        for(int j = 0 ; j < static_cast<int> (no_supported_op.size()); j++)302        {303            LOG_ERROR() << no_supported_op[j] << "","";304        }305        LOG_ERROR() << ""}\\n"";306        return false;307    }308    for(i = 0; i < layer_num; i++)309    {310        const te_caffe::LayerParameter& layer_param = caffe_net.layer(i);311        const std::string& caffe_op_name = layer_param.type();312 313        //if(!FindOpLoadMethod(caffe_op_name))314        //{315        //    LOG_ERROR() << ""cannot find load function for operator: "" << caffe_op_name << ""\\n"";316        //    break;317       // }318 319        StaticNode* node = CreateStaticNode(graph, layer_param.name());320 321        if(!LoadNode(graph, node, layer_param, tensor_name_map))322            break;323 324        op_load_t op_func = any_cast<op_load_t>(GetOpLoadMethod(caffe_op_name));325 326        if(!op_func(graph, node, layer_param))327            break;328    }329 330    if(i < layer_num)331        return false;332 333    return true;334}335 336bool CaffeBuddy::LoadModel(const std::vector<std::string>& file_list, StaticGraph* graph)337{338    if(file_list.size() != GetFileNum())339        return false;340 341    te_caffe::NetParameter test_net;342 343    if(!LoadTextFile(file_list[0].c_str(), test_net))344        return false;345 346    te_caffe::NetParameter train_net;347 348    if(!LoadBinaryFile(file_list[1].c_str(), train_net))349        return false;350 351    SetGraphSource(graph, file_list[1]);352    SetGraphSourceFormat(graph, ""caffe"");353    SetGraphConstTensorFile(graph, file_list[1]);354    SetGraphLayout(graph, TENGINE_LAYOUT_NCHW);355    SetModelLayout(graph, TENGINE_LAYOUT_NCHW);356    SetModelFormat(graph, MODEL_FORMAT_CAFFE);357 358    return LoadGraph(test_net, train_net, graph);359}360 361bool CaffeBuddy::LoadModel(const std::vector<const void*>& addr_list, const std::vector<int>& size_list,362                           StaticGraph* graph, bool transfer_mem)363{364    te_caffe::NetParameter test_net;365    te_caffe::NetParameter train_net;366 367    if(addr_list.size() != GetFileNum())368        return false;369 370    /* the first one is  proto file, the second one is parameter file */371 372    {373        std::string prototxt_str(static_cast<const char*>(addr_list[0]), size_list[0]);374        if(!google::protobuf::TextFormat::ParseFromString(prototxt_str, &test_net))375        {376            LOG_ERROR() << ""failed to parse proto file\\n"";377            return false;378        }379    }380 381    if(!train_net.ParseFromArray(addr_list[1], size_list[1]))382    {383        LOG_ERROR() << ""failed to parse parameter file\\n"";384        return false;385    }386 387    SetGraphSource(graph, ""from_mem"");388    SetGraphSourceFormat(graph, ""caffe"");389    SetGraphConstTensorFile(graph, ""memory"");390    SetGraphLayout(graph, TENGINE_LAYOUT_NCHW);391    SetModelLayout(graph, TENGINE_LAYOUT_NCHW);392    SetModelFormat(graph, MODEL_FORMAT_CAFFE);393 394    return LoadGraph(test_net, train_net, graph);395}396 397bool CaffeBuddy::LoadGraph(te_caffe::NetParameter& test_net, te_caffe::NetParameter& train_net, StaticGraph* graph)398{399    name_map_t tensor_name_map;400 401    SetGraphIdentity(graph, ""caffe"", test_net.name(), ""0"");402 403    /* create the layer name map of the train_net */404    std::unordered_map<std::string, const te_caffe::LayerParameter*> train_name_map;405 406    int layer_number;407 408    layer_number = train_net.layer_size();409 410    int i;411 412    for(i = 0; i < layer_number; i++)413    {414        const te_caffe::LayerParameter& layer_param = train_net.layer(i);415 416        train_name_map[layer_param.name()] = &layer_param;417    }418 419    layer_number = test_net.layer_size();420    int n;421 422    std::vector<std::string> no_supported_op;423    for(int i =0; i < layer_number; i++)424    {425        const te_caffe::LayerParameter& layer_param = test_net.layer(i);426        const std::string& caffe_op_name = layer_param.type();427 428        if(!FindOpLoadMethod(caffe_op_name))429        {430            auto it = find(no_supported_op.begin(), no_supported_op.end(), caffe_op_name);431            if(it == no_supported_op.end())432                no_supported_op.push_back(caffe_op_name);433        }434    }435    if(no_supported_op.size() != 0)436    {437        LOG_ERROR() << ""These ""<< no_supported_op.size() << ""ops are not supported:\\n"";438        LOG_ERROR() << ""{"";439        for(int j = 0 ; j < static_cast<int> (no_supported_op.size()); j++)440        {441            LOG_ERROR() << no_supported_op[j] << "","";442        }443        LOG_ERROR() << ""}\\n"";444        return false;445    }446    for(n = 0; n < layer_number; n++)447    {448        const te_caffe::LayerParameter& layer_param = test_net.layer(n);449        const std::string& caffe_op_name = layer_param.type();450 451        //if(!FindOpLoadMethod(caffe_op_name))452        //{453        //    LOG_ERROR() << ""cannot find load function for operator: "" << caffe_op_name << ""\\n"";454        //    break;455        //}456 457        StaticNode* node = CreateStaticNode(graph, layer_param.name());458 459        if(!LoadNode(graph, node, layer_param, tensor_name_map))460            break;461 462        op_load_t op_func = any_cast<op_load_t>(GetOpLoadMethod(caffe_op_name));463 464        if(!op_func(graph, node, layer_param))465            break;466 467        /*Load pre-trained parameters*/468        if(train_name_map.count(layer_param.name()))469        {470            const te_caffe::LayerParameter* p_train;471 472            p_train = train_name_map[layer_param.name()];473 474            if(p_train->blobs_size())475            {   476                blob_load_t func = blob_load_map[caffe_op_name];477                if(!func(graph, node, *p_train))478                    break;479            }480        }481    }482 483    if(n < layer_number)484        return false;485 486    return true;487}488 489static void LoadCaffeBlob(StaticGraph* graph, StaticNode* node, const std::vector<std::string>& name_list,490                          const std::vector<std::string>& layout_list, const te_caffe::LayerParameter& layer_param)491 492{493    unsigned int blob_num = layer_param.blobs_size();494 495    for(unsigned int i = 0; i < blob_num && i < name_list.size(); i++)496    {497        std::string new_tensor_name = GetNodeName(node) + ""/"" + name_list[i];498 499        StaticTensor* tensor = CreateStaticConstTensor(graph, new_tensor_name);500 501        /* load tensor data*/502 503        const te_caffe::BlobProto& blob = layer_param.blobs(i);504 505        std::vector<int> dims;506 507        if(blob.has_shape())508        {509            for(int i = 0; i < blob.shape().dim_size(); i++)510            {511                dims.push_back(blob.shape().dim(i));512            }513        }514        else515        {516            std::vector<int> temp;517            temp.push_back(blob.num());518            temp.push_back(blob.channels());519            temp.push_back(blob.height());520            temp.push_back(blob.width());521 522            int start = 0;523 524            while(temp[start] == 1)525                start++;526 527            for(unsigned int i = start; i < temp.size(); i++)528                dims.push_back(temp[i]);529        }530 531        SetTensorDim(tensor, dims);532        SetTensorDataType(tensor, DataType::GetTypeID(""float32""));533 534        int mem_size = blob.data_size() * 4;535 536        SetTensorSize(tensor, mem_size);537 538        float* ptr = ( float* )std::malloc(mem_size + 128);539 540        for(int i = 0; i < blob.data_size(); i++)541            ptr[i] = blob.data(i);542 543        SetConstTensorBuffer(tensor, ptr);544        SetConstTensorFileLocation(tensor, -1, 0);545 546        StaticNode* new_node = CreateStaticNode(graph, new_tensor_name);547 548        StaticOp* const_op = CreateStaticOp(graph, ""Const"");549 550        SetNodeOp(new_node, const_op);551 552        AddNodeOutputTensor(new_node, tensor);553 554        AddNodeInputTensor(node, tensor);555    }556}557 558static void CreatePresetNode(StaticGraph* graph, StaticNode* node, const char* name, const char* layout,559                             std::vector<int>& dims, float val)560{561    std::string new_tensor_name = GetNodeName(node) + ""/"" + name;562    StaticTensor* tensor = CreateStaticConstTensor(graph, new_tensor_name);563 564    SetTensorDim(tensor, dims);565    SetTensorDataType(tensor, DataType::GetTypeID(""float32""));566 567    int elem_size = 1;568 569    for(unsigned int i = 0; i < dims.size(); i++)570    {571        elem_size *= dims[i];572    }573 574    SetTensorSize(tensor, elem_size * sizeof(float));575 576    float* ptr = ( float* )std::malloc(elem_size * sizeof(float));577 578    for(int i = 0; i < elem_size; i++)579        ptr[i] = val;580 581    SetConstTensorBuffer(tensor, ptr);582    SetConstTensorFileLocation(tensor, -1, 0);583 584    StaticNode* new_node = CreateStaticNode(graph, new_tensor_name);585 586    StaticOp* const_op = CreateStaticOp(graph, ""Const"");587 588    SetNodeOp(new_node, const_op);589 590    AddNodeOutputTensor(new_node, tensor);591 592    AddNodeInputTensor(node, tensor);593}594 595static bool LoadBatchNormBlob(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)596{597    const te_caffe::BlobProto& rescale_blob = layer_param.blobs(2);598 599    StaticOp* op = GetNodeOp(node);600 601    BatchNormParam param = any_cast<BatchNormParam>(GetOperatorParam(op));602 603    param.rescale_factor = rescale_blob.data(0);604 605    SetOperatorParam(op, param);606 607    /* for compatible reason, create the two tensors: gamma (1.0) and beta (0.0) */608 609    /* get the dim, i.e., channel size */610 611    const te_caffe::BlobProto& mean_blob = layer_param.blobs(0);612 613    std::vector<int> dims;614    dims.push_back(mean_blob.shape().dim(0));615 616    CreatePresetNode(graph, node, ""gamma"", ""W"", dims, 1.0f);617    CreatePresetNode(graph, node, ""beta"", ""W"", dims, 0.0f);618 619    std::vector<std::string> name_list = {""means"", ""vars""};620    std::vector<std::string> layout_list = {""W"", ""W""};621 622    LoadCaffeBlob(graph, node, name_list, layout_list, layer_param);623 624    return true;625}626 627static bool LoadFullyConnectedBlob(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)628{629    std::vector<std::string> name_list = {""weight"", ""bias""};630    std::vector<std::string> layout_list = {""HW"", ""W""};631 632    LoadCaffeBlob(graph, node, name_list, layout_list, layer_param);633 634    return true;635}636 637static bool LoadScaleBlob(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)638{639    std::vector<std::string> name_list = {""gamma"", ""beta""};640    std::vector<std::string> layout_list = {""CHW"", ""W""};641 642    LoadCaffeBlob(graph, node, name_list, layout_list, layer_param);643 644    return true;645}646static bool LoadPReLuBlob(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)647{648    std::vector<std::string> name_list = {""slope""};649    std::vector<std::string> layout_list = {""W""};650    LoadCaffeBlob(graph, node, name_list, layout_list, layer_param);651 652    return true;653}654static bool LoadNormalizeBlob(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)655{656    std::vector<std::string> name_list = {""scale""};657    std::vector<std::string> layout_list = {""W""};658    LoadCaffeBlob(graph, node, name_list, layout_list, layer_param);659 660    return true;661}662static bool LoadConvolutionBlob(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)663{664    std::vector<std::string> name_list = {""weight"", ""bias""};665    std::vector<std::string> layout_list = {""NCHW"", ""W""};666 667    LoadCaffeBlob(graph, node, name_list, layout_list, layer_param);668 669    return true;670}671static bool LoadDeconvolutionBlob(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)672{673    std::vector<std::string> name_list = {""weight"", ""bias""};674    std::vector<std::string> layout_list = {""NCHW"", ""C""};675 676    LoadCaffeBlob(graph, node, name_list, layout_list, layer_param);677 678    return true;679}680static bool LoadCaffeInputOp(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)681{682    StaticOp* op = CreateStaticOp(graph, ""InputOp"");683 684    SetNodeOp(node, op);685 686    const te_caffe::InputParameter& input_param = layer_param.input_param();687 688    if(input_param.shape_size())689    {690        std::vector<int> dim;691        const te_caffe::BlobShape& blob_shape = input_param.shape(0);692 693        for(int i = 0; i < blob_shape.dim_size(); i++)694        {695            dim.push_back(blob_shape.dim(i));696        }697 698        StaticTensor* tensor = GetNodeOutputTensor(graph, node, 0);699 700        SetTensorDim(tensor, dim);701    }702 703    AddGraphInputNode(graph, node);704 705    return true;706}707 708static bool LoadCaffeSoftmax(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)709{710    const te_caffe::SoftmaxParameter& softmax_param = layer_param.softmax_param();711 712    SoftmaxParam param = any_cast<SoftmaxParam>(OpManager::GetOpDefParam(""Softmax""));713 714    if(softmax_param.has_axis())715        param.axis = softmax_param.axis();716    else717        param.axis = 1;718 719    StaticOp* op = CreateStaticOp(graph, ""Softmax"");720 721    SetOperatorParam(op, param);722 723    SetNodeOp(node, op);724 725    return true;726}727 728static bool LoadCaffeReorg(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)729{730    const te_caffe::ReorgParameter& caffe_param = layer_param.reorg_param();731 732    ReorgParam param = any_cast<ReorgParam>(OpManager::GetOpDefParam(""Reorg""));733 734    param.stride = caffe_param.stride();735 736    StaticOp* op = CreateStaticOp(graph, ""Reorg"");737 738    SetOperatorParam(op, param);739 740    SetNodeOp(node, op);741 742    return true;743}744static bool LoadCaffeRegion(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)745{746    const te_caffe::RegionParameter& caffe_param = layer_param.region_param();747 748    RegionParam param = any_cast<RegionParam>(OpManager::GetOpDefParam(""Region""));749 750    param.num_classes = caffe_param.num_classes();751    param.num_box = caffe_param.num_box();752    param.side = caffe_param.side();753    param.coords = caffe_param.coords();754    param.confidence_threshold = caffe_param.confidence_threshold();755    param.nms_threshold = caffe_param.nms_threshold();756 757    for(int i = 0; i < ( int )caffe_param.biases_size(); ++i)758    {759        param.biases.push_back(caffe_param.biases(i));760    }761 762    StaticOp* op = CreateStaticOp(graph, ""Region"");763 764    SetOperatorParam(op, param);765 766    SetNodeOp(node, op);767 768    return true;769}770 771static bool LoadCaffeNormalize(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)772{773    const te_caffe::NormalizeParameter& normalize_param = layer_param.norm_param();774 775    NormalizeParam param = any_cast<NormalizeParam>(OpManager::GetOpDefParam(""Normalize""));776 777    param.across_spatial = normalize_param.across_spatial();778    param.channel_shared = normalize_param.channel_shared();779 780    StaticOp* op = CreateStaticOp(graph, ""Normalize"");781 782    SetOperatorParam(op, param);783 784    SetNodeOp(node, op);785 786    return true;787}788 789static bool LoadCaffeSlice(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)790{791    const te_caffe::SliceParameter& slice_param = layer_param.slice_param();792    SliceParam param = any_cast<SliceParam>(OpManager::GetOpDefParam(""Slice""));793    if(slice_param.has_axis())794        param.axis = slice_param.axis();795    else796        param.axis = 1;797    param.iscaffe = true;798    param.slice_point_.clear();799    std::copy(slice_param.slice_point().begin(), slice_param.slice_point().end(),800              std::back_inserter(param.slice_point_));801    StaticOp* op = CreateStaticOp(graph, ""Slice"");802    SetOperatorParam(op, param);803    SetNodeOp(node, op);804 805    return true;806}807static bool LoadCaffeReLu(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)808{809    ReLuParam param = any_cast<ReLuParam>(OpManager::GetOpDefParam(""ReLu""));810 811    const te_caffe::ReLUParameter& caffe_param = layer_param.relu_param();812 813    if(caffe_param.has_negative_slope())814        param.negative_slope = static_cast<float>(caffe_param.negative_slope());815    else816        param.negative_slope = 0.f;817 818    StaticOp* op = CreateStaticOp(graph, ""ReLu"");819    SetOperatorParam(op, param);820 821    SetNodeOp(node, op);822 823    return true;824}825 826static bool LoadCaffeSplit(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)827{828    SplitParam param = any_cast<SplitParam>(OpManager::GetOpDefParam(""Split""));829    param.is_caffe = true;830    StaticOp* op = CreateStaticOp(graph, ""Split"");831    SetOperatorParam(op, param);832    SetNodeOp(node, op);833    return true;834}835 836static bool LoadCaffeConcat(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)837{838    ConcatParam param = any_cast<ConcatParam>(OpManager::GetOpDefParam(""Concat""));839 840    const te_caffe::ConcatParameter& concat_param = layer_param.concat_param();841 842    if(concat_param.has_concat_dim())843        param.axis = static_cast<int>(concat_param.concat_dim());844    else845        param.axis = concat_param.axis();846 847    StaticOp* op = CreateStaticOp(graph, ""Concat"");848 849    SetOperatorParam(op, param);850 851    SetNodeOp(node, op);852 853    return true;854}855static bool LoadCaffePermute(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)856{857    PermuteParam param = any_cast<PermuteParam>(OpManager::GetOpDefParam(""Permute""));858 859    const te_caffe::PermuteParameter& permute_param = layer_param.permute_param();860 861    param.order0 = permute_param.order(0);862    param.order1 = permute_param.order(1);863    param.order2 = permute_param.order(2);864    param.order3 = permute_param.order(3);865 866    StaticOp* op = CreateStaticOp(graph, ""Permute"");867 868    SetOperatorParam(op, param);869 870    SetNodeOp(node, op);871 872    return true;873}874static bool LoadCaffeFlatten(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)875{876    FlattenParam param = any_cast<FlattenParam>(OpManager::GetOpDefParam(""Flatten""));877 878    const te_caffe::FlattenParameter& flatten_param = layer_param.flatten_param();879 880    param.axis = flatten_param.axis();881 882    StaticOp* op = CreateStaticOp(graph, ""Flatten"");883 884    SetOperatorParam(op, param);885 886    SetNodeOp(node, op);887 888    return true;889}890static bool LoadCaffePriorBox(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)891{892    PriorBoxParam param = any_cast<PriorBoxParam>(OpManager::GetOpDefParam(""PriorBox""));893 894    const te_caffe::PriorBoxParameter& caffe_param = layer_param.prior_box_param();895    // offset896    param.offset = caffe_param.offset();897    // img_size898    if(caffe_param.has_img_h() && caffe_param.has_img_w())899    {900        param.img_h = caffe_param.img_h();901        param.img_w = caffe_param.img_w();902    }903    else if(caffe_param.has_img_size())904    {905        param.img_h = caffe_param.img_size();906        param.img_w = caffe_param.img_size();907    }908    else909    {910        param.img_h = 0;911        param.img_w = 0;912    }913    // step914    if(caffe_param.has_step_h() && caffe_param.has_step_w())915    {916        param.step_h = caffe_param.step_h();917        param.step_w = caffe_param.step_w();918    }919    else if(caffe_param.has_step())920    {921        param.step_h = caffe_param.step();922        param.step_w = caffe_param.step();923    }924    else925    {926        param.step_h = 0;927        param.step_w = 0;928    }929 930    // min_size, max_size931    for(int i = 0; i < caffe_param.min_size_size(); ++i)932    {933        param.min_size.push_back(caffe_param.min_size(i));934    }935    for(int i = 0; i < caffe_param.max_size_size(); ++i)936    {937        param.max_size.push_back(caffe_param.max_size(i));938    }939 940    // variance941    for(int i = 0; i < caffe_param.variance_size(); ++i)942    {943        param.variance.push_back(caffe_param.variance(i));944    }945    // clip946    param.clip = caffe_param.clip();947    // flip948    param.flip = caffe_param.flip();949    // aspect_ratio950    for(int i = 0; i < caffe_param.aspect_ratio_size(); ++i)951    {952        param.aspect_ratio.push_back(caffe_param.aspect_ratio(i));953    }954 955    StaticOp* op = CreateStaticOp(graph, ""PriorBox"");956 957    SetOperatorParam(op, param);958 959    SetNodeOp(node, op);960 961    return true;962}963static bool LoadCaffeResize(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)964{965    ResizeParam param = any_cast<ResizeParam>(OpManager::GetOpDefParam(""Resize""));966 967    const te_caffe::Resize1_Parameter& caffe_param = layer_param.resize1_param();968    //969    param.scale_h = caffe_param.out_height_scale();970    param.scale_w = caffe_param.out_width_scale();971    param.type = caffe_param.resize_type();972    StaticOp* op = CreateStaticOp(graph, ""Resize"");973 974    SetOperatorParam(op, param);975 976    SetNodeOp(node, op);977 978    return true;979}980static bool LoadCaffeROIPooling(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)981{982    ROIPoolingParam param = any_cast<ROIPoolingParam>(OpManager::GetOpDefParam(""ROIPooling""));983 984    const te_caffe::ROIPoolingParameter& caffe_param = layer_param.roi_pooling_param();985    //986    param.pooled_h = caffe_param.pooled_h();987    param.pooled_w = caffe_param.pooled_w();988    param.spatial_scale = caffe_param.spatial_scale();989 990    StaticOp* op = CreateStaticOp(graph, ""ROIPooling"");991 992    SetOperatorParam(op, param);993 994    SetNodeOp(node, op);995 996    return true;997}998static bool LoadCaffeRPN(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)999{1000    RPNParam param = any_cast<RPNParam>(OpManager::GetOpDefParam(""RPN""));1001 1002    const te_caffe::RPNParameter& caffe_param = layer_param.rpn_param();1003    //1004    param.feat_stride = caffe_param.feat_stride();1005    param.basesize = caffe_param.basesize();1006    param.min_size = caffe_param.boxminsize();1007    param.per_nms_topn = caffe_param.per_nms_topn();1008    param.post_nms_topn = caffe_param.post_nms_topn();1009    param.nms_thresh = caffe_param.nms_thresh();1010 1011    for(int i = 0; i < caffe_param.scale_size(); ++i)1012    {1013        param.anchor_scales.push_back(caffe_param.scale(i));1014    }1015    for(int i = 0; i < caffe_param.ratio_size(); ++i)1016    {1017        param.ratios.push_back(caffe_param.ratio(i));1018    }1019 1020    StaticOp* op = CreateStaticOp(graph, ""RPN"");1021 1022    SetOperatorParam(op, param);1023    SetOperatorDynamicShape(op);1024    SetNodeOp(node, op);1025 1026    return true;1027}1028static bool LoadCaffeDetectionOutput(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)1029{1030    DetectionOutputParam param = any_cast<DetectionOutputParam>(OpManager::GetOpDefParam(""DetectionOutput""));1031 1032    const te_caffe::DetectionOutputParameter& caffe_param = layer_param.detection_output_param();1033 1034    param.num_classes = caffe_param.num_classes();1035    param.confidence_threshold = caffe_param.confidence_threshold();1036    param.keep_top_k = caffe_param.keep_top_k();1037    param.nms_threshold = caffe_param.nms_param().nms_threshold();1038    if(caffe_param.nms_param().has_top_k())1039    {1040        param.nms_top_k = caffe_param.nms_param().top_k();1041    }1042    StaticOp* op = CreateStaticOp(graph, ""DetectionOutput"");1043    SetOperatorParam(op, param);1044    SetOperatorDynamicShape(op);1045    SetNodeOp(node, op);1046 1047    return true;1048}1049static bool LoadCaffeReshape(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)1050{1051    ReshapeParam param = any_cast<ReshapeParam>(OpManager::GetOpDefParam(""Reshape""));1052 1053    const te_caffe::ReshapeParameter& caffe_param = layer_param.reshape_param();1054    // dims1055    if(caffe_param.shape().dim_size() == 6)1056    {1057        param.re_shape.push_back(caffe_param.shape().dim(0));1058        param.re_shape.push_back(caffe_param.shape().dim(1));1059        param.re_shape.push_back(caffe_param.shape().dim(2));1060        param.re_shape.push_back(caffe_param.shape().dim(3));                        1061        param.re_shape.push_back(caffe_param.shape().dim(4));1062        param.re_shape.push_back(caffe_param.shape().dim(4));1063    } else if(caffe_param.shape().dim_size() == 5)1064    {1065        param.re_shape.push_back(caffe_param.shape().dim(0));1066        param.re_shape.push_back(caffe_param.shape().dim(1));1067        param.re_shape.push_back(caffe_param.shape().dim(2));1068        param.re_shape.push_back(caffe_param.shape().dim(3));                        1069        param.re_shape.push_back(caffe_param.shape().dim(4));1070    } else if(caffe_param.shape().dim_size() == 4)1071    {1072        param.re_shape.push_back(caffe_param.shape().dim(0));1073        param.re_shape.push_back(caffe_param.shape().dim(1));1074        param.re_shape.push_back(caffe_param.shape().dim(2));1075        param.re_shape.push_back(caffe_param.shape().dim(3));                        1076 1077    }1078    else if(caffe_param.shape().dim_size() == 3)1079    {1080        param.re_shape.push_back(caffe_param.shape().dim(0));1081        param.re_shape.push_back(caffe_param.shape().dim(1));1082        param.re_shape.push_back(caffe_param.shape().dim(2));1083    }1084    else if(caffe_param.shape().dim_size() == 2)1085    {1086        param.re_shape.push_back(caffe_param.shape().dim(0));1087        param.re_shape.push_back(caffe_param.shape().dim(1));1088    }1089    else if(caffe_param.shape().dim_size() == 1)1090    {1091        param.re_shape.push_back(caffe_param.shape().dim(0));1092    }1093 1094    StaticOp* op = CreateStaticOp(graph, ""Reshape"");1095    SetOperatorParam(op, param);1096    SetNodeOp(node, op);1097 1098    return true;1099 1100}1101static EltType ConvertCaffeEltwise(te_caffe::EltwiseParameter_EltwiseOp method)1102{1103    if(method == te_caffe::EltwiseParameter_EltwiseOp_PROD)1104        return ELT_PROD;1105    else if(method == te_caffe::EltwiseParameter_EltwiseOp_MAX)1106        return ELT_MAX;1107 1108    /* for others, return SUM */1109 1110    return ELT_SUM;1111}1112 1113static bool LoadCaffeEltwise(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)1114{1115    const te_caffe::EltwiseParameter& eltwise_param = layer_param.eltwise_param();1116    EltwiseParam param = any_cast<EltwiseParam>(OpManager::GetOpDefParam(""Eltwise""));1117    // defalt: SUM1118    param.type = ELT_SUM;1119    if(eltwise_param.has_operation())1120        param.type = ConvertCaffeEltwise(eltwise_param.operation());1121 1122    param.caffe_flavor = 1;1123    param.shift = eltwise_param.shift();1124    param.scale = eltwise_param.scale();1125    param.power = eltwise_param.power();1126 1127    StaticOp* op = CreateStaticOp(graph, ""Eltwise"");1128    SetOperatorParam(op, param);1129    SetNodeOp(node, op);1130 1131    return true;1132}1133 1134static bool LoadCaffeDropout(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)1135{1136    StaticOp* op = CreateStaticOp(graph, ""Dropout"");1137 1138    SetNodeOp(node, op);1139 1140    return true;1141}1142 1143static bool LoadCaffeAccuracy(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)1144{1145    StaticOp* op = CreateStaticOp(graph, ""Accuracy"");1146 1147    SetNodeOp(node, op);1148 1149    AddGraphOutputNode(graph, node);1150 1151    return true;1152}1153 1154static bool LoadCaffeConvolution(StaticGraph* graph, StaticNode* node, const te_caffe::LayerParameter& layer_param)1155{1156    const te_caffe::ConvolutionParameter& conv_param = layer_param.convolution_param();1157    // const te_caffe::LayerParameter& layer_param = caffe_net.layer(i);1158    const std::string& caffe_op_name = layer_param.type();1159    ConvParam param = any_cast<ConvParam>(OpManager::GetOpDefParam(""Convolution""));1160 1161    if(conv_param.has_kernel_h() && conv_param.has_kernel_w())1162    {1163        param.kernel_h = conv_param.kernel_h();1164        param.kernel_w = conv_param.kernel_w();1165    }1166    else1167    {1168        param.kernel_h = conv_param.kernel_size(0);1169        param.kernel_w = conv_param.kernel_size(0);1170    }1171 1172    if(conv_param.has_stride_h() && conv_param.has_stride_w())1173    {1174        param.stride_h = conv_param.stride_h();1175        param.stride_w = conv_param.stride_w();1176    }1177    else if(conv_param.stride_size())1178    {1179        param.stride_h = conv_param.stride(0);1180        param.stride_w = conv_param.stride(0);1181    }1182 1183    if(conv_param.has_pad_h() && conv_param.has_pad_w())1184    {1185        param.pad_h0 = conv_param.pad_h();1186        param.pad_h1 = conv_param.pad_h();1187        param.pad_w0 = conv_param.pad_w();1188        param.pad_w1 = conv_param.pad_w();1189    }1190    else if(conv_param.pad_size())1191    {1192        param.pad_h0 = conv_param.pad(0);1193        param.pad_h1 = conv_param.pad(0);1194        param.pad_w0 = conv_param.pad(0);1195        param.pad_w1 = conv_param.pad(0);1196    }1197 1198    param.output_channel = conv_param.num_output();1199 1200    if(conv_param.has_group())

Showing the first 1,200 of 82424 lines. Download the file for the rest.