From cc95822cd644307c314d3ebde46c596868f55db3 Mon Sep 17 00:00:00 2001 From: Wook Song Date: Fri, 27 Sep 2019 14:09:02 +0900 Subject: [PATCH] [Tests] Make tool scripts written in python be compatible with python3 There are several platforms which do not support python2.7 by default anymore. To prepare this issue, this patch revises all python scripts in tests to be also compatible with python3. Signed-off-by: Wook Song --- tests/gen24bBMP.py | 32 +++++++++++++++----- .../generateGoldenTestResult.py | 16 +++++----- tests/nnstreamer_filter_caffe2/checkLabel.py | 27 ++++++++++++----- .../nnstreamer_filter_custom/checkScaledTensor.py | 6 ++-- .../nnstreamer_filter_python/checkScaledTensor.py | 6 ++-- tests/nnstreamer_filter_pytorch/checkLabel.py | 13 ++++++--- tests/nnstreamer_filter_tensorflow/checkLabel.py | 34 ++++++++++++++-------- .../checkLabel.py | 9 ++++-- tests/nnstreamer_repo_rnn/generateTestCase.py | 17 +++++++---- tests/transform_dimchg/checkResult.py | 6 ++-- tests/transform_stand/checkResult.py | 6 ++-- tests/transform_typecast/checkResult.py | 6 ++-- 12 files changed, 116 insertions(+), 62 deletions(-) diff --git a/tests/gen24bBMP.py b/tests/gen24bBMP.py index efc9417..b9d9c0f 100644 --- a/tests/gen24bBMP.py +++ b/tests/gen24bBMP.py @@ -14,6 +14,24 @@ from struct import pack import random import sys +## +# @brief Convert given data to bytes +# @param[in] data The data to be converted to bytes array +# @return bytes converted from the data + +def convert_to_bytes(data): + """ + Convert given data to bytes + + @param data: The data to be converted to bytes + @rtype : bytes + @return : bytes converted from the data + """ + + if isinstance(data, bytes): + return data + else: + return pack(" import sys +import os import struct import string +sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__)))) +from gen24bBMP import convert_to_bytes + +if sys.version_info < (3,): + range = xrange def readbyte (filename): - F = open(filename, 'r') - readbyte = F.read() - F.close() - return readbyte + F = open(filename, 'rb') + readbyte = F.read() + F.close() + return readbyte + bytearr = readbyte(sys.argv[1]) softmax = [] -for i in xrange(10): - byte = bytearr[i * 4] + bytearr[i * 4 + 1] + bytearr[i * 4 + 2] + bytearr[i * 4 + 3] - softmax.append(struct.unpack('f', byte)) +for i in range(10): + byte = b'' + byte += convert_to_bytes(bytearr[i * 4]) + byte += convert_to_bytes(bytearr[i * 4 + 1]) + byte += convert_to_bytes(bytearr[i * 4 + 2]) + byte += convert_to_bytes(bytearr[i * 4 + 3]) + softmax.append(struct.unpack('f', byte)) pred = softmax.index(max(softmax)) -answer = string.atoi(sys.argv[2].strip()) +answer = int(sys.argv[2].strip()) exit(pred != answer) diff --git a/tests/nnstreamer_filter_custom/checkScaledTensor.py b/tests/nnstreamer_filter_custom/checkScaledTensor.py index 8e563a0..2383435 100644 --- a/tests/nnstreamer_filter_custom/checkScaledTensor.py +++ b/tests/nnstreamer_filter_custom/checkScaledTensor.py @@ -30,8 +30,8 @@ def compare (data1, width1, height1, data2, width2, height2, innerdim): for y in range(0, height2): for x in range(0, width2): for c in range(0, innerdim): - ix = x * width1 / width2 - iy = y * height1 / height2 + ix = x * width1 // width2 + iy = y * height1 // height2 if data1[count + c + ix * innerdim + iy * width1 * innerdim] != data2[count2 + c + x * innerdim + y * width2 * innerdim]: print("At "+str(x)+","+str(y)) return 5 @@ -45,7 +45,7 @@ def compare (data1, width1, height1, data2, width2, height2, innerdim): return 0 def readfile (filename): - F = open(filename, 'r') + F = open(filename, 'rb') readfile = F.read() F.close() return readfile diff --git a/tests/nnstreamer_filter_python/checkScaledTensor.py b/tests/nnstreamer_filter_python/checkScaledTensor.py index 659b52c..40de0ce 100644 --- a/tests/nnstreamer_filter_python/checkScaledTensor.py +++ b/tests/nnstreamer_filter_python/checkScaledTensor.py @@ -31,8 +31,8 @@ def compare (data1, width1, height1, data2, width2, height2, innerdim): for y in range(0, height2): for x in range(0, width2): for c in range(0, innerdim): - ix = x * width1 / width2 - iy = y * height1 / height2 + ix = x * width1 // width2 + iy = y * height1 // height2 if data1[count + c + ix * innerdim + iy * width1 * innerdim] != data2[count2 + c + x * innerdim + y * width2 * innerdim]: print("At "+str(x)+","+str(y)) return 5 @@ -47,7 +47,7 @@ def compare (data1, width1, height1, data2, width2, height2, innerdim): ## @brief Read file and return its content def readfile (filename): - F = open(filename, 'r') + F = open(filename, 'rb') readfile = F.read() F.close() return readfile diff --git a/tests/nnstreamer_filter_pytorch/checkLabel.py b/tests/nnstreamer_filter_pytorch/checkLabel.py index 4584e66..c5ceafa 100644 --- a/tests/nnstreamer_filter_pytorch/checkLabel.py +++ b/tests/nnstreamer_filter_pytorch/checkLabel.py @@ -9,16 +9,21 @@ # @author Parichay Kapoor import sys +import os +import struct +import string +sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__)))) +from gen24bBMP import convert_to_bytes # Read the bytes from the file def readbyte (filename): - with open(filename, 'r') as f: - readbyte = f.read() - return readbyte + with open(filename, 'rb') as f: + readbyte = f.read() + return readbyte # Verify that the output of test case verifies the filename of the input onehot = readbyte(sys.argv[1]) -onehot = [str(ord(x)) for x in onehot] +onehot = [convert_to_bytes(x) for x in onehot] idx = onehot.index(max(onehot)) label = str(idx) diff --git a/tests/nnstreamer_filter_tensorflow/checkLabel.py b/tests/nnstreamer_filter_tensorflow/checkLabel.py index 08bd426..7f4cd50 100644 --- a/tests/nnstreamer_filter_tensorflow/checkLabel.py +++ b/tests/nnstreamer_filter_tensorflow/checkLabel.py @@ -9,28 +9,38 @@ # @author HyoungJoo Ahn import sys +import os import struct import string +sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__)))) +from gen24bBMP import convert_to_bytes + +if sys.version_info < (3,): + range = xrange def readbyte (filename): - F = open(filename, 'r') - readbyte = F.read() - F.close() - return readbyte + F = open(filename, 'rb') + readbyte = F.read() + F.close() + return readbyte def readlabel (filename): - F = open(filename, 'r') - line = F.readlines() - F.close() - return line + F = open(filename, 'r') + line = F.readlines() + F.close() + return line bytearr = readbyte(sys.argv[1]) softmax = [] -for i in xrange(10): - byte = bytearr[i * 4] + bytearr[i * 4 + 1] + bytearr[i * 4 + 2] + bytearr[i * 4 + 3] - softmax.append(struct.unpack('f', byte)) +for i in range(10): + byte = b'' + byte += convert_to_bytes(bytearr[i * 4]) + byte += convert_to_bytes(bytearr[i * 4 + 1]) + byte += convert_to_bytes(bytearr[i * 4 + 2]) + byte += convert_to_bytes(bytearr[i * 4 + 3]) + softmax.append(struct.unpack('f', byte)) pred = softmax.index(max(softmax)) -answer = string.atoi(sys.argv[2].strip()) +answer = int(sys.argv[2].strip()) exit(pred != answer) diff --git a/tests/nnstreamer_filter_tensorflow_lite/checkLabel.py b/tests/nnstreamer_filter_tensorflow_lite/checkLabel.py index 9499791..e6de4a4 100644 --- a/tests/nnstreamer_filter_tensorflow_lite/checkLabel.py +++ b/tests/nnstreamer_filter_tensorflow_lite/checkLabel.py @@ -9,10 +9,15 @@ # @author HyoungJoo Ahn import sys +import os +import struct +import string +sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__)))) +from gen24bBMP import convert_to_bytes def readbyte (filename): - F = open(filename, 'r') + F = open(filename, 'rb') readbyte = F.read() F.close() return readbyte @@ -25,7 +30,7 @@ def readlabel (filename): return line onehot = readbyte(sys.argv[1]) -onehot = [str(ord(x)) for x in onehot] +onehot = [convert_to_bytes(x) for x in onehot] idx = onehot.index(max(onehot)) label_list = readlabel(sys.argv[2]) diff --git a/tests/nnstreamer_repo_rnn/generateTestCase.py b/tests/nnstreamer_repo_rnn/generateTestCase.py index b3bd865..724fb2b 100644 --- a/tests/nnstreamer_repo_rnn/generateTestCase.py +++ b/tests/nnstreamer_repo_rnn/generateTestCase.py @@ -17,6 +17,10 @@ from __future__ import print_function import sys import os import array as arr +import struct +import string +sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__)))) +from gen24bBMP import convert_to_bytes def location(c, w, h): return c+4*(w+4*h) @@ -46,31 +50,32 @@ def genFrame(seq, out): for h in range(0,4): w=0 for i in range(0,4): - out[location(i,w,h)]=frame[location(i,w,h)] + out[location(i,w,h)] = frame[location(i,w,h)] for w in range(1,3): for c in range(0,4): sum = frame[location(0,w,h)] sum += out[location(0,w,h)] - out[location(0,w,h)] = sum/2 + out[location(0,w,h)] = sum // 2 w=3 for i in range(0,4): out[location(i,w,h)] = out[location(i,w,h)] - return frame.tostring(), out + return frame, out filename = "video_4x4xBGRx.xraw" -f = open(filename, "w") +f = open(filename, "wb") out = arr.array('B',[0]*64) for seq in range(0, 10): outfilename = "rnn.golden" - string, out=genFrame(seq,out) + frame, out = genFrame(seq,out) + if(seq == 9): with open(outfilename,'wb') as file: file.write(out.tostring()) - f.write(string) + f.write(frame) f.close() diff --git a/tests/transform_dimchg/checkResult.py b/tests/transform_dimchg/checkResult.py index 5ee10ac..9cf036d 100644 --- a/tests/transform_dimchg/checkResult.py +++ b/tests/transform_dimchg/checkResult.py @@ -18,10 +18,10 @@ import sys def testDimchgFirstDimGoHigher(dataA, dataB, dim1, repeatblocksize, elementsize): if (len(dataA) != len(dataB)): return 1 - loop = len(dataA) / repeatblocksize + loop = len(dataA) // repeatblocksize if ((loop * repeatblocksize) != len(dataA)): return 2 - ncpy = repeatblocksize / dim1 / elementsize + ncpy = repeatblocksize // dim1 // elementsize if ((ncpy * dim1 * elementsize) != repeatblocksize): return 3 for x in range(0, loop): @@ -33,7 +33,7 @@ def testDimchgFirstDimGoHigher(dataA, dataB, dim1, repeatblocksize, elementsize) return 4 def readfile (filename): - F = open(filename, 'r') + F = open(filename, 'rb') readfile = F.read() F.close return readfile diff --git a/tests/transform_stand/checkResult.py b/tests/transform_stand/checkResult.py index 9c24176..06bbc89 100644 --- a/tests/transform_stand/checkResult.py +++ b/tests/transform_stand/checkResult.py @@ -22,8 +22,8 @@ def testStandardization (fna, fnb, typeasize, typebsize,typeapack, typebpack): if (0 < (lena % typeasize)) or (0 < (lenb % typebsize)): return 10 - num = lena / typeasize - if num != (lenb / typebsize): + num = lena // typeasize + if num != (lenb // typebsize): return 11 limitb = 2 ** (8 * typebsize) maskb = limitb - 1 @@ -36,7 +36,7 @@ def testStandardization (fna, fnb, typeasize, typebsize,typeapack, typebpack): return 0 def readfile (filename): - F = open(filename, 'r') + F = open(filename, 'rb') readfile = F.read() F.close return readfile diff --git a/tests/transform_typecast/checkResult.py b/tests/transform_typecast/checkResult.py index 441b2a5..70a9834 100644 --- a/tests/transform_typecast/checkResult.py +++ b/tests/transform_typecast/checkResult.py @@ -22,8 +22,8 @@ def testTypeCast (fna, fnb, typea, typeasize, typeapack, typeb, typebsize, typeb if (0 < (lena % typeasize)) or (0 < (lenb % typebsize)): return 10 - num = lena / typeasize - if num != (lenb / typebsize): + num = lena // typeasize + if num != (lenb // typebsize): return 11 limitb = 2 ** (8 * typebsize) maskb = limitb - 1 @@ -50,7 +50,7 @@ def testTypeCast (fna, fnb, typea, typeasize, typeapack, typeb, typebsize, typeb return 0 def readfile (filename): - F = open(filename, 'r') + F = open(filename, 'rb') readfile = F.read() F.close return readfile -- 2.7.4