Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding support for axpy caffe layer in external caffe2ncnn tool #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1271,9 +1271,10 @@ Layer* Net::create_custom_layer(int index)

int Net::forward_layer(int layer_index, std::vector<Mat>& blob_mats, Option& opt) const
{
printf("dims %d\n", blob_mats[0].dims);
const Layer* layer = layers[layer_index];

// fprintf(stderr, "forward_layer %d %s\n", layer_index, layer->name.c_str());


if (layer->one_blob_only)
{
Expand All @@ -1283,7 +1284,15 @@ int Net::forward_layer(int layer_index, std::vector<Mat>& blob_mats, Option& opt

if (blob_mats[bottom_blob_index].dims == 0)
{
printf("needing %s \n", blobs[bottom_blob_index].name.c_str());
printf("forwarding_layer %d %s\n", blobs[bottom_blob_index].producer,
layers[blobs[bottom_blob_index].producer]->name.c_str());
int ret = forward_layer(blobs[bottom_blob_index].producer, blob_mats, opt);
printf("forwarded_layer %d %s\n", blobs[bottom_blob_index].producer,
layers[blobs[bottom_blob_index].producer]->name.c_str());
printf("dims of %s : %d %d %d\n", blobs[bottom_blob_index].name.c_str(), blob_mats[bottom_blob_index].c,
blob_mats[bottom_blob_index].h,
blob_mats[bottom_blob_index].w);
if (ret != 0)
return ret;
}
Expand Down Expand Up @@ -1348,7 +1357,17 @@ int Net::forward_layer(int layer_index, std::vector<Mat>& blob_mats, Option& opt

if (blob_mats[bottom_blob_index].dims == 0)
{
int ret = forward_layer(blobs[bottom_blob_index].producer, blob_mats, opt);
printf("needing %s \n", blobs[bottom_blob_index].name.c_str());

printf("forwarding_layer %d %s\n", blobs[bottom_blob_index].producer,
layers[blobs[bottom_blob_index].producer]->name.c_str());
int ret = forward_layer(blobs[bottom_blob_index].producer, blob_mats, opt);
printf("forwarded_layer %d %s\n", blobs[bottom_blob_index].producer,
layers[blobs[bottom_blob_index].producer]->name.c_str());
printf("dims of %s: %d %d %d\n", blobs[bottom_blob_index].name.c_str(), blob_mats[bottom_blob_index].c,
blob_mats[bottom_blob_index].h,
blob_mats[bottom_blob_index].w);

if (ret != 0)
return ret;
}
Expand Down
30 changes: 28 additions & 2 deletions tools/caffe/caffe2ncnn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,17 @@ int main(int argc, char** argv)
// global definition line
// [layer count] [blob count]
int layer_count = proto.layer_size();
int layer_suppl = 0;
int blob_suppl = 0;
std::set<std::string> blob_names;
for (int i=0; i<layer_count; i++)
{
const caffe::LayerParameter& layer = proto.layer(i);
if (layer.type() == "Axpy")
{
layer_suppl+=1;
blob_suppl+=1;
}

for (int j=0; j<layer.bottom_size(); j++)
{
Expand Down Expand Up @@ -446,7 +453,7 @@ int main(int argc, char** argv)
++it;
}
}
fprintf(pp, "%lu %lu\n", layer_count + bottom_reference.size(), blob_names.size() + splitncnn_blob_count);
fprintf(pp, "%lu %lu\n", layer_count + bottom_reference.size() + layer_suppl, blob_names.size() + splitncnn_blob_count + blob_suppl);

// populate
blob_name_decorated.clear();
Expand Down Expand Up @@ -498,6 +505,25 @@ int main(int argc, char** argv)
{
fprintf(pp, "%-16s", "Clip");
}
else if (layer.type() == "Axpy")
{
// // zero : split in order not to modiyfy x
// fprintf(pp, "%-16s %-16s 1 1", "Split", (layer.name() + "_split").c_str());
// fprintf(pp, " %s %s\n", layer.bottom(1).c_str(), (layer.bottom(1)+"_split").c_str());

//first scale layer
fprintf(pp, "%-16s %-16s 2 1", "Scale", (layer.name() + "_ax").c_str());
fprintf(pp, " %s %s %s", (layer.bottom(1) //+"_split"
).c_str(), layer.bottom(0).c_str(),
(layer.name()+"_ax").c_str());
fprintf(pp, " 0=-233 1=0\n");
//then sum layer
fprintf(pp, "%-16s %-16s 2 1", "Eltwise", (layer.name()+ "_xpy").c_str());
fprintf(pp, " %s %s %s", layer.bottom(2).c_str(), (layer.name()+"_ax").c_str(),
layer.top(0).c_str());
fprintf(pp, " 0=1 -23301=2,1.0,1.0\n");
continue;
}
else
{
fprintf(pp, "%-16s", layer.type().c_str());
Expand Down Expand Up @@ -937,7 +963,7 @@ int main(int argc, char** argv)
}
else if (layer.type() == "Eltwise")
{
const caffe::EltwiseParameter& eltwise_param = layer.eltwise_param();
const caffe::EltwiseParameter& eltwise_param = layer.eltwise_param();
int coeff_size = eltwise_param.coeff_size();
fprintf(pp, " 0=%d", (int)eltwise_param.operation());
fprintf(pp, " -23301=%d", coeff_size);
Expand Down