From 78f205ffa54668d9a1f412172cacbf2fe76a5b72 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Fri, 11 May 2018 13:29:28 +0300 Subject: [PATCH] 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