From de4d8005a9601a8901c77373937e9f6764a6bb1a Mon Sep 17 00:00:00 2001 From: Vitaliy Lyudvichenko Date: Sun, 12 Jul 2015 23:19:07 +0300 Subject: [PATCH] Added .caffemodel files downloader for tests on post-build step. --- modules/dnn/CMakeLists.txt | 11 ++++- modules/dnn/scripts/download_model.py | 71 +++++++++++++++++++++++++++ modules/dnn/scripts/test_models.json | 7 +++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 modules/dnn/scripts/download_model.py create mode 100644 modules/dnn/scripts/test_models.json diff --git a/modules/dnn/CMakeLists.txt b/modules/dnn/CMakeLists.txt index 330addf0c..fafb82257 100644 --- a/modules/dnn/CMakeLists.txt +++ b/modules/dnn/CMakeLists.txt @@ -33,9 +33,18 @@ ocv_module_include_directories(include src/caffe ${PROTOBUF_INCLUDE_DIR}) ocv_create_module(${PROTOBUF_LIBRARIES}) +ocv_add_samples() ocv_add_accuracy_tests() ocv_add_perf_tests() -ocv_add_samples() + +OCV_OPTION(${the_module}_DOWNLOAD_CAFFE_MODELS "Use GoogLeNet Caffe model for testing" ON IF BUILD_TESTS AND PYTHON2_EXECUTABLE AND DEFINED ENV{OPENCV_TEST_DATA_PATH}) +if(BUILD_TESTS AND ${the_module}_DOWNLOAD_CAFFE_MODELS) + add_custom_command( TARGET opencv_test_${name} POST_BUILD + COMMAND ${PYTHON2_EXECUTABLE} download_model.py test_models.json + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/scripts ) +else() + add_definitions(-DDISABLE_CAFFE_MODEL_TESTS=1) +endif() else()#build as standalone module (for development purposes) diff --git a/modules/dnn/scripts/download_model.py b/modules/dnn/scripts/download_model.py new file mode 100644 index 000000000..2feb6d39e --- /dev/null +++ b/modules/dnn/scripts/download_model.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python +import os +import sys +import time +import urllib +import hashlib +import argparse +import json + + +def reporthook(count, block_size, total_size): + """ + From http://blog.moleculea.com/2012/10/04/urlretrieve-progres-indicator/ + """ + global start_time + if count == 0: + start_time = time.time() + return + duration = time.time() - start_time + progress_size = int(count * block_size) + speed = int(progress_size / (1024 * duration)) + percent = int(count * block_size * 100 / total_size) + sys.stdout.write("\r...%d%%, %d MB, %d KB/s, %d seconds passed" % + (percent, progress_size / (1024 * 1024), speed, duration)) + sys.stdout.flush() + +# Closure-d function for checking SHA1. +def model_checks_out(filename, sha1): + with open(filename, 'r') as f: + return hashlib.sha1(f.read()).hexdigest() == sha1 + +def model_download(filename, url, sha1): + # Check if model exists. + if os.path.exists(filename) and model_checks_out(filename, sha1): + print("Model {} already exists.".format(filename)) + return + + # Download and verify model. + urllib.urlretrieve(url, filename, reporthook) + if not model_checks_out(): + print("ERROR: model {} did not download correctly!".format(url)) + sys.exit(1) + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description="Downloading trained model binaries.") + parser.add_argument("download_list") + args = parser.parse_args() + + test_dir = os.environ.get("OPENCV_TEST_DATA_PATH") + if not test_dir: + print "ERROR: OPENCV_TEST_DATA_PATH environment not specified" + sys.exit(1) + + try: + with open(args.download_list, 'r') as f: + models_to_download = json.load(f) + except: + print "ERROR: Can't pasrse {}".format(args.download_list) + sys.exit(1) + + for model_name in models_to_download: + model = models_to_download[model_name] + + dst_dir = os.path.join(test_dir, os.path.dirname(model['file'])) + dst_file = os.path.join(test_dir, model['file']) + if not os.path.exists(dst_dir): + print "ERROR: Can't find module testdata path '{}'".format(dst_dir) + sys.exit(1) + + print "Downloading model '{}' to {} from {} ...".format(model_name, dst_file, model['url']) + model_download(dst_file, model['url'], model['sha1']) \ No newline at end of file diff --git a/modules/dnn/scripts/test_models.json b/modules/dnn/scripts/test_models.json new file mode 100644 index 000000000..f2ed34595 --- /dev/null +++ b/modules/dnn/scripts/test_models.json @@ -0,0 +1,7 @@ +{ + "googlenet": { + "file": "dnn/bvlc_googlenet.caffemodel", + "url": "http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel", + "sha1": "d41971236422c20b1d7d92769670f3642ef4611a" + } +} \ No newline at end of file