npy文件是numpy專用的二進制文件,npy內容由一個字典組成,字典中的每一個鍵對應一層網絡模型參數(包括權重w和偏置b)。
對於某些網絡模型參數保存爲.npy形式的文件,我們需要讀取這些文件的參數,並fine tune到當前網絡相應的變量。
def load_initial_weights(session):
# Load the weights into memory
weights_dict = np.load('C:\\Downloads\\bvlc_alexnet.npy', encoding = 'bytes').item()
# Loop over all layer names stored in the weights dict
for op_name in weights_dict:
# Check if the layer is one of the layers that should be reinitialized
if op_name not in ['fc6','fc7','fc8']:
with tf.variable_scope('shared/'+op_name,reuse = True):
# Loop over list of weights/biases and assign them to their corresponding tf variable
for data in weights_dict[op_name]:
# Biases
if len(data.shape) == 1:
var = tf.get_variable('biases', trainable = True)
print(var.name)
session.run(var.assign(data))
# Weights
else:
var = tf.get_variable('weights', trainable = True)
session.run(var.assign(data))