def imagine2(dim, code, save=False, visualize=True): # set the shape shape = (1, dim, dim, zed) code1 = code[0:zed] code2 = code[zed:zed * 2] # overall tone means = code1 - 0.5 # -0.5 or 0.5 variances = code2 * .3 + .2 # .2 or .5 # final latent z = np.random.normal(loc=means, scale=variances, size=shape) gened = gm.infer(z) gened *= 0.5 # normalize to 0..1 gened += 0.5 im = gened[0] if visualize: # visualize vis.show_autoscaled(im, name='imagined', limit=600.) fmap = np.transpose(z, (3, 1, 2, 0)) + .5 vis.show_batch_autoscaled(fmap, name='z_map', limit=600.) # outputs the image return im
def process(self,lastframe,thisframe): # graify, downscale fg = thisframe.copy() # fg = cv2.cvtColor(fg, cv2.COLOR_BGR2GRAY) pyramid_downscaler = 2 pyramid = [] for i in range(pyramid_downscaler): fg = pyr(fg,-1) pyramid.append(fg) # size: -1 -2 -3 probmap = None for idx,img in enumerate(pyramid): cv2.imshow('pyr'+str(idx), img) # inferrence img.shape = (1,)+img.shape img = img.astype('float32') j = clf.infer(img/255.-0.5)[0] # j = j * (j>0.5) # vis.show_autoscaled(j[0],name='infer_pyr_'+str(idx),limit=300.) j = pyr(j, idx) # idx: [0 1 2] if j is None: print('j is none',img.dtype) if probmap is None: probmap = j else: if len(j.shape)==2: j.shape+=(1,) offy = int((probmap.shape[0] - j.shape[0])//2) offx = int((probmap.shape[1] - j.shape[1])//2) probmap[offy:offy+j.shape[0],offx:offx+j.shape[1],:] += j # cv2.imshow('infer_pyr_1',j[0]) # probmap = cv2.blur(probmap,(3,3)) (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(probmap) # image = orig.copy() if maxVal>0.9: shift = 50 # a small offset since convnet cut the border off scaler = (thisframe.shape[1] - shift*2) / probmap.shape[1] # black-white ring cv2.circle(thisframe, (int(maxLoc[0]*scaler+shift),int(maxLoc[1]*scaler+shift)), 30, (0, 0, 0), 4) cv2.circle(thisframe, (int(maxLoc[0]*scaler+shift),int(maxLoc[1]*scaler+shift)), 30, (255, 255, 255), 2) vis.show_autoscaled(probmap,name='scaled_back',limit=300.) return thisframe
def qrtest(): uvout, qrout = random_qr_pipeline(128, dirty=True) print('uvout', uvout.shape, 'qrout', qrout.shape) # qrimg, uvimg = [pipeline(i) for i in [qrimg,uvimg]] vis.show_autoscaled(qrout, name='qr') vis.show_autoscaled(uvout, name='uv')
def show(save=False): from cv2tools import vis, filt i = np.random.normal(loc=0., scale=1., size=(1, 8, 8, zed)) gened = gm.infer(i) gened *= 0.5 gened += 0.5 vis.show_autoscaled(gened[0], name='generated', limit=800) if save != False: cv2.imwrite(save, im * 255)
def compose_batch(self, output_size, batch_size, show=True): bs = batch_size bgrw, bgs = random_walk(bs, ipos=.5, ispd=5., acc=1.) #rw position and speed fgrw, fgs = random_walk(bs, ipos=.5, ispd=5., acc=2.) bgrw -= np.mean(bgrw) # normalization fgrw -= np.mean(fgrw) # +np.random.uniform(40)-20 # normalization batch_img = [] batch_gt = [] fg_gamma = rn(1) + .4 fg_beta = .25 - rn(.5) bg_gamma = rn(1) + .4 bg_beta = .25 - rn(.5) has_fg = rn(1) > 0.5 for i in range(len(bgrw)): if True: # test flag img, gt = self.compose_one(output_size, fgrw[i], bgrw[i], fg_blur=fgs[i] / 2, bg_blur=bgs[i] / 2, fg_gamma=fg_gamma, bg_gamma=bg_gamma, fg_beta=fg_beta, bg_beta=bg_beta, has_fg=has_fg) else: img, gt = self.compose_one(output_size, fgrw[i] * 0, bgrw[i] * 0, fg_blur=fgs[i] / 2 * 0, bg_blur=bgs[i] / 2 * 0) batch_img.append(img) batch_gt.append(gt) if show == True: for k in range(len(batch_img)): vis.show_autoscaled(batch_img[k], name='img', limit=130.) vis.show_autoscaled(batch_gt[k], name='gt', limit=130.) # both generated and ground truth return np.stack(batch_img, axis=0), np.stack(batch_gt, axis=0)
def jeffdemo(): jeff = load_image('../jeff.jpg') h, w = jeff.shape[0:2] jeff = vis.resize_perfect(jeff, 192, 192) print(jeff.shape) vis.show_autoscaled(jeff) jefff = np.divide(jeff, 255., dtype=np.float32) jeffff = forenet_infer(jefff) print(jeffff.shape) jeffff = np.transpose(jeffff, [2, 0, 1]) jeffff.shape += (1, ) vis.show_batch_autoscaled(jeffff * 0.5 + 0.5) cv2.waitKey(0)
def r(ep): for i in range(ep): env.step() if (i + 1) % 5 == 0: epop = env.population best = epop[-1] print('iter {}/{} '.format(i+1, ep)+\ 'max:{:.6f} min:{:.6f} avg:{:.6f} avg_len:{:.1f}'.format( epop[-1].fitness, epop[0].fitness, env.get_mean_fitness(), env.get_mean_length(), )+' popsize:{}'.format(len(epop))\ +'params:[{}]'.format(best.points[0]) ) c = env.get_blank_canvas() env.population[-1].draw_on(c) vis.show_autoscaled(c, name='best') c = env.get_blank_canvas() env.population[0].draw_on(c) vis.show_autoscaled(c, name='worst') vis.show_autoscaled(env.target, name='targ') cv2.waitKey(1)
def render(self): if self.target_drawn == False: vis.show_autoscaled(self.target,limit=300,name='target') self.target_drawn = True vis.show_autoscaled(self.canvas,limit=300,name='canvas')
def show(): vis.show_autoscaled(jeff,name='jeff') canvas = getcanvas() vis.show_autoscaled(canvas) cv2.waitKey(1)