parse yolo layers

This commit is contained in:
Francesco Gatti
2020-05-30 18:44:02 +02:00
2 changed files with 88 additions and 21 deletions
+84 -14
View File
@@ -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<int> 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<<name<<std::endl;
//std::cout<<value<<std::endl;
if(name == "width")
fields.width = std::stoi(value);
else if (name == "height")
fields.height = std::stoi(value);
else if (name == "channels")
fields.channels = std::stoi(value);
else if (name == "batch_normalize")
fields.batch_normalize = std::stoi(value);
else if (name == "filters")
fields.filters = std::stoi(value);
else if (name == "activation")
fields.activation = value;
return true;
}
@@ -31,27 +81,47 @@ namespace tk { namespace dnn {
return new tk::dnn::Network(dim);
}
void darknetAddLayer(tk::dnn::Network *net, darknetFields_t &fields) {
void darknetAddLayer(tk::dnn::Network *net, darknetFields_t &f, std::string wgs_path) {
if(net == nullptr)
FatalError("Cant add a layer without a Net\n");
std::cout<<"Add layer: "<<fields.type<<"\n";
if(fields.type == "convolutional") {
std::cout<<"Add layer: "<<f.type<<"\n";
if(f.type == "convolutional") {
std::string wgs = wgs_path + "/c" + std::to_string(net->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 "<<layerIdx<<" "<<net->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<tk::dnn::Layer*> layers;
for(int i=0; i<f.layers.size(); i++) {
int layerIdx = net->num_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) {
+4 -7
View File
@@ -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;