darknet parse all net to be tested
This commit is contained in:
@@ -0,0 +1,350 @@
|
||||
#include <iostream>
|
||||
#include "tkdnn.h"
|
||||
|
||||
const char *input_bin = "dla34/debug/input.bin";
|
||||
const char *conv1_bin = "dla34/layers/features-init_block-conv1-conv.bin";
|
||||
const char *conv2_bin = "dla34/layers/features-init_block-conv2-conv.bin";
|
||||
const char *conv3_bin = "dla34/layers/features-init_block-conv3-conv.bin";
|
||||
// s - stage, t - tree
|
||||
const char *s1_t1_conv1_bin = "dla34/layers/features-stage1-tree1-body-conv1-conv.bin";
|
||||
const char *s1_t1_conv2_bin = "dla34/layers/features-stage1-tree1-body-conv2-conv.bin";
|
||||
const char *s1_t1_project = "dla34/layers/features-stage1-tree1-project_conv-conv.bin";
|
||||
const char *s1_t2_conv1_bin = "dla34/layers/features-stage1-tree2-body-conv1-conv.bin";
|
||||
const char *s1_t2_conv2_bin = "dla34/layers/features-stage1-tree2-body-conv2-conv.bin";
|
||||
const char *s1_root_conv1_bin = "dla34/layers/features-stage1-root-conv-conv.bin";
|
||||
const char *s2_t1_t1_conv1_bin = "dla34/layers/features-stage2-tree1-tree1-body-conv1-conv.bin";
|
||||
const char *s2_t1_t1_conv2_bin = "dla34/layers/features-stage2-tree1-tree1-body-conv2-conv.bin";
|
||||
const char *s2_t1_t1_project = "dla34/layers/features-stage2-tree1-tree1-project_conv-conv.bin";
|
||||
const char *s2_t1_t2_conv1_bin = "dla34/layers/features-stage2-tree1-tree2-body-conv1-conv.bin";
|
||||
const char *s2_t1_t2_conv2_bin = "dla34/layers/features-stage2-tree1-tree2-body-conv2-conv.bin";
|
||||
const char *s2_t1_root_conv1_bin = "dla34/layers/features-stage2-tree1-root-conv-conv.bin";
|
||||
const char *s2_t2_t1_conv1_bin = "dla34/layers/features-stage2-tree2-tree1-body-conv1-conv.bin";
|
||||
const char *s2_t2_t1_conv2_bin = "dla34/layers/features-stage2-tree2-tree1-body-conv2-conv.bin";
|
||||
const char *s2_t2_t2_conv1_bin = "dla34/layers/features-stage2-tree2-tree2-body-conv1-conv.bin";
|
||||
const char *s2_t2_t2_conv2_bin = "dla34/layers/features-stage2-tree2-tree2-body-conv2-conv.bin";
|
||||
const char *s2_t2_root_conv1_bin = "dla34/layers/features-stage2-tree2-root-conv-conv.bin";
|
||||
const char *s3_t1_t1_conv1_bin = "dla34/layers/features-stage3-tree1-tree1-body-conv1-conv.bin";
|
||||
const char *s3_t1_t1_conv2_bin = "dla34/layers/features-stage3-tree1-tree1-body-conv2-conv.bin";
|
||||
const char *s3_t1_t1_project = "dla34/layers/features-stage3-tree1-tree1-project_conv-conv.bin";
|
||||
const char *s3_t1_t2_conv1_bin = "dla34/layers/features-stage3-tree1-tree2-body-conv1-conv.bin";
|
||||
const char *s3_t1_t2_conv2_bin = "dla34/layers/features-stage3-tree1-tree2-body-conv2-conv.bin";
|
||||
const char *s3_t1_root_conv1_bin = "dla34/layers/features-stage3-tree1-root-conv-conv.bin";
|
||||
const char *s3_t2_t1_conv1_bin = "dla34/layers/features-stage3-tree2-tree1-body-conv1-conv.bin";
|
||||
const char *s3_t2_t1_conv2_bin = "dla34/layers/features-stage3-tree2-tree1-body-conv2-conv.bin";
|
||||
const char *s3_t2_t2_conv1_bin = "dla34/layers/features-stage3-tree2-tree2-body-conv1-conv.bin";
|
||||
const char *s3_t2_t2_conv2_bin = "dla34/layers/features-stage3-tree2-tree2-body-conv2-conv.bin";
|
||||
const char *s3_t2_root_conv1_bin = "dla34/layers/features-stage3-tree2-root-conv-conv.bin";
|
||||
const char *s4_t1_conv1_bin = "dla34/layers/features-stage4-tree1-body-conv1-conv.bin";
|
||||
const char *s4_t1_conv2_bin = "dla34/layers/features-stage4-tree1-body-conv2-conv.bin";
|
||||
const char *s4_t1_project = "dla34/layers/features-stage4-tree1-project_conv-conv.bin";
|
||||
const char *s4_t2_conv1_bin = "dla34/layers/features-stage4-tree2-body-conv1-conv.bin";
|
||||
const char *s4_t2_conv2_bin = "dla34/layers/features-stage4-tree2-body-conv2-conv.bin";
|
||||
const char *s4_root_conv1_bin = "dla34/layers/features-stage4-root-conv-conv.bin";
|
||||
|
||||
//final
|
||||
const char *fc_bin = "dla34/layers/output.bin";
|
||||
|
||||
const char *output_bin = "dla34/debug/output.bin";
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
// Network layout
|
||||
tk::dnn::dataDim_t dim(1, 3, 224, 224, 1);
|
||||
tk::dnn::Network net(dim);
|
||||
tk::dnn::Layer *last1, *last2, *last3, *last4;
|
||||
|
||||
|
||||
tk::dnn::Conv2d conv1(&net, 16, 7, 7, 1, 1, 3, 3, conv1_bin, true);
|
||||
tk::dnn::Activation relu1(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
tk::dnn::Conv2d conv2(&net, 16, 3, 3, 1, 1, 1, 1, conv2_bin, true);
|
||||
tk::dnn::Activation relu2(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
tk::dnn::Conv2d conv3(&net, 32, 3, 3, 2, 2, 1, 1, conv3_bin, true);
|
||||
tk::dnn::Activation relu3(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
last1 = &relu3;
|
||||
|
||||
// level 2
|
||||
// tree 1
|
||||
tk::dnn::Conv2d s1_t1_conv1(&net, 64, 3, 3, 2, 2, 1, 1, s1_t1_conv1_bin, true);
|
||||
tk::dnn::Activation s1_t1_relu1(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
tk::dnn::Conv2d s1_t1_conv2(&net, 64, 3, 3, 1, 1, 1, 1, s1_t1_conv2_bin, true);
|
||||
last2 = &s1_t1_conv2;
|
||||
|
||||
// get the basicblock input and apply maxpool conv2d and relu
|
||||
tk::dnn::Layer *route_s1_t1_layers[1] = { last1 };
|
||||
tk::dnn::Route route_s1_t1(&net, route_s1_t1_layers, 1);
|
||||
// downsample
|
||||
tk::dnn::Pooling s1_t1_maxpool1(&net, 2, 2, 2, 2, 0, 0, tk::dnn::POOLING_MAX);
|
||||
// project
|
||||
tk::dnn::Conv2d s1_t1_residual1_conv1(&net, 64, 1, 1, 1, 1, 0, 0, s1_t1_project, true);
|
||||
|
||||
tk::dnn::Shortcut s1_t1_s1(&net, last2);
|
||||
tk::dnn::Activation s1_t1_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
last1 = &s1_t1_relu;
|
||||
|
||||
// tree 2
|
||||
tk::dnn::Conv2d s1_t2_conv1(&net, 64, 3, 3, 1, 1, 1, 1, s1_t2_conv1_bin, true);
|
||||
tk::dnn::Activation s1_t2_relu1(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
tk::dnn::Conv2d s1_t2_conv2(&net, 64, 3, 3, 1, 1, 1, 1, s1_t2_conv2_bin, true);
|
||||
|
||||
tk::dnn::Shortcut s1_t2_s1(&net, last1);
|
||||
tk::dnn::Activation s1_t2_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
last2 = &s1_t2_relu;
|
||||
|
||||
// root
|
||||
// join last1 and net in single input 128, 56, 56
|
||||
tk::dnn::Layer *route_s1_root_layers[2] = { last2, last1 };
|
||||
tk::dnn::Route route_s1_root(&net, route_s1_root_layers, 2);
|
||||
tk::dnn::Conv2d s1_root_conv1(&net, 64, 1, 1, 1, 1, 0, 0, s1_root_conv1_bin, true);
|
||||
tk::dnn::Activation s1_root_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
last1 = &s1_root_relu;
|
||||
// level 3
|
||||
// tree 1
|
||||
// tree 1
|
||||
tk::dnn::Conv2d s2_t1_t1_conv1(&net, 128, 3, 3, 2, 2, 1, 1, s2_t1_t1_conv1_bin, true);
|
||||
tk::dnn::Activation s2_t1_t1_relu1(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
tk::dnn::Conv2d s2_t1_t1_conv2(&net, 128, 3, 3, 1, 1, 1, 1, s2_t1_t1_conv2_bin, true);
|
||||
last2 = &s2_t1_t1_conv2;
|
||||
|
||||
// get the basicblock input and apply maxpool conv2d and relu
|
||||
tk::dnn::Layer *route_s2_t1_t1_layers[1] = { last1 };
|
||||
tk::dnn::Route route_s2_t1_t1(&net, route_s2_t1_t1_layers, 1);
|
||||
// downsample
|
||||
tk::dnn::Pooling s2_t1_t1_maxpool1(&net, 2, 2, 2, 2, 0, 0, tk::dnn::POOLING_MAX);
|
||||
last4 = &s2_t1_t1_maxpool1;
|
||||
// project
|
||||
tk::dnn::Conv2d s2_t1_t1_residual1_conv1(&net, 128, 1, 1, 1, 1, 0, 0, s2_t1_t1_project, true);
|
||||
|
||||
tk::dnn::Shortcut s2_t1_t1_s1(&net, last2);
|
||||
tk::dnn::Activation s2_t1_t1_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
last1 = &s2_t1_t1_relu;
|
||||
|
||||
// tree 2
|
||||
tk::dnn::Conv2d s2_t1_t2_conv1(&net, 128, 3, 3, 1, 1, 1, 1, s2_t1_t2_conv1_bin, true);
|
||||
tk::dnn::Activation s2_t1_t2_relu1(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
tk::dnn::Conv2d s2_t1_t2_conv2(&net, 128, 3, 3, 1, 1, 1, 1, s2_t1_t2_conv2_bin, true);
|
||||
|
||||
tk::dnn::Shortcut s2_t1_t2_s1(&net, last1);
|
||||
tk::dnn::Activation s2_t1_t2_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
last2 = &s2_t1_t2_relu;
|
||||
|
||||
// root
|
||||
// join last1 and net in single input 128, 56, 56
|
||||
tk::dnn::Layer *route_s2_t1_root_layers[2] = { last2, last1 };
|
||||
tk::dnn::Route route_s2_t1_root(&net, route_s2_t1_root_layers, 2);
|
||||
tk::dnn::Conv2d s2_t1_root_conv1(&net, 128, 1, 1, 1, 1, 0, 0, s2_t1_root_conv1_bin, true);
|
||||
tk::dnn::Activation s2_t1_root_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
last1 = &s2_t1_root_relu;
|
||||
last3 = &s2_t1_root_relu;
|
||||
// tree 2
|
||||
// tree 1
|
||||
tk::dnn::Conv2d s2_t2_t1_conv1(&net, 128, 3, 3, 1, 1, 1, 1, s2_t2_t1_conv1_bin, true);
|
||||
tk::dnn::Activation s2_t2_t1_relu1(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
tk::dnn::Conv2d s2_t2_t1_conv2(&net, 128, 3, 3, 1, 1, 1, 1, s2_t2_t1_conv2_bin, true);
|
||||
tk::dnn::Shortcut s2_t2_t1_s1(&net, last1);
|
||||
tk::dnn::Activation s2_t2_t1_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
last1 = &s2_t2_t1_relu;
|
||||
|
||||
// tree 2
|
||||
tk::dnn::Conv2d s2_t2_t2_conv1(&net, 128, 3, 3, 1, 1, 1, 1, s2_t2_t2_conv1_bin, true);
|
||||
tk::dnn::Activation s2_t2_t2_relu1(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
tk::dnn::Conv2d s2_t2_t2_conv2(&net, 128, 3, 3, 1, 1, 1, 1, s2_t2_t2_conv2_bin, true);
|
||||
|
||||
tk::dnn::Shortcut s2_t2_t2_s1(&net, last1);
|
||||
tk::dnn::Activation s2_t2_t2_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
last2 = &s2_t2_t2_relu;
|
||||
|
||||
// root
|
||||
// join last1 and net in single input 128, 56, 56
|
||||
tk::dnn::Layer *route_s2_t2_root_layers[4] = { last2, last1, last4, last3};
|
||||
tk::dnn::Route route_s2_t2_root(&net, route_s2_t2_root_layers, 4);
|
||||
tk::dnn::Conv2d s2_t2_root_conv1(&net, 128, 1, 1, 1, 1, 0, 0, s2_t2_root_conv1_bin, true);
|
||||
tk::dnn::Activation s2_t2_root_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
|
||||
last1 = &s2_t2_root_relu;
|
||||
// level 4
|
||||
// tree 1
|
||||
// tree 1
|
||||
tk::dnn::Conv2d s3_t1_t1_conv1(&net, 256, 3, 3, 2, 2, 1, 1, s3_t1_t1_conv1_bin, true);
|
||||
tk::dnn::Activation s3_t1_t1_relu1(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
tk::dnn::Conv2d s3_t1_t1_conv2(&net, 256, 3, 3, 1, 1, 1, 1, s3_t1_t1_conv2_bin, true);
|
||||
last2 = &s3_t1_t1_conv2;
|
||||
|
||||
// get the basicblock input and apply maxpool conv2d and relu
|
||||
tk::dnn::Layer *route_s3_t1_t1_layers[1] = { last1 };
|
||||
tk::dnn::Route route_s3_t1_t1(&net, route_s3_t1_t1_layers, 1);
|
||||
// downsample
|
||||
tk::dnn::Pooling s3_t1_t1_maxpool1(&net, 2, 2, 2, 2, 0, 0, tk::dnn::POOLING_MAX);
|
||||
last4 = &s3_t1_t1_maxpool1;
|
||||
// project
|
||||
tk::dnn::Conv2d s3_t1_t1_residual1_conv1(&net, 256, 1, 1, 1, 1, 0, 0, s3_t1_t1_project, true);
|
||||
|
||||
tk::dnn::Shortcut s3_t1_t1_s1(&net, last2);
|
||||
tk::dnn::Activation s3_t1_t1_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
last1 = &s3_t1_t1_relu;
|
||||
|
||||
// tree 2
|
||||
tk::dnn::Conv2d s3_t1_t2_conv1(&net, 256, 3, 3, 1, 1, 1, 1, s3_t1_t2_conv1_bin, true);
|
||||
tk::dnn::Activation s3_t1_t2_relu1(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
tk::dnn::Conv2d s3_t1_t2_conv2(&net, 256, 3, 3, 1, 1, 1, 1, s3_t1_t2_conv2_bin, true);
|
||||
|
||||
tk::dnn::Shortcut s3_t1_t2_s1(&net, last1);
|
||||
tk::dnn::Activation s3_t1_t2_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
last2 = &s3_t1_t2_relu;
|
||||
|
||||
// root
|
||||
// join last1 and net in single input 256, 56, 56
|
||||
tk::dnn::Layer *route_s3_t1_root_layers[2] = { last2, last1 };
|
||||
tk::dnn::Route route_s3_t1_root(&net, route_s3_t1_root_layers, 2);
|
||||
tk::dnn::Conv2d s3_t1_root_conv1(&net, 256, 1, 1, 1, 1, 0, 0, s3_t1_root_conv1_bin, true);
|
||||
tk::dnn::Activation s3_t1_root_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
last1 = &s3_t1_root_relu;
|
||||
last3 = &s3_t1_root_relu;
|
||||
// tree 2
|
||||
// tree 1
|
||||
tk::dnn::Conv2d s3_t2_t1_conv1(&net, 256, 3, 3, 1, 1, 1, 1, s3_t2_t1_conv1_bin, true);
|
||||
tk::dnn::Activation s3_t2_t1_relu1(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
tk::dnn::Conv2d s3_t2_t1_conv2(&net, 256, 3, 3, 1, 1, 1, 1, s3_t2_t1_conv2_bin, true);
|
||||
tk::dnn::Shortcut s3_t2_t1_s1(&net, last1);
|
||||
tk::dnn::Activation s3_t2_t1_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
last1 = &s3_t2_t1_relu;
|
||||
|
||||
// tree 2
|
||||
tk::dnn::Conv2d s3_t2_t2_conv1(&net, 256, 3, 3, 1, 1, 1, 1, s3_t2_t2_conv1_bin, true);
|
||||
tk::dnn::Activation s3_t2_t2_relu1(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
tk::dnn::Conv2d s3_t2_t2_conv2(&net, 256, 3, 3, 1, 1, 1, 1, s3_t2_t2_conv2_bin, true);
|
||||
|
||||
tk::dnn::Shortcut s3_t2_t2_s1(&net, last1);
|
||||
tk::dnn::Activation s3_t2_t2_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
last2 = &s3_t2_t2_relu;
|
||||
|
||||
// root
|
||||
// join last1 and net in single input 256, 56, 56
|
||||
tk::dnn::Layer *route_s3_t2_root_layers[4] = { last2, last1, last4, last3};
|
||||
tk::dnn::Route route_s3_t2_root(&net, route_s3_t2_root_layers, 4);
|
||||
tk::dnn::Conv2d s3_t2_root_conv1(&net, 256, 1, 1, 1, 1, 0, 0, s3_t2_root_conv1_bin, true);
|
||||
tk::dnn::Activation s3_t2_root_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
last1 = &s3_t2_root_relu;
|
||||
// level 4
|
||||
// tree 1
|
||||
tk::dnn::Conv2d s4_t1_conv1(&net, 512, 3, 3, 2, 2, 1, 1, s4_t1_conv1_bin, true);
|
||||
tk::dnn::Activation s4_t1_relu1(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
tk::dnn::Conv2d s4_t1_conv2(&net, 512, 3, 3, 1, 1, 1, 1, s4_t1_conv2_bin, true);
|
||||
last2 = &s4_t1_conv2;
|
||||
|
||||
// get the basicblock input and apply maxpool conv2d and relu
|
||||
tk::dnn::Layer *route_s4_t1_layers[1] = { last1 };
|
||||
tk::dnn::Route route_s4_t1(&net, route_s4_t1_layers, 1);
|
||||
// downsample
|
||||
tk::dnn::Pooling s4_t1_maxpool1(&net, 2, 2, 2, 2, 0, 0, tk::dnn::POOLING_MAX);
|
||||
last4 = &s4_t1_maxpool1;
|
||||
// project
|
||||
tk::dnn::Conv2d s4_t1_residual1_conv1(&net, 512, 1, 1, 1, 1, 0, 0, s4_t1_project, true);
|
||||
|
||||
tk::dnn::Shortcut s4_t1_s1(&net, last2);
|
||||
tk::dnn::Activation s4_t1_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
last1 = &s4_t1_relu;
|
||||
|
||||
// tree 2
|
||||
tk::dnn::Conv2d s4_t2_conv1(&net, 512, 3, 3, 1, 1, 1, 1, s4_t2_conv1_bin, true);
|
||||
tk::dnn::Activation s4_t2_relu1(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
tk::dnn::Conv2d s4_t2_conv2(&net, 512, 3, 3, 1, 1, 1, 1, s4_t2_conv2_bin, true);
|
||||
|
||||
tk::dnn::Shortcut s4_t2_s1(&net, last1);
|
||||
tk::dnn::Activation s4_t2_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
last2 = &s4_t2_relu;
|
||||
|
||||
// root
|
||||
// join last1 and net in single input 128, 56, 56
|
||||
tk::dnn::Layer *route_s4_root_layers[3] = { last2, last1, last4 };
|
||||
tk::dnn::Route route_s4_root(&net, route_s4_root_layers, 3);
|
||||
tk::dnn::Conv2d s4_root_conv1(&net, 512, 1, 1, 1, 1, 0, 0, s4_root_conv1_bin, true);
|
||||
tk::dnn::Activation s4_root_relu(&net, CUDNN_ACTIVATION_RELU);
|
||||
|
||||
//final
|
||||
tk::dnn::Pooling avgpool(&net, 7, 7, 7, 7, 0, 0, tk::dnn::POOLING_AVERAGE);
|
||||
tk::dnn::Dense fc(&net, 1000, fc_bin);
|
||||
|
||||
// Load input
|
||||
dnnType *data;
|
||||
dnnType *input_h;
|
||||
readBinaryFile(input_bin, dim.tot(), &input_h, &data);
|
||||
//printDeviceVector(64, data, true);
|
||||
|
||||
//print network model
|
||||
net.print();
|
||||
|
||||
//convert network to tensorRT
|
||||
tk::dnn::NetworkRT netRT(&net, net.getNetworkRTName("dla34"));
|
||||
|
||||
|
||||
tk::dnn::dataDim_t out_dim;
|
||||
out_dim = net.layers[net.num_layers-1]->output_dim;
|
||||
dnnType *cudnn_out, *rt_out;
|
||||
|
||||
tk::dnn::dataDim_t dim1 = dim; //input dim
|
||||
printCenteredTitle(" CUDNN inference ", '=', 30);
|
||||
{
|
||||
dim1.print();
|
||||
TIMER_START
|
||||
net.infer(dim1, data);
|
||||
TIMER_STOP
|
||||
dim1.print();
|
||||
}
|
||||
cudnn_out = net.layers[net.num_layers-1]->dstData;
|
||||
|
||||
|
||||
// printDeviceVector(64, cudnn_out, true);
|
||||
|
||||
tk::dnn::dataDim_t dim2 = dim;
|
||||
printCenteredTitle(" TENSORRT inference ", '=', 30);
|
||||
{
|
||||
dim2.print();
|
||||
TIMER_START
|
||||
netRT.infer(dim2, data);
|
||||
TIMER_STOP
|
||||
dim2.print();
|
||||
}
|
||||
rt_out = (dnnType *)netRT.buffersRT[1];
|
||||
|
||||
|
||||
printCenteredTitle(std::string(" RESNET CHECK RESULTS ").c_str(), '=', 30);
|
||||
dnnType *out, *out_h;
|
||||
int odim = out_dim.tot();
|
||||
readBinaryFile(output_bin, odim, &out_h, &out);
|
||||
|
||||
std::cout<<"CUDNN vs correct";
|
||||
int ret_cudnn = checkResult(odim, cudnn_out, out) == 0 ? 0: ERROR_CUDNN;
|
||||
std::cout<<"TRT vs correct";
|
||||
int ret_tensorrt = checkResult(odim, rt_out, out) == 0 ? 0 : ERROR_TENSORRT;
|
||||
std::cout<<"CUDNN vs TRT ";
|
||||
int ret_cudnn_tensorrt = checkResult(odim, cudnn_out, rt_out) == 0 ? 0 : ERROR_CUDNNvsTENSORRT;
|
||||
|
||||
return ret_cudnn | ret_tensorrt | ret_cudnn_tensorrt;
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
import torch
|
||||
import urllib
|
||||
from PIL import Image
|
||||
from torchvision import transforms
|
||||
import numpy as np
|
||||
import struct
|
||||
import os
|
||||
|
||||
from pytorchcv.model_provider import get_model as ptcv_get_model
|
||||
from torch.autograd import Variable
|
||||
|
||||
from torchsummary import summary
|
||||
import torch.nn as nn
|
||||
|
||||
from torch.jit import trace
|
||||
|
||||
def create_folders():
|
||||
if not os.path.exists('debug'):
|
||||
os.makedirs('debug')
|
||||
if not os.path.exists('layers'):
|
||||
os.makedirs('layers')
|
||||
|
||||
def bin_write(f, data):
|
||||
data =data.flatten()
|
||||
fmt = 'f'*len(data)
|
||||
bin = struct.pack(fmt, *data)
|
||||
f.write(bin)
|
||||
|
||||
def hook(module, input, output):
|
||||
setattr(module, "_value_hook", output)
|
||||
|
||||
def load_ex_image(model):
|
||||
# Download an example image from the pytorch website
|
||||
url, filename = (
|
||||
"https://github.com/pytorch/hub/raw/master/dog.jpg", "dog.jpg")
|
||||
try:
|
||||
urllib.URLopener().retrieve(url, filename)
|
||||
except:
|
||||
urllib.request.urlretrieve(url, filename)
|
||||
|
||||
# sample execution (requires torchvision)
|
||||
input_image = Image.open(filename)
|
||||
print("input_image: ",input_image.size)
|
||||
preprocess = transforms.Compose([
|
||||
transforms.Resize(256),
|
||||
transforms.CenterCrop(224),
|
||||
transforms.ToTensor(),
|
||||
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[
|
||||
0.229, 0.224, 0.225]),
|
||||
])
|
||||
input_tensor = preprocess(input_image)
|
||||
print("input_tensor: ",input_tensor.shape)
|
||||
# create a mini-batch as expected by the model
|
||||
input_batch = input_tensor.unsqueeze(0)
|
||||
|
||||
# move the input and model to GPU for speed if available
|
||||
if torch.cuda.is_available():
|
||||
input_batch = input_batch.to('cuda')
|
||||
model.to('cuda')
|
||||
|
||||
return model, input_batch
|
||||
|
||||
def exp_input(model, input_batch):
|
||||
# Export the input batch
|
||||
model(input_batch)
|
||||
i = input_batch.cpu().data.numpy()
|
||||
i = np.array(i, dtype=np.float32)
|
||||
i.tofile("debug/input.bin", format="f")
|
||||
print("input: ", i.shape)
|
||||
|
||||
def print_wb_output(model):
|
||||
f = None
|
||||
for n, m in model.named_modules():
|
||||
m.eval()
|
||||
if 'DLAResBlock' in str(m.type):
|
||||
continue
|
||||
|
||||
in_output = m._value_hook
|
||||
o = in_output.data.numpy()
|
||||
o = np.array(o, dtype=np.float32)
|
||||
|
||||
t = '-'.join(n.split('.'))
|
||||
o.tofile("debug/" + t + ".bin", format="f")
|
||||
print('------- ', n, ' ------')
|
||||
print("debug ",o.shape)
|
||||
|
||||
if not(' of Conv2d' in str(m.type) or ' of Linear' in str(m.type) or ' of BatchNorm2d' in str(m.type)):
|
||||
continue
|
||||
|
||||
if ' of Conv2d' in str(m.type) or ' of Linear' in str(m.type):
|
||||
file_name = "layers/" + t + ".bin"
|
||||
print("open file: ", file_name)
|
||||
f = open(file_name, mode='wb')
|
||||
|
||||
w = np.array([])
|
||||
b = np.array([])
|
||||
if 'weight' in m._parameters and m._parameters['weight'] is not None:
|
||||
w = m._parameters['weight'].data.numpy()
|
||||
w = np.array(w, dtype=np.float32)
|
||||
print (" weights shape:", np.shape(w))
|
||||
|
||||
if 'bias' in m._parameters and m._parameters['bias'] is not None:
|
||||
b = m._parameters['bias'].data.numpy()
|
||||
b = np.array(b, dtype=np.float32)
|
||||
print (" bias shape:", np.shape(b))
|
||||
|
||||
if 'BatchNorm2d' in str(m.type):
|
||||
b = m._parameters['bias'].data.numpy()
|
||||
b = np.array(b, dtype=np.float32)
|
||||
s = m._parameters['weight'].data.numpy()
|
||||
s = np.array(s, dtype=np.float32)
|
||||
rm = m.running_mean.data.numpy()
|
||||
rm = np.array(rm, dtype=np.float32)
|
||||
rv = m.running_var.data.numpy()
|
||||
rv = np.array(rv, dtype=np.float32)
|
||||
bin_write(f,b)
|
||||
bin_write(f,s)
|
||||
bin_write(f,rm)
|
||||
bin_write(f,rv)
|
||||
print (" b shape:", np.shape(b))
|
||||
print (" s shape:", np.shape(s))
|
||||
print (" rm shape:", np.shape(rm))
|
||||
print (" rv shape:", np.shape(rv))
|
||||
|
||||
else:
|
||||
bin_write(f,w)
|
||||
if b.size > 0 and b is not None:
|
||||
bin_write(f,b)
|
||||
|
||||
if ' of BatchNorm2d' in str(m.type) or ' of Linear' in str(m.type):
|
||||
f.close()
|
||||
print("close file")
|
||||
f = None
|
||||
|
||||
if __name__ == '__main__':
|
||||
model = ptcv_get_model("dla34", pretrained=True)
|
||||
model.eval()
|
||||
|
||||
# load an example image and load it on model
|
||||
model, input_batch = load_ex_image(model)
|
||||
model.eval()
|
||||
with torch.no_grad():
|
||||
output = model(input_batch)
|
||||
|
||||
# create folders debug and layers if do not exist
|
||||
create_folders()
|
||||
|
||||
# add output attribute to the layers
|
||||
for n, m in model.named_modules():
|
||||
m.register_forward_hook(hook)
|
||||
|
||||
# export input bin
|
||||
exp_input(model, input_batch)
|
||||
|
||||
print_wb_output(model)
|
||||
|
||||
with open("dla34.txt", 'w') as f:
|
||||
for item in list(model.children()):
|
||||
f.write("%s\n" % item)
|
||||
|
||||
summary(model, (3, 224, 224))
|
||||
# print(trace(model, input_batch))
|
||||
@@ -0,0 +1,60 @@
|
||||
name: dla34
|
||||
channels:
|
||||
- defaults
|
||||
dependencies:
|
||||
- _libgcc_mutex=0.1=main
|
||||
- _pytorch_select=0.2=gpu_0
|
||||
- blas=1.0=mkl
|
||||
- ca-certificates=2019.10.16=0
|
||||
- certifi=2019.9.11=py36_0
|
||||
- cffi=1.13.1=py36h2e261b9_0
|
||||
- cudatoolkit=10.0.130=0
|
||||
- cudnn=7.6.0=cuda10.0_0
|
||||
- freetype=2.9.1=h8a8886c_1
|
||||
- intel-openmp=2019.4=243
|
||||
- jpeg=9b=h024ee3a_2
|
||||
- libedit=3.1.20181209=hc058e9b_0
|
||||
- libffi=3.2.1=hd88cf55_4
|
||||
- libgcc-ng=9.1.0=hdf63c60_0
|
||||
- libgfortran-ng=7.3.0=hdf63c60_0
|
||||
- libpng=1.6.37=hbc83047_0
|
||||
- libstdcxx-ng=9.1.0=hdf63c60_0
|
||||
- libtiff=4.0.10=h2733197_2
|
||||
- mkl=2019.4=243
|
||||
- mkl-service=2.3.0=py36he904b0f_0
|
||||
- mkl_fft=1.0.14=py36ha843d7b_0
|
||||
- mkl_random=1.1.0=py36hd6b4f25_0
|
||||
- ncurses=6.1=he6710b0_1
|
||||
- ninja=1.9.0=py36hfd86e86_0
|
||||
- numpy=1.17.2=py36haad9e8e_0
|
||||
- numpy-base=1.17.2=py36hde5b4d6_0
|
||||
- olefile=0.46=py36_0
|
||||
- openssl=1.1.1d=h7b6447c_3
|
||||
- pillow=6.2.0=py36h34e0f95_0
|
||||
- pip=19.3.1=py36_0
|
||||
- pycparser=2.19=py36_0
|
||||
- python=3.6.9=h265db76_0
|
||||
- readline=7.0=h7b6447c_5
|
||||
- setuptools=41.6.0=py36_0
|
||||
- six=1.12.0=py36_0
|
||||
- sqlite=3.30.1=h7b6447c_0
|
||||
- tk=8.6.8=hbc83047_0
|
||||
- wheel=0.33.6=py36_0
|
||||
- xz=5.2.4=h14c3975_4
|
||||
- zlib=1.2.11=h7b6447c_3
|
||||
- zstd=1.3.7=h0b5b093_0
|
||||
- pip:
|
||||
- chardet==3.0.4
|
||||
- decorator==4.4.1
|
||||
- idna==2.8
|
||||
- lxml==4.4.2
|
||||
- networkx==2.4
|
||||
- nltk==3.4.5
|
||||
- pytorchcv==0.0.55
|
||||
- requests==2.22.0
|
||||
- summary==0.2.0
|
||||
- torch==1.3.0
|
||||
- torchsummary==1.5.1
|
||||
- torchvision==0.4.1
|
||||
- urllib3==1.25.8
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
name: resnet101
|
||||
channels:
|
||||
- defaults
|
||||
dependencies:
|
||||
- _libgcc_mutex=0.1=main
|
||||
- _pytorch_select=0.2=gpu_0
|
||||
- blas=1.0=mkl
|
||||
- ca-certificates=2019.10.16=0
|
||||
- certifi=2019.9.11=py36_0
|
||||
- cffi=1.13.1=py36h2e261b9_0
|
||||
- cudatoolkit=10.0.130=0
|
||||
- cudnn=7.6.0=cuda10.0_0
|
||||
- freetype=2.9.1=h8a8886c_1
|
||||
- intel-openmp=2019.4=243
|
||||
- jpeg=9b=h024ee3a_2
|
||||
- libedit=3.1.20181209=hc058e9b_0
|
||||
- libffi=3.2.1=hd88cf55_4
|
||||
- libgcc-ng=9.1.0=hdf63c60_0
|
||||
- libgfortran-ng=7.3.0=hdf63c60_0
|
||||
- libpng=1.6.37=hbc83047_0
|
||||
- libstdcxx-ng=9.1.0=hdf63c60_0
|
||||
- libtiff=4.0.10=h2733197_2
|
||||
- mkl=2019.4=243
|
||||
- mkl-service=2.3.0=py36he904b0f_0
|
||||
- mkl_fft=1.0.14=py36ha843d7b_0
|
||||
- mkl_random=1.1.0=py36hd6b4f25_0
|
||||
- ncurses=6.1=he6710b0_1
|
||||
- ninja=1.9.0=py36hfd86e86_0
|
||||
- numpy=1.17.2=py36haad9e8e_0
|
||||
- numpy-base=1.17.2=py36hde5b4d6_0
|
||||
- olefile=0.46=py36_0
|
||||
- openssl=1.1.1d=h7b6447c_3
|
||||
- pillow=6.2.0=py36h34e0f95_0
|
||||
- pip=19.3.1=py36_0
|
||||
- pycparser=2.19=py36_0
|
||||
- python=3.6.9=h265db76_0
|
||||
- pytorch=1.2.0=cuda100py36h938c94c_0
|
||||
- readline=7.0=h7b6447c_5
|
||||
- setuptools=41.6.0=py36_0
|
||||
- six=1.12.0=py36_0
|
||||
- sqlite=3.30.1=h7b6447c_0
|
||||
- tk=8.6.8=hbc83047_0
|
||||
- wheel=0.33.6=py36_0
|
||||
- xz=5.2.4=h14c3975_4
|
||||
- zlib=1.2.11=h7b6447c_3
|
||||
- zstd=1.3.7=h0b5b093_0
|
||||
- pip:
|
||||
- chardet==3.0.4
|
||||
- idna==2.8
|
||||
- pytorchcv==0.0.55
|
||||
- requests==2.22.0
|
||||
- torch==1.3.0
|
||||
- torchsummary==1.5.1
|
||||
- torchvision==0.4.1
|
||||
- urllib3==1.25.8
|
||||
|
||||
@@ -0,0 +1,338 @@
|
||||
#include <iostream>
|
||||
#include "tkdnn.h"
|
||||
|
||||
const char *input_bin = "resnet101/debug/input.bin";
|
||||
const char *conv1_bin = "resnet101/layers/conv1.bin";
|
||||
|
||||
//layer1
|
||||
const char *layer1_bin[]={
|
||||
"resnet101/layers/layer1-0-conv1.bin",
|
||||
"resnet101/layers/layer1-0-conv2.bin",
|
||||
"resnet101/layers/layer1-0-conv3.bin",
|
||||
"resnet101/layers/layer1-0-downsample-0.bin",
|
||||
|
||||
"resnet101/layers/layer1-1-conv1.bin",
|
||||
"resnet101/layers/layer1-1-conv2.bin",
|
||||
"resnet101/layers/layer1-1-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer1-2-conv1.bin",
|
||||
"resnet101/layers/layer1-2-conv2.bin",
|
||||
"resnet101/layers/layer1-2-conv3.bin"};
|
||||
|
||||
|
||||
//layer2
|
||||
const char *layer2_bin[]={
|
||||
"resnet101/layers/layer2-0-conv1.bin",
|
||||
"resnet101/layers/layer2-0-conv2.bin",
|
||||
"resnet101/layers/layer2-0-conv3.bin",
|
||||
"resnet101/layers/layer2-0-downsample-0.bin",
|
||||
|
||||
"resnet101/layers/layer2-1-conv1.bin",
|
||||
"resnet101/layers/layer2-1-conv2.bin",
|
||||
"resnet101/layers/layer2-1-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer2-2-conv1.bin",
|
||||
"resnet101/layers/layer2-2-conv2.bin",
|
||||
"resnet101/layers/layer2-2-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer2-3-conv1.bin",
|
||||
"resnet101/layers/layer2-3-conv2.bin",
|
||||
"resnet101/layers/layer2-3-conv3.bin"
|
||||
};
|
||||
//layer3
|
||||
const char *layer3_bin[]={
|
||||
"resnet101/layers/layer3-0-conv1.bin",
|
||||
"resnet101/layers/layer3-0-conv2.bin",
|
||||
"resnet101/layers/layer3-0-conv3.bin",
|
||||
"resnet101/layers/layer3-0-downsample-0.bin",
|
||||
|
||||
"resnet101/layers/layer3-1-conv1.bin",
|
||||
"resnet101/layers/layer3-1-conv2.bin",
|
||||
"resnet101/layers/layer3-1-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-2-conv1.bin",
|
||||
"resnet101/layers/layer3-2-conv2.bin",
|
||||
"resnet101/layers/layer3-2-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-3-conv1.bin",
|
||||
"resnet101/layers/layer3-3-conv2.bin",
|
||||
"resnet101/layers/layer3-3-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-4-conv1.bin",
|
||||
"resnet101/layers/layer3-4-conv2.bin",
|
||||
"resnet101/layers/layer3-4-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-5-conv1.bin",
|
||||
"resnet101/layers/layer3-5-conv2.bin",
|
||||
"resnet101/layers/layer3-5-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-6-conv1.bin",
|
||||
"resnet101/layers/layer3-6-conv2.bin",
|
||||
"resnet101/layers/layer3-6-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-7-conv1.bin",
|
||||
"resnet101/layers/layer3-7-conv2.bin",
|
||||
"resnet101/layers/layer3-7-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-8-conv1.bin",
|
||||
"resnet101/layers/layer3-8-conv2.bin",
|
||||
"resnet101/layers/layer3-8-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-9-conv1.bin",
|
||||
"resnet101/layers/layer3-9-conv2.bin",
|
||||
"resnet101/layers/layer3-9-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-10-conv1.bin",
|
||||
"resnet101/layers/layer3-10-conv2.bin",
|
||||
"resnet101/layers/layer3-10-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-11-conv1.bin",
|
||||
"resnet101/layers/layer3-11-conv2.bin",
|
||||
"resnet101/layers/layer3-11-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-12-conv1.bin",
|
||||
"resnet101/layers/layer3-12-conv2.bin",
|
||||
"resnet101/layers/layer3-12-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-13-conv1.bin",
|
||||
"resnet101/layers/layer3-13-conv2.bin",
|
||||
"resnet101/layers/layer3-13-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-14-conv1.bin",
|
||||
"resnet101/layers/layer3-14-conv2.bin",
|
||||
"resnet101/layers/layer3-14-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-15-conv1.bin",
|
||||
"resnet101/layers/layer3-15-conv2.bin",
|
||||
"resnet101/layers/layer3-15-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-16-conv1.bin",
|
||||
"resnet101/layers/layer3-16-conv2.bin",
|
||||
"resnet101/layers/layer3-16-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-17-conv1.bin",
|
||||
"resnet101/layers/layer3-17-conv2.bin",
|
||||
"resnet101/layers/layer3-17-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-18-conv1.bin",
|
||||
"resnet101/layers/layer3-18-conv2.bin",
|
||||
"resnet101/layers/layer3-18-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-19-conv1.bin",
|
||||
"resnet101/layers/layer3-19-conv2.bin",
|
||||
"resnet101/layers/layer3-19-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-20-conv1.bin",
|
||||
"resnet101/layers/layer3-20-conv2.bin",
|
||||
"resnet101/layers/layer3-20-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-21-conv1.bin",
|
||||
"resnet101/layers/layer3-21-conv2.bin",
|
||||
"resnet101/layers/layer3-21-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer3-22-conv1.bin",
|
||||
"resnet101/layers/layer3-22-conv2.bin",
|
||||
"resnet101/layers/layer3-22-conv3.bin"};
|
||||
|
||||
|
||||
//layer4
|
||||
const char *layer4_bin[]={
|
||||
"resnet101/layers/layer4-0-conv1.bin",
|
||||
"resnet101/layers/layer4-0-conv2.bin",
|
||||
"resnet101/layers/layer4-0-conv3.bin",
|
||||
"resnet101/layers/layer4-0-downsample-0.bin",
|
||||
|
||||
"resnet101/layers/layer4-1-conv1.bin",
|
||||
"resnet101/layers/layer4-1-conv2.bin",
|
||||
"resnet101/layers/layer4-1-conv3.bin",
|
||||
|
||||
"resnet101/layers/layer4-2-conv1.bin",
|
||||
"resnet101/layers/layer4-2-conv2.bin",
|
||||
"resnet101/layers/layer4-2-conv3.bin"};
|
||||
|
||||
//final
|
||||
const char *fc_bin = "resnet101/layers/fc.bin";
|
||||
|
||||
const char *output_bin = "resnet101/debug/fc.bin";
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
// Network layout
|
||||
tk::dnn::dataDim_t dim(1, 3, 224, 224, 1);
|
||||
tk::dnn::Network net(dim);
|
||||
|
||||
tk::dnn::Conv2d conv1(&net, 64, 7, 7, 2, 2, 3, 3, conv1_bin, true);
|
||||
tk::dnn::Activation relu3(&net, CUDNN_ACTIVATION_RELU);
|
||||
tk::dnn::Pooling maxpool4(&net, 3, 3, 2, 2, 1, 1, tk::dnn::POOLING_MAX);
|
||||
|
||||
//layer 1
|
||||
int id_layer1_bin = 0;
|
||||
tk::dnn::Layer *last = &maxpool4;
|
||||
for(int i=0; i<3;i++)
|
||||
{
|
||||
tk::dnn::Conv2d *layer1_0_conv1 = new tk::dnn::Conv2d(&net, 64, 1, 1, 1, 1, 0, 0, layer1_bin[id_layer1_bin++], true);
|
||||
tk::dnn::Activation *relu1_0_1 = new tk::dnn::Activation(&net, CUDNN_ACTIVATION_RELU);
|
||||
tk::dnn::Conv2d *layer1_0_conv2 = new tk::dnn::Conv2d(&net, 64, 3, 3, 1, 1, 1, 1, layer1_bin[id_layer1_bin++], true);
|
||||
tk::dnn::Activation *relu1_0_2 = new tk::dnn::Activation(&net, CUDNN_ACTIVATION_RELU);
|
||||
tk::dnn::Conv2d *layer1_0_conv3 = new tk::dnn::Conv2d(&net, 256, 1, 1, 1, 1, 0, 0, layer1_bin[id_layer1_bin++], true);
|
||||
if(i==0) {
|
||||
tk::dnn::Layer *route_1_0_layers[1] = { last };
|
||||
tk::dnn::Route *route_1_0 = new tk::dnn::Route(&net, route_1_0_layers, 1);
|
||||
tk::dnn::Conv2d *layer1_0_downsample_0 = new tk::dnn::Conv2d(&net, 256, 1, 1, 1, 1, 0, 0, layer1_bin[id_layer1_bin++], true);
|
||||
tk::dnn::Shortcut *s1_0 = new tk::dnn::Shortcut(&net, layer1_0_conv3);
|
||||
} else {
|
||||
tk::dnn::Shortcut *s1_0 = new tk::dnn::Shortcut(&net, last);
|
||||
}
|
||||
tk::dnn::Activation *layer1_0_relu = new tk::dnn::Activation(&net, CUDNN_ACTIVATION_RELU);
|
||||
last = layer1_0_relu;
|
||||
}
|
||||
|
||||
// tk::dnn::Activation *last_activation = (tk::dnn::Activation *) net.layers[net.num_layers-1];
|
||||
// layer 2
|
||||
int id_layer2_bin = 0;
|
||||
for(int i=0; i<4;i++)
|
||||
{
|
||||
tk::dnn::Conv2d *layer1_0_conv1 = new tk::dnn::Conv2d(&net, 128, 1, 1, 1, 1, 0, 0, layer2_bin[id_layer2_bin++], true);
|
||||
tk::dnn::Activation *relu1_0_1 = new tk::dnn::Activation(&net, CUDNN_ACTIVATION_RELU);
|
||||
tk::dnn::Conv2d *layer1_0_conv2;
|
||||
if(i==0)
|
||||
layer1_0_conv2 = new tk::dnn::Conv2d(&net, 128, 3, 3, 2, 2, 1, 1, layer2_bin[id_layer2_bin++], true);
|
||||
else
|
||||
layer1_0_conv2 = new tk::dnn::Conv2d(&net, 128, 3, 3, 1, 1, 1, 1, layer2_bin[id_layer2_bin++], true);
|
||||
|
||||
tk::dnn::Activation *relu1_0_2 = new tk::dnn::Activation(&net, CUDNN_ACTIVATION_RELU);
|
||||
tk::dnn::Conv2d *layer1_0_conv3 = new tk::dnn::Conv2d(&net, 512, 1, 1, 1, 1, 0, 0, layer2_bin[id_layer2_bin++], true);
|
||||
if(i==0)
|
||||
{
|
||||
tk::dnn::Layer *route_1_0_layers[1] = { last };
|
||||
tk::dnn::Route *route_1_0 = new tk::dnn::Route(&net, route_1_0_layers, 1);
|
||||
tk::dnn::Conv2d *layer1_0_downsample_0 = new tk::dnn::Conv2d(&net, 512, 1, 1, 2, 2, 0, 0, layer2_bin[id_layer2_bin++], true);
|
||||
tk::dnn::Shortcut *s1_0 = new tk::dnn::Shortcut(&net, layer1_0_conv3);
|
||||
}
|
||||
else
|
||||
{
|
||||
tk::dnn::Shortcut *s1_0 = new tk::dnn::Shortcut(&net, last);
|
||||
}
|
||||
tk::dnn::Activation *layer1_0_relu = new tk::dnn::Activation(&net, CUDNN_ACTIVATION_RELU);
|
||||
last = layer1_0_relu;
|
||||
}
|
||||
|
||||
// layer 3
|
||||
int id_layer3_bin = 0;
|
||||
for(int i=0; i<23;i++)
|
||||
{
|
||||
tk::dnn::Conv2d *layer1_0_conv1 = new tk::dnn::Conv2d(&net, 256, 1, 1, 1, 1, 0, 0, layer3_bin[id_layer3_bin++], true);
|
||||
tk::dnn::Activation *relu1_0_1 = new tk::dnn::Activation(&net, CUDNN_ACTIVATION_RELU);
|
||||
tk::dnn::Conv2d *layer1_0_conv2;
|
||||
if(i==0)
|
||||
layer1_0_conv2 = new tk::dnn::Conv2d(&net, 256, 3, 3, 2, 2, 1, 1, layer3_bin[id_layer3_bin++], true);
|
||||
else
|
||||
layer1_0_conv2 = new tk::dnn::Conv2d(&net, 256, 3, 3, 1, 1, 1, 1, layer3_bin[id_layer3_bin++], true);
|
||||
|
||||
tk::dnn::Activation *relu1_0_2 = new tk::dnn::Activation(&net, CUDNN_ACTIVATION_RELU);
|
||||
tk::dnn::Conv2d *layer1_0_conv3 = new tk::dnn::Conv2d(&net, 1024, 1, 1, 1, 1, 0, 0, layer3_bin[id_layer3_bin++], true);
|
||||
if(i==0)
|
||||
{
|
||||
tk::dnn::Layer *route_1_0_layers[1] = { last };
|
||||
tk::dnn::Route *route_1_0 = new tk::dnn::Route(&net, route_1_0_layers, 1);
|
||||
tk::dnn::Conv2d *layer1_0_downsample_0 = new tk::dnn::Conv2d(&net, 1024, 1, 1, 2, 2, 0, 0, layer3_bin[id_layer3_bin++], true);
|
||||
tk::dnn::Shortcut *s1_0 = new tk::dnn::Shortcut(&net, layer1_0_conv3);
|
||||
}
|
||||
else
|
||||
{
|
||||
tk::dnn::Shortcut *s1_0 = new tk::dnn::Shortcut(&net, last);
|
||||
}
|
||||
tk::dnn::Activation *layer1_0_relu = new tk::dnn::Activation(&net, CUDNN_ACTIVATION_RELU);
|
||||
last = layer1_0_relu;
|
||||
}
|
||||
|
||||
// layer 4
|
||||
int id_layer4_bin = 0;
|
||||
for(int i=0; i<3;i++)
|
||||
{
|
||||
tk::dnn::Conv2d *layer1_0_conv1 = new tk::dnn::Conv2d(&net, 512, 1, 1, 1, 1, 0, 0, layer4_bin[id_layer4_bin++], true);
|
||||
tk::dnn::Activation *relu1_0_1 = new tk::dnn::Activation(&net, CUDNN_ACTIVATION_RELU);
|
||||
tk::dnn::Conv2d *layer1_0_conv2;
|
||||
if(i==0)
|
||||
layer1_0_conv2 = new tk::dnn::Conv2d(&net, 512, 3, 3, 2, 2, 1, 1, layer4_bin[id_layer4_bin++], true);
|
||||
else
|
||||
layer1_0_conv2 = new tk::dnn::Conv2d(&net, 512, 3, 3, 1, 1, 1, 1, layer4_bin[id_layer4_bin++], true);
|
||||
|
||||
tk::dnn::Activation *relu1_0_2 = new tk::dnn::Activation(&net, CUDNN_ACTIVATION_RELU);
|
||||
tk::dnn::Conv2d *layer1_0_conv3 = new tk::dnn::Conv2d(&net, 2048, 1, 1, 1, 1, 0, 0, layer4_bin[id_layer4_bin++], true);
|
||||
if(i==0)
|
||||
{
|
||||
tk::dnn::Layer *route_1_0_layers[1] = { last };
|
||||
tk::dnn::Route *route_1_0 = new tk::dnn::Route(&net, route_1_0_layers, 1);
|
||||
tk::dnn::Conv2d *layer1_0_downsample_0 = new tk::dnn::Conv2d(&net, 2048, 1, 1, 2, 2, 0, 0, layer4_bin[id_layer4_bin++], true);
|
||||
tk::dnn::Shortcut *s1_0 = new tk::dnn::Shortcut(&net, layer1_0_conv3);
|
||||
}
|
||||
else
|
||||
{
|
||||
tk::dnn::Shortcut *s1_0 = new tk::dnn::Shortcut(&net, last);
|
||||
}
|
||||
tk::dnn::Activation *layer1_0_relu = new tk::dnn::Activation(&net, CUDNN_ACTIVATION_RELU);
|
||||
last = layer1_0_relu;
|
||||
}
|
||||
|
||||
//final
|
||||
tk::dnn::Pooling avgpool(&net, 7, 7, 7, 7, 0, 0, tk::dnn::POOLING_AVERAGE);
|
||||
tk::dnn::Dense fc(&net, 1000, fc_bin);
|
||||
|
||||
// Load input
|
||||
dnnType *data;
|
||||
dnnType *input_h;
|
||||
readBinaryFile(input_bin, dim.tot(), &input_h, &data);
|
||||
//printDeviceVector(64, data, true);
|
||||
|
||||
//print network model
|
||||
net.print();
|
||||
|
||||
//convert network to tensorRT
|
||||
tk::dnn::NetworkRT netRT(&net, net.getNetworkRTName("resnet101"));
|
||||
|
||||
|
||||
tk::dnn::dataDim_t out_dim;
|
||||
out_dim = net.layers[net.num_layers-1]->output_dim;
|
||||
dnnType *cudnn_out, *rt_out;
|
||||
|
||||
tk::dnn::dataDim_t dim1 = dim; //input dim
|
||||
printCenteredTitle(" CUDNN inference ", '=', 30);
|
||||
{
|
||||
dim1.print();
|
||||
TIMER_START
|
||||
net.infer(dim1, data);
|
||||
TIMER_STOP
|
||||
dim1.print();
|
||||
}
|
||||
cudnn_out = net.layers[net.num_layers-1]->dstData;
|
||||
|
||||
//printDeviceVector(64, cudnn_out, true);
|
||||
|
||||
tk::dnn::dataDim_t dim2 = dim;
|
||||
printCenteredTitle(" TENSORRT inference ", '=', 30);
|
||||
{
|
||||
dim2.print();
|
||||
TIMER_START
|
||||
netRT.infer(dim2, data);
|
||||
TIMER_STOP
|
||||
dim2.print();
|
||||
}
|
||||
rt_out = (dnnType *)netRT.buffersRT[1];
|
||||
|
||||
|
||||
printCenteredTitle(std::string(" RESNET CHECK RESULTS ").c_str(), '=', 30);
|
||||
dnnType *out, *out_h;
|
||||
int odim = out_dim.tot();
|
||||
readBinaryFile(output_bin, odim, &out_h, &out);
|
||||
|
||||
std::cout<<"CUDNN vs correct";
|
||||
int ret_cudnn = checkResult(odim, cudnn_out, out) == 0 ? 0: ERROR_CUDNN;
|
||||
std::cout<<"TRT vs correct";
|
||||
int ret_tensorrt = checkResult(odim, rt_out, out) == 0 ? 0 : ERROR_TENSORRT;
|
||||
std::cout<<"CUDNN vs TRT ";
|
||||
int ret_cudnn_tensorrt = checkResult(odim, cudnn_out, rt_out) == 0 ? 0 : ERROR_CUDNNvsTENSORRT;
|
||||
|
||||
return ret_cudnn | ret_tensorrt | ret_cudnn_tensorrt;
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
import torch
|
||||
import urllib
|
||||
from PIL import Image
|
||||
from torchvision import transforms
|
||||
import numpy as np
|
||||
import struct
|
||||
import os
|
||||
|
||||
from pytorchcv.model_provider import get_model as ptcv_get_model
|
||||
from torch.autograd import Variable
|
||||
|
||||
from torchsummary import summary
|
||||
import torch.nn as nn
|
||||
|
||||
from torch.jit import trace
|
||||
|
||||
def create_folders():
|
||||
if not os.path.exists('debug'):
|
||||
os.makedirs('debug')
|
||||
if not os.path.exists('layers'):
|
||||
os.makedirs('layers')
|
||||
|
||||
def bin_write(f, data):
|
||||
data =data.flatten()
|
||||
fmt = 'f'*len(data)
|
||||
bin = struct.pack(fmt, *data)
|
||||
f.write(bin)
|
||||
|
||||
def hook(module, input, output):
|
||||
setattr(module, "_value_hook", output)
|
||||
|
||||
def load_ex_image(model):
|
||||
# Download an example image from the pytorch website
|
||||
url, filename = (
|
||||
"https://github.com/pytorch/hub/raw/master/dog.jpg", "dog.jpg")
|
||||
try:
|
||||
urllib.URLopener().retrieve(url, filename)
|
||||
except:
|
||||
urllib.request.urlretrieve(url, filename)
|
||||
|
||||
# sample execution (requires torchvision)
|
||||
input_image = Image.open(filename)
|
||||
print("input_image: ",input_image.size)
|
||||
preprocess = transforms.Compose([
|
||||
transforms.Resize(256),
|
||||
transforms.CenterCrop(224),
|
||||
transforms.ToTensor(),
|
||||
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[
|
||||
0.229, 0.224, 0.225]),
|
||||
])
|
||||
input_tensor = preprocess(input_image)
|
||||
print("input_tensor: ",input_tensor.shape)
|
||||
# create a mini-batch as expected by the model
|
||||
input_batch = input_tensor.unsqueeze(0)
|
||||
|
||||
# move the input and model to GPU for speed if available
|
||||
if torch.cuda.is_available():
|
||||
input_batch = input_batch.to('cuda')
|
||||
model.to('cuda')
|
||||
|
||||
return model, input_batch
|
||||
|
||||
def exp_input(model, input_batch):
|
||||
# Export the input batch
|
||||
model(input_batch)
|
||||
i = input_batch.cpu().data.numpy()
|
||||
i = np.array(i, dtype=np.float32)
|
||||
i.tofile("debug/input.bin", format="f")
|
||||
print("input: ", i.shape)
|
||||
|
||||
def print_wb_output(model):
|
||||
f = None
|
||||
for n, m in model.named_modules():
|
||||
in_output = m._value_hook
|
||||
o = in_output.data.numpy()
|
||||
o = np.array(o, dtype=np.float32)
|
||||
t = '-'.join(n.split('.'))
|
||||
o.tofile("debug/" + t + ".bin", format="f")
|
||||
print('------- ', n, ' ------')
|
||||
print("debug ",o.shape)
|
||||
|
||||
if not(' of Conv2d' in str(m.type) or ' of Linear' in str(m.type) or ' of BatchNorm2d' in str(m.type)):
|
||||
continue
|
||||
|
||||
if ' of Conv2d' in str(m.type) or ' of Linear' in str(m.type):
|
||||
file_name = "layers/" + t + ".bin"
|
||||
print("open file: ", file_name)
|
||||
f = open(file_name, mode='wb')
|
||||
|
||||
w = np.array([])
|
||||
b = np.array([])
|
||||
if 'weight' in m._parameters and m._parameters['weight'] is not None:
|
||||
w = m._parameters['weight'].data.numpy()
|
||||
w = np.array(w, dtype=np.float32)
|
||||
print (" weights shape:", np.shape(w))
|
||||
|
||||
if 'bias' in m._parameters and m._parameters['bias'] is not None:
|
||||
b = m._parameters['bias'].data.numpy()
|
||||
b = np.array(b, dtype=np.float32)
|
||||
print (" bias shape:", np.shape(b))
|
||||
|
||||
if 'BatchNorm2d' in str(m.type):
|
||||
b = m._parameters['bias'].data.numpy()
|
||||
b = np.array(b, dtype=np.float32)
|
||||
s = m._parameters['weight'].data.numpy()
|
||||
s = np.array(s, dtype=np.float32)
|
||||
rm = m.running_mean.data.numpy()
|
||||
rm = np.array(rm, dtype=np.float32)
|
||||
rv = m.running_var.data.numpy()
|
||||
rv = np.array(rv, dtype=np.float32)
|
||||
bin_write(f,b)
|
||||
bin_write(f,s)
|
||||
bin_write(f,rm)
|
||||
bin_write(f,rv)
|
||||
print (" b shape:", np.shape(b))
|
||||
print (" s shape:", np.shape(s))
|
||||
print (" rm shape:", np.shape(rm))
|
||||
print (" rv shape:", np.shape(rv))
|
||||
|
||||
else:
|
||||
bin_write(f,w)
|
||||
if b.size > 0:
|
||||
bin_write(f,b)
|
||||
|
||||
if ' of BatchNorm2d' in str(m.type) or ' of Linear' in str(m.type):
|
||||
f.close()
|
||||
print("close file")
|
||||
f = None
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
model = torch.hub.load('pytorch/vision', 'resnet101', pretrained=True)
|
||||
model.eval()
|
||||
|
||||
# load an example image and load it on model
|
||||
model, input_batch = load_ex_image(model)
|
||||
model.eval()
|
||||
with torch.no_grad():
|
||||
output = model(input_batch)
|
||||
|
||||
# create folders debug and layers if do not exist
|
||||
create_folders()
|
||||
|
||||
# add output attribute to the layers
|
||||
for n, m in model.named_modules():
|
||||
m.register_forward_hook(hook)
|
||||
|
||||
# export input bin
|
||||
exp_input(model, input_batch)
|
||||
|
||||
print_wb_output(model)
|
||||
|
||||
with open("resnet101.txt", 'w') as f:
|
||||
for item in list(model.children()):
|
||||
f.write("%s\n" % item)
|
||||
|
||||
summary(model, (3, 224, 224))
|
||||
# print(trace(model, input_batch))
|
||||
Reference in New Issue
Block a user