From 78f205ffa54668d9a1f412172cacbf2fe76a5b72 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Fri, 11 May 2018 13:29:28 +0300 Subject: [PATCH 1/2] python: better Python 3 support --- modules/dnn/misc/quantize_face_detector.py | 4 ++++ modules/dnn/test/imagenet_cls_test_alexnet.py | 15 ++++++++------- modules/dnn/test/pascal_semsegm_test_fcn.py | 19 ++++++++++--------- modules/ts/misc/table_formatter.py | 2 ++ modules/ts/misc/testlog_parser.py | 4 ++++ platforms/android/build_sdk.py | 1 + samples/python/deconvolution.py | 2 +- 7 files changed, 30 insertions(+), 17 deletions(-) diff --git a/modules/dnn/misc/quantize_face_detector.py b/modules/dnn/misc/quantize_face_detector.py index 833b8ba05b..c66b735847 100644 --- a/modules/dnn/misc/quantize_face_detector.py +++ b/modules/dnn/misc/quantize_face_detector.py @@ -1,10 +1,14 @@ from __future__ import print_function +import sys import argparse import cv2 as cv import tensorflow as tf import numpy as np import struct +if sys.version_info > (3,): + long = int + from tensorflow.python.tools import optimize_for_inference_lib from tensorflow.tools.graph_transforms import TransformGraph from tensorflow.core.framework.node_def_pb2 import NodeDef diff --git a/modules/dnn/test/imagenet_cls_test_alexnet.py b/modules/dnn/test/imagenet_cls_test_alexnet.py index b6922f77df..0d2564c1b7 100644 --- a/modules/dnn/test/imagenet_cls_test_alexnet.py +++ b/modules/dnn/test/imagenet_cls_test_alexnet.py @@ -1,3 +1,4 @@ +from __future__ import print_function from abc import ABCMeta, abstractmethod import numpy as np import sys @@ -156,7 +157,7 @@ class DnnCaffeModel(Framework): class ClsAccEvaluation: - log = file + log = sys.stdout img_classes = {} batch_size = 0 @@ -198,26 +199,26 @@ class ClsAccEvaluation: fw_accuracy.append(100 * correct_answers[i] / float(samples_handled)) frameworks_out.append(out) inference_time[i] += end - start - print >> self.log, samples_handled, 'Accuracy for', frameworks[i].get_name() + ':', fw_accuracy[i] - print >> self.log, "Inference time, ms ", \ - frameworks[i].get_name(), inference_time[i] / samples_handled * 1000 + print(samples_handled, 'Accuracy for', frameworks[i].get_name() + ':', fw_accuracy[i], file=self.log) + print("Inference time, ms ", \ + frameworks[i].get_name(), inference_time[i] / samples_handled * 1000, file=self.log) for i in range(1, len(frameworks)): log_str = frameworks[0].get_name() + " vs " + frameworks[i].get_name() + ':' diff = np.abs(frameworks_out[0] - frameworks_out[i]) l1_diff = np.sum(diff) / diff.size - print >> self.log, samples_handled, "L1 difference", log_str, l1_diff + print(samples_handled, "L1 difference", log_str, l1_diff, file=self.log) blobs_l1_diff[i] += l1_diff blobs_l1_diff_count[i] += 1 if np.max(diff) > blobs_l_inf_diff[i]: blobs_l_inf_diff[i] = np.max(diff) - print >> self.log, samples_handled, "L_INF difference", log_str, blobs_l_inf_diff[i] + print(samples_handled, "L_INF difference", log_str, blobs_l_inf_diff[i], file=self.log) self.log.flush() for i in range(1, len(blobs_l1_diff)): log_str = frameworks[0].get_name() + " vs " + frameworks[i].get_name() + ':' - print >> self.log, 'Final l1 diff', log_str, blobs_l1_diff[i] / blobs_l1_diff_count[i] + print('Final l1 diff', log_str, blobs_l1_diff[i] / blobs_l1_diff_count[i], file=self.log) if __name__ == "__main__": parser = argparse.ArgumentParser() diff --git a/modules/dnn/test/pascal_semsegm_test_fcn.py b/modules/dnn/test/pascal_semsegm_test_fcn.py index 90fb050e0a..af4bc2aede 100644 --- a/modules/dnn/test/pascal_semsegm_test_fcn.py +++ b/modules/dnn/test/pascal_semsegm_test_fcn.py @@ -1,3 +1,4 @@ +from __future__ import print_function from abc import ABCMeta, abstractmethod import numpy as np import sys @@ -145,7 +146,7 @@ class PASCALDataFetch(DatasetImageFetch): class SemSegmEvaluation: - log = file + log = sys.stdout def __init__(self, log_path,): self.log = open(log_path, 'w') @@ -174,28 +175,28 @@ class SemSegmEvaluation: pix_acc, mean_acc, miou = get_metrics(conf_mats[i]) name = frameworks[i].get_name() - print >> self.log, samples_handled, 'Pixel accuracy, %s:' % name, 100 * pix_acc - print >> self.log, samples_handled, 'Mean accuracy, %s:' % name, 100 * mean_acc - print >> self.log, samples_handled, 'Mean IOU, %s:' % name, 100 * miou - print >> self.log, "Inference time, ms ", \ - frameworks[i].get_name(), inference_time[i] / samples_handled * 1000 + print(samples_handled, 'Pixel accuracy, %s:' % name, 100 * pix_acc, file=self.log) + print(samples_handled, 'Mean accuracy, %s:' % name, 100 * mean_acc, file=self.log) + print(samples_handled, 'Mean IOU, %s:' % name, 100 * miou, file=self.log) + print("Inference time, ms ", \ + frameworks[i].get_name(), inference_time[i] / samples_handled * 1000, file=self.log) for i in range(1, len(frameworks)): log_str = frameworks[0].get_name() + " vs " + frameworks[i].get_name() + ':' diff = np.abs(frameworks_out[0] - frameworks_out[i]) l1_diff = np.sum(diff) / diff.size - print >> self.log, samples_handled, "L1 difference", log_str, l1_diff + print(samples_handled, "L1 difference", log_str, l1_diff, file=self.log) blobs_l1_diff[i] += l1_diff blobs_l1_diff_count[i] += 1 if np.max(diff) > blobs_l_inf_diff[i]: blobs_l_inf_diff[i] = np.max(diff) - print >> self.log, samples_handled, "L_INF difference", log_str, blobs_l_inf_diff[i] + print(samples_handled, "L_INF difference", log_str, blobs_l_inf_diff[i], file=self.log) self.log.flush() for i in range(1, len(blobs_l1_diff)): log_str = frameworks[0].get_name() + " vs " + frameworks[i].get_name() + ':' - print >> self.log, 'Final l1 diff', log_str, blobs_l1_diff[i] / blobs_l1_diff_count[i] + print('Final l1 diff', log_str, blobs_l1_diff[i] / blobs_l1_diff_count[i], file=self.log) if __name__ == "__main__": parser = argparse.ArgumentParser() diff --git a/modules/ts/misc/table_formatter.py b/modules/ts/misc/table_formatter.py index 7cad7b4001..6069dea3b4 100755 --- a/modules/ts/misc/table_formatter.py +++ b/modules/ts/misc/table_formatter.py @@ -206,6 +206,8 @@ class table(object): cell.width = len(max(cell.text, key = lambda line: len(line))) def reformatTextValue(self, value): + if sys.version_info > (3,): # PY3 fix + unicode = str if isinstance(value, str): vstr = value elif isinstance(value, unicode): diff --git a/modules/ts/misc/testlog_parser.py b/modules/ts/misc/testlog_parser.py index f3d8546788..f52307238f 100755 --- a/modules/ts/misc/testlog_parser.py +++ b/modules/ts/misc/testlog_parser.py @@ -7,6 +7,10 @@ import os.path import sys from xml.dom.minidom import parse +if sys.version_info > (3,): + long = int + def cmp(a, b): return (a>b)-(a Date: Fri, 11 May 2018 13:52:15 +0300 Subject: [PATCH 2/2] cmake: added check_flake8 target --- CMakeLists.txt | 19 ++++++++++++++++++- cmake/FindFlake8.cmake | 27 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 cmake/FindFlake8.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index d50c92b954..f6a054702a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -338,6 +338,7 @@ OCV_OPTION(CV_DISABLE_OPTIMIZATION "Disable explicit optimized code (dispatch OCV_OPTION(CV_TRACE "Enable OpenCV code trace" ON) OCV_OPTION(ENABLE_PYLINT "Add target with Pylint checks" (${BUILD_DOCS} OR ${BUILD_EXAMPLES}) IF (NOT CMAKE_CROSSCOMPILING AND NOT APPLE_FRAMEWORK) ) +OCV_OPTION(ENABLE_FLAKE8 "Add target with Python flake8 checker" (${BUILD_DOCS} OR ${BUILD_EXAMPLES}) IF (NOT CMAKE_CROSSCOMPILING AND NOT APPLE_FRAMEWORK) ) if(ENABLE_IMPL_COLLECTION) add_definitions(-DCV_COLLECT_IMPL_DATA) @@ -645,9 +646,22 @@ if(BUILD_JAVA) endif() endif() -if(ENABLE_PYLINT) +if(ENABLE_PYLINT AND PYTHON_DEFAULT_AVAILABLE) include(cmake/OpenCVPylint.cmake) endif() +if(ENABLE_FLAKE8 AND PYTHON_DEFAULT_AVAILABLE) + find_package(Flake8 QUIET) + if(NOT FLAKE8_FOUND OR NOT FLAKE8_EXECUTABLE) + include("${CMAKE_CURRENT_LIST_DIR}/cmake/FindFlake8.cmake") + endif() + if(FLAKE8_FOUND) + add_custom_target(check_flake8 + COMMAND "${FLAKE8_EXECUTABLE}" . --count --select=E9,E901,E999,F821,F822,F823 --show-source --statistics --exclude='.git,__pycache__,*.config.py,svgfig.py' + WORKING_DIRECTORY "${OpenCV_SOURCE_DIR}" + COMMENT "Running flake8" + ) + endif() +endif() if(ANDROID AND ANDROID_EXECUTABLE AND ANT_EXECUTABLE AND (ANT_VERSION VERSION_GREATER 1.7) AND (ANDROID_TOOLS_Pkg_Revision GREATER 13)) @@ -1449,6 +1463,9 @@ status(" Python (for build):" PYTHON_DEFAULT_AVAILABLE THEN "${PYTHON_DEFAULT_ if(PYLINT_FOUND AND PYLINT_EXECUTABLE) status(" Pylint:" PYLINT_FOUND THEN "${PYLINT_EXECUTABLE} (ver: ${PYLINT_VERSION}, checks: ${PYLINT_TOTAL_TARGETS})" ELSE NO) endif() +if(FLAKE8_FOUND AND FLAKE8_EXECUTABLE) + status(" Flake8:" FLAKE8_FOUND THEN "${FLAKE8_EXECUTABLE} (ver: ${FLAKE8_VERSION})" ELSE NO) +endif() # ========================== java ========================== if(BUILD_JAVA OR BUILD_opencv_java) diff --git a/cmake/FindFlake8.cmake b/cmake/FindFlake8.cmake new file mode 100644 index 0000000000..b18225a011 --- /dev/null +++ b/cmake/FindFlake8.cmake @@ -0,0 +1,27 @@ +# - Find Flake8 +# Find the Flake8 executable and extract the version number +# +# OUTPUT Variables +# +# FLAKE8_FOUND +# True if the flake8 package was found +# FLAKE8_EXECUTABLE +# The flake8 executable location +# FLAKE8_VERSION +# A string denoting the version of flake8 that has been found + +find_host_program(FLAKE8_EXECUTABLE flake8 PATHS /usr/bin) + +if(FLAKE8_EXECUTABLE) + execute_process(COMMAND ${FLAKE8_EXECUTABLE} --version OUTPUT_VARIABLE FLAKE8_VERSION_RAW ERROR_QUIET) + if(FLAKE8_VERSION_RAW MATCHES "^([0-9\\.]+[0-9])") + set(FLAKE8_VERSION "${CMAKE_MATCH_1}") + else() + set(FLAKE8_VERSION "unknown") + endif() +endif() + +include(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(Flake8 DEFAULT_MSG FLAKE8_EXECUTABLE) + +mark_as_advanced(FLAKE8_EXECUTABLE FLAKE8_VERSION)