forked from wang-xinyu/tensorrtx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.hpp
More file actions
137 lines (115 loc) · 5.66 KB
/
common.hpp
File metadata and controls
137 lines (115 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifndef REAL_ESRGAN_COMMON_H_
#define REAL_ESRGAN_COMMON_H_
#include <fstream>
#include <map>
#include <sstream>
#include <vector>
#include <opencv2/opencv.hpp>
#include "NvInfer.h"
using namespace nvinfer1;
// TensorRT weight files have a simple space delimited format:
// [type] [size] <data x size in hex>
std::map<std::string, Weights> loadWeights(const std::string file) {
std::cout << "Loading weights: " << file << std::endl;
std::map<std::string, Weights> weightMap;
// Open weights file
std::ifstream input(file);
assert(input.is_open() && "Unable to load weight file. please check if the .wts file path is right!!!!!!");
// Read number of weight blobs
int32_t count;
input >> count;
assert(count > 0 && "Invalid weight map file.");
while (count--)
{
Weights wt{ DataType::kFLOAT, nullptr, 0 };
uint32_t size;
// Read name and type of blob
std::string name;
input >> name >> std::dec >> size;
wt.type = DataType::kFLOAT;
// Load blob
uint32_t* val = reinterpret_cast<uint32_t*>(malloc(sizeof(val) * size));
for (uint32_t x = 0, y = size; x < y; ++x)
{
input >> std::hex >> val[x];
}
wt.values = val;
wt.count = size;
weightMap[name] = wt;
}
return weightMap;
}
ITensor* residualDenseBlock(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, ITensor* x, std::string lname)
{
IConvolutionLayer* conv_1 = network->addConvolutionNd(*x, 32, DimsHW{ 3, 3 }, weightMap[lname + ".conv1.weight"], weightMap[lname + ".conv1.bias"]);
conv_1->setStrideNd(DimsHW{ 1, 1 });
conv_1->setPaddingNd(DimsHW{ 1, 1 });
IActivationLayer* leaky_relu_1 = network->addActivation(*conv_1->getOutput(0), ActivationType::kLEAKY_RELU);
leaky_relu_1->setAlpha(0.2);
ITensor* x1 = leaky_relu_1->getOutput(0);
ITensor* concat_input2[] = { x, x1 };
IConcatenationLayer* concat2 = network->addConcatenation(concat_input2, 2);
concat2->setAxis(0);
IConvolutionLayer* conv_2 = network->addConvolutionNd(*concat2->getOutput(0), 32, DimsHW{ 3, 3 }, weightMap[lname + ".conv2.weight"], weightMap[lname + ".conv2.bias"]);
conv_2->setStrideNd(DimsHW{ 1, 1 });
conv_2->setPaddingNd(DimsHW{ 1, 1 });
IActivationLayer* leaky_relu_2 = network->addActivation(*conv_2->getOutput(0), ActivationType::kLEAKY_RELU);
leaky_relu_2->setAlpha(0.2);
ITensor* x2 = leaky_relu_2->getOutput(0);
ITensor* concat_input3[] = { x, x1, x2 };
IConcatenationLayer* concat3 = network->addConcatenation(concat_input3, 3);
concat3->setAxis(0);
IConvolutionLayer* conv_3 = network->addConvolutionNd(*concat3->getOutput(0), 32, DimsHW{ 3, 3 }, weightMap[lname + ".conv3.weight"], weightMap[lname + ".conv3.bias"]);
conv_3->setStrideNd(DimsHW{ 1, 1 });
conv_3->setPaddingNd(DimsHW{ 1, 1 });
IActivationLayer* leaky_relu_3 = network->addActivation(*conv_3->getOutput(0), ActivationType::kLEAKY_RELU);
leaky_relu_3->setAlpha(0.2);
ITensor* x3 = leaky_relu_3->getOutput(0);
ITensor* concat_input4[] = { x, x1, x2, x3 };
IConcatenationLayer* concat4 = network->addConcatenation(concat_input4, 4);
concat4->setAxis(0);
IConvolutionLayer* conv_4 = network->addConvolutionNd(*concat4->getOutput(0), 32, DimsHW{ 3, 3 }, weightMap[lname + ".conv4.weight"], weightMap[lname + ".conv4.bias"]);
conv_4->setStrideNd(DimsHW{ 1, 1 });
conv_4->setPaddingNd(DimsHW{ 1, 1 });
IActivationLayer* leaky_relu_4 = network->addActivation(*conv_4->getOutput(0), ActivationType::kLEAKY_RELU);
leaky_relu_4->setAlpha(0.2);
ITensor* x4 = leaky_relu_4->getOutput(0);
ITensor* concat_input5[] = { x, x1, x2, x3, x4 };
IConcatenationLayer* concat5 = network->addConcatenation(concat_input5, 5);
concat5->setAxis(0);
IConvolutionLayer* conv_5 = network->addConvolutionNd(*concat5->getOutput(0), 64, DimsHW{ 3, 3 }, weightMap[lname + ".conv5.weight"], weightMap[lname + ".conv5.bias"]);
conv_5->setStrideNd(DimsHW{ 1, 1 });
conv_5->setPaddingNd(DimsHW{ 1, 1 });
ITensor* x5 = conv_5->getOutput(0);
float *scval = reinterpret_cast<float*>(malloc(sizeof(float)));
*scval = 0.2;
Weights scale{ DataType::kFLOAT, scval, 1 };
float *shval = reinterpret_cast<float*>(malloc(sizeof(float)));
*shval = 0.0;
Weights shift{ DataType::kFLOAT, shval, 1 };
float *pval = reinterpret_cast<float*>(malloc(sizeof(float)));
*pval = 1.0;
Weights power{ DataType::kFLOAT, pval, 1 };
IScaleLayer* scaled = network->addScale(*x5, ScaleMode::kUNIFORM, shift, scale, power);
IElementWiseLayer* ew1 = network->addElementWise(*scaled->getOutput(0), *x, ElementWiseOperation::kSUM);
return ew1->getOutput(0);
}
ITensor* RRDB(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, ITensor* x, std::string lname)
{
ITensor* out = residualDenseBlock(network, weightMap, x, lname + ".rdb1");
out = residualDenseBlock(network, weightMap, out, lname + ".rdb2");
out = residualDenseBlock(network, weightMap, out, lname + ".rdb3");
float *scval = reinterpret_cast<float*>(malloc(sizeof(float)));
*scval = 0.2;
Weights scale{ DataType::kFLOAT, scval, 1 };
float *shval = reinterpret_cast<float*>(malloc(sizeof(float)));
*shval = 0.0;
Weights shift{ DataType::kFLOAT, shval, 1 };
float *pval = reinterpret_cast<float*>(malloc(sizeof(float)));
*pval = 1.0;
Weights power{ DataType::kFLOAT, pval, 1 };
IScaleLayer* scaled = network->addScale(*out, ScaleMode::kUNIFORM, shift, scale, power);
IElementWiseLayer* ew1 = network->addElementWise(*scaled->getOutput(0), *x, ElementWiseOperation::kSUM);
return ew1->getOutput(0);
}
#endif