diff --git a/include/tkDNN/DarknetParser.h b/include/tkDNN/DarknetParser.h index 5ba2e54..da6cfc1 100644 --- a/include/tkDNN/DarknetParser.h +++ b/include/tkDNN/DarknetParser.h @@ -8,9 +8,30 @@ namespace tk { namespace dnn { std::string type = ""; int width = 0; int height = 0; - int channels = 3; + int channels = 0; + int batch_normalize=0; + int groups = 0; + int filters=0; + int size_x=0; + int size_y=0; + int stride_x=0; + int stride_y=0; + int padding_x = 0; + int padding_y = 0; + int n_mask = 0; + int classes = 0; + int num = 0; + float scale_xy = 0; + std::vector layers; + std::string activation = ""; + }; + std::ostream& operator<<(std::ostream& os, const darknetFields_t& f){ + os << f.width << " " << f.height << " " << f.channels << " " << f.batch_normalize<< " " << f.filters << " " << " " << f.activation; + return os; + } + std::string darknetParseType(const std::string& line){ size_t start = line.find("["); size_t end = line.find("]"); @@ -21,7 +42,36 @@ namespace tk { namespace dnn { return type; } - bool darknetParseFields(const std::string& line, darknetFields_t &fields){ + bool divideNameAndValue(const std::string& line, std::string&name, std::string& value){ + size_t sep = line.find("="); + if(sep == std::string::npos) + return false; + + name = line.substr(0, sep); + value = line.substr(sep+1, line.size() - (sep+1)); + return true; + } + + bool darknetParseFields(const std::string& line, darknetFields_t& fields){ + + std::string name,value; + if(!divideNameAndValue(line, name, value)) + return false; + //std::cout<num_layers) + ".bin"; + printf("%d (%d,%d) (%d,%d) (%d,%d) %s %d %d\n", f.filters, f.size_x, f.size_y, f.stride_x, f.stride_y, f.padding_x, f.padding_y, wgs.c_str(), f.batch_normalize, f.groups); + new tk::dnn::Conv2d(net, f.filters, f.size_x, f.size_y, f.stride_x, + f.stride_y, f.padding_x, f.padding_y, wgs, f.batch_normalize, false, f.groups); - } else if(fields.type == "shortcut") { + } else if(f.type == "shortcut") { + if(f.layers.size() != 1) FatalError("no layers to shortcut\n"); + int layerIdx = net->num_layers + f.layers[0]; + if(layerIdx < 0 || layerIdx >= net->num_layers) FatalError("impossible to shortcut\n"); + std::cout<<"shortcut to "<layers[layerIdx]->getLayerName()<<"\n"; + new tk::dnn::Shortcut(net, net->layers[layerIdx]); - } else if(fields.type == "upsample") { + } else if(f.type == "upsample") { + new tk::dnn::Upsample(net, f.stride_x); - } else if(fields.type == "route") { + } else if(f.type == "route") { + if(f.layers.size() == 0) FatalError("no layers to Route\n"); + std::vector layers; + for(int i=0; inum_layers + f.layers[i]; + if(layerIdx < 0 || layerIdx >= net->num_layers) FatalError("impossible to shortcut\n"); + layers.push_back(net->layers[layerIdx]); + } + new tk::dnn::Route(net, layers.data(), layers.size()); + + } else if(f.type == "yolo") { + std::string wgs = wgs_path + "/g" + std::to_string(net->num_layers) + ".bin"; + new tk::dnn::Yolo(net, f.classes, f.num, wgs, f.n_mask, f.scale_xy); - } else if(fields.type == "yolo") { - } else{ - FatalError("layer not supported: " + fields.type); + FatalError("layer not supported: " + f.type); } } - tk::dnn::Network* darknetParser(std::string cfg_file) { + tk::dnn::Network* darknetParser(std::string cfg_file, std::string wgs_path) { tk::dnn::Network *net = nullptr; @@ -79,7 +149,7 @@ namespace tk { namespace dnn { if(fields.type == "net") net = darknetAddNet(fields); else - darknetAddLayer(net, fields); + darknetAddLayer(net, fields, wgs_path); } // new type @@ -98,7 +168,7 @@ namespace tk { namespace dnn { // end of filled type if(fields.type != "") { - darknetAddLayer(net, fields); + darknetAddLayer(net, fields, wgs_path); } if(net == nullptr) { diff --git a/tests/yolo3/yolo3.cpp b/tests/yolo3/yolo3.cpp index 1fc62a6..9ca1eeb 100644 --- a/tests/yolo3/yolo3.cpp +++ b/tests/yolo3/yolo3.cpp @@ -5,16 +5,13 @@ int main() { - tk::dnn::Network *net = tk::dnn::darknetParser("../tests/yolo3/yolov3.cfg"); - - // Network layout - //tk::dnn::dataDim_t dim(1, 3, 416, 416, 1); - //tk::dnn::Network net(dim); - - /* // create yolo3 model std::string bin_path = "yolo3"; downloadWeightsifDoNotExist("yolo3/layers/input.bin", bin_path, "https://cloud.hipert.unimore.it/s/jPXmHyptpLoNdNR/download"); + + tk::dnn::Network *net = tk::dnn::darknetParser("../tests/yolo3/yolov3.cfg", "yolo3/layers"); + net->print(); + /* int classes = 80;