1 Star 0 Fork 0

kangchi/pytorch2caffe

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
extract_caffe_blobs.py 1.61 KB
一键复制 编辑 原始数据 按行查看 历史
wanglaotou 提交于 2019-10-16 20:10 . first commite
import math
import numpy as np
import os
import sys
import cv2
import caffe
np.set_printoptions(threshold=sys.maxsize)
file_path = "./12/2858.jpg"
PNet_model_def = "pnet.prototxt"
PNet_model_weights = "pnet.caffemodel"
# caffe.set_device(1)
caffe.set_mode_cpu()
# Load models.
PNet = caffe.Net(PNet_model_def, PNet_model_weights, caffe.TEST)
# Transform to fill data.
im = cv2.imread(file_path, 1)
if im.shape[2] == 1:
im = cv2.cvtColor(im, cv2.COLOR_GRAY2BGR)
im = im.astype(np.float32)
print('Image In:', im.shape, 'Net In:',PNet.blobs['data'].data.shape)
# bgr -> rgb
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) #im = im[...,::-1]
im_resized = cv2.resize(im, (PNet.blobs['data'].data.shape[3], PNet.blobs['data'].data.shape[2]), 0, 0, interpolation=cv2.INTER_LINEAR)
# h,w,c -> c,h,w
im_resized = np.transpose(im_resized, (2, 0, 1))
im_resized = (im_resized - 127.5) * 0.0078125
# c,h,w -> 1,c,h,w
PNet.blobs['data'].data[0] = im_resized
'''
im = cv2.resize(im, (PNet.blobs['data'].data.shape[3], PNet.blobs['data'].data.shape[2]), 0, 0, interpolation=cv2.INTER_LINEAR)
transformer = caffe.io.Transformer({'data': PNet.blobs['data'].data.shape})
transformer.set_transpose('data', (2, 0, 1))
transformer.set_mean('data', np.array((127.5, 127.5, 127.5)))
transformer.set_raw_scale('data', 1/127.5)
im = transformer.preprocess('data', im)
PNet.blobs['data'].data[...] = im
'''
# Extract the net output blobs.
outputs = PNet.forward()
for blob in outputs.keys():
fn = "./" + blob + ".txt"
outf = open(fn, "w")
outf.write(str(outputs[blob]))
outf.close()
print(outputs.keys())
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/kangchi/pytorch2caffe.git
git@gitee.com:kangchi/pytorch2caffe.git
kangchi
pytorch2caffe
pytorch2caffe
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385