Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1.  
  2. import caffe
  3. from caffe.proto import caffe_pb2
  4. from caffe.io import blobproto_to_array
  5. import numpy as np # for array
  6. import glob
  7. import os.path
  8.  
  9. # directory = "C:/dev/caffe/caffe-windows"
  10. net_path = "./examples/mytest/lenet/deploy.prototxt"
  11. model_path = "./examples/mytest/mytest_iter_100000.caffemodel"
  12. mean_path = "./data/mean.binaryproto"
  13. eval_path = "./data/eval.txt"
  14.  
  15. # --
  16. #net_path : deploy.prototxt, model_path : deploy.prototxt 識別なので、Caffe.TESTと記載
  17. net = caffe.Net(net_path, model_path,caffe.TEST)
  18. # --
  19.  
  20. # for preprocess
  21. transformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape})
  22. # channel_swapはRGBをσ(RGB) = BGRにしたいときに用いる。
  23. # 対して、set_transposeは、[H, W, K] => [K, H, W]としたいときとかに用いる。
  24. transformer.set_transpose('data', (2,0,1))
  25.  
  26. mean_blob = caffe_pb2.BlobProto()
  27. with open(mean_path, "rb") as f:
  28. mean_blob.ParseFromString(f.read())
  29. mean_array = blobproto_to_array(mean_blob)
  30. mean_array = np.asarray(mean_blob.data).reshape((mean_blob.channels, mean_blob.height, mean_blob.width))
  31. transformer.set_mean('data', mean_array)
  32.  
  33. #上記の説明参照
  34. transformer.set_raw_scale('data', 255)
  35. # scaling to 1/255 = 0.003..
  36. transformer.set_input_scale('data', 0.00390625)
  37.  
  38. # sample test for 11181 images
  39. cnt = 0 #number of the collect answer
  40. num = 11181
  41.  
  42. numbers = [str(i+60000).zfill(7) for i in range(1,num+1)]
  43. filename = [s + ".bmp" for s in numbers]
  44. images = "data/mytest/check"
  45.  
  46.  
  47. import csv
  48. reader = csv.reader(open(eval_path), lineterminator = ' ')
  49. ansList = []
  50. for row in reader:
  51. i = 0
  52. (fl, i) = row[0].split(' ')
  53. ansList.append((int(i)))
  54.  
  55.  
  56. for i, fname in enumerate(filename):
  57.  
  58. fn = images + "/" + fname
  59.  
  60. image = caffe.io.load_image(fn, color = False)
  61. # image : (H×W×K) ndarray => set_transposeが効いて、outputは(K×W×H) ndarray型になる。
  62. proc = transformer.preprocess('data', image)
  63. # (K×W×H) ndarray のinputデータ : proc
  64. # net.inputs[0]はdeploy.prototxtのdataレイヤーに記載の'data'(topプロパティ)
  65. out = net.forward_all(**{net.inputs[0]:proc})
  66. # net.outputs[0]はdeploy.prototxtのdataレイヤーに記載の'prob'(topプロパティ)
  67. predictions = out[net.outputs[0]]
  68. answer = np.argmax(predictions)
  69.  
  70. print predictions, "<= prob"
  71. print net.blobs['ip2'].data ," <= ip2"
  72.  
  73. print fn, i,", true answer =>", ansList[i],", eval =>", answer, ansList[i] == answer
  74. cnt = cnt + 1 if (ansList[i] == answer) else cnt
  75. print cnt, "/", num
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement