Created
October 11, 2017 14:24
-
-
Save paul356/a0fd807146aabd9c755779e28c801fdc to your computer and use it in GitHub Desktop.
python translation of TestModel.m in CVPR paper "Strucutured Feature Learning for Pos Estimation" code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import caffe | |
import numpy | |
import matplotlib.pyplot as pyplot | |
import skimage.io as skimage_io | |
import scipy.misc as scipy_misc | |
import math | |
name = 'lsp_basic' | |
deploy_file = './protofiles/deploy.prototxt' | |
model_file = './cache/lspmodel.caffemodel'; # our provided model | |
caffe.set_mode_gpu(); | |
caffe.set_device(0); | |
net = caffe.Net(deploy_file, model_file, caffe.TEST) | |
def prepare_test_set(): | |
test_frames = range(1001, 2001) | |
return [{'source' : './dataset/LSP/images/im%04d.jpg' % i} for i in test_frames] | |
def pad_after_read(image_path): | |
img = skimage_io.imread(image_path) | |
w = img.shape[1] | |
h = img.shape[0] | |
ch = img.shape[2] | |
if h > w: | |
pad = (h - w) / 2 | |
img = numpy.concatenate((numpy.zeros([h, pad, ch], img.dtype), img, numpy.zeros([h, h - w - pad, ch], img.dtype)), 1) | |
elif h < w: | |
pad = (w - h) / 2 | |
img = numpy.concatenate((numpy.zeros([pad, w, ch], img.dtype), img, numpy.zeros([w - h - pad, w, ch], img.dtype)), 0) | |
return img | |
def resize_image(img, upScale): | |
if upScale != 1.: | |
h = int(img.shape[0] * upScale) | |
w = int(img.shape[1] * upScale) | |
img = scipy_misc.imresize(img, [h, w]) | |
return img | |
def preprocess_image(image_path): | |
img = pad_after_read(image_path) | |
print "image type %s" % img.dtype | |
print "image shape %s" % list(img.shape) | |
upScale = 1. | |
if img.shape[0] > 600: | |
upScale = 0.8 | |
else: | |
if img.shape[0] < 336: | |
upScale = 368./min(img.shape[0], img.shape[1]) | |
else: | |
upScale = 1. | |
savedType = img.dtype | |
resize_image(img, upScale) | |
assert(img.dtype == savedType) | |
img = img.astype(numpy.float32) | |
# adjust value to -128 to 128 | |
img = img - 128.; | |
# interchange height and weight | |
img = img.swapaxes(0, 1) | |
img = img[:, :, [2,1,0]]; | |
return img, upScale | |
def pyramid_image_resp(img): | |
psize = [224, 224] | |
padx = psize[0] / 2 | |
pady = psize[1] / 2 | |
sc = math.pow(2, 0.1) | |
max_scale = int(math.floor(math.log(float(min(img.shape[:2]))/max(psize)) / math.log(sc))) | |
resp = range(0, len(range(1, max_scale))) | |
for i in range(1, max_scale): | |
scaled = resize_image(img, 1./math.pow(sc, i-1)) | |
net.blobs['data'].reshape([scaled.shape[0], scaled.shape[1], 3, 1]) | |
net.blobs['data'].data[...] = scaled | |
scores = net.forward() | |
resp[i] = scores['prob'][0].swapaxes(0, 1) | |
return resp | |
if __name__ == '__main__': | |
test_images = prepare_test_set() | |
[img, upScale] = preprocess_image(test_images[0]['source']) | |
resp_arr = pyramid_image_resp(img) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment