1 Star 0 Fork 0

梯度下降法/PSPNet-Keras-tensorflow

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
weight_converter.py 1.49 KB
一键复制 编辑 原始数据 按行查看 历史
VladKry 提交于 2018-01-23 11:43 . Merge branch 'master' into master
#!/usr/bin/env python
from __future__ import print_function
import sys
from os.path import splitext
import numpy as np
import caffe
# Not needed because Tensorflow and Caffe do convolution the same way
# Needed for conversion to Theano
def rot90(W):
for i in range(W.shape[0]):
for j in range(W.shape[1]):
W[i, j] = np.rot90(W[i, j], 2)
return W
weights = {}
assert "prototxt" in splitext(
sys.argv[1])[1], "First argument must be caffe prototxt %s" % sys.argv[1]
assert "caffemodel" in splitext(
sys.argv[2])[1], "Second argument must be caffe weights %s" % sys.argv[2]
net = caffe.Net(sys.argv[1], sys.argv[2], caffe.TEST)
for k, v in net.params.items():
print("Layer %s, has %d params." % (k, len(v)))
if len(v) == 1:
W = v[0].data[...]
W = np.transpose(W, (2, 3, 1, 0))
weights[k] = {"weights": W}
elif len(v) == 2:
W = v[0].data[...]
W = np.transpose(W, (2, 3, 1, 0))
b = v[1].data[...]
weights[k] = {"weights": W, "biases": b}
elif len(v) == 4: # Batchnorm layer
k = k.replace('/', '_')
scale = v[0].data[...]
offset = v[1].data[...]
mean = v[2].data[...]
variance = v[3].data[...]
weights[k] = {"mean": mean, "variance": variance,
"scale": scale, "offset": offset}
else:
print("Undefined layer")
exit()
arr = np.asarray(weights)
weights_name = splitext(sys.argv[2])[0] + ".npy"
np.save(weights_name.lower(), arr)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/GradientDescent/PSPNet-Keras-tensorflow.git
git@gitee.com:GradientDescent/PSPNet-Keras-tensorflow.git
GradientDescent
PSPNet-Keras-tensorflow
PSPNet-Keras-tensorflow
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385