From e9ec582223a17c2d719e3a32f2515247555d183c Mon Sep 17 00:00:00 2001 From: Davide Sapienza Date: Tue, 29 Oct 2019 15:08:44 +0100 Subject: [PATCH] Shortcat ok --- CMakeLists.txt | 2 - src/LayerWgs.cpp | 20 +++++- src/utils.cpp | 3 +- tests/resnet101/resnet101.cpp | 24 ++++--- tests/resnet101/resnet101_weightsexporter.py | 67 +++++++++++++++----- 5 files changed, 87 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 27fe9cb..7c5a301 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,8 +92,6 @@ target_link_libraries(test_yolo3_berkeley tkDNN) add_executable(test_yolo3_flir tests/yolo3_flir/yolo3_flir.cpp) target_link_libraries(test_yolo3_flir tkDNN) -add_executable(test_yolo3_BCDS6 tests/yolo3_BCDS6/yolo3_BCDS6.cpp) -target_link_libraries(test_yolo3_BCDS6 tkDNN) add_executable(test_resnet101 tests/resnet101/resnet101.cpp) target_link_libraries(test_resnet101 tkDNN) diff --git a/src/LayerWgs.cpp b/src/LayerWgs.cpp index f27da0f..dfdde70 100644 --- a/src/LayerWgs.cpp +++ b/src/LayerWgs.cpp @@ -19,7 +19,14 @@ LayerWgs::LayerWgs(Network *net, int inputs, int outputs, readBinaryFile(weights_path.c_str(), inputs*outputs*kh*kw*kl, &data_h, &data_d, seek); seek += inputs*outputs*kh*kw*kl; readBinaryFile(weights_path.c_str(), outputs, &bias_h, &bias_d, seek); - + +std::cout<<"w:\n"; + printDeviceVector(64, data_d, true); + +std::cout<<"b:\n"; + printDeviceVector(64, bias_d, true); +std::cout<<"step----------------------------------------\n"; + this->batchnorm = batchnorm; if(batchnorm) { seek += outputs; @@ -29,6 +36,17 @@ LayerWgs::LayerWgs(Network *net, int inputs, int outputs, seek += outputs; readBinaryFile(weights_path.c_str(), outputs, &variance_h, &variance_d, seek); +std::cout<<"s:\n"; + printDeviceVector(64, scales_d, true); + +std::cout<<"m:\n"; + printDeviceVector(64, mean_d, true); + +std::cout<<"v:\n"; + printDeviceVector(64, variance_d, true); + + std::cout<<"END----------------------------------------\n"; + float eps = CUDNN_BN_MIN_EPSILON; power_h = new dnnType[outputs]; diff --git a/src/utils.cpp b/src/utils.cpp index e6f71f8..c8f90d8 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -54,6 +54,7 @@ void printDeviceVector(int size, dnnType* vec_d, bool device) vec = new dnnType[size]; cudaDeviceSynchronize(); cudaMemcpy(vec, vec_d, size*sizeof(dnnType), cudaMemcpyDeviceToHost); + cudaDeviceSynchronize(); } else { vec = vec_d; } @@ -78,7 +79,7 @@ int checkResult(int size, dnnType *data_d, dnnType *correct_d, bool device) { cudaDeviceSynchronize(); cudaMemcpy(data_h, data_d, size*sizeof(dnnType), cudaMemcpyDeviceToHost); cudaMemcpy(correct_h, correct_d, size*sizeof(dnnType), cudaMemcpyDeviceToHost); - + cudaDeviceSynchronize(); } else { data_h = data_d; correct_h = correct_d; diff --git a/tests/resnet101/resnet101.cpp b/tests/resnet101/resnet101.cpp index da931d5..26fb3dd 100644 --- a/tests/resnet101/resnet101.cpp +++ b/tests/resnet101/resnet101.cpp @@ -148,7 +148,7 @@ const char *layer4_2_conv3_bin = "../tests/resnet101/layers/layer4-2-conv3.bin"; //final const char *fc_bin = "../tests/resnet101/layers/fc.bin"; -const char *output_bin = "../tests/resnet101/debug/layer1-0-conv3.bin"; +const char *output_bin = "../tests/resnet101/debug/layer1-0-relu.bin"; int main() { @@ -161,16 +161,21 @@ int main() 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 tk::dnn::Conv2d layer1_0_conv1(&net, 64, 1, 1, 1, 1, 0, 0, layer1_0_conv1_bin, true); + tk::dnn::Activation relu1_0_1(&net, CUDNN_ACTIVATION_RELU); tk::dnn::Conv2d layer1_0_conv2(&net, 64, 3, 3, 1, 1, 1, 1, layer1_0_conv2_bin, true); + tk::dnn::Activation relu1_0_2(&net, CUDNN_ACTIVATION_RELU); tk::dnn::Conv2d layer1_0_conv3(&net, 256, 1, 1, 1, 1, 0, 0, layer1_0_conv3_bin, true); - -/* + + tk::dnn::Layer *m83_layers[1] = { &maxpool4 }; + tk::dnn::Route m83 (&net, m83_layers, 1); + tk::dnn::Conv2d layer1_0_downsample_0(&net, 256, 1, 1, 1, 1, 0, 0, layer1_0_downsample_0_bin, true); + + tk::dnn::Shortcut s1_0 (&net, &layer1_0_conv3); tk::dnn::Activation layer1_0_relu(&net, CUDNN_ACTIVATION_RELU); - tk::dnn::Conv2d layer1_0_downsample_0(&net, 256, 1, 1, 1, 1, 1, 1, layer1_0_downsample_0, true); - +/* + tk::dnn::Conv2d layer1_1_conv1(&net, 64, 1, 1, 1, 1, 1, 1, layer1_1_conv1_bin, true); tk::dnn::Conv2d layer1_1_conv2(&net, 64, 3, 3, 1, 1, 1, 1, layer1_1_conv2_bin, true); tk::dnn::Conv2d layer1_1_conv3(&net, 256, 1, 1, 1, 1, 1, 1, layer1_1_conv3_bin, true); @@ -347,6 +352,7 @@ int main() dnnType *data; dnnType *input_h; readBinaryFile(input_bin, dim.tot(), &input_h, &data); + printDeviceVector(64, data, true); //print network model net.print(); @@ -356,7 +362,7 @@ int main() */ tk::dnn::dataDim_t out_dim; - out_dim = layer1_0_conv3.output_dim; + out_dim = net.layers[net.num_layers-1]->output_dim; dnnType *cudnn_out, *rt_out; tk::dnn::dataDim_t dim1 = dim; //input dim @@ -368,7 +374,9 @@ int main() TIMER_STOP dim1.print(); } - cudnn_out = layer1_0_conv3.dstData; + cudnn_out = net.layers[net.num_layers-1]->dstData; + + printDeviceVector(64, cudnn_out, true); /* tk::dnn::dataDim_t dim2 = dim; printCenteredTitle(" TENSORRT inference ", '=', 30); diff --git a/tests/resnet101/resnet101_weightsexporter.py b/tests/resnet101/resnet101_weightsexporter.py index fd5a81d..05f3ba2 100644 --- a/tests/resnet101/resnet101_weightsexporter.py +++ b/tests/resnet101/resnet101_weightsexporter.py @@ -4,6 +4,14 @@ from PIL import Image from torchvision import transforms from torchsummary import summary import numpy as np +import struct + +def bin_write(f, data): + data =data.flatten() + # print(data) + fmt = 'f'*len(data) + bin = struct.pack(fmt, *data) + f.write(bin) def hook(module, input, output): setattr(module, "_value_hook", output) @@ -12,7 +20,7 @@ def hook(module, input, output): def print_wb(model, folder): for name, param in model.named_parameters(): - print ("Layer", name) + # print ("Layer", name) t = name.split('.')[0:-1] arg = name.split('.')[-1] t = '-'.join(t) @@ -43,20 +51,22 @@ def print_wb_output(model, input_batch): f = None for n, m in model.named_modules(): in_output = m._value_hook - print(n, ' ----------------------------------------------------------------') o = in_output.data.numpy() o = np.array(o, dtype=np.float32) t = '-'.join(n.split('.')) o.tofile("debug/" + t + ".bin", format="f") - if 'Conv2d' in str(m.type) or 'Linear' in str(m.type): - f = open("layers/" + t + ".bin", mode='wb') - - if f is None: + 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') + print(n, ' ----------------------------------------------------------------') # print(m._parameters) - print(m.type) + #print(m.type) w = np.array([]) b = np.array([]) @@ -71,20 +81,43 @@ def print_wb_output(model, input_batch): b = m._parameters['bias'].data.numpy() b = np.array(b, dtype=np.float32) print (" bias shape:", np.shape(b)) - else: - b = np.zeros(w.shape[0], dtype=np.float32) - print (" bias shape:", np.shape(b)) + # else: + # b = np.zeros(w.shape[0], dtype=np.float32) + # print (" bias shape:", np.shape(b)) if 'BatchNorm2d' in str(m.type): - s = np.zeros(w.shape[0], dtype=np.float32)+1 - s.tofile(f, format="f") + 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) + #s.tofile(f, format="f") + bin_write(f,b) + bin_write(f,s) + bin_write(f,rm) + bin_write(f,rv) + print (" s shape:", np.shape(s)) + print (" rm shape:", np.shape(rm)) + print (" rv shape:", np.shape(rv)) - w.tofile(f, format="f") - b.tofile(f, format="f") + else: + + # w.tofile(f, format="f") + bin_write(f,w) + # print("w- ",w) + if b.size > 0: + # b.tofile(f, format="f") + bin_write(f,b) + # print("b - ",b) - if 'BatchNorm2d' in str(m.type) or 'Linear' in str(m.type): + if ' of BatchNorm2d' in str(m.type) or ' of Linear' in str(m.type): f.close() + print("close file") f = None + # return @@ -120,9 +153,9 @@ if __name__ == '__main__': output = model(input_batch) # Tensor of shape 1000, with confidence scores over Imagenet's 1000 classes - print(output) + # print(output) print_wb_output(model, input_batch) - print(list(model.children())) + # print(list(model.children()))