权重转换及修改

1. Tensorflow转换为keras

读取tensorflow权重:参考https://www.jb51.net/article/142183.htm

import tensorflow as tf
checkpoint_path = 'weights/checkpoints/ssd_300_vgg.ckpt'#ssd_300_vgg.ckpt文件夹内存放的是.data和.index
reader = tf.train.NewCheckpointReader(checkpoint_path)
var_to_shape_map = reader.get_variable_to_shape_map()
for key in var_to_shape_map:
  print("tensor_name: ", key)
#  print(reader.get_tensor(key))

#或使用pywrap_tensorflow中的NewCheckpointReader
from tensorflow.python import pywrap_tensorflow
reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path) 

#或使用print_tensors_in_checkpoint_file打印ckpt里的内容, 内部使用的是pywrap_tensorflow.NewCheckpointReader
from tensorflow.python.tools.inspect_checkpoint import print_tensors_in_checkpoint_file  
print_tensors_in_checkpoint_file(file_name=checkpoint_path, #ckpt文件名字
                                 tensor_name=None, # 如果为None,则默认为ckpt里的所有变量
                                 all_tensors=False, # bool 是否打印所有的tensor的值
                                 all_tensor_names=True) # bool 是否打印所有的tensor的name

转换为keras的.h5权重,参考https://blog.csdn.net/zhongxing9006/article/details/80898830(未测试)

2、caffe转keras(已测试)

读取caffe权重,参考https://github.com/TerryBryant/MyWork/blob/master/caffe/model_parse/parse_vgg.py

保存成.npy格式,放在caffe2keras文件夹下,内容为conv1_1.npy等等

以下记录转换成Keras的过程,以目标检测中于VGG的RefineDet为例

因为需要知道Keras的模型格式,所以在weights_source_path对应的.h5基础上更改(可以自己先简单训练一个,若转换成Keras版的SSD就用SSD训练, 类别数随意,也不需要训练多好,新模型会覆盖)

import numpy as np
import h5py
import os
import shutil

basePath = 'caffe2keras/'
total_npy = os.listdir(basePath) # caffe weights 
weights_destination_path = 'caffe2keras_VGG_coco_refinedet_320x320.h5'
weights_source_path = '1ssd300_pascal_07+12+epoch-113_loss-5.7822_val_loss-6.6391.h5'
shutil.copy(weights_source_path, weights_destination_path)
weights_destination_file = h5py.File(weights_destination_path)

#a = []
#b = []   
for caffe_npy in total_npy:
    flag = 0
    name = caffe_npy[:caffe_npy.rindex('_')].replace('-','_')
    weight_type = caffe_npy[caffe_npy.rindex('_')+1:-4]+':0'
    orig_weight = np.load(os.path.join(basePath, caffe_npy))
    ###########some layers need to be treated specially##############
    if name=='conv4_3_norm':
        weight_type = 'conv4_3_norm_gamma:0'
        flag = 1
    if name=='conv5_3_norm':
        weight_type = 'conv5_3_norm_gamma:0'
        flag = 1
    #################################################################
    if weight_type=='weight:0':
        weight_type = 'kernel:0'
    if flag or weight_type=='bias:0':
        caffe_weight = orig_weight
    else:
        caffe_weight = orig_weight.transpose(2, 3, 1, 0)#the shape of caffe is different from keras
#    a.append(weights_destination_file['model_weights'][name][name][weight_type].shape)# to see the shape in keras
    del weights_destination_file['model_weights'][name][name][weight_type]
    weights_destination_file['model_weights'][name][name].create_dataset(name=weight_type, data=caffe_weight)
#    b.append(weights_destination_file['model_weights'][name][name][weight_type].shape)# to see the shape we are writing
weights_destination_file.flush()

有的模型会有['model_weights'],有的模型没有,具体看自己导入的模型。

 

3、caffe转TensorFlow

参考https://github.com/sampepose/flownet2-tf/tree/master/scripts/caffe

4、Keras查看.h5文件内容

参考https://www.cnblogs.com/arkenstone/p/7858178.html

你可能感兴趣的:(tools)