def createCharacter(self): chrs = dict() for i in range(NUM_LETTERS): chrs[i] = { '_posx': None } chrs[i] = { '_posy': None } chrs[i] = { '_size': None } chrs[i] = { '_id_directive': None } chrs[i] = { '_blur': None } (chrs[i]['_posx'], tmp) = self.RIPL.assume( "posx" + str(i), lisp_parser.parse("(uniform-discrete 0 150)")) #0 -140 captcha (chrs[i]['_posy'], tmp) = self.RIPL.assume( "posy" + str(i), lisp_parser.parse( "(uniform-discrete 0 150)")) #0 - 100 captcha #56-64 ocr (chrs[i]['_size'], tmp) = self.RIPL.assume( "size" + str(i), lisp_parser.parse("(uniform-discrete 30 70)")) #30-70 captcha (chrs[i]['_id_directive'], tmp) = self.RIPL.assume( "id" + str(i), lisp_parser.parse("(uniform-discrete 0 2)")) #0 -2 (chrs[i]['_blur'], tmp) = self.RIPL.assume( "blur" + str(i), lisp_parser.parse("(uniform-continuous 0.0 1.0)")) # 0 - 10 return chrs
def createCharacter(self): chrs = dict() for i in range(NUM_LETTERS): chrs[i]={'_posx':None};chrs[i]={'_posy':None};chrs[i]={'_size':None};chrs[i]={'_id_directive':None};chrs[i]={'_blur':None}; (chrs[i]['_posx'], tmp) = self.RIPL.assume("posx"+str(i), lisp_parser.parse("(uniform-discrete 0 150)")) #0 -140 captcha (chrs[i]['_posy'], tmp) = self.RIPL.assume("posy"+str(i), lisp_parser.parse("(uniform-discrete 0 150)")) #0 - 100 captcha #56-64 ocr (chrs[i]['_size'], tmp) = self.RIPL.assume("size"+str(i), lisp_parser.parse("(uniform-discrete 30 70)")) #30-70 captcha (chrs[i]['_id_directive'], tmp) = self.RIPL.assume("id"+str(i), lisp_parser.parse("(uniform-discrete 0 2)")) #0 -2 (chrs[i]['_blur'], tmp) = self.RIPL.assume("blur"+str(i), lisp_parser.parse("(uniform-continuous 0.0 1.0)")) # 0 - 10 return chrs
def explore_Chain_LoadProgram(self): MyRIPL = self.RIPL self.LoadProgram(explore_Joint=1) MyRIPL.observe( lisp_parser.parse( "(noisy-image-compare test-image rendered-image pflip)"), "true")
def update(self, s): ast = parse(s) if ast == self.ast: return print('ast changed!') new_fun_asts = parsed_funs(ast) new, removed, modified = self.diff_funs(self.function_asts, new_fun_asts) self.function_asts = new_fun_asts self.ast = ast if modified: print('ast modified! changed function %s' % (modified, )) if new or removed or len(modified) > 1: raise ValueError("can't cope with that change yet: new:%r removed:%r modified:%r" % (new, removed, modified)) if modified: (name, ) = modified old = self.funs[name] function = Function(name=old.name, params=new_fun_asts[name][2:-1], ast=new_fun_asts[name][-1], env=old.env, funs=old.funs) self.funs[name] = function if name not in self.funs.snapshots: return snapshot, t = self.funs.snapshots[name] print('restoring snapshot from %s' % (t, )) self.state = snapshot self.funs.set_eval_tree(self.state) else: # must have been in top level expression self.state = self.orig_eval self.funs.set_eval_tree(self.state)
def predictCharacteres(self): chrs = dict() for i in range(MAX_LETTERS): chrs[i]={'posx':None};chrs[i]={'posy':None};chrs[i]={'size_x':None}; chrs[i]={'size_y':None}; chrs[i]={'rotate_z':None}; chrs[i]={'id':None};chrs[i]={'blur':None}; chrs[i]={'present':None}; (chrs[i]['posx'], _) = self.RIPL.predict(lisp_parser.parse('(posx '+str(i)+')')) (chrs[i]['posy'], _) = self.RIPL.predict(lisp_parser.parse('(posy '+str(i)+')')) (chrs[i]['size_x'], _) = self.RIPL.predict(lisp_parser.parse('(size_x '+str(i)+')')) (chrs[i]['size_y'], _) = self.RIPL.predict(lisp_parser.parse('(size_y '+str(i)+')')) (chrs[i]['rotate_z'], _) = self.RIPL.predict(lisp_parser.parse('(rotate_z '+str(i)+')')) (chrs[i]['id'], _) = self.RIPL.predict(lisp_parser.parse('(id '+str(i)+')')) #(chrs[i]['blur'], _) = self.RIPL.predict(lisp_parser.parse('(blur '+str(i)+')')) (chrs[i]['blur'], _) = self.RIPL.predict(lisp_parser.parse('blur')) (chrs[i]['present'], _) = self.RIPL.predict(lisp_parser.parse('(letter-present '+str(i)+')')) return chrs
def predictCharacteres(self): chrs = dict() for i in range(MAX_LETTERS): chrs[i] = { 'posx': None } chrs[i] = { 'posy': None } chrs[i] = { 'size_x': None } chrs[i] = { 'size_y': None } chrs[i] = { 'rotate_z': None } chrs[i] = { 'id': None } chrs[i] = { 'blur': None } chrs[i] = { 'present': None } (chrs[i]['posx'], _) = self.RIPL.predict(lisp_parser.parse('(posx ' + str(i) + ')')) (chrs[i]['posy'], _) = self.RIPL.predict(lisp_parser.parse('(posy ' + str(i) + ')')) (chrs[i]['size_x'], _) = self.RIPL.predict( lisp_parser.parse('(size_x ' + str(i) + ')')) (chrs[i]['size_y'], _) = self.RIPL.predict( lisp_parser.parse('(size_y ' + str(i) + ')')) (chrs[i]['rotate_z'], _) = self.RIPL.predict( lisp_parser.parse('(rotate_z ' + str(i) + ')')) (chrs[i]['id'], _) = self.RIPL.predict(lisp_parser.parse('(id ' + str(i) + ')')) (chrs[i]['blur'], _) = self.RIPL.predict(lisp_parser.parse('(blur ' + str(i) + ')')) #(chrs[i]['blur'], _) = self.RIPL.predict(lisp_parser.parse('blur')) (chrs[i]['present'], _) = self.RIPL.predict( lisp_parser.parse('(letter-present ' + str(i) + ')')) return chrs
def __init__(self, s, env=None, funs=None): self.ast = parse(s) self.function_asts = parsed_funs(self.ast) self.done = False self.i = 0 if env is None: env = [builtins, {}] if funs is None: funs = GlobalFunctions() self.funs = funs self.state = Eval(self.ast, env, funs) funs.set_eval_tree(self.state) self.orig_eval = copy.deepcopy(self.state)
def run(s, env=None, funs=None): ast = parse(s) if env is None: env = [builtins, {}] if funs is None: funs = {} work = trampoline(eval(ast, env, funs)) while True: try: next(work) except StopIteration as e: if len(e.args) == 0: return None else: return e.args[0]
def LoadProgram(self): MyRIPL = self.RIPL MyRIPL.clear() # To delete previous sessions data. chrs = self.createCharacter() (_alpha,tmp) = MyRIPL.assume("alpha", lisp_parser.parse("(uniform-continuous 0.01 8)")) (_pflip,tmp)= MyRIPL.assume("pflip", lisp_parser.parse("(uniform-continuous 0.0 1.0)")) MyRIPL.assume("LOAD-IMAGE", lisp_parser.parse("1")) MyRIPL.assume("RENDER-IMAGE", lisp_parser.parse("10")) MyRIPL.assume("NOISY-COMP", lisp_parser.parse("3")) MyRIPL.assume("load-image", lisp_parser.parse("(load-remote-xrp 4444 LOAD-IMAGE)")) MyRIPL.assume("render-image", lisp_parser.parse("(load-remote-xrp 4444 RENDER-IMAGE)")) MyRIPL.assume("noisy-image-compare", lisp_parser.parse("(load-remote-xrp 4444 NOISY-COMP)")) MyRIPL.assume("test-image", lisp_parser.parse("(load-image 0 0 0 0 0)")) #FIXME - dynamic args arguments = "" for i in range(NUM_LETTERS): arguments += " posx"+str(i)+" "+"posy"+str(i)+" "+"id"+str(i)+" "+"size"+str(i)+" "+"blur"+str(i) + " 1" MyRIPL.assume("rendered-image", lisp_parser.parse("(render-image " + arguments + ")")) #FIXME - dynamic args MyRIPL.observe(lisp_parser.parse("(noisy-image-compare test-image rendered-image pflip)"), "true") r = openGLText.openGLText() logsArray = [] pxdisagreeArr = [] blurArr = [] pflipArr = [] baselineIm = Image.open("cvprtext.png").convert("L") baselineIm = asarray(baselineIm) cnt = 0 while cnt < 200: MyRIPL.infer(50) pflip = MyRIPL.report_value(_pflip) alpha = MyRIPL.report_value(_alpha) things = [] for i in range(len(chrs)): posx,posy,size,_id,blur = self.reportCharacters(chrs[i]) things.append({'id':chr(int(_id)+65), 'size':size, 'left':posx, 'top':posy,'blur_sigsq':blur}) print "left():",posx," top():", posy," size():", size,chr(_id+65)," blur: ",blur,"| pflip:", pflip, "|", alpha print "####\n" im = r.get_rendered_image(things) scipy.misc.imsave('inference.jpg', im) logscore = MyRIPL.logscore() logsArray.append(logscore['logscore']) compound = im+baselineIm intersection_ones = len(where(compound == 2)[0]) intersection_zeros = len(where(compound == 0)[0]) intersection = intersection_zeros + intersection_ones pxdisagreeArr.append(float(RES-intersection)/RES) pflipArr.append(pflip) blurArr.append(blur) print 'LOGSCORE:', logscore, "|", cnt cnt = cnt + 1 #self.initPlottingHarness(baselineIm,im,range(cnt),logsArray,pxdisagreeArr,pflipArr,blurArr,cnt) self.demoPlotting(baselineIm,im,range(cnt),logsArray,pxdisagreeArr,pflipArr,blurArr,cnt) """ # add directives needed
def LoadProgram(self): MyRIPL = self.RIPL MyRIPL.clear() # To delete previous sessions data. chrs = self.createCharacter() (_alpha, tmp) = MyRIPL.assume("alpha", lisp_parser.parse("(uniform-continuous 0.01 8)")) (_pflip, tmp) = MyRIPL.assume( "pflip", lisp_parser.parse("(uniform-continuous 0.0 1.0)")) MyRIPL.assume("LOAD-IMAGE", lisp_parser.parse("1")) MyRIPL.assume("RENDER-IMAGE", lisp_parser.parse("10")) MyRIPL.assume("NOISY-COMP", lisp_parser.parse("3")) MyRIPL.assume("load-image", lisp_parser.parse("(load-remote-xrp 4444 LOAD-IMAGE)")) MyRIPL.assume("render-image", lisp_parser.parse("(load-remote-xrp 4444 RENDER-IMAGE)")) MyRIPL.assume("noisy-image-compare", lisp_parser.parse("(load-remote-xrp 4444 NOISY-COMP)")) MyRIPL.assume( "test-image", lisp_parser.parse("(load-image 0 0 0 0 0)")) #FIXME - dynamic args arguments = "" for i in range(NUM_LETTERS): arguments += " posx" + str(i) + " " + "posy" + str( i) + " " + "id" + str(i) + " " + "size" + str( i) + " " + "blur" + str(i) + " 1" MyRIPL.assume("rendered-image", lisp_parser.parse("(render-image " + arguments + ")")) #FIXME - dynamic args MyRIPL.observe( lisp_parser.parse( "(noisy-image-compare test-image rendered-image pflip)"), "true") r = openGLText.openGLText() logsArray = [] pxdisagreeArr = [] blurArr = [] pflipArr = [] baselineIm = Image.open("cvprtext.png").convert("L") baselineIm = asarray(baselineIm) cnt = 0 while cnt < 200: MyRIPL.infer(50) pflip = MyRIPL.report_value(_pflip) alpha = MyRIPL.report_value(_alpha) things = [] for i in range(len(chrs)): posx, posy, size, _id, blur = self.reportCharacters(chrs[i]) things.append({ 'id': chr(int(_id) + 65), 'size': size, 'left': posx, 'top': posy, 'blur_sigsq': blur }) print "left():", posx, " top():", posy, " size():", size, chr( _id + 65), " blur: ", blur, "| pflip:", pflip, "|", alpha print "####\n" im = r.get_rendered_image(things) scipy.misc.imsave('inference.jpg', im) logscore = MyRIPL.logscore() logsArray.append(logscore['logscore']) compound = im + baselineIm intersection_ones = len(where(compound == 2)[0]) intersection_zeros = len(where(compound == 0)[0]) intersection = intersection_zeros + intersection_ones pxdisagreeArr.append(float(RES - intersection) / RES) pflipArr.append(pflip) blurArr.append(blur) print 'LOGSCORE:', logscore, "|", cnt cnt = cnt + 1 #self.initPlottingHarness(baselineIm,im,range(cnt),logsArray,pxdisagreeArr,pflipArr,blurArr,cnt) self.demoPlotting(baselineIm, im, range(cnt), logsArray, pxdisagreeArr, pflipArr, blurArr, cnt) """ # add directives needed
def load_image_helper(self, itr): MyRIPL = self.RIPL MyRIPL.forget(self._loadimage) (self._loadimage, _) = MyRIPL.assume( "test-image" + str(itr), lisp_parser.parse("(load-image " + str(itr) + " 0 0 0 0)"))
def inference_LoadProgram(self): MyRIPL = self.RIPL try: self.LoadProgram(explore_Joint=0) except ConnectionError: return -1 MyRIPL.observe(lisp_parser.parse("(noisy-image-compare test-image rendered-image pflip)"), "true") r = openGLText.openGLText(self.filename) logsArray = [] pxdisagreeArr = [] lettercountArr = [] globalBlurArr =[] blurArr = dict() for i in range(MAX_LETTERS): blurArr[i] = [] pflipArr = [] baselineIm = Image.open(self.filename).convert("L") baselineIm = asarray(baselineIm) chrs = self.predictCharacteres() cnt = 0 ## directory to hold all plots now = SEED + '_MAX_LETTERS_'+ str(MAX_LETTERS)+ '_' + str(datetime.datetime.now()) dst = "dump/"+now os.makedirs(dst); os.makedirs(dst+'/composite');os.makedirs(dst+'/logscore');os.makedirs(dst+'/pxdisagree');os.makedirs(dst+'/globalblur'); os.makedirs(dst+'/variance');os.makedirs(dst+'/blur');os.makedirs(dst+'/lettercount');os.makedirs(dst+'/images'); os.makedirs(dst+'/pickle'); while cnt < 352: MyRIPL.infer(100) pflip = MyRIPL.report_value(self._pflip) global_blur = MyRIPL.report_value(self._global_blur) globalBlurArr.append(global_blur) things = [] letter_count = 0 for i in range(MAX_LETTERS): posx,posy,size_x,size_y,rotate_z,_id,blur,present = self.reportCharacters(chrs,i) print present, pflip if present == True: letter_count += 1 things.append({'id':chr(int(_id)+65), 'size_x':size_x, 'size_y':size_y, 'rotate_z':rotate_z, 'left':posx, 'top':posy,'blur_sigsq':blur}) #print "left():",posx," top():", posy," size_x:", size_x, " size_y:", size_y, " rotate_z:", rotate_z,chr(_id+65)," blur: ",blur,"| pflip:", pflip, '\n' blurArr[i].append(blur) else: blurArr[i].append(0) cnt = cnt + 1 im = r.get_rendered_image(things,global_blur) scipy.misc.imsave(dst+str('/images/')+str(cnt)+'.png', im) logscore = MyRIPL.logscore() logsArray.append(logscore['logscore']) print 'LOGSCORE:', logscore, "|", cnt diff = sum(abs(im-baselineIm)) pxdisagreeArr.append(diff) pflipArr.append(pflip) lettercountArr.append(letter_count) ###################################### PLOTTING HARNESS ############################################ self.composite(baselineIm,im,range(cnt),logsArray,pxdisagreeArr,globalBlurArr,pflipArr,dst+str('/composite/')+str(cnt)+'.png') if cnt%30 == 0: OBJ = {'logscore': logsArray, 'pxdisagree':pxdisagreeArr, 'variance':pflipArr, 'letter-count':lettercountArr, 'global-blur':globalBlurArr, 'letter-blur': blurArr } pickle.dump(OBJ, open(dst+str('/pickle/')+str(cnt)+'.pkl','wb')) self.savePlot(range(cnt),logsArray,"LOG-SCORE","Iterations","logscore", dst+str('/logscore/')+str(cnt)+'.png') self.savePlot(range(cnt),pxdisagreeArr,"PX-DISAGREE","Iterations","pxdisagree", dst+str('/pxdisagree/')+str(cnt)+'.png') self.savePlot(range(cnt),pflipArr,"VARIANCE","Iterations","variance", dst+str('/variance/')+str(cnt)+'.png') self.savePlot(range(cnt),lettercountArr,"LETTER-COUNT","Iterations","letter count", dst+str('/lettercount/')+str(cnt)+'.png') self.savePlot(range(cnt),globalBlurArr,"GLOBAL-BLUR","Iterations","blur", dst+str('/globalblur/')+str(cnt)+'.png') for jj in range(MAX_LETTERS): self.savePlot(range(cnt),blurArr[jj],"BLUR-LETTER-"+str(jj),"Iterations","blur", dst+str('/blur/')+str(cnt)+'_'+str(jj)+'.png')
def load_image_helper(self,itr): MyRIPL = self.RIPL MyRIPL.forget(self._loadimage) (self._loadimage,_) = MyRIPL.assume("test-image"+str(itr), lisp_parser.parse("(load-image " + str(itr) + " 0 0 0 0)"))
def explore_Joint_LoadProgram(self): MyRIPL = self.RIPL self.LoadProgram(explore_Joint=1) MyRIPL.predict(lisp_parser.parse("(noisy-image-compare test-image rendered-image pflip)"))
def explore_Chain_LoadProgram(self): MyRIPL = self.RIPL self.LoadProgram(explore_Joint=1) MyRIPL.observe(lisp_parser.parse("(noisy-image-compare test-image rendered-image pflip)"), "true")
def LoadProgram(self,explore_Joint = 0): MyRIPL = self.RIPL MyRIPL.clear() # To delete previous sessions data. MyRIPL.assume("use-enumeration", 0.1) #MyRIPL.assume("flip-relax", lisp_parser.parse("(mem (lambda(letter-id) (beta 1 2)))")) #beta 1 2 #MyRIPL.assume("letter-present", lisp_parser.parse("(mem (lambda(letter-id) (bernoulli (flip-relax letter-id) )))")) MyRIPL.assume("letter-present", lisp_parser.parse("(mem (lambda(letter-id) (bernoulli 0.5)))")) """MyRIPL.assume("flip-relax-relax", lisp_parser.parse("(mem (lambda(letter-id) (uniform-continuous 1.0 3.0)))")) MyRIPL.assume("flip-relax", lisp_parser.parse("(mem (lambda(letter-id) (beta 10 (flip-relax-relax letter-id))))")) MyRIPL.assume("letter-present", lisp_parser.parse("(mem (lambda(letter-id) (bernoulli (flip-relax letter-id) )))"))""" self.RIPL.assume("posx", lisp_parser.parse("(mem (lambda (letter-id) (uniform-discrete 0 150)))")) #0 -140 captcha self.RIPL.assume("posy", lisp_parser.parse("(mem (lambda (letter-id) (uniform-discrete 0 150)))")) #0 - 100 captcha #56-64 ocr self.RIPL.assume("size_x", lisp_parser.parse("(mem (lambda (letter-id) (uniform-discrete 50 100)))")) #30-70 captcha self.RIPL.assume("size_y", lisp_parser.parse("(mem (lambda (letter-id) (size_x letter-id)))")) #30-70 captcha self.RIPL.assume("rotate_z", lisp_parser.parse("(mem (lambda (letter-id) (uniform-continuous 0.0 0.1)))")) #-40 40 #self.RIPL.assume("id", lisp_parser.parse("(mem (lambda (letter-id) (uniform-discrete 0 4)))")) #0 -2 self.RIPL.assume("id", lisp_parser.parse("(mem (lambda (letter-id) (get-letter-id)))")) #0 -2 #self.RIPL.assume("blur", lisp_parser.parse("(mem (lambda (letter-id) (* 7 (beta 1 2))))")) # best ############# NO HBT ############# self.RIPL.assume("blur", lisp_parser.parse("(mem (lambda (letter-id) 3 ))")) # 0 - 10 #(self._global_blur,tmp) = self.RIPL.assume("global-blur", lisp_parser.parse("(* 2 (beta 1 10)))")) # 0 - 10 ############# NO HBT ############# (self._global_blur,tmp) = self.RIPL.assume("global-blur", lisp_parser.parse("0")) # 0 - 10 #(self._pflip,tmp)= MyRIPL.assume("pflip", lisp_parser.parse("(gamma 1 1)")) ##this is variance (uniform-continuous 0.0 0.5) ############# NO HBT ############# (self._pflip,tmp)= MyRIPL.assume("pflip", lisp_parser.parse("0.01")) ##this is variance (uniform-continuous 0.0 0.5) MyRIPL.assume("LOAD-IMAGE", lisp_parser.parse("1")) MyRIPL.assume("RENDER-IMAGE", lisp_parser.parse("10")) MyRIPL.assume("NOISY-COMP", lisp_parser.parse("3")) MyRIPL.assume("load-image", lisp_parser.parse("(load-remote-xrp 4444 LOAD-IMAGE)")) MyRIPL.assume("render-image", lisp_parser.parse("(load-remote-xrp 4444 RENDER-IMAGE)")) MyRIPL.assume("noisy-image-compare", lisp_parser.parse("(load-remote-xrp 4444 NOISY-COMP)")) if explore_Joint == 0: (self._loadimage,_) = MyRIPL.assume("test-image", lisp_parser.parse("(load-image -1.0 0 0 0 0)")) #FIXME - dynamic args else: (self._loadimage,_) = MyRIPL.assume("test-image", lisp_parser.parse("(load-image 0 0 0 0 0)")) arguments = "" for i in range(MAX_LETTERS): arguments += " (posx "+str(i)+") " + " (posy "+str(i)+") " + " (id "+str(i)+") " + " (size_x "+str(i)+") " + " (size_y "+str(i)+") " + " (rotate_z "+str(i)+") " + " (blur "+str(i)+") " + " (letter-present "+str(i)+") " #arguments += " (posx "+str(i)+") " + " (posy "+str(i)+") " + " (id "+str(i)+") " + " (size_x "+str(i)+") " + " (size_y "+str(i)+") " + " (rotate_z "+str(i)+") " + " blur" + " (letter-present "+str(i)+") " MyRIPL.assume("rendered-image", lisp_parser.parse("(render-image " + arguments + " global-blur )")) #FIXME - dynamic args #Number of letters present letter_args = "" for i in range(MAX_LETTERS): letter_args += " (letter-present " + str(i) + " ) " (num_letters,tmp) = MyRIPL.predict(lisp_parser.parse("(+ " + letter_args + " )")) chrs = self.predictCharacteres() directives=list() directives.append(num_letters) """directives.append(self._pflip) for letter in range(len(chrs)): for drt in chrs[letter].values(): directives.append(drt)""" self.directives = directives
def LoadProgram(self, explore_Joint=0): MyRIPL = self.RIPL MyRIPL.clear() # To delete previous sessions data. MyRIPL.assume("use-enumeration", 0.1) #MyRIPL.assume("flip-relax", lisp_parser.parse("(mem (lambda(letter-id) (beta 1 2)))")) #beta 1 2 #MyRIPL.assume("letter-present", lisp_parser.parse("(mem (lambda(letter-id) (bernoulli (flip-relax letter-id) )))")) MyRIPL.assume( "letter-present", lisp_parser.parse("(mem (lambda(letter-id) (bernoulli 0.5)))")) """MyRIPL.assume("flip-relax-relax", lisp_parser.parse("(mem (lambda(letter-id) (uniform-continuous 1.0 3.0)))")) MyRIPL.assume("flip-relax", lisp_parser.parse("(mem (lambda(letter-id) (beta 10 (flip-relax-relax letter-id))))")) MyRIPL.assume("letter-present", lisp_parser.parse("(mem (lambda(letter-id) (bernoulli (flip-relax letter-id) )))"))""" self.RIPL.assume( "posx", lisp_parser.parse( "(mem (lambda (letter-id) (uniform-discrete 0 150)))") ) #0 -140 captcha self.RIPL.assume( "posy", lisp_parser.parse( "(mem (lambda (letter-id) (uniform-discrete 0 150)))") ) #0 - 100 captcha #56-64 ocr self.RIPL.assume( "size_x", lisp_parser.parse( "(mem (lambda (letter-id) (uniform-discrete 50 100)))") ) #30-70 captcha self.RIPL.assume( "size_y", lisp_parser.parse("(mem (lambda (letter-id) (size_x letter-id)))" )) #30-70 captcha self.RIPL.assume( "rotate_z", lisp_parser.parse( "(mem (lambda (letter-id) (uniform-continuous 0.0 0.1)))") ) #-40 40 #self.RIPL.assume("id", lisp_parser.parse("(mem (lambda (letter-id) (uniform-discrete 0 4)))")) #0 -2 self.RIPL.assume( "id", lisp_parser.parse( "(mem (lambda (letter-id) (get-letter-id)))")) #0 -2 #self.RIPL.assume("blur", lisp_parser.parse("(mem (lambda (letter-id) (* 7 (beta 1 2))))")) # best ############# NO HBT ############# self.RIPL.assume( "blur", lisp_parser.parse("(mem (lambda (letter-id) 3 ))")) # 0 - 10 #(self._global_blur,tmp) = self.RIPL.assume("global-blur", lisp_parser.parse("(* 2 (beta 1 10)))")) # 0 - 10 ############# NO HBT ############# (self._global_blur, tmp) = self.RIPL.assume("global-blur", lisp_parser.parse("0")) # 0 - 10 #(self._pflip,tmp)= MyRIPL.assume("pflip", lisp_parser.parse("(gamma 1 1)")) ##this is variance (uniform-continuous 0.0 0.5) ############# NO HBT ############# (self._pflip, tmp) = MyRIPL.assume("pflip", lisp_parser.parse( "0.01")) ##this is variance (uniform-continuous 0.0 0.5) MyRIPL.assume("LOAD-IMAGE", lisp_parser.parse("1")) MyRIPL.assume("RENDER-IMAGE", lisp_parser.parse("10")) MyRIPL.assume("NOISY-COMP", lisp_parser.parse("3")) MyRIPL.assume("load-image", lisp_parser.parse("(load-remote-xrp 4444 LOAD-IMAGE)")) MyRIPL.assume("render-image", lisp_parser.parse("(load-remote-xrp 4444 RENDER-IMAGE)")) MyRIPL.assume("noisy-image-compare", lisp_parser.parse("(load-remote-xrp 4444 NOISY-COMP)")) if explore_Joint == 0: (self._loadimage, _) = MyRIPL.assume( "test-image", lisp_parser.parse( "(load-image -1.0 0 0 0 0)")) #FIXME - dynamic args else: (self._loadimage, _) = MyRIPL.assume("test-image", lisp_parser.parse("(load-image 0 0 0 0 0)")) arguments = "" for i in range(MAX_LETTERS): arguments += " (posx " + str(i) + ") " + " (posy " + str( i) + ") " + " (id " + str(i) + ") " + " (size_x " + str( i) + ") " + " (size_y " + str( i) + ") " + " (rotate_z " + str( i) + ") " + " (blur " + str( i) + ") " + " (letter-present " + str(i) + ") " #arguments += " (posx "+str(i)+") " + " (posy "+str(i)+") " + " (id "+str(i)+") " + " (size_x "+str(i)+") " + " (size_y "+str(i)+") " + " (rotate_z "+str(i)+") " + " blur" + " (letter-present "+str(i)+") " MyRIPL.assume( "rendered-image", lisp_parser.parse("(render-image " + arguments + " global-blur )")) #FIXME - dynamic args #Number of letters present letter_args = "" for i in range(MAX_LETTERS): letter_args += " (letter-present " + str(i) + " ) " (num_letters, tmp) = MyRIPL.predict(lisp_parser.parse("(+ " + letter_args + " )")) chrs = self.predictCharacteres() directives = list() directives.append(num_letters) """directives.append(self._pflip) for letter in range(len(chrs)): for drt in chrs[letter].values(): directives.append(drt)""" self.directives = directives
def LoadProgram(self): MyRIPL = self.RIPL MyRIPL.clear() # To delete previous sessions data. MyRIPL.assume("flip-relax", lisp_parser.parse("(mem (lambda(letter-id) (beta 1 2)))")) MyRIPL.assume("letter-present", lisp_parser.parse("(mem (lambda(letter-id) (bernoulli (flip-relax letter-id) )))")) """MyRIPL.assume("flip-relax-relax", lisp_parser.parse("(mem (lambda(letter-id) (uniform-continuous 1.0 3.0)))")) MyRIPL.assume("flip-relax", lisp_parser.parse("(mem (lambda(letter-id) (beta 10 (flip-relax-relax letter-id))))")) MyRIPL.assume("letter-present", lisp_parser.parse("(mem (lambda(letter-id) (bernoulli (flip-relax letter-id) )))"))""" self.RIPL.assume("posx", lisp_parser.parse("(mem (lambda (letter-id) (uniform-discrete 0 150)))")) #0 -140 captcha self.RIPL.assume("posy", lisp_parser.parse("(mem (lambda (letter-id) (uniform-discrete 0 150)))")) #0 - 100 captcha #56-64 ocr self.RIPL.assume("size_x", lisp_parser.parse("(mem (lambda (letter-id) (uniform-discrete 30 70)))")) #30-70 captcha self.RIPL.assume("size_y", lisp_parser.parse("(mem (lambda (letter-id) (size_x letter-id)))")) #30-70 captcha self.RIPL.assume("rotate_z", lisp_parser.parse("(mem (lambda (letter-id) (uniform-continuous -40.0 40.0)))")) #30-70 captcha self.RIPL.assume("id", lisp_parser.parse("(mem (lambda (letter-id) (uniform-discrete 0 2)))")) #0 -2 #self.RIPL.assume("blur", lisp_parser.parse("(mem (lambda (letter-id) (gamma 1 1)))")) # 0 - 10 self.RIPL.assume("blur", lisp_parser.parse("(* 10 (beta 1 10))")) # 0 - 10 (_alpha,tmp) = MyRIPL.assume("alpha", lisp_parser.parse("(uniform-continuous 0 20)")) (_pflip,tmp)= MyRIPL.assume("pflip", lisp_parser.parse("(/ (beta 1 5) 2)")) #beta 6 1 works reasonably MyRIPL.assume("LOAD-IMAGE", lisp_parser.parse("1")) MyRIPL.assume("RENDER-IMAGE", lisp_parser.parse("10")) MyRIPL.assume("NOISY-COMP", lisp_parser.parse("3")) MyRIPL.assume("load-image", lisp_parser.parse("(load-remote-xrp 4444 LOAD-IMAGE)")) MyRIPL.assume("render-image", lisp_parser.parse("(load-remote-xrp 4444 RENDER-IMAGE)")) MyRIPL.assume("noisy-image-compare", lisp_parser.parse("(load-remote-xrp 4444 NOISY-COMP)")) MyRIPL.assume("test-image", lisp_parser.parse("(load-image 0 0 0 0 0)")) #FIXME - dynamic args arguments = "" for i in range(MAX_LETTERS): #arguments += " (posx "+str(i)+") " + " (posy "+str(i)+") " + " (id "+str(i)+") " + " (size_x "+str(i)+") " + " (size_y "+str(i)+") " + " (rotate_z "+str(i)+") " + " (blur "+str(i)+") " + " (letter-present "+str(i)+") " arguments += " (posx "+str(i)+") " + " (posy "+str(i)+") " + " (id "+str(i)+") " + " (size_x "+str(i)+") " + " (size_y "+str(i)+") " + " (rotate_z "+str(i)+") " + " blur" + " (letter-present "+str(i)+") " MyRIPL.assume("rendered-image", lisp_parser.parse("(render-image " + arguments + ")")) #FIXME - dynamic args MyRIPL.observe(lisp_parser.parse("(noisy-image-compare test-image rendered-image pflip)"), "true") r = openGLText.openGLText() r.loadImage("") logsArray = [] pxdisagreeArr = [] blurArr = [] pflipArr = [] baselineIm = Image.open("demo.jpg").convert("L") baselineIm = asarray(baselineIm) chrs = self.predictCharacteres() cnt = 0 while cnt < 200: MyRIPL.infer(100) pflip = MyRIPL.report_value(_pflip) things = [] for i in range(MAX_LETTERS): posx,posy,size_x,size_y,rotate_z,_id,blur,present = self.reportCharacters(chrs,i) print present, pflip if present == True: things.append({'id':chr(int(_id)+65), 'size_x':size_x, 'size_y':size_y, 'rotate_z':rotate_z, 'left':posx, 'top':posy,'blur_sigsq':blur}) print "left():",posx," top():", posy," size_x:", size_x, " size_y:", size_y, " rotate_z:", rotate_z,chr(_id+65)," blur: ",blur,"| pflip:", pflip print "####\n" im = r.get_rendered_image(things) scipy.misc.imsave('inference.jpg', im) logscore = MyRIPL.logscore() logsArray.append(logscore['logscore']) compound = im+baselineIm intersection_ones = len(where(compound == 2)[0]) intersection_zeros = len(where(compound == 0)[0]) intersection = intersection_zeros + intersection_ones pxdisagreeArr.append(float(RES-intersection)/RES) pflipArr.append(pflip) blurArr.append(blur) print 'LOGSCORE:', logscore, "|", cnt cnt = cnt + 1 self.initPlottingHarness(baselineIm,im,range(cnt),logsArray,pxdisagreeArr,pflipArr,blurArr,cnt) #self.demoPlotting(baselineIm,im,range(cnt),logsArray,pxdisagreeArr,pflipArr,blurArr,cnt) """ # add directives needed
def explore_Joint_LoadProgram(self): MyRIPL = self.RIPL self.LoadProgram(explore_Joint=1) MyRIPL.predict( lisp_parser.parse( "(noisy-image-compare test-image rendered-image pflip)"))
import client_changed import lisp_parser # From here: http://norvig.com/lispy.html Port = 8081 MyRIPL = client_changed.RemoteRIPL("http://ripl.pcp-project.com:" + str(Port)) # Tricky coin MyRIPL.clear() # To delete previous sessions data. MyRIPL.assume("is-tricky", ['bernoulli', 0.1]) # You can either write the Church code via the Python lists: # MyRIPL.assume("head-probability", ['if', 'is-tricky', ['beta', 1.0, 1.0], 0.5]) # Or to parse it from the string: MyRIPL.assume("head-probability", lisp_parser.parse("(if is-tricky (beta 1.0 1.0) 0.5)")) (is_tricky, _) = MyRIPL.predict("is-tricky") # It could be done through the first ASSUME, # but we are using the PREDICT just to # check its operability. print(client_changed.directives_to_string(MyRIPL.report_directives())) for i in range(9): MyRIPL.observe(['bernoulli', 'head-probability'], 'true') print MyRIPL.report_value(is_tricky) HowManyTimesIsTricky = 0 NumberOfSamples = 100 IntermediateMHIterations = 100
def inference_LoadProgram(self): MyRIPL = self.RIPL try: self.LoadProgram(explore_Joint=0) except ConnectionError: return -1 MyRIPL.observe( lisp_parser.parse( "(noisy-image-compare test-image rendered-image pflip)"), "true") r = openGLText.openGLText(self.filename) logsArray = [] pxdisagreeArr = [] lettercountArr = [] globalBlurArr = [] blurArr = dict() for i in range(MAX_LETTERS): blurArr[i] = [] pflipArr = [] baselineIm = Image.open(self.filename).convert("L") baselineIm = asarray(baselineIm) chrs = self.predictCharacteres() cnt = 0 ## directory to hold all plots now = SEED + '_MAX_LETTERS_' + str(MAX_LETTERS) + '_' + str( datetime.datetime.now()) dst = "dump/" + now os.makedirs(dst) os.makedirs(dst + '/composite') os.makedirs(dst + '/logscore') os.makedirs(dst + '/pxdisagree') os.makedirs(dst + '/globalblur') os.makedirs(dst + '/variance') os.makedirs(dst + '/blur') os.makedirs(dst + '/lettercount') os.makedirs(dst + '/images') os.makedirs(dst + '/pickle') while cnt < 352: MyRIPL.infer(100) pflip = MyRIPL.report_value(self._pflip) global_blur = MyRIPL.report_value(self._global_blur) globalBlurArr.append(global_blur) things = [] letter_count = 0 for i in range(MAX_LETTERS): posx, posy, size_x, size_y, rotate_z, _id, blur, present = self.reportCharacters( chrs, i) print present, pflip if present == True: letter_count += 1 things.append({ 'id': chr(int(_id) + 65), 'size_x': size_x, 'size_y': size_y, 'rotate_z': rotate_z, 'left': posx, 'top': posy, 'blur_sigsq': blur }) #print "left():",posx," top():", posy," size_x:", size_x, " size_y:", size_y, " rotate_z:", rotate_z,chr(_id+65)," blur: ",blur,"| pflip:", pflip, '\n' blurArr[i].append(blur) else: blurArr[i].append(0) cnt = cnt + 1 im = r.get_rendered_image(things, global_blur) scipy.misc.imsave(dst + str('/images/') + str(cnt) + '.png', im) logscore = MyRIPL.logscore() logsArray.append(logscore['logscore']) print 'LOGSCORE:', logscore, "|", cnt diff = sum(abs(im - baselineIm)) pxdisagreeArr.append(diff) pflipArr.append(pflip) lettercountArr.append(letter_count) ###################################### PLOTTING HARNESS ############################################ self.composite(baselineIm, im, range(cnt), logsArray, pxdisagreeArr, globalBlurArr, pflipArr, dst + str('/composite/') + str(cnt) + '.png') if cnt % 30 == 0: OBJ = { 'logscore': logsArray, 'pxdisagree': pxdisagreeArr, 'variance': pflipArr, 'letter-count': lettercountArr, 'global-blur': globalBlurArr, 'letter-blur': blurArr } pickle.dump( OBJ, open(dst + str('/pickle/') + str(cnt) + '.pkl', 'wb')) self.savePlot(range(cnt), logsArray, "LOG-SCORE", "Iterations", "logscore", dst + str('/logscore/') + str(cnt) + '.png') self.savePlot(range(cnt), pxdisagreeArr, "PX-DISAGREE", "Iterations", "pxdisagree", dst + str('/pxdisagree/') + str(cnt) + '.png') self.savePlot(range(cnt), pflipArr, "VARIANCE", "Iterations", "variance", dst + str('/variance/') + str(cnt) + '.png') self.savePlot(range(cnt), lettercountArr, "LETTER-COUNT", "Iterations", "letter count", dst + str('/lettercount/') + str(cnt) + '.png') self.savePlot(range(cnt), globalBlurArr, "GLOBAL-BLUR", "Iterations", "blur", dst + str('/globalblur/') + str(cnt) + '.png') for jj in range(MAX_LETTERS): self.savePlot( range(cnt), blurArr[jj], "BLUR-LETTER-" + str(jj), "Iterations", "blur", dst + str('/blur/') + str(cnt) + '_' + str(jj) + '.png')
def parse(program): return lisp_parser.parse(program, actions=Actions())
import client_changed import lisp_parser # From here: http://norvig.com/lispy.html Port = 8081 MyRIPL = client_changed.RemoteRIPL("http://ripl.pcp-project.com:" + str(Port)) # Tricky coin MyRIPL.clear() # To delete previous sessions data. MyRIPL.assume("is-tricky", ['bernoulli', 0.1]) # You can either write the Church code via the Python lists: # MyRIPL.assume("head-probability", ['if', 'is-tricky', ['beta', 1.0, 1.0], 0.5]) # Or to parse it from the string: MyRIPL.assume("head-probability", lisp_parser.parse("(if is-tricky (beta 1.0 1.0) 0.5)")) (is_tricky, _) = MyRIPL.predict("is-tricky") # It could be done through the first ASSUME, # but we are using the PREDICT just to # check its operability. print(client_changed.directives_to_string(MyRIPL.report_directives())) for i in range(9): MyRIPL.observe(['bernoulli', 'head-probability'], 'true') print MyRIPL.report_value(is_tricky) HowManyTimesIsTricky = 0 NumberOfSamples = 100 IntermediateMHIterations = 100 for sample_number in range(NumberOfSamples): MyRIPL.infer(IntermediateMHIterations, 5)
def LoadProgram(self): MyRIPL = self.RIPL MyRIPL.clear() # To delete previous sessions data. MyRIPL.assume("posx", lisp_parser.parse("(uniform-discrete 20 160)")) MyRIPL.assume("posy", lisp_parser.parse("(uniform-discrete 20 160)")) MyRIPL.assume("size", lisp_parser.parse("(uniform-discrete 30 60)")) MyRIPL.assume("id", lisp_parser.parse("(uniform-discrete 0 25)")) MyRIPL.assume("blur", lisp_parser.parse("(* (beta 1 2) 40)")) MyRIPL.assume("alpha", lisp_parser.parse("(uniform-continuous 0.01 8)")) MyRIPL.assume("pflip", lisp_parser.parse("(beta 1 alpha)")) #### REMOVE LATER """MyRIPL.observe("posx",120) MyRIPL.observe("posy", 90) MyRIPL.observe("blur", 0.0) MyRIPL.observe("size", 40)""" #### REMOVE LATER ENDS MyRIPL.assume("LOAD-IMAGE", lisp_parser.parse("1")) MyRIPL.assume("RENDER-IMAGE", lisp_parser.parse("10")) MyRIPL.assume("NOISY-COMP", lisp_parser.parse("3")) MyRIPL.assume("load-image", lisp_parser.parse("(load-remote-xrp 4444 LOAD-IMAGE)")) MyRIPL.assume("render-image", lisp_parser.parse("(load-remote-xrp 4444 RENDER-IMAGE)")) MyRIPL.assume("noisy-image-compare", lisp_parser.parse("(load-remote-xrp 4444 NOISY-COMP)")) MyRIPL.assume("test-image", lisp_parser.parse("(load-image 0 0 0 0 0)")) #FIXME - dynamic args MyRIPL.assume("rendered-image", lisp_parser.parse("(render-image posx posy id size blur)")) #FIXME - dynamic args MyRIPL.observe(lisp_parser.parse("(noisy-image-compare test-image rendered-image pflip)"), "true") r = Renderer.Renderer() r.state = dict() r.state['params'] = {'room_self.size_x':r.size_x, 'room_self.size_y':r.size_y} r.state['blur'] = True logsArray = [] cnt = 0 while cnt < 400: MyRIPL.infer(50) posx = MyRIPL.report_value(1) posy = MyRIPL.report_value(2) size = MyRIPL.report_value(3) _id = MyRIPL.report_value(4) blur = MyRIPL.report_value(5) alpha = MyRIPL.report_value(6) pflip = MyRIPL.report_value(7) print posx,posy,size,chr(_id+65),blur, "| alpha:", alpha,"| pflip:", pflip things = [] things.append({'id':chr(int(_id)+65), 'size':size, 'left':posx, 'top':posy,'blur_sigsq':blur}) im = r.get_rendered_image(things) scipy.misc.imsave('inference.jpg', im) logscore = MyRIPL.logscore() logsArray.append(logscore['logscore']) print 'LOGSCORE:', logscore, "|", cnt cnt = cnt + 1 X = range(cnt) pyplot.plot( X, logsArray, '-' ) pyplot.xlabel( 'Iterations' ) pyplot.ylabel( 'Logscore' ) pyplot.savefig( 'Simple.png' ) pyplot.show() """ # add directives needed