imagenet_cls_test_googlenet.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import numpy as np
  2. import sys
  3. import os
  4. import argparse
  5. from imagenet_cls_test_alexnet import MeanChannelsFetch, CaffeModel, DnnCaffeModel, ClsAccEvaluation
  6. try:
  7. import caffe
  8. except ImportError:
  9. raise ImportError('Can\'t find Caffe Python module. If you\'ve built it from sources without installation, '
  10. 'configure environment variable PYTHONPATH to "git/caffe/python" directory')
  11. try:
  12. import cv2 as cv
  13. except ImportError:
  14. raise ImportError('Can\'t find OpenCV Python module. If you\'ve built it from sources without installation, '
  15. 'configure environment variable PYTHONPATH to "opencv_build_dir/lib" directory (with "python3" subdirectory if required)')
  16. if __name__ == "__main__":
  17. parser = argparse.ArgumentParser()
  18. parser.add_argument("--imgs_dir", help="path to ImageNet validation subset images dir, ILSVRC2012_img_val dir")
  19. parser.add_argument("--img_cls_file", help="path to file with classes ids for images, val.txt file from this "
  20. "archive: http://dl.caffe.berkeleyvision.org/caffe_ilsvrc12.tar.gz")
  21. parser.add_argument("--prototxt", help="path to caffe prototxt, download it here: "
  22. "https://github.com/BVLC/caffe/blob/master/models/bvlc_alexnet/deploy.prototxt")
  23. parser.add_argument("--caffemodel", help="path to caffemodel file, download it here: "
  24. "http://dl.caffe.berkeleyvision.org/bvlc_alexnet.caffemodel")
  25. parser.add_argument("--log", help="path to logging file")
  26. parser.add_argument("--batch_size", help="size of images in batch", default=500, type=int)
  27. parser.add_argument("--frame_size", help="size of input image", default=224, type=int)
  28. parser.add_argument("--in_blob", help="name for input blob", default='data')
  29. parser.add_argument("--out_blob", help="name for output blob", default='prob')
  30. args = parser.parse_args()
  31. data_fetcher = MeanChannelsFetch(args.frame_size, args.imgs_dir)
  32. frameworks = [CaffeModel(args.prototxt, args.caffemodel, args.in_blob, args.out_blob),
  33. DnnCaffeModel(args.prototxt, args.caffemodel, '', args.out_blob)]
  34. acc_eval = ClsAccEvaluation(args.log, args.img_cls_file, args.batch_size)
  35. acc_eval.process(frameworks, data_fetcher)