コード例 #1
0
def test_full_integral_image_correctness():
	''' Test generated full integral image correctness,
	    note that this relies on the corectness of interpreter and reference.py '''

	block_size = (20, 20)
	size = tuple(x*3 for x in block_size)

	# generate random test image
	test_image = [[float(random.randint(0, 255)) for i in xrange(size[0])] for j in xrange(size[1])]

	# reference implementation
	integral_ref = reference.gen_integral_image(test_image)
	sq_integral_ref = reference.gen_integral_squared_image(test_image)

	# pointer config
	buffer_size = block_size[0]*block_size[1]
	src_ptr = 0
	integral_ptr = buffer_size
	sq_integral_ptr = 2*buffer_size

	# set up interpreter for integral image calculation
	pe_dim = [s//b for s, b in zip(size, block_size)]
	def code_gen(code, block_size, args):
		return gen_code.gen_full_integral_image(code, src_ptr, integral_ptr, sq_integral_ptr, pe_dim, block_size)
	code = Code()
	code.set_generator(optimiser_wrapper(code_gen), block_size)

	sim = Interpreter(code, test_image, block_size)
	sim.run()

	# get result of simulator with scaling, truncation turned off and float output
	integral_test = sim.gen_output_image(1, False, False, True)
	sq_integral_test = sim.gen_output_image(2, False, False, True)

	# comparison of reference with blip sim
	integral_err = compare_images(integral_ref, integral_test)
	sq_integral_err = compare_images(sq_integral_ref, sq_integral_test)

	err_eps = 0.001
	if not ((integral_err < err_eps) and (sq_integral_err < err_eps)):
		print 'integral comp:', integral_err
		print 'squared integral comp:', sq_integral_err
		
		print 'rendering instruction stream to file, can take a while'
		try:
			f = open('unoptimised_full_integral_image_trace.txt', 'w')
			def tag_str(instr): return ', '.join(instr.tag) if hasattr(instr, 'tag') else ''
			f.write('\n'.join(str(x).ljust(40) + ' tags: ' + tag_str(x) for x in code_gen(Code())))
			f.close()

			optim_gen = optimiser_wrapper(code_gen, block_size, {})
			f = open('bad_full_integral_image_trace.txt', 'w')
			def tag_str(instr): return ', '.join(instr.tag) if hasattr(instr, 'tag') else ''
			f.write('\n'.join(str(x).ljust(40) + ' tags: ' + tag_str(x) for x in optim_gen(Code())))
			f.close()
		except Exception, e:
			print 'could render instruction stream to file'
			print 'err: ' + str(e)
		
		assert False
コード例 #2
0
def gen_integral_image_correctness():
	''' test if generated integral image is correct, 
	    note that this relies on the corectness of interpreter and reference.py '''

#		size = (120, 80)
#		block_size = (40, 40)
	size = (80, 80)
	block_size = size 

	# generate random test image
	test_image = [[float(random.randint(0, 255)) for i in xrange(size[0])] for j in xrange(size[1])]

	# reference implementation
	integral_ref = reference.gen_integral_image(test_image)
	sq_integral_ref = reference.gen_integral_squared_image(test_image)
	
	# pointer config
	buffer_size = block_size[0]*block_size[1]
	src_ptr = 0
	integral_ptr = buffer_size
	sq_integral_ptr = 2*buffer_size

	# set up interpreter for integral image calculation
	def code_gen(code, block_size, args):
		return gen_code.gen_integral_image(code, src_ptr, integral_ptr, sq_integral_ptr, block_size)
	code = Code()
	code.set_generator(optimiser_wrapper(code_gen), block_size)

	sim = Interpreter(code, test_image, block_size)
	sim.run()

	# get result of simulator with scaling, truncation turned off and float output
	integral_test = sim.gen_output_image(1, False, False, True)
	sq_integral_test = sim.gen_output_image(2, False, False, True)

	# comparison of reference with blip sim
	integral_err = compare_images(integral_ref, integral_test)
	sq_integral_err = compare_images(sq_integral_ref, sq_integral_test)

	err_eps = 0.001
	if not ((integral_err < err_eps) and (sq_integral_err < err_eps)):
		print 'integral comp:', integral_err
		print 'squared integral comp:', sq_integral_err 
		assert False
コード例 #3
0
def test_gen_fullintegral_sum2_2():
	block_size = (12,12)
	size = tuple(b*2 for  b in block_size)
	image = [[float(random.randint(0, 255)) for i in xrange(size[0])] for  j in xrange(size[1])]
	test_image = reference.gen_integral_image(image)
	ptr = 0

	def run_test(image, position, shape, ptr, block_size):
		px, py = position
		x, y, w, h = shape
		xx = px + x
		yy = py + y
		points =  ((xx, yy), (xx+w-1, yy), (xx, yy+h-1), (xx+w-1, yy+h-1))

		def code_gen(code, block_size, args):
			return gen_code.gen_fullintegral_sum2_2(code, code.r(4), ptr, points, block_size)
		code = Code()
		code.set_generator(optimiser_wrapper(code_gen), block_size)

		sim = Interpreter(code, image, block_size)
		sim.run()
		# extract value
		return sim.procs[0][0].get_reg_by_name('r4')

	def run_ref(image, position, shape):
		px, py = position
		x, y, w, h = shape
		xx = x + px
		yy = y + py
		return reference.calc_integral_value(image, xx, yy, w-1, h-1)
	shape = (0, 0, 6, 6)
	# cover the 4 position for which this function is valid
	positions = [(8, 0), (0, 8), (8, 14), (14, 8)]
	failed = False
	for position in positions:
		test_res = run_test(test_image, position, shape, ptr, block_size)
		ref_res = run_ref(test_image, position, shape)
		if test_res != ref_res:
			print 'position', position, 'failed, ref = ', ref_res, 'res = ', test_res
			failed = True
	assert not failed
コード例 #4
0
def test_full_integral_image_correctness():
	''' Test generated full integral image correctness,
	    note that this relies on the correctness of interpreter and reference.py '''

	block_size = (20, 20)
	size = tuple(x*3 for x in block_size)

	# generate random test image
	test_image = [[float(random.randint(0, 255)) for i in xrange(size[0])] for j in xrange(size[1])]

	# reference implementation
	integral_ref = reference.gen_integral_image(test_image)
	sq_integral_ref = reference.gen_integral_squared_image(test_image)

	# pointer config
	buffer_size = block_size[0]*block_size[1]
	src_ptr = 0
	integral_ptr = buffer_size
	sq_integral_ptr = 2*buffer_size

	# set up interpreter for integral image calculation
	pe_dim = [s//b for s, b in zip(size, block_size)]
	code = Code()
	def code_gen(code, block_size, args): 
		return gen_code.gen_full_integral_image(code, src_ptr, integral_ptr, sq_integral_ptr, pe_dim, block_size)
	code.set_generator(code_gen, block_size)

	sim = Interpreter(code, test_image, block_size)
	sim.run()

	# get result of simulator with scaling, truncation turned off and float output
	integral_test = sim.gen_output_image(1, False, False, True)
	sq_integral_test = sim.gen_output_image(2, False, False, True)

	# comparison of reference with blip sim
	integral_err = compare_images(integral_ref, integral_test)
	sq_integral_err = compare_images(sq_integral_ref, sq_integral_test)

	err_eps = 0.001
	assert (integral_err < err_eps) and (sq_integral_err < err_eps)
コード例 #5
0
def test_fullintegral_sum():
	block_size = (12,12)
	size = tuple(b*3 for  b in block_size)
	image = [[float(random.randint(0, 255)) for i in xrange(size[0])] for  j in xrange(size[1])]
	test_image = reference.gen_integral_image(image)

	def run_test(image, position, shape, block_size):
		def code_gen(code, block_size, args):
			return gen_code.gen_fullintegral_sum(code, code.r(4), position, shape, ptr, block_size)
		code = Code()
		code.set_generator(optimiser_wrapper(code_gen), block_size)

		sim = Interpreter(code, image, block_size)
		sim.run()
		# extract value
		return sim.procs[0][0].get_reg_by_name('r4')

	def run_ref(image, position, shape):
		px, py = position
		x, y, w, h = shape
		xx = x + px
		yy = y + py
		return reference.calc_integral_value(image, xx, yy, w-1, h-1)
	
	# set up interpreter for integral image calculation
	ptr = 0
	max_shape_width = 6
	max_shape_height = 6
	success = True
	for i in xrange(block_size[1]):
		for j in xrange(block_size[0]):
			position = (j, i)
			shape = (0, 0, random.randint(1, max_shape_width), random.randint(1, max_shape_height))
			test_res = run_test(test_image, position, shape, block_size)
			ref_res = run_ref(test_image, position, shape)
			if test_res != ref_res:
				print 'error on', position, 'ref = ', ref_res, 'res = ', test_res
				success = False
	assert success