Repository navigation
Expand file tree
/
Copy pathweights.cpp
More file actions
151 lines (137 loc) · 4.23 KB
/
Copy pathweights.cpp
File metadata and controls
151 lines (137 loc) · 4.23 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <ctime>
#include <string>
#include <boost/regex.hpp>
#include <boost/filesystem.hpp>
#include <glog/logging.h>
#include <torch/nn.h>
#include <torch/torch.h>
#include <torch/script.h>
#include "utils.hpp"
#include "weights.hpp"
namespace bfs = boost::filesystem;
uint8_t HexChar2Num(uint8_t c) {
if (isdigit((int)c)) {
return c - '0';
}
if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
return 0xFF;
}
uint8_t Num2HexChar(uint8_t n) {
if (n < 10) {
return n + '0';
}
if (n < 16) {
return n + 'A' - 10;
}
return 0xFF;
}
uint8_t HexWord2Num(uint16_t c) {
uint8_t lo = HexChar2Num((uint8_t)(c >> 8));
uint8_t hi = HexChar2Num((uint8_t)(c & 0xFF));
CHECK_NE(hi, 0xFF);
CHECK_NE(lo, 0xFF);
return (hi << 4) | lo;
}
uint16_t Num2HexWord(uint8_t n) {
uint8_t lo = Num2HexChar((uint8_t)(n >> 4));
uint8_t hi = Num2HexChar((uint8_t)(n & 0xF));
return (uint16_t(hi << 8) | uint16_t(lo));
}
template<typename _IS, typename _Elem>
uint64_t LoadBuffer(_IS &inStream, std::vector<_Elem> &buf) {
uint64_t nBufLen = 0;
CHECK(inStream.read((char*)&nBufLen, sizeof(nBufLen)));
CHECK_EQ(nBufLen % sizeof(_Elem), 0);
buf.resize(nBufLen / sizeof(_Elem));
CHECK(inStream.read((char*)buf.data(), nBufLen));
return nBufLen;
}
template<typename _OS, typename _Elem>
uint64_t SaveBuffer(_OS &outStream, const std::vector<_Elem> &buf) {
uint64_t nBufLen = buf.size() * sizeof(_Elem);
CHECK(outStream.write((char*)&nBufLen, sizeof(nBufLen)));
if (nBufLen > 0) {
CHECK(outStream.write((char*)buf.data(), nBufLen));
}
return nBufLen;
}
std::pair<std::string, std::vector<char>> LoadNamedBuffer(std::ifstream &inStream) {
std::vector<char> name, buf;
CHECK_GT(LoadBuffer(inStream, name), 0);
CHECK_GT(LoadBuffer(inStream, buf), 0);
std::string strName(name.begin(), name.end());
return std::make_pair(std::move(strName), std::move(buf));
}
void SaveNamedBuffer(std::ofstream &outStream, const std::string &strName,
const std::vector<char> &buf) {
std::vector<char> name(strName.begin(), strName.end());
CHECK_GT(SaveBuffer(outStream, name), 0);
CHECK_GT(SaveBuffer(outStream, buf), 0);
}
NAMED_PARAMS LoadWeights(const std::string &strFilename) {
std::ifstream inFile(strFilename);
CHECK(inFile.is_open());
NAMED_PARAMS namedParams;
for (; inFile.peek() != EOF ;) {
auto [strName, buf] = LoadNamedBuffer(inFile);
auto tensor = torch::pickle_load(std::move(buf)).toTensor();
namedParams[std::move(strName)] = std::move(tensor);
}
return namedParams;
}
void SaveWeights(const NAMED_PARAMS &weights, const std::string &strFilename) {
std::ofstream outFile(strFilename);
CHECK(outFile.is_open());
for (const auto ¶m : weights) {
auto buf = torch::pickle_save(param.second);
SaveNamedBuffer(outFile, param.first, buf);
}
}
bool InitModuleWeight(const std::string &strModuleType,
NAMED_PARAMS &weights, NAMED_PARAMS &buffers) {
auto iWeight = weights.find("weight");
auto iBias = weights.find("bias");
if (strModuleType.find("Conv2d") != std::string::npos) {
CHECK(iWeight != weights.end());
torch::nn::init::xavier_normal_(iWeight->second);
if (iBias != weights.end()) {
torch::nn::init::normal_(iBias->second);
}
} else if (strModuleType.find("Linear") != std::string::npos) {
CHECK(iWeight != weights.end());
torch::nn::init::xavier_normal_(iWeight->second);
if (iBias != weights.end()) {
torch::nn::init::normal_(iBias->second);
}
} else if (strModuleType.find("BatchNorm2d") != std::string::npos) {
CHECK(iWeight != weights.end());
CHECK(iBias != weights.end());
auto iMean = buffers.find("running_mean");
if (iMean != buffers.end()) {
iMean->second.fill_(0);
}
auto iVar = buffers.find("running_var");
if (iVar != buffers.end()) {
iVar->second.fill_(1);
}
torch::nn::init::constant_(iWeight->second, 1.);
torch::nn::init::constant_(iBias->second, 0.);
} else if (strModuleType.find("LSTM") != std::string::npos) {
for (auto &w : weights) {
CHECK_GT(w.first.size(), 5);
auto strPrefix = w.first.substr(0, 5);
if (strPrefix == "bias_") {
torch::nn::init::constant_(w.second, 0.);
} else {
CHECK(strPrefix == "weight");
torch::nn::init::xavier_normal_(w.second);
}
}
} else if (strModuleType.find("YoloLayer") != std::string::npos) {
} else {
return false;
}
return true;
}