-
Notifications
You must be signed in to change notification settings - Fork 1
/
compress_trained_weights.py
153 lines (127 loc) · 4.21 KB
/
compress_trained_weights.py
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
152
153
import torch
import numpy as np
from torch import nn
import torchvision
import model
# LOAD_PATH = "./snapshots_weight_Kai_extraImg_extraLoss_2x2exp/R11Epoch40.pth"
# LOAD_PATH = "./snapshots_weight_trongan93/R6Epoch100.pth"
LOAD_PATH ="./snapshots_weight_trongan93/R6Epoch1.pth" # test model
device = torch.device('cpu')
scale_factor = 1
DCE_net = model.simpleEnhanceNet(scale_factor).to(device) # proposed training model
DCE_net.load_state_dict(torch.load(LOAD_PATH))
# print("Structure : \n", DCE_net)
# print("=" * 80)
# print("Exporting weight file " + LOAD_PATH)
# LOAD_PATH = LOAD_PATH.replace('pth', 'bin')
# with open(LOAD_PATH, "wb") as file:
# for param_name in DCE_net.state_dict():
# if param_name.find("num_batches_tracked") != -1:
# continue
# layer_weight = DCE_net.state_dict()[param_name].flatten().numpy()
# for weight in layer_weight:
# file.write(weight)
# print(LOAD_PATH + " Weight file exported")
# exit()
weight_list = list(DCE_net.state_dict().keys())
print("weigth_list : ", weight_list)
# for i in weight_list:
# w = DCE_net.state_dict()[i].to(device).numpy()
# w = w.flatten()
# if i.find(".weight") != -1:
# print(i, "\t", w.shape, "\t", (w.max() * 16384).astype('int16'), (w.min() * 16384).astype('int16'))
# elif i.find(".bias") != -1:
# print(i, "\t", w.shape, "\t", (w.max() * 16384 * 16384).astype('int32'), (w.min() * 16384 * 16384).astype('int32')) # 2^28 => 268435456
# exit()
quantized_scale = 64
with open("q_Train_Model_Weight.h", "w") as f:
print("export ", weight_list[0])
f.write("const short conv1_w[864] = {\n")
w = DCE_net.state_dict()[weight_list[0]].to(device).numpy()
w = w.flatten()
count = 0
for param in w:
count += 1
f.write(str(int(param * (2**quantized_scale))))
if count != w.size:
f.write(", ")
else:
f.write("};\n")
if count % 16 == 0:
f.write("\n")
f.write("\n")
print("export ", weight_list[1])
f.write("const int conv1_b[32] = {\n")
w = DCE_net.state_dict()[weight_list[1]].to(device).numpy()
w = w.flatten()
count = 0
for param in w:
count += 1
f.write(str(int(param * (2**quantized_scale) * (2**quantized_scale))))
if count != w.size:
f.write(", ")
else:
f.write("};\n")
if count % 8 == 0:
f.write("\n")
f.write("\n")
print("export ", weight_list[2])
f.write("const short conv2_w[9216] = {\n")
w = DCE_net.state_dict()[weight_list[2]].to(device).numpy()
w = w.flatten()
count = 0
for param in w:
count += 1
f.write(str(int(param * (2**quantized_scale))))
if count != w.size:
f.write(", ")
else:
f.write("};\n")
if count % 16 == 0:
f.write("\n")
f.write("\n")
print("export ", weight_list[3])
f.write("const int conv2_b[32] = {\n")
w = DCE_net.state_dict()[weight_list[3]].to(device).numpy()
w = w.flatten()
count = 0
for param in w:
count += 1
f.write(str(int(param * (2**quantized_scale) * (2**quantized_scale))))
if count != w.size:
f.write(", ")
else:
f.write("};\n")
if count % 8 == 0:
f.write("\n")
f.write("\n")
print("export ", weight_list[4])
f.write("const short conv3_w[864] = {\n")
w = DCE_net.state_dict()[weight_list[4]].to(device).numpy()
w = w.flatten()
count = 0
for param in w:
count += 1
f.write(str(int(param * (2**quantized_scale))))
if count != w.size:
f.write(", ")
else:
f.write("};\n")
if count % 16 == 0:
f.write("\n")
f.write("\n")
print("export ", weight_list[5])
f.write("const int conv3_b[3] = {\n")
w = DCE_net.state_dict()[weight_list[5]].to(device).numpy()
w = w.flatten()
count = 0
for param in w:
count += 1
f.write(str(int(param * (2**quantized_scale) * (2**quantized_scale))))
if count != w.size:
f.write(", ")
else:
f.write("};\n")
if count % 8 == 0:
f.write("\n")
f.write("\n")