def run_kernel(src_code, image_src, block_size, args, nr_reg = 8, no_sequencer=True): ''' Run a kernel, this assumes that the output is in buffer #1 and output is unscaled float. ''' code = compile_code(src_code, args, block_size, nr_reg, no_sequencer) sim = Interpreter(code, image_src, block_size, 4, nr_reg) sim.run_kernel() return sim.gen_output_image(1, False, False, False)
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
def test_lid(): block_size = (2, 2) def test_code(code, block_size, args): yield Imm(code.r(7), 0) yield Lid(code.r(0), code.r(7)) yield Imm(code.r(6), 1) yield Lid(code.r(1), code.r(6)) yield Imm(code.r(6), block_size[0]) yield Mul(code.r(0), code.r(0), code.r(6)) yield Add(code.r(0), code.r(1), code.r(0)) yield MemW(code.r(0), code.r(0)) code = Code() code.set_generator(test_code, block_size) print str(code) image = zeros(block_size[1], block_size[0]) sim = Interpreter(code, image, block_size) sim.run_kernel() out_image = sim.gen_output_image(0, False) width, height = block_size for i in xrange(height): for j in xrange(width): assert out_image[i][j] == (i*width + j)
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_test(image, position, shape, block_size): code = Code() out_reg = code.alloc_reg() def code_gen(code, block_size, args): return gen_code.gen_integral_sum(code, out_reg, position, shape, ptr, block_size) 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(str(out_reg))
def run_test(image, coeff): code = Code() block_size = (16, 16) in_ptr = 0 out_ptr = block_size[0]*block_size[1] args = {} def codegen(code, block_size, args): return map_neighborhood_to_pixel(code, in_ptr, out_ptr, coeff, pixel_op, args, block_size) code.set_generator(codegen, block_size, args) sim = Interpreter(code, image, block_size) sim.run() return sim.gen_output_image(1)
def run_test(image, args, block_size): im_size = len(image[0]), len(image) bwidth, bheight = block_size assert(im_size == block_size) # only one pe code = Code() code.set_generator(codegen, block_size, args) sim = Interpreter(code, image, block_size) sim.run() output = sim.gen_output_image(1, False) return output
def run_test(image, offset): code = Code() block_size = (16, 16) in_ptr = 0 out_ptr = block_size[0]*block_size[1] args = {'offset' : offset} def codegen(code, block_size, args): return map_image_to_pixel(code, in_ptr, out_ptr, pixel_op, args, block_size) code.set_generator(codegen, block_size, args) sim = Interpreter(code, image, block_size) sim.run() return sim.gen_output_image(1)
def run_test(images, th, alpha, block_size): image0 = images[0] im_size = len(image0) bwidth, bheight = block_size assert(im_size == bwidth * bheight) # only one pe code = Code() code.set_generator(gen_bbs, block_size, {'th':th, 'alpha':alpha}) output = [] sim = None for im in images: # interpreter expects 2D array im_tr = [[im[i*width + j] for j in xrange(bwidth)] for i in xrange(bheight)] if not sim: sim = Interpreter(code, im_tr, block_size) else: # restart code gen sim.reset() # set new image sim.set_src_image(im_tr) sim.run() im_out = sim.gen_output_image(1) # convert to 1D vector im_out_1D = [] for row in im_out: for v in row: im_out_1D.append(v) output.append(im_out_1D) return output
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(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 test_sleep_wakeup(): ''' Test sleep and wakeup opcodes. ''' block_size = (2, 2) def test_code(code, block_size, args): yield Imm(code.r(0), 1) yield Sleep() yield Imm(code.r(0), 2) yield WakeUp() code = Code() code.set_generator(test_code, block_size) print str(code) image = zeros(block_size[1], block_size[0]) sim = Interpreter(code, image, block_size) sim.run_kernel() # second imm should not be executed assert sim.procs[0][0].get_reg_by_name('r0') == 1
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
def run_test(image, cascade): block_size = (64, 64) print 'XXX histogram equalisation is not implemented yet, use violajones impl' print ' before executing simulator' image = reference.equalizeHist(image) args = {'haar_classifier':cascade} # now execute the codegen code = Code() code.set_generator(gen_code.gen_detect_faces_opt, block_size, args) #print '# instructions: %i'%(code.instr_size()) sim = Interpreter(code, image, block_size, 4) sim.run() detections_pixmap = sim.gen_output_image(1) # result is saved in first buffer # convert the number of rejections in the stages to detections detections = gen_code.convert_pixelmap_to_detections(detections_pixmap, cascade.size) return detections
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)
def test_sleep_wakeup2(): ''' Test sleep and wakeup opcodes. ''' block_size = (2, 2) def test_code_sleep(code, block_size, args): yield Sleep() code = Code() code.set_generator(test_code_sleep, block_size) image = zeros(block_size[1], block_size[0]) sim = Interpreter(code, image, block_size) # check if attribute is correct assert sim.procs[0][0].is_powerdown() == False sim.run_kernel() assert sim.procs[0][0].is_powerdown() == True # check if attribute is correct after wakeup def test_code_sleep_wakeup(code, block_size, args): yield Sleep() yield WakeUp() code2 = Code() code2.set_generator(test_code_sleep_wakeup, block_size) sim2 = Interpreter(code2, image, block_size) assert sim2.procs[0][0].is_powerdown() == False sim2.run_kernel() assert sim2.procs[0][0].is_powerdown() == False
def run_test(codegen_function, image, cascade, block_size): print 'running %s'%codegen_function.__name__ print 'XXX histogram equalisation is not implemented yet, use violajones impl' print ' before executing simulator' image = reference.equalizeHist(image) width, height = block_size pe_dim = (len(image[0])//width, len(image)//height) args = {'haar_classifier': cascade, 'pe_dim':pe_dim} # now execute the codegen code = Code() code.set_generator(optimiser_wrapper(codegen_function), block_size, args) sim = Interpreter(code, image, block_size, 4) sim.run() detections_pixmap = sim.gen_output_image(1) # result is saved in first buffer # convert the number of rejections in the stages to detections detections = gen_code.convert_pixelmap_to_detections(detections_pixmap, cascade.size) return detections
def run_test(position, integral_test, sq_integral_test, haar_size, block_size): integral_ptr = 0 sq_integral_ptr = block_size[0]*block_size[1] code = Code() out_reg = code.alloc_reg() def code_gen(code, block_size, args): return gen_code.gen_calc_variance(code, out_reg, position, integral_ptr, sq_integral_ptr, haar_size, block_size) code.set_generator(optimiser_wrapper(code_gen), block_size) sim = Interpreter(code, integral_test, block_size) # hack: in order to avoid calculating integral images, inject random values into the sq_integral buffer # this is easy since their is only a single PE for i, row in enumerate(sq_integral_test): for j, v in enumerate(row): sim.procs[0][0].memory.set(sq_integral_ptr + len(row)*i+j, v) sim.run() pe = sim.procs[0][0] # extract value return (1./(pe.get_reg_by_name(str(out_reg)))), pe
def run_codegen_function(test_image, code_gen, block_size, args, buffer_sel = 1, **kwargs): image2buffer = kwargs['image2buffer'] if 'image2buffer' in kwargs else {} im_size = len(test_image[0]), len(test_image) pe_dim = [s//b for s,b in zip(im_size, block_size)] # fill this in for all functions args['pe_dim'] = pe_dim code = Code() code.set_generator(code_gen, block_size, args) sim = Interpreter(code, test_image, block_size) for buffer_nr, image in image2buffer.iteritems(): sim.set_src_image(image, buffer_nr) sim.run() return sim.gen_output_image(buffer_sel, False, False, True), sim
def step(self, instr): for a in self.analysis: a.process(instr) Interpreter.step(self, instr)
def __init__(self, code, image, block_size, pe_nr_buffer = 4, pe_nr_reg = NR_REG): Interpreter.__init__(self, code, image, block_size, pe_nr_buffer, pe_nr_reg) self.analysis = []