-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadWeight.cpp
More file actions
82 lines (79 loc) · 1.99 KB
/
LoadWeight.cpp
File metadata and controls
82 lines (79 loc) · 1.99 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
#include "LoadWeight.h"
LoadWeight::LoadWeight(const string weight_dir)
{
m_weight_dir = weight_dir;
}
LoadWeight::~LoadWeight()
{
}
void LoadWeight::print_weight()
{
}
bool LoadWeight::load_weights()
{
DIR * pDir;
struct dirent * ptr;
if (!(pDir = opendir(m_weight_dir.c_str())))
{
cout << "Folder does not Exist" << endl;
return false;
}
while ((ptr = readdir(pDir)) != 0)
{
if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) continue;
if (load_weight_file(m_weight_dir + "/" + ptr->d_name, ptr->d_name) == false)
{
printf("The %s is an error!!!", ptr->d_name);
return false;
}
}
return true;
}
bool LoadWeight::load_weight_file(const string &weight_file_path,const string & weight_name)
{
//cout << weight_file_path << endl;
ifstream inF;
inF.open(weight_file_path,std::ifstream::binary);
if (!inF.is_open())
{
printf("Read File: %s Error ....\n",weight_file_path.c_str());
return false;
}
// 获取文件大小
inF.seekg(0, std::ifstream::end);
//将这个数据转化为long
long size = inF.tellg();
//inF的数据大小
inF.seekg(0);
//将输入流文件重新设置为0
//buffer.resize(size);
//printf("文件:[%s] 共有:%ld(字节) ...... \n", weight_file_path.c_str(),size);
vector<double> m_buffer;
m_buffer.resize(size / 8);
inF.read((char*)&m_buffer[0],size);
//cout << m_buffer.size() << endl;
m_weights.insert({ weight_name,m_buffer });
///*
//for (int i=0; i < size / 8; i++)
//{
// cout << m_weights[weight_name][i] << endl;
//}
//.*/
inF.close();
return true;
}
vector<double> LoadWeight::load_weight(const string &weight_name)
{
m_weights.clear();
if (load_weight_file(m_weight_dir + "\\" + weight_name, weight_name) == false)
{
printf("The %s is an error!!!", weight_name);
throw "Load the weight of %s error!!!",weight_name;
}
cout << weight_name << endl;
return m_weights[weight_name];
}
unordered_map<string, vector<double>> LoadWeight::GetWeights()
{
return m_weights;
}