Caffe2 - Python API
A deep learning, cross platform ML framework
helpers.py
1 
3 import numpy as np
4 import skimage.io
5 import skimage.transform
6 import urllib2
7 
8 def crop_center(img,cropx,cropy):
9  y,x,c = img.shape
10  startx = x//2-(cropx//2)
11  starty = y//2-(cropy//2)
12  return img[starty:starty+cropy,startx:startx+cropx]
13 
14 def rescale(img, input_height, input_width):
15  # print("Original image shape:" + str(img.shape) + " --> it should be in H, W, C!")
16  # print("Model's input shape is %dx%d") % (input_height, input_width)
17  aspect = img.shape[1]/float(img.shape[0])
18  # print("Orginal aspect ratio: " + str(aspect))
19  if(aspect>1):
20  # landscape orientation - wide image
21  res = int(aspect * input_height)
22  imgScaled = skimage.transform.resize(img, (input_width, res))
23  if(aspect<1):
24  # portrait orientation - tall image
25  res = int(input_width/aspect)
26  imgScaled = skimage.transform.resize(img, (res, input_height))
27  if(aspect == 1):
28  imgScaled = skimage.transform.resize(img, (input_width, input_height))
29  return imgScaled
30 
31 def load(img):
32  # load and transform image
33  img = skimage.img_as_float(skimage.io.imread(img)).astype(np.float32)
34  return img
35 
36 def chw(img):
37  # switch to CHW
38  img = img.swapaxes(1, 2).swapaxes(0, 1)
39  return img
40 
41 def bgr(img):
42  # switch to BGR
43  img = img[(2, 1, 0), :, :]
44  return img
45 
46 def removeMean(img, mean):
47  # remove mean for better results
48  img = img * 255 - mean
49  return img
50 
51 def batch(img):
52  # add batch size
53  img = img[np.newaxis, :, :, :].astype(np.float32)
54  return img
55 
56 def parseResults(results):
57  results = np.asarray(results)
58  results = np.delete(results, 1)
59  index = 0
60  highest = 0
61  arr = np.empty((0,2), dtype=object)
62  arr[:,0] = int(10)
63  arr[:,1:] = float(10)
64  for i, r in enumerate(results):
65  # imagenet index begins with 1!
66  i=i+1
67  arr = np.append(arr, np.array([[i,r]]), axis=0)
68  if (r > highest):
69  highest = r
70  index = i
71 
72  # top 3 results
73  print "Raw top 3 results:", sorted(arr, key=lambda x: x[1], reverse=True)[:3]
74 
75  # now we can grab the code list
76  with open('inference_codes.txt', 'r') as f: for line in f:
77  code, result = line.partition(":")[::2]
78  if (code.strip() == str(index)):
79  answer = "The image contains a %s with a %s percent probability." % \
80  (result.strip()[1:-2], highest*100)
81  f.closed
82  return answer
83 
84 def loadToNCHW(img, mean, input_size):
85  img = load(img)
86  img = rescale(img, input_size, input_size)
87  img = crop_center(img, input_size, input_size)
88  img = chw(img)
89  img = bgr(img)
90  img = removeMean(img, mean)
91  img = batch(img)
92  return img
93