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 show(threshold=.5): from cv2tools import vis, filt bs = 16 j = np.random.choice(len(xt) - 16) minibatch = xt[j:j + bs] code, rec, code2, rec2, noisy_x = test(minibatch, threshold) code = np.transpose(code[0:1], axes=(3, 1, 2, 0)) code2 = np.transpose(code2[0:1], axes=(3, 1, 2, 0)) vis.show_batch_autoscaled(code, name='code(quant)', limit=600.) vis.show_batch_autoscaled(code2, name='code2(no quant)', limit=600.) vis.show_batch_autoscaled(noisy_x, name='input') vis.show_batch_autoscaled(rec, name='recon(quant)') vis.show_batch_autoscaled(rec2, name='recon(no quant)') #.................. import csv import numpy np.set_printoptions(threshold=np.inf) f_code = open("code.csv", "w") w_code = csv.writer(f_code) w_code.writerow(code2) f_code = open("input.csv", "w") w_code = csv.writer(f_code) w_code.writerow(noisy_x) f_code = open("rec2.csv", "w") w_code = csv.writer(f_code) w_code.writerow(rec2)
def testPour(): u, q = pour(16) vis.show_batch_autoscaled(u, name='uv') vis.show_batch_autoscaled(q, name='qr') print('deque len:', len(dek))
def show(threshold=.5): from cv2tools import vis,filt import math bs = 16 j = np.random.choice(len(xt)-16) minibatch = xt[j:j+bs] code, rec, code2, rec2, noisy_x = test(minibatch,threshold) code = np.transpose(code[0:1],axes=(3,1,2,0)) code2 = np.transpose(code2[0:1],axes=(3,1,2,0)) vis.show_batch_autoscaled(code, name='code(quant)', limit=600.) vis.show_batch_autoscaled(code2, name='code2(no quant)', limit=600.) vis.show_batch_autoscaled(noisy_x,name='input') vis.show_batch_autoscaled(rec,name='recon(quant)') vis.show_batch_autoscaled(rec2,name='recon(no quant)') #.................. #import csv #import numpy #np.set_printoptions(threshold=np.inf) #f_code = open("code.csv","w") #w_code = csv.writer(f_code) #w_code.writerow(code2) #f_code = open("input.csv","w") # w_code = csv.writer(f_code) #w_code.writerow(noisy_x) # f_code = open("rec2.csv","w") # w_code = csv.writer(f_code) # w_code.writerow(rec2) #compute the Mean Sqare Error mse=noisy_x-rec2 #print(mse) sq_mse=np.power(mse,2) #abs_mse=abs(mse) print("******************") #print(sq_mse) sum_sq_error=sq_mse.sum() #print (sum_sq_error) #print("legth") #print(len(noisy_x[0][0])) print("MSE=") mean=sum_sq_error/(32*32*16) print(mean) temp=1/mean PSNR=10*math.log10(temp) print("PSNR") print(PSNR)
def test(): vf2t = getvf2t() # print(vf2t.minibatch(5)) # for i in range(10): # print(vf2t.randone()) for i in range(20): a, b = vf2t.minibatch(20, side=128) # cv2.imshow('yolo', b) vis.show_batch_autoscaled(b) cv2.waitKey(0)
def show(threshold=.5): from cv2tools import vis,filt bs = 16 j = np.random.choice(len(xt)-16) minibatch = xt[j:j+bs] code, rec, code2, rec2, noisy_x = test(minibatch,threshold) code = np.transpose(code[0:1],axes=(3,1,2,0)) code2 = np.transpose(code2[0:1],axes=(3,1,2,0)) vis.show_batch_autoscaled(code, name='code(quant)', limit=600.) vis.show_batch_autoscaled(code2, name='code2(no quant)', limit=600.) vis.show_batch_autoscaled(noisy_x,name='input') vis.show_batch_autoscaled(rec,name='recon(quant)') vis.show_batch_autoscaled(rec2,name='recon(no quant)')
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 show(): # evaluate result on validation set from cv2tools import filt, vis import cv2 # generate our own set of samples from scratch xt, yt = needsamples(1) index = np.random.choice(len(xt)) mbx = xt[index:index + 1] mby = yt[index:index + 1] gru_state = None resarr = [] for i in range(len(mbx[0])): # timesteps resy, state = stateful_predict(gru_state, mbx[0:1, i:i + 1]) resarr.append(resy) # [1,1,h,w,1] gru_state = state resarr = np.concatenate(resarr, axis=1) print(resarr.shape) vis.show_batch_autoscaled(mbx[0], name='input image') vis.show_batch_autoscaled(resarr[0], name='inference') vis.show_batch_autoscaled(mby[0], name='ground truth')
def show(): import cv2 from cv2tools import vis, filt index = np.random.choice(len(xt)) img = xt[index:index + 1] tiledx, y_softmaxed, shifted_means, variances = test(img) for idxt, dist in enumerate(y_softmaxed[0]): print('step', idxt, 'guess:', np.argmax(dist)) tiledx += 0.5 tiledx_copy = tiledx.copy() tiledx = (tiledx * 255.).astype('uint16') # 16-bit-ify if tiledx.shape[-1] == 1: tiledx = np.tile(tiledx, (1, 1, 1, 1, 3)) # colorify shifted_means += np.array([img.shape[1] - 1, img.shape[2] - 1], dtype='float32') / 2 # shift from image center to image coordinates # draw the circles... for idxt, receptors in enumerate(shifted_means[0]): tmp = tiledx[0, idxt] * 0 # [HWC] for idxr, receptor in enumerate(receptors): # additively paint circles tmp += cv2.circle(np.zeros_like(tmp, dtype='uint8'), (int(receptor[1] * 16), int(receptor[0] * 16)), radius=int(np.sqrt(variances[idxr, 0]) * 16), color=(80, 140, 180), thickness=-1, lineType=cv2.LINE_AA, shift=4) tiledx[0, idxt] = tiledx[0, idxt] * 0.5 + tmp * 0.5 # blend with original tiledx = tiledx.clip(min=0, max=255).astype('uint8') vis.show_batch_autoscaled(tiledx_copy[0], name='input sequence') vis.show_batch_autoscaled(tiledx[0], name='attention over time')
def show(threshold=.5): from cv2tools import vis,filt import math bs = 16 j = np.random.choice(len(xt)-16) minibatch = xt[j:j+bs] code, rec, code2, rec2, noisy_x = test(minibatch,threshold) code = np.transpose(code[0:1],axes=(3,1,2,0)) code2 = np.transpose(code2[0:1],axes=(3,1,2,0)) vis.show_batch_autoscaled(code, name='code(quant)', limit=600.) vis.show_batch_autoscaled(code2, name='code2(no quant)', limit=600.) vis.show_batch_autoscaled(noisy_x,name='input') vis.show_batch_autoscaled(rec,name='recon(quant)') vis.show_batch_autoscaled(rec2,name='recon(no quant)') mse=noisy_x-rec2 #print(mse) sq_mse=np.power(mse,2) #abs_mse=abs(mse) print("******************") #print(sq_mse) sum_sq_error=sq_mse.sum() #print (sum_sq_error) #print("legth") #print(len(noisy_x[0][0])) print("MSE=") mean=sum_sq_error/(32*32*16) print(mean) temp=1/mean PSNR=10*math.log10(temp) print("PSNR") print(PSNR)
def t(): uv, qr = pour(10) y, sigmred = test(qr) y[:, :, :, 2:3] = sigmred vis.show_batch_autoscaled(y, name='output') vis.show_batch_autoscaled(qr, name='input') vis.show_batch_autoscaled(uv, name='gt')
def show(): # evaluate result on validation set from drone_samples_queue import needsamples from cv2tools import filt, vis import cv2 # generate our own set of samples from scratch mbx, mby = needsamples(10) mbx.shape = (10, ) + mbx.shape[2:] mby.shape = (10, ) + mby.shape[2:] res = predict(mbx) print(res.shape) vis.show_batch_autoscaled(mbx, name='input image') vis.show_batch_autoscaled(res, name='inference') vis.show_batch_autoscaled(mby, name='ground truth')