You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@singa.apache.org by ka...@apache.org on 2016/12/23 03:44:36 UTC
[2/6] incubator-singa git commit: SINGA-278 Convert trained caffe
parameters to singa
SINGA-278 Convert trained caffe parameters to singa
Convert trained caffe parameters to singa
run vgg model as an example:
use opencv to load image
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/c7ceb070
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/c7ceb070
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/c7ceb070
Branch: refs/heads/master
Commit: c7ceb070a0ead49b6cd0fbe50d4e7d01e81b382a
Parents: cb81caa
Author: Xiangrui <ca...@gmail.com>
Authored: Mon Dec 5 11:16:16 2016 +0800
Committer: Xiangrui <ca...@gmail.com>
Committed: Mon Dec 5 11:23:36 2016 +0800
----------------------------------------------------------------------
examples/caffe_vgg/predict.py | 46 +++++++++++++++++---------------------
python/singa/converter.py | 12 +++++-----
2 files changed, 28 insertions(+), 30 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/c7ceb070/examples/caffe_vgg/predict.py
----------------------------------------------------------------------
diff --git a/examples/caffe_vgg/predict.py b/examples/caffe_vgg/predict.py
index 72a411e..819fb44 100644
--- a/examples/caffe_vgg/predict.py
+++ b/examples/caffe_vgg/predict.py
@@ -17,12 +17,15 @@
import numpy as np
import os
import argparse
-from PIL import Image
+#from PIL import Image
from singa import device
from singa import tensor
from singa import converter
from singa import layer
+from singa import net
+
+net.verbose = True
def convert_model(prototxt, caffemodel):
@@ -51,41 +54,39 @@ def load_test_data(test_dir, mean):
print paths
test = []
for path in paths:
- img = Image.open(os.path.join(test_dir, path))
+ import cv2
+ im = cv2.resize(cv2.imread(os.path.join(test_dir, path)), (224, 224)).astype(np.float32)
+ im[:,:,0] -= 103.939
+ im[:,:,1] -= 116.779
+ im[:,:,2] -= 123.68
+ im = im.transpose((2,0,1))
+
+ #img = Image.open(os.path.join(test_dir, path))
# BGR is the default model in caffe
# convert RGB to BGR
- img = img.convert('RGB')
+ #img = img.convert('RGB')
#r, g, b = img.split()
#img = Image.merge('RGB', (b, g, r))
- resized = img.resize((224, 224))
+ #resized = img.resize((224, 224))
#resized.save('./load/' + path)
#print 'image shape1: ', img.size
# order of dimensions: width,height,channel
- img_ary = np.asarray(resized, dtype=np.float32)
+ #img_ary = np.asarray(resized, dtype=np.float32)
#print 'img_ary shape: ', img_ary.shape
#print 'img_ary[0][0]: ', img_ary[0][0]
- img_ary -= mean
+ #img_ary -= mean
#print 'img_ary[0][0]: ', img_ary[0][0]
#img2 = Image.fromarray(img_ary, 'RGB')
#img2.save('./load/' + path)
#print 'image shape1: ', img_ary.shape
- img_ary = np.swapaxes(img_ary, 0, 2)
+ #img_ary = np.swapaxes(img_ary, 0, 2)
#img_ary = np.transpose(img_ary, (2, 1, 0))
#print 'img_ary[0][0]', img_ary[0][0][0], img_ary[1][0][0], img_ary[2][0][0]
#print 'image shape2: ', img_ary.shape
- test.append(img_ary)
+ test.append(im)
return np.asarray(test)
-def subtract_mean_pixel(ary, mean_pixel):
- print 'input array shape: ', ary.shape
- for i in range(ary.shape[0]):
- for j in range(3):
- #print 'before subtraction:\n', np.average(ary[i][j])
- ary[i][j] -= mean_pixel[j]
- #print 'after subtraction:\n', np.average(ary[i][j])
-
-
def predict(net, images, dev, synset_list=None, topk=5):
'''Predict the label of each image.
@@ -102,15 +103,11 @@ def predict(net, images, dev, synset_list=None, topk=5):
y.to_host()
prob = tensor.to_numpy(y)
# prob = np.average(prob, 0)
- labels = np.flipud(np.argsort(prob)) # sort prob in descending order
+ labels = np.fliplr(np.argsort(prob)) # sort prob in descending order
print labels[:, 0:topk]
- #syn_labels = []
for i in range(labels.shape[0]):
l = [synset_list[labels[i][j]] for j in range(topk)]
print l
- #syn_labels.append(l)
- #return labels[:, 0:topk]
- #return syn_labels
if __name__ == '__main__':
@@ -122,7 +119,6 @@ if __name__ == '__main__':
parser.add_argument('model_bin', default='./vgg16.caffemodel')
parser.add_argument('imgclass', default='./synset_words.txt')
parser.add_argument('testdir', default='./test/')
- #parser.add_argument('--use_cpu', action='store_true')
args = parser.parse_args()
check_path(args.model_txt)
@@ -142,8 +138,8 @@ if __name__ == '__main__':
#mean_BGR =[103.939, 116.779, 123.68]
mean_RGB = [123.68, 116.779, 103.939]
test_images = load_test_data(args.testdir, mean_RGB)
- print 'test norm: ', np.linalg.norm(test_images) / test_images.size
- #subtract_mean_pixel(test_images, mean_RGB)
+ print 'test norm: ', np.sum(np.abs(test_images)) / test_images.size
+ #print 'test norm: ', np.linalg.norm(test_images) / test_images.size
# predict for two images
predict(model, test_images, dev, synset_list=syn_li, topk=5)
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/c7ceb070/python/singa/converter.py
----------------------------------------------------------------------
diff --git a/python/singa/converter.py b/python/singa/converter.py
index d14d362..c486791 100644
--- a/python/singa/converter.py
+++ b/python/singa/converter.py
@@ -202,11 +202,12 @@ class CaffeConverter:
wmat = np.array(layer.blobs[0].data, dtype=np.float32).reshape(wmat_dim)
bias = np.array(layer.blobs[1].data, dtype=np.float32)
- channels = layer.blobs[0].channels;
- if channels == 3 or channels == 4: # RGB or RGBA
- if first_conv:
- print 'Swapping BGR of caffe into RGB in singa'
- wmat[:, [0, 2], :, :] = wmat[:, [2, 0], :, :]
+ #channels = layer.blobs[0].channels;
+ #if channels == 3 or channels == 4: # RGB or RGBA
+ # if first_conv:
+ # print 'Swapping BGR of caffe into RGB in singa'
+ # wmat[:, [0, 2], :, :] = wmat[:, [2, 0], :, :]
+ print layer.name, ' wmat_dim: ', wmat_dim
wdim = []
if layer.type == 'InnerProduct':
@@ -218,6 +219,7 @@ class CaffeConverter:
chw *= wmat_dim[k]
wdim.extend([nb_filters, chw])
#print 'wdim:', wdim
+ print layer.name, ' wdim: ', wdim
w = np.reshape(wmat, wdim)
if layer.type == 'InnerProduct':