-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathweights_loader.py
170 lines (129 loc) · 7.14 KB
/
weights_loader.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import tensorflow as tf
import os
import numpy as np
import os.path
import test
import net
# IMPORTANT: Weights order in the binary file is [ 'biases','gamma','moving_mean','moving_variance','kernel']
# IMPORTANT: biases ARE NOT the usual biases to add after the conv2d! They refer to the betas (offsets) in the Batch Normalization!
# IMPORTANT: the biases added after the conv2d are set to zero!
# IMPORTANT: to use the weights they actually need to be de-normalized because of the Batch Normalization! ( see later )
def load_conv_layer_bn(name,loaded_weights,shape,offset):
# Conv layer with Batch norm
n_kernel_weights = shape[0]*shape[1]*shape[2]*shape[3]
n_output_channels = shape[-1]
n_bn_mean = n_output_channels
n_bn_var = n_output_channels
n_biases = n_output_channels
n_bn_gamma = n_output_channels
n_weights_conv_bn = (n_kernel_weights + n_output_channels * 4)
# IMPORTANT: This is where (n_kernel_weights + n_output_channels * 4) comes from:
# n_params = kernel_shape + n_biases + n_bn_means + n_bn_var + n_bn_gammas
# n_params = kernel_shape + n_biases + n_output_channels + n_output_channels + n_output_channels
# n_params = kernel_shape + n_output_channels + n_output_channels + n_output_channels + n_output_channels
# n_params = kernel_shape + n_output_channels*4
# IMPORTANT: YOLOv2 sets the biases in every convolution = 0 and keeps only the betas (offsets) of the Batch Normalization!
# So in the end there will be only mean,var,beta(offset),gamma(scale) for every single output channel!
print('Loading '+str(n_weights_conv_bn)+' weights of '+name+' ...')
biases = loaded_weights[offset:offset+n_biases]
offset = offset + n_biases
gammas = loaded_weights[offset:offset+n_bn_gamma]
offset = offset + n_bn_gamma
means = loaded_weights[offset:offset+n_bn_mean]
offset = offset + n_bn_mean
var = loaded_weights[offset:offset+n_bn_var]
offset = offset + n_bn_var
kernel_weights = loaded_weights[offset:offset+n_kernel_weights]
offset = offset + n_kernel_weights
# IMPORTANT: DarkNet conv_weights are serialized Caffe-style: (out_dim, in_dim, height, width)
kernel_weights = np.reshape(kernel_weights,(shape[3],shape[2],shape[0],shape[1]),order='C')
# IMPORTANT: Denormalize the weights with the Batch Normalization parameters
for i in range(n_output_channels):
scale = gammas[i] / np.sqrt(var[i] + net.bn_epsilon)
kernel_weights[i,:,:,:] = kernel_weights[i,:,:,:] * scale
biases[i] = biases[i] - means[i] * scale
# IMPORTANT: Set weights to Tensorflow order: (height, width, in_dim, out_dim)
kernel_weights = np.transpose(kernel_weights,[2,3,1,0])
return biases,kernel_weights,offset
def load_conv_layer(name,loaded_weights,shape,offset):
# Conv layer without Batch norm
n_kernel_weights = shape[0]*shape[1]*shape[2]*shape[3]
n_output_channels = shape[-1]
n_biases = n_output_channels
n_weights_conv = (n_kernel_weights + n_output_channels)
# The number of weights is a conv layer without batchnorm is: (kernel_height*kernel_width + n_biases)
print('Loading '+str(n_weights_conv)+' weights of '+name+' ...')
biases = loaded_weights[offset:offset+n_biases]
offset = offset + n_biases
kernel_weights = loaded_weights[offset:offset+n_kernel_weights]
offset = offset + n_kernel_weights
# IMPORTANT: DarkNet conv_weights are serialized Caffe-style: (out_dim, in_dim, height, width)
# IMPORTANT: We would like to set these to Tensorflow order: (height, width, in_dim, out_dim)
kernel_weights = np.reshape(kernel_weights,(shape[3],shape[2],shape[0],shape[1]),order='C')
kernel_weights = np.transpose(kernel_weights,[2,3,1,0])
return biases,kernel_weights,offset
def load(sess,weights_path,ckpt_folder_path,saver):
if(os.path.exists(ckpt_folder_path)):
print('Found a checkpoint!')
checkpoint_files_path = os.path.join(ckpt_folder_path,"model.ckpt")
saver.restore(sess,checkpoint_files_path)
print('Loaded weights from checkpoint!')
return True
print('No checkpoint found!')
print('Loading weights from file and creating new checkpoint...')
# Get the size in bytes of the binary
size = os.path.getsize(weights_path)
# Load the binary to an array of float32
loaded_weights = []
loaded_weights = np.fromfile(weights_path, dtype='f')
# Delete the first 4 that are not real params...
loaded_weights = loaded_weights[4:]
print('Total number of params to load = {}'.format(len(loaded_weights)))
# IMPORTANT: starting from offset=0, layer by layer, we will get the exact number of parameters required and assign them!
# Conv1 , 3x3, 3->16
offset = 0
biases,kernel_weights,offset = load_conv_layer_bn('conv1',loaded_weights,[3,3,3,16],offset)
sess.run(tf.assign(net.b1,biases))
sess.run(tf.assign(net.w1,kernel_weights))
# Conv2 , 3x3, 16->32
biases,kernel_weights,offset = load_conv_layer_bn('conv2',loaded_weights,[3,3,16,32],offset)
sess.run(tf.assign(net.b2,biases))
sess.run(tf.assign(net.w2,kernel_weights))
# Conv3 , 3x3, 32->64
biases,kernel_weights,offset = load_conv_layer_bn('conv3',loaded_weights,[3,3,32,64],offset)
sess.run(tf.assign(net.b3,biases))
sess.run(tf.assign(net.w3,kernel_weights))
# Conv4 , 3x3, 64->128
biases,kernel_weights,offset = load_conv_layer_bn('conv4',loaded_weights,[3,3,64,128],offset)
sess.run(tf.assign(net.b4,biases))
sess.run(tf.assign(net.w4,kernel_weights))
# Conv5 , 3x3, 128->256
biases,kernel_weights,offset = load_conv_layer_bn('conv5',loaded_weights,[3,3,128,256],offset)
sess.run(tf.assign(net.b5,biases))
sess.run(tf.assign(net.w5,kernel_weights))
# Conv6 , 3x3, 256->512
biases,kernel_weights,offset = load_conv_layer_bn('conv6',loaded_weights,[3,3,256,512],offset)
sess.run(tf.assign(net.b6,biases))
sess.run(tf.assign(net.w6,kernel_weights))
# Conv7 , 3x3, 512->1024
biases,kernel_weights,offset = load_conv_layer_bn('conv7',loaded_weights,[3,3,512,1024],offset)
sess.run(tf.assign(net.b7,biases))
sess.run(tf.assign(net.w7,kernel_weights))
# Conv8 , 3x3, 1024->1024
biases,kernel_weights,offset = load_conv_layer_bn('conv8',loaded_weights,[3,3,1024,1024],offset)
sess.run(tf.assign(net.b8,biases))
sess.run(tf.assign(net.w8,kernel_weights))
# Conv9 , 1x1, 1024->125
biases,kernel_weights,offset = load_conv_layer('conv9',loaded_weights,[1,1,1024,125],offset)
sess.run(tf.assign(net.b9,biases))
sess.run(tf.assign(net.w9,kernel_weights))
# These two numbers MUST be equal!
print('Final offset = {}'.format(offset))
print('Total number of params in the weight file = {}'.format(len(loaded_weights)))
# Saving checkpoint!
if not os.path.exists(ckpt_folder_path):
print('Saving new checkpoint to the new checkpoint directory ./ckpt/ !')
os.makedirs(ckpt_folder_path)
checkpoint_files_path = os.path.join(ckpt_folder_path, "model.ckpt")
saver.save(sess,checkpoint_files_path)
#######################################################################################################################################