Shortcat ok

This commit is contained in:
Davide Sapienza
2019-10-29 15:08:44 +01:00
parent 058f9b795f
commit e9ec582223
5 changed files with 87 additions and 29 deletions
-2
View File
@@ -92,8 +92,6 @@ target_link_libraries(test_yolo3_berkeley tkDNN)
add_executable(test_yolo3_flir tests/yolo3_flir/yolo3_flir.cpp) add_executable(test_yolo3_flir tests/yolo3_flir/yolo3_flir.cpp)
target_link_libraries(test_yolo3_flir tkDNN) 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) add_executable(test_resnet101 tests/resnet101/resnet101.cpp)
target_link_libraries(test_resnet101 tkDNN) target_link_libraries(test_resnet101 tkDNN)
+18
View File
@@ -20,6 +20,13 @@ LayerWgs::LayerWgs(Network *net, int inputs, int outputs,
seek += inputs*outputs*kh*kw*kl; seek += inputs*outputs*kh*kw*kl;
readBinaryFile(weights_path.c_str(), outputs, &bias_h, &bias_d, seek); 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; this->batchnorm = batchnorm;
if(batchnorm) { if(batchnorm) {
seek += outputs; seek += outputs;
@@ -29,6 +36,17 @@ LayerWgs::LayerWgs(Network *net, int inputs, int outputs,
seek += outputs; seek += outputs;
readBinaryFile(weights_path.c_str(), outputs, &variance_h, &variance_d, seek); 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; float eps = CUDNN_BN_MIN_EPSILON;
power_h = new dnnType[outputs]; power_h = new dnnType[outputs];
+2 -1
View File
@@ -54,6 +54,7 @@ void printDeviceVector(int size, dnnType* vec_d, bool device)
vec = new dnnType[size]; vec = new dnnType[size];
cudaDeviceSynchronize(); cudaDeviceSynchronize();
cudaMemcpy(vec, vec_d, size*sizeof(dnnType), cudaMemcpyDeviceToHost); cudaMemcpy(vec, vec_d, size*sizeof(dnnType), cudaMemcpyDeviceToHost);
cudaDeviceSynchronize();
} else { } else {
vec = vec_d; vec = vec_d;
} }
@@ -78,7 +79,7 @@ int checkResult(int size, dnnType *data_d, dnnType *correct_d, bool device) {
cudaDeviceSynchronize(); cudaDeviceSynchronize();
cudaMemcpy(data_h, data_d, size*sizeof(dnnType), cudaMemcpyDeviceToHost); cudaMemcpy(data_h, data_d, size*sizeof(dnnType), cudaMemcpyDeviceToHost);
cudaMemcpy(correct_h, correct_d, size*sizeof(dnnType), cudaMemcpyDeviceToHost); cudaMemcpy(correct_h, correct_d, size*sizeof(dnnType), cudaMemcpyDeviceToHost);
cudaDeviceSynchronize();
} else { } else {
data_h = data_d; data_h = data_d;
correct_h = correct_d; correct_h = correct_d;
+14 -6
View File
@@ -148,7 +148,7 @@ const char *layer4_2_conv3_bin = "../tests/resnet101/layers/layer4-2-conv3.bin";
//final //final
const char *fc_bin = "../tests/resnet101/layers/fc.bin"; 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() int main()
{ {
@@ -161,15 +161,20 @@ int main()
tk::dnn::Activation relu3(&net, CUDNN_ACTIVATION_RELU); tk::dnn::Activation relu3(&net, CUDNN_ACTIVATION_RELU);
tk::dnn::Pooling maxpool4(&net, 3, 3, 2, 2, 1, 1, tk::dnn::POOLING_MAX); tk::dnn::Pooling maxpool4(&net, 3, 3, 2, 2, 1, 1, tk::dnn::POOLING_MAX);
//layer 1 //layer 1
tk::dnn::Conv2d layer1_0_conv1(&net, 64, 1, 1, 1, 1, 0, 0, layer1_0_conv1_bin, true); 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::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::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::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_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_conv2(&net, 64, 3, 3, 1, 1, 1, 1, layer1_1_conv2_bin, true);
@@ -347,6 +352,7 @@ int main()
dnnType *data; dnnType *data;
dnnType *input_h; dnnType *input_h;
readBinaryFile(input_bin, dim.tot(), &input_h, &data); readBinaryFile(input_bin, dim.tot(), &input_h, &data);
printDeviceVector(64, data, true);
//print network model //print network model
net.print(); net.print();
@@ -356,7 +362,7 @@ int main()
*/ */
tk::dnn::dataDim_t out_dim; 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; dnnType *cudnn_out, *rt_out;
tk::dnn::dataDim_t dim1 = dim; //input dim tk::dnn::dataDim_t dim1 = dim; //input dim
@@ -368,7 +374,9 @@ int main()
TIMER_STOP TIMER_STOP
dim1.print(); 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; tk::dnn::dataDim_t dim2 = dim;
printCenteredTitle(" TENSORRT inference ", '=', 30); printCenteredTitle(" TENSORRT inference ", '=', 30);
+50 -17
View File
@@ -4,6 +4,14 @@ from PIL import Image
from torchvision import transforms from torchvision import transforms
from torchsummary import summary from torchsummary import summary
import numpy as np 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): def hook(module, input, output):
setattr(module, "_value_hook", output) setattr(module, "_value_hook", output)
@@ -12,7 +20,7 @@ def hook(module, input, output):
def print_wb(model, folder): def print_wb(model, folder):
for name, param in model.named_parameters(): for name, param in model.named_parameters():
print ("Layer", name) # print ("Layer", name)
t = name.split('.')[0:-1] t = name.split('.')[0:-1]
arg = name.split('.')[-1] arg = name.split('.')[-1]
t = '-'.join(t) t = '-'.join(t)
@@ -43,20 +51,22 @@ def print_wb_output(model, input_batch):
f = None f = None
for n, m in model.named_modules(): for n, m in model.named_modules():
in_output = m._value_hook in_output = m._value_hook
print(n, ' ----------------------------------------------------------------')
o = in_output.data.numpy() o = in_output.data.numpy()
o = np.array(o, dtype=np.float32) o = np.array(o, dtype=np.float32)
t = '-'.join(n.split('.')) t = '-'.join(n.split('.'))
o.tofile("debug/" + t + ".bin", format="f") o.tofile("debug/" + t + ".bin", format="f")
if 'Conv2d' in str(m.type) or 'Linear' in str(m.type): if not(' of Conv2d' in str(m.type) or ' of Linear' in str(m.type) or ' of BatchNorm2d' in str(m.type)):
f = open("layers/" + t + ".bin", mode='wb')
if f is None:
continue 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._parameters)
print(m.type) #print(m.type)
w = np.array([]) w = np.array([])
b = np.array([]) b = np.array([])
@@ -71,20 +81,43 @@ def print_wb_output(model, input_batch):
b = m._parameters['bias'].data.numpy() b = m._parameters['bias'].data.numpy()
b = np.array(b, dtype=np.float32) b = np.array(b, dtype=np.float32)
print (" bias shape:", np.shape(b)) print (" bias shape:", np.shape(b))
else: # else:
b = np.zeros(w.shape[0], dtype=np.float32) # b = np.zeros(w.shape[0], dtype=np.float32)
print (" bias shape:", np.shape(b)) # print (" bias shape:", np.shape(b))
if 'BatchNorm2d' in str(m.type): if 'BatchNorm2d' in str(m.type):
s = np.zeros(w.shape[0], dtype=np.float32)+1 b = m._parameters['bias'].data.numpy()
s.tofile(f, format="f") 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") else:
b.tofile(f, format="f")
if 'BatchNorm2d' in str(m.type) or 'Linear' in str(m.type): # 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 ' of BatchNorm2d' in str(m.type) or ' of Linear' in str(m.type):
f.close() f.close()
print("close file")
f = None f = None
# return
@@ -120,9 +153,9 @@ if __name__ == '__main__':
output = model(input_batch) output = model(input_batch)
# Tensor of shape 1000, with confidence scores over Imagenet's 1000 classes # Tensor of shape 1000, with confidence scores over Imagenet's 1000 classes
print(output) # print(output)
print_wb_output(model, input_batch) print_wb_output(model, input_batch)
print(list(model.children())) # print(list(model.children()))